Skip to content

Commit 3d8451e

Browse files
committed
Land rapid7#8997, add local 'ls' support to Meterpreter sessions
2 parents 05e002e + 0e59f47 commit 3d8451e

File tree

1 file changed

+135
-0
lines changed
  • lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi

1 file changed

+135
-0
lines changed

lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/fs.rb

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Console::CommandDispatcher::Stdapi::Fs
4141
@@upload_opts = Rex::Parser::Arguments.new(
4242
"-h" => [ false, "Help banner." ],
4343
"-r" => [ false, "Upload recursively." ])
44+
4445
#
4546
# Options for the ls command
4647
#
@@ -54,6 +55,16 @@ class Console::CommandDispatcher::Stdapi::Fs
5455
"-l" => [ false, "List in long format (default)" ],
5556
"-R" => [ false, "Recursively list subdirectories encountered" ])
5657

58+
#
59+
# Options for the lls command
60+
#
61+
@@lls_opts = Rex::Parser::Arguments.new(
62+
"-h" => [ false, "Help banner." ],
63+
"-S" => [ true, "Search string." ],
64+
"-t" => [ false, "Sort by time" ],
65+
"-s" => [ false, "Sort by size" ],
66+
"-r" => [ false, "Reverse sort order" ])
67+
5768
#
5869
# List of supported commands.
5970
#
@@ -71,6 +82,7 @@ def commands
7182
'lcd' => 'Change local working directory',
7283
'lpwd' => 'Print local working directory',
7384
'ls' => 'List files',
85+
'lls' => 'List local files',
7486
'mkdir' => 'Make directory',
7587
'pwd' => 'Print working directory',
7688
'rm' => 'Delete the specified file',
@@ -95,6 +107,7 @@ def commands
95107
'lcd' => [],
96108
'lpwd' => [],
97109
'ls' => ['stdapi_fs_stat', 'stdapi_fs_ls'],
110+
'lls' => [],
98111
'mkdir' => ['stdapi_fs_mkdir'],
99112
'pwd' => ['stdapi_fs_getwd'],
100113
'rmdir' => ['stdapi_fs_delete_dir'],
@@ -680,6 +693,128 @@ def cmd_ls(*args)
680693
#
681694
alias cmd_dir cmd_ls
682695

696+
def cmd_lls_help
697+
print_line "Usage: lls [options]"
698+
print_line
699+
print_line "Lists contents of a local directory or file info"
700+
print_line @@lls_opts.usage
701+
end
702+
703+
#
704+
# Get list local path information for lls command
705+
#
706+
def list_local_path(path, sort, order, search_term = nil)
707+
# Single file as path
708+
if !::File.directory?(path)
709+
perms = pretty_perms(path)
710+
stat = ::File.stat(path)
711+
print_line("#{perms} #{stat.size} #{stat.ftype[0,3]} #{stat.mtime} #{path}")
712+
return
713+
end
714+
715+
# Enumerate each item...
716+
# No need to sort as Table will do it for us
717+
columns = [ 'Mode', 'Size', 'Type', 'Last modified', 'Name' ]
718+
tbl = Rex::Text::Table.new(
719+
'Header' => "Listing Local: #{path}",
720+
'SortIndex' => columns.index(sort),
721+
'SortOrder' => order,
722+
'Columns' => columns)
723+
724+
items = 0
725+
files = ::Dir.entries(path)
726+
727+
files.each do |file|
728+
file_path = ::File.join(path, file)
729+
730+
perms = pretty_perms(file_path)
731+
stat = ::File.stat(file_path)
732+
733+
row = [
734+
perms ? perms : '',
735+
stat.size ? stat.size.to_s : '',
736+
stat.ftype ? stat.ftype[0,3] : '',
737+
stat.mtime ? stat.mtime : '',
738+
file
739+
]
740+
if file != '.' && file != '..'
741+
if row.join(' ') =~ /#{search_term}/
742+
tbl << row
743+
items += 1
744+
end
745+
end
746+
end
747+
if items > 0
748+
print_line(tbl.to_s)
749+
else
750+
print_line("No entries exist in #{path}")
751+
end
752+
end
753+
754+
# Code from prettymode in lib/rex/post/file_stat.rb
755+
# adapted for local file usage
756+
def pretty_perms(path)
757+
m = ::File.stat(path).mode
758+
om = '%04o' % m
759+
perms = ''
760+
761+
3.times {
762+
perms = ((m & 01) == 01 ? 'x' : '-') + perms
763+
perms = ((m & 02) == 02 ? 'w' : '-') + perms
764+
perms = ((m & 04) == 04 ? 'r' : '-') + perms
765+
m >>= 3
766+
}
767+
768+
return "#{om}/#{perms}"
769+
end
770+
771+
#
772+
# List local files
773+
#
774+
def cmd_lls(*args)
775+
# Set Defaults
776+
path = ::Dir.pwd
777+
sort = 'Name'
778+
order = :forward
779+
search_term = nil
780+
781+
# Parse the args
782+
@@lls_opts.parse(args) { |opt, idx, val|
783+
case opt
784+
# Sort options
785+
when '-s'
786+
sort = 'Size'
787+
when '-t'
788+
sort = 'Last modified'
789+
# Output options
790+
when '-r'
791+
order = :reverse
792+
# Search
793+
when '-S'
794+
search_term = val
795+
if search_term.nil?
796+
print_error("Enter a search term")
797+
return true
798+
else
799+
search_term = /#{search_term}/nmi
800+
end
801+
# Help and path
802+
when "-h"
803+
cmd_lls_help
804+
return 0
805+
when nil
806+
path = val
807+
end
808+
}
809+
810+
list_local_path(path, sort, order, search_term)
811+
end
812+
813+
#
814+
# Alias the lls command to dir, for those of us who have windows muscle-memory
815+
#
816+
alias cmd_ldir cmd_lls
817+
683818
#
684819
# Make one or more directory.
685820
#

0 commit comments

Comments
 (0)