Skip to content

Commit 35c0f0e

Browse files
committed
Land rapid7#8596, Fix rex arguments parser to handle adjacent flags
2 parents 47a659f + 0eaffde commit 35c0f0e

File tree

2 files changed

+38
-18
lines changed

2 files changed

+38
-18
lines changed

lib/rex/parser/arguments.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,20 @@ def parse(args, &_block)
4343
next
4444
end
4545

46-
if arg[0] == '-'
47-
cfs = arg[0..2]
46+
if arg.length > 1 && arg[0] == '-' && arg[1] != '-'
47+
arg.split('').each do |flag|
48+
fmt.each_pair do |fmtspec, val|
49+
next if fmtspec != "-#{flag}"
4850

49-
fmt.each_pair do |fmtspec, val|
50-
next if fmtspec != cfs
51+
param = nil
5152

52-
param = nil
53+
if val[0]
54+
param = args[idx + 1]
55+
skip_next = true
56+
end
5357

54-
if val[0]
55-
param = args[idx + 1]
56-
skip_next = true
58+
yield fmtspec, idx, param
5759
end
58-
59-
yield fmtspec, idx, param
6060
end
6161
else
6262
yield nil, idx, arg

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ class Console::CommandDispatcher::Stdapi::Sys
7171
"-c" => [ false, "Filter only child processes of the current shell" ],
7272
"-h" => [ false, "Help menu." ])
7373

74+
#
75+
# Options for the 'pgrep' command.
76+
#
77+
@@pgrep_opts = Rex::Parser::Arguments.new(
78+
"-S" => [ true, "Filter on process name" ],
79+
"-U" => [ true, "Filter on user name" ],
80+
"-A" => [ true, "Filter on architecture" ],
81+
"-x" => [ false, "Filter for exact matches rather than regex" ],
82+
"-s" => [ false, "Filter only SYSTEM processes" ],
83+
"-c" => [ false, "Filter only child processes of the current shell" ],
84+
"-l" => [ false, "Display process name with PID" ],
85+
"-f" => [ false, "Display process path and args with PID (combine with -l)" ],
86+
"-h" => [ false, "Help menu." ])
87+
7488
#
7589
# Options for the 'suspend' command.
7690
#
@@ -418,9 +432,19 @@ def cmd_pkill_help
418432
# Filters processes by name
419433
#
420434
def cmd_pgrep(*args)
421-
if args.include?('-h')
422-
cmd_pgrep_help
423-
return true
435+
f_flag = false
436+
l_flag = false
437+
438+
@@pgrep_opts.parse(args) do |opt, idx, val|
439+
case opt
440+
when '-h'
441+
cmd_pgrep_help
442+
return true
443+
when '-l'
444+
l_flag = true
445+
when '-f'
446+
f_flag = true
447+
end
424448
end
425449

426450
all_processes = client.sys.process.get_processes
@@ -430,10 +454,6 @@ def cmd_pgrep(*args)
430454
return true
431455
end
432456

433-
# XXX fix Rex parser to properly handle adjacent short flags
434-
f_flag = args.include?('-f') || args.include?('-lf') || args.include?('-fl')
435-
l_flag = args.include?('-l') || args.include?('-lf') || args.include?('-fl')
436-
437457
processes.each do |p|
438458
if l_flag
439459
if f_flag
@@ -451,7 +471,7 @@ def cmd_pgrep(*args)
451471
def cmd_pgrep_help
452472
print_line("Usage: pgrep [ options ] pattern")
453473
print_line("Filter processes by name.")
454-
print_line @@ps_opts.usage
474+
print_line @@pgrep_opts.usage
455475
end
456476

457477
#

0 commit comments

Comments
 (0)