Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/ui/console/local_file_system.rb
Views: 11783
# -*- coding: binary -*-12module Msf3module Ui4module Console5###6#7# This module provides commands for the local file system8#9###10module LocalFileSystem11#12# Options for the lls command13#14@@lls_opts = Rex::Parser::Arguments.new(15'-h' => [ false, 'Help banner' ],16'-S' => [ true, 'Search string on filename (as regular expression)' ],17'-t' => [ false, 'Sort by time' ],18'-s' => [ false, 'Sort by size' ],19'-r' => [ false, 'Reverse sort order' ]20)2122#23# List of supported local commands.24#25# @return [Hash] Hash of local commands26def local_fs_commands27{28'getlwd' => 'Print local working directory (alias for lpwd)',29'lcat' => 'Read the contents of a local file to the screen',30'lcd' => 'Change local working directory',31'lmkdir' => 'Create new directory on local machine',32'lpwd' => 'Print local working directory',33'lls' => 'List local files',34'ldir' => 'List local files (alias for lls)'35}36end3738#39# List local files40#41# @param [Array] args42# @return [Rex::Text::Table] The results lls command43def cmd_lls(*args)44# Set Defaults45path = ::Dir.pwd46sort = 'Name'47order = :forward48search_term = nil4950# Parse the args51@@lls_opts.parse(args) do |opt, _idx, val|52case opt53# Sort options54when '-s'55sort = 'Size'56when '-t'57sort = 'Last modified'58# Output options59when '-r'60order = :reverse61# Search62when '-S'63search_term = val64if search_term.nil?65print_error('Enter a search term')66return true67else68search_term = /#{search_term}/nmi69end70# Help and path71when '-h'72cmd_lls_help73return 074when nil75path = val76end77end7879list_local_path(path, sort, order, search_term)80end8182#83# Help output for lss command84#85# @return [Rex::Parser::Arguments]86def cmd_lls_help87print_line 'Usage: lls [options]'88print_line89print_line 'Lists contents of a local directory or file info'90print_line @@lls_opts.usage91end9293#94# Alias the lls command to dir, for those of us who have windows muscle-memory95#96alias cmd_ldir cmd_lls9798#99# Change the local working directory.100#101# @param [Array] args102# @return [TrueClass]103def cmd_lcd(*args)104if args.empty?105print_line('Usage: lcd directory')106return true107end108109::Dir.chdir(args[0])110111true112end113114#115# Tab completion for the lcd command116#117# @param [String] str118# @param [Array] words119def cmd_lcd_tabs(str, words)120tab_complete_directory(str, words)121end122123alias cmd_lls_tabs cmd_lcd_tabs124125#126# Get list local path information for lls command127#128# @param [String] path129# @param [String] sort130# @param [Symbol] order131# @param [nil] search_term132# @return [Rex::Text::Table, String] The results lcd command133def list_local_path(path, sort, order, search_term = nil)134# Single file as path135unless ::File.directory?(path)136perms = pretty_perms(path)137stat = ::File.stat(path)138print_line("#{perms} #{stat.size} #{stat.ftype[0, 3]} #{stat.mtime} #{path}")139return140end141142# Enumerate each item...143# No need to sort as Table will do it for us144columns = [ 'Mode', 'Size', 'Type', 'Last modified', 'Name' ]145tbl = Rex::Text::Table.new(146'Header' => "Listing Local: #{path}",147'SortIndex' => columns.index(sort),148'SortOrder' => order,149'Columns' => columns150)151152items = 0153files = ::Dir.entries(path)154155files.each do |file|156file_path = ::File.join(path, file)157158perms = pretty_perms(file_path)159stat = ::File.stat(file_path)160161row = [162perms || '',163stat.size ? stat.size.to_s : '',164stat.ftype ? stat.ftype[0, 3] : '',165stat.mtime || '',166file167]168if file != '.' && file != '..' && (row.join(' ') =~ /#{search_term}/)169tbl << row170items += 1171end172end173if items > 0174print_line(tbl.to_s)175else176print_line("No entries exist in #{path}")177end178end179180#181# Reads the contents of a local file and prints them to the screen.182#183# @param [Array] args184# @return [TrueClass]185def cmd_lcat(*args)186if args.empty? || args.include?('-h') || args.include?('--help')187print_line('Usage: lcat file')188return true189end190191path = args[0]192path = ::File.expand_path(path) if path =~ path_expand_regex193194if ::File.stat(path).directory?195print_error("#{path} is a directory")196else197fd = ::File.new(path, 'rb')198begin199print(fd.read) until fd.eof?200# EOFError is raised if file is empty, do nothing, just catch201rescue EOFError202end203fd.close204end205206true207end208209#210# Tab completion for the lcat command211#212# @param [Object] str213# @param [Object] words214# @return [Array] List of matches215def cmd_lcat_tabs(str, words)216tab_complete_filenames(str, words)217end218219#220# Create new directory on local machine221#222# @param [Array] args223# @return [Array]224def cmd_lmkdir(*args)225if args.empty?226print_line('Usage: lmkdir </path/to/directory>')227return228end229230args.each do |path|231::FileUtils.mkdir_p(path)232print_line("Directory '#{path}' created successfully.")233rescue ::StandardError => e234print_error("Error creating #{path} directory: #{e}")235end236end237238#239# Display the local working directory.240#241# @param [Array] args242# @return [TrueClass]243def cmd_lpwd(*args)244print_line(::Dir.pwd)245true246end247248alias cmd_getlwd cmd_lpwd249250#251# Code from prettymode in lib/rex/post/file_stat.rb252# adapted for local file usage253#254# @param [Object] path255# @return [String]256def pretty_perms(path)257m = ::File.stat(path).mode258om = '%04o' % m259perms = ''2602613.times do262perms = ((m & 0o1) == 0o1 ? 'x' : '-') + perms263perms = ((m & 0o2) == 0o2 ? 'w' : '-') + perms264perms = ((m & 0o4) == 0o4 ? 'r' : '-') + perms265m >>= 3266end267268"#{om}/#{perms}"269end270271private272273# @return [Regexp]274def path_expand_regex275if shell.session.platform == 'windows'276/%(\w*)%/277else278/\$(([A-Za-z0-9_]+)|\{([A-Za-z0-9_]+)\})|^~/279end280end281end282end283end284end285286287