Skip to content

Commit 282c7eb

Browse files
author
Brent Cook
committed
add -S regex search to ls, normalize arg parsing
from @sempervictus Merge forked changes to cmd_ls allowing for the use of string matching on listing output via Rex::Ui::Text::Table's SearchTerm facility. Example: ``` meterpreter > ls chef -R -S wget No entries exist in chef/backup/chef/handlers No entries exist in chef/backup/chef/ohai_plugins No entries exist in chef/backup/chef No entries exist in chef/backup No entries exist in chef/cache/cookbooks/avast/attributes No entries exist in chef/cache/cookbooks/avast/recipes No entries exist in chef/cache/cookbooks/avast No entries exist in chef/cache/cookbooks/chef-client/attributes No entries exist in chef/cache/cookbooks/chef-client/libraries No entries exist in chef/cache/cookbooks/chef-client/recipes No entries exist in chef/cache/cookbooks/chef-client No entries exist in chef/cache/cookbooks/chef_handler/attributes No entries exist in chef/cache/cookbooks/chef_handler/libraries No entries exist in chef/cache/cookbooks/chef_handler/providers No entries exist in chef/cache/cookbooks/chef_handler/recipes No entries exist in chef/cache/cookbooks/chef_handler/resources No entries exist in chef/cache/cookbooks/chef_handler No entries exist in chef/cache/cookbooks/cron/providers No entries exist in chef/cache/cookbooks/cron/recipes No entries exist in chef/cache/cookbooks/cron/resources No entries exist in chef/cache/cookbooks/cron No entries exist in chef/cache/cookbooks/logrotate/attributes No entries exist in chef/cache/cookbooks/logrotate/definitions No entries exist in chef/cache/cookbooks/logrotate/libraries No entries exist in chef/cache/cookbooks/logrotate/recipes No entries exist in chef/cache/cookbooks/logrotate No entries exist in chef/cache/cookbooks/ohai/attributes No entries exist in chef/cache/cookbooks/ohai/files/default/plugins No entries exist in chef/cache/cookbooks/ohai/files/default No entries exist in chef/cache/cookbooks/ohai/files No entries exist in chef/cache/cookbooks/ohai/recipes No entries exist in chef/cache/cookbooks/ohai No entries exist in chef/cache/cookbooks/svit-windows/attributes No entries exist in chef/cache/cookbooks/svit-windows/recipes No entries exist in chef/cache/cookbooks/svit-windows/templates/default/plugins No entries exist in chef/cache/cookbooks/svit-windows/templates/default No entries exist in chef/cache/cookbooks/svit-windows/templates No entries exist in chef/cache/cookbooks/svit-windows No entries exist in chef/cache/cookbooks/windows/attributes No entries exist in chef/cache/cookbooks/windows/files/default/handlers No entries exist in chef/cache/cookbooks/windows/files/default No entries exist in chef/cache/cookbooks/windows/files No entries exist in chef/cache/cookbooks/windows/libraries No entries exist in chef/cache/cookbooks/windows/providers No entries exist in chef/cache/cookbooks/windows/recipes No entries exist in chef/cache/cookbooks/windows/resources No entries exist in chef/cache/cookbooks/windows No entries exist in chef/cache/cookbooks No entries exist in chef/cache No entries exist in chef/handlers No entries exist in chef/log No entries exist in chef/ohai_plugins No entries exist in chef/run Listing: chef ============= Mode Size Type Last modified Name ---- ---- ---- ------------- ---- 100666/rw-rw-rw- 161 fil 2014-07-21 11:08:26 -0400 wget.ps1 100666/rw-rw-rw- 1285 fil 2014-07-21 11:08:26 -0400 wget.vbs meterpreter > ```
1 parent edbd71f commit 282c7eb

File tree

1 file changed

+71
-39
lines changed
  • lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi

1 file changed

+71
-39
lines changed

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

Lines changed: 71 additions & 39 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.
@@ -387,7 +399,15 @@ def cmd_lpwd(*args)
387399

388400
alias cmd_getlwd cmd_lpwd
389401

390-
def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
402+
403+
def cmd_ls_help
404+
print_line "Usage: ls [options]"
405+
print_line
406+
print_line "Lists contents of directory or file info, searchable"
407+
print_line @@ls_opts.usage
408+
end
409+
410+
def list_path(path, columns, sort, order, short, recursive = false, depth = 0, search_term = nil)
391411

392412
# avoid infinite recursion
393413
if depth > 100
@@ -398,7 +418,8 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
398418
'Header' => "Listing: #{path}",
399419
'SortIndex' => columns.index(sort),
400420
'SortOrder' => order,
401-
'Columns' => columns)
421+
'Columns' => columns,
422+
'SearchTerm' => search_term)
402423

403424
items = 0
404425

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

421442
if fname != '.' && fname != '..'
422-
tbl << row
423-
items += 1
443+
if row.join(' ') =~ /#{search_term}/
444+
tbl << row
445+
items += 1
446+
end
424447

425448
if recursive && ffstat && ffstat.directory?
426449
if client.fs.file.is_glob?(path)
@@ -430,7 +453,7 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
430453
child_path = path + ::File::SEPARATOR + fname
431454
end
432455
begin
433-
list_path(child_path, columns, sort, order, short, recursive, depth + 1)
456+
list_path(child_path, columns, sort, order, short, recursive, depth + 1, search_term)
434457
rescue RequestError
435458
end
436459
end
@@ -448,39 +471,48 @@ def list_path(path, columns, sort, order, short, recursive = false, depth = 0)
448471
# Lists files
449472
#
450473
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
474+
# Set defaults
475+
path = client.fs.dir.getwd
476+
search_term = nil
477+
sort = 'Name'
478+
short = nil
479+
order = :forward
480+
recursive = nil
481+
482+
# Parse the args
483+
@@ls_opts.parse(args) { |opt, idx, val|
484+
case opt
485+
# Sort options
486+
when '-s'
487+
sort = 'Size'
488+
when '-t'
489+
sort = 'Last modified'
490+
# Output options
491+
when '-x'
492+
short = true
493+
when '-l'
494+
short = nil
495+
when '-r'
496+
order = :reverse
497+
when '-R'
498+
recursive = true
499+
# Search
500+
when '-S'
501+
search_term = val
502+
if search_term.nil?
503+
print_error("Enter a search term")
504+
return true
505+
else
506+
search_term = /#{search_term}/nmi
507+
end
508+
# Help and path
509+
when "-h"
510+
cmd_ls_help
511+
return 0
512+
when nil
513+
path = val
514+
end
515+
}
484516

485517
columns = [ 'Mode', 'Size', 'Type', 'Last modified', 'Name' ]
486518
columns.insert(4, 'Short Name') if short
@@ -499,7 +531,7 @@ def cmd_ls(*args)
499531

500532
stat = client.fs.file.stat(stat_path)
501533
if stat.directory?
502-
list_path(path, columns, sort, order, short, recursive)
534+
list_path(path, columns, sort, order, short, recursive, 0, search_term)
503535
else
504536
print_line("#{stat.prettymode} #{stat.size} #{stat.ftype[0,3]} #{stat.mtime} #{path}")
505537
end

0 commit comments

Comments
 (0)