Skip to content

Commit 85f7d73

Browse files
author
Brent Cook
committed
add pgrep as well
1 parent c9a85f5 commit 85f7d73

File tree

1 file changed

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

1 file changed

+39
-8
lines changed

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ def commands
9393
"getsid" => "Get the SID of the user that the server is running as",
9494
"getenv" => "Get one or more environment variable values",
9595
"kill" => "Terminate a process",
96-
"pkill" => "Terminate a process by name",
96+
"pkill" => "Terminate processes by name",
97+
"pgrep" => "Filter processes by name",
9798
"ps" => "List running processes",
9899
"reboot" => "Reboots the remote computer",
99100
"reg" => "Modify and interact with the remote registry",
@@ -115,7 +116,8 @@ def commands
115116
"getsid" => [ "stdapi_sys_config_getsid" ],
116117
"getenv" => [ "stdapi_sys_config_getenv" ],
117118
"kill" => [ "stdapi_sys_process_kill" ],
118-
"pkill" => [ "stdapi_sys_process_kill" ],
119+
"pkill" => [ "stdapi_sys_process_kill", "stdapi_sys_process_get_processes" ],
120+
"pgrep" => [ "stdapi_sys_process_get_processes" ],
119121
"ps" => [ "stdapi_sys_process_get_processes" ],
120122
"reboot" => [ "stdapi_sys_power_exitwindows" ],
121123
"reg" => [
@@ -403,6 +405,35 @@ def cmd_pkill_help
403405
print_line @@ps_opts.usage
404406
end
405407

408+
#
409+
# Filters processes by name
410+
#
411+
def cmd_pgrep(*args)
412+
if args.include?('-h')
413+
cmd_pgrep_help
414+
return true
415+
end
416+
417+
all_processes = client.sys.process.get_processes
418+
processes = match_processes(all_processes, args, quiet: true)
419+
420+
if processes.length == 0 || processes.length == all_processes.length
421+
return true
422+
end
423+
424+
pids = processes.collect { |p| p['pid'] }
425+
pids.each do | pid |
426+
print_line(pid.to_s)
427+
end
428+
true
429+
end
430+
431+
def cmd_pgrep_help
432+
print_line("Usage: pgrep [ options ] pattern")
433+
print_line("Filter processes by name.")
434+
print_line @@ps_opts.usage
435+
end
436+
406437
#
407438
# validates an array of pids against the running processes on target host
408439
# behavior can be controlled to allow/deny proces 0 and the session's process
@@ -449,7 +480,7 @@ def validate_pids(pids, allow_pid_0 = false, allow_session_pid = false)
449480
valid_pids
450481
end
451482

452-
def match_processes(processes, args)
483+
def match_processes(processes, args, quiet: false)
453484

454485
search_proc = nil
455486
search_user = nil
@@ -479,18 +510,18 @@ def match_processes(processes, args)
479510
print_error "Enter an architecture"
480511
processes = []
481512
else
482-
print_line "Filtering on arch '#{val}"
513+
print_line "Filtering on arch '#{val}" if !quiet
483514
processes = processes.select do |p|
484515
p['arch'] == val
485516
end
486517
end
487518
when "-s"
488-
print_line "Filtering on SYSTEM processes..."
519+
print_line "Filtering on SYSTEM processes..." if !quiet
489520
processes = processes.select do |p|
490521
["NT AUTHORITY\\SYSTEM", "root"].include? p['user']
491522
end
492523
when "-c"
493-
print_line "Filtering on child processes of the current shell..."
524+
print_line "Filtering on child processes of the current shell..." if !quiet
494525
current_shell_pid = client.sys.process.getpid
495526
processes = processes.select do |p|
496527
p['ppid'] == current_shell_pid
@@ -499,7 +530,7 @@ def match_processes(processes, args)
499530
end
500531

501532
unless search_proc.nil?
502-
print_line "Filtering on '#{search_proc}'"
533+
print_line "Filtering on '#{search_proc}'" if !quiet
503534
if exact_match
504535
processes = processes.select do |p|
505536
p['name'] == search_proc
@@ -513,7 +544,7 @@ def match_processes(processes, args)
513544
end
514545

515546
unless search_user.nil?
516-
print_line "Filtering on user '#{search_user}'"
547+
print_line "Filtering on user '#{search_user}'" if !quiet
517548
if exact_match
518549
processes = processes.select do |p|
519550
p['user'] == search_user

0 commit comments

Comments
 (0)