Skip to content

Commit 0fc8abc

Browse files
author
Brent Cook
committed
Land rapid7#5341, session command search and Rex table improvements
2 parents b9ac612 + e34c751 commit 0fc8abc

File tree

4 files changed

+318
-129
lines changed

4 files changed

+318
-129
lines changed

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

Lines changed: 96 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ class Console::CommandDispatcher::Stdapi::Fs
3030
@@upload_opts = Rex::Parser::Arguments.new(
3131
"-h" => [ false, "Help banner." ],
3232
"-r" => [ false, "Upload recursively." ])
33+
#
34+
# Options for the ls command
35+
#
36+
@@ls_opts = Rex::Parser::Arguments.new(
37+
"-h" => [ false, "Help banner." ],
38+
"-S" => [ true, "Search string." ],
39+
"-t" => [ false, "Sort by time" ],
40+
"-s" => [ false, "Sort by size" ],
41+
"-r" => [ false, "Reverse sort order" ],
42+
"-x" => [ false, "Show short file names" ],
43+
"-l" => [ false, "List in long format (default)" ],
44+
"-R" => [ false, "Recursively list subdirectories encountered" ])
3345

3446
#
3547
# List of supported commands.
@@ -223,22 +235,34 @@ def cmd_rm(*args)
223235

224236
alias :cmd_del :cmd_rm
225237

226-
#
227-
# Move source to destination
228-
#
229-
def cmd_mv(*args)
230-
if (args.length < 2)
231-
print_line("Usage: mv oldfile newfile")
232-
return true
233-
end
238+
#
239+
# Move source to destination
240+
#
241+
def cmd_mv(*args)
242+
if (args.length < 2)
243+
print_line("Usage: mv oldfile newfile")
244+
return true
245+
end
246+
client.fs.file.mv(args[0],args[1])
247+
return true
248+
end
234249

235-
client.fs.file.mv(args[0],args[1])
250+
alias :cmd_move :cmd_mv
251+
alias :cmd_rename :cmd_mv
236252

237-
return true
238-
end
253+
#
254+
# Move source to destination
255+
#
256+
def cmd_cp(*args)
257+
if (args.length < 2)
258+
print_line("Usage: cp oldfile newfile")
259+
return true
260+
end
261+
client.fs.file.cp(args[0],args[1])
262+
return true
263+
end
239264

240-
alias :cmd_move :cmd_mv
241-
alias :cmd_rename :cmd_mv
265+
alias :cmd_copy :cmd_cp
242266

243267

244268
def cmd_download_help
@@ -387,7 +411,15 @@ def cmd_lpwd(*args)
387411

388412
alias cmd_getlwd cmd_lpwd
389413

390-
def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
414+
415+
def cmd_ls_help
416+
print_line "Usage: ls [options]"
417+
print_line
418+
print_line "Lists contents of directory or file info, searchable"
419+
print_line @@ls_opts.usage
420+
end
421+
422+
def list_path(path, columns, sort, order, short, recursive = false, depth = 0, search_term = nil)
391423

392424
# avoid infinite recursion
393425
if depth > 100
@@ -398,7 +430,8 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
398430
'Header' => "Listing: #{path}",
399431
'SortIndex' => columns.index(sort),
400432
'SortOrder' => order,
401-
'Columns' => columns)
433+
'Columns' => columns,
434+
'SearchTerm' => search_term)
402435

403436
items = 0
404437

@@ -419,8 +452,10 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
419452
row.insert(4, p['FileShortName'] || '') if short
420453

421454
if fname != '.' && fname != '..'
422-
tbl << row
423-
items += 1
455+
if row.join(' ') =~ /#{search_term}/
456+
tbl << row
457+
items += 1
458+
end
424459

425460
if recursive && ffstat && ffstat.directory?
426461
if client.fs.file.is_glob?(path)
@@ -430,7 +465,7 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
430465
child_path = path + ::File::SEPARATOR + fname
431466
end
432467
begin
433-
list_path(child_path, columns, sort, order, short, recursive, depth + 1)
468+
list_path(child_path, columns, sort, order, short, recursive, depth + 1, search_term)
434469
rescue RequestError
435470
end
436471
end
@@ -448,39 +483,48 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
448483
# Lists files
449484
#
450485
def cmd_ls(*args)
451-
452-
# Check sort column
453-
sort = args.include?('-S') ? 'Size' : 'Name'
454-
sort = args.include?('-t') ? 'Last modified' : sort
455-
args.delete('-S')
456-
args.delete('-t')
457-
458-
# Check whether to include the short name option
459-
short = args.include?('-x')
460-
args.delete('-x')
461-
462-
# Check sort order
463-
order = args.include?('-r') ? :reverse : :forward
464-
args.delete('-r')
465-
466-
# Check for recursive mode
467-
recursive = !args.delete('-R').nil?
468-
469-
args.delete('-l')
470-
471-
# Check for cries of help
472-
if args.length > 1 || args.any? { |a| a[0] == '-' }
473-
print_line('Usage: ls [dir] [-x] [-S] [-t] [-r]')
474-
print_line(' -x Show short file names')
475-
print_line(' -S Sort by size')
476-
print_line(' -t Sort by time modified')
477-
print_line(' -r Reverse sort order')
478-
print_line(' -l List in long format (default)')
479-
print_line(' -R Recursively list subdirectories encountered.')
480-
return true
481-
end
482-
483-
path = args[0] || client.fs.dir.getwd
486+
# Set defaults
487+
path = client.fs.dir.getwd
488+
search_term = nil
489+
sort = 'Name'
490+
short = nil
491+
order = :forward
492+
recursive = nil
493+
494+
# Parse the args
495+
@@ls_opts.parse(args) { |opt, idx, val|
496+
case opt
497+
# Sort options
498+
when '-s'
499+
sort = 'Size'
500+
when '-t'
501+
sort = 'Last modified'
502+
# Output options
503+
when '-x'
504+
short = true
505+
when '-l'
506+
short = nil
507+
when '-r'
508+
order = :reverse
509+
when '-R'
510+
recursive = true
511+
# Search
512+
when '-S'
513+
search_term = val
514+
if search_term.nil?
515+
print_error("Enter a search term")
516+
return true
517+
else
518+
search_term = /#{search_term}/nmi
519+
end
520+
# Help and path
521+
when "-h"
522+
cmd_ls_help
523+
return 0
524+
when nil
525+
path = val
526+
end
527+
}
484528

485529
columns = [ 'Mode', 'Size', 'Type', 'Last modified', 'Name' ]
486530
columns.insert(4, 'Short Name') if short
@@ -499,7 +543,7 @@ def cmd_ls(*args)
499543

500544
stat = client.fs.file.stat(stat_path)
501545
if stat.directory?
502-
list_path(path, columns, sort, order, short, recursive)
546+
list_path(path, columns, sort, order, short, recursive, 0, search_term)
503547
else
504548
print_line("#{stat.prettymode} #{stat.size} #{stat.ftype[0,3]} #{stat.mtime} #{path}")
505549
end

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ def cleanup
5151
"-p" => [ true, "The remote port to connect to." ],
5252
"-L" => [ true, "The local host to listen on (optional)." ])
5353

54+
#
55+
# Options for the netstat command.
56+
#
57+
@@netstat_opts = Rex::Parser::Arguments.new(
58+
"-h" => [ false, "Help banner." ],
59+
"-S" => [ true, "Search string." ])
60+
61+
#
62+
# Options for ARP command.
63+
#
64+
@@arp_opts = Rex::Parser::Arguments.new(
65+
"-h" => [ false, "Help banner." ],
66+
"-S" => [ true, "Search string." ])
67+
5468
#
5569
# List of supported commands.
5670
#
@@ -107,6 +121,23 @@ def name
107121
#
108122
def cmd_netstat(*args)
109123
connection_table = client.net.config.netstat
124+
search_term = nil
125+
@@netstat_opts.parse(args) { |opt, idx, val|
126+
case opt
127+
when '-S'
128+
search_term = val
129+
if search_term.nil?
130+
print_error("Enter a search term")
131+
return true
132+
else
133+
search_term = /#{search_term}/nmi
134+
end
135+
when "-h"
136+
@@netstat_opts.usage
137+
return 0
138+
139+
end
140+
}
110141
tbl = Rex::Ui::Text::Table.new(
111142
'Header' => "Connection list",
112143
'Indent' => 4,
@@ -119,7 +150,8 @@ def cmd_netstat(*args)
119150
"User",
120151
"Inode",
121152
"PID/Program name"
122-
])
153+
],
154+
'SearchTerm' => search_term)
123155

124156
connection_table.each { |connection|
125157
tbl << [ connection.protocol, connection.local_addr_str, connection.remote_addr_str,
@@ -138,6 +170,23 @@ def cmd_netstat(*args)
138170
#
139171
def cmd_arp(*args)
140172
arp_table = client.net.config.arp_table
173+
search_term = nil
174+
@@arp_opts.parse(args) { |opt, idx, val|
175+
case opt
176+
when '-S'
177+
search_term = val
178+
if search_term.nil?
179+
print_error("Enter a search term")
180+
return true
181+
else
182+
search_term = /#{search_term}/nmi
183+
end
184+
when "-h"
185+
@@arp_opts.usage
186+
return 0
187+
188+
end
189+
}
141190
tbl = Rex::Ui::Text::Table.new(
142191
'Header' => "ARP cache",
143192
'Indent' => 4,
@@ -146,7 +195,8 @@ def cmd_arp(*args)
146195
"IP address",
147196
"MAC address",
148197
"Interface"
149-
])
198+
],
199+
'SearchTerm' => search_term)
150200

151201
arp_table.each { |arp|
152202
tbl << [ arp.ip_addr, arp.mac_addr, arp.interface ]

0 commit comments

Comments
 (0)