@@ -93,7 +93,8 @@ def commands
93
93
"getsid" => "Get the SID of the user that the server is running as" ,
94
94
"getenv" => "Get one or more environment variable values" ,
95
95
"kill" => "Terminate a process" ,
96
- "pkill" => "Terminate a process by name" ,
96
+ "pkill" => "Terminate processes by name" ,
97
+ "pgrep" => "Filter processes by name" ,
97
98
"ps" => "List running processes" ,
98
99
"reboot" => "Reboots the remote computer" ,
99
100
"reg" => "Modify and interact with the remote registry" ,
@@ -115,7 +116,8 @@ def commands
115
116
"getsid" => [ "stdapi_sys_config_getsid" ] ,
116
117
"getenv" => [ "stdapi_sys_config_getenv" ] ,
117
118
"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" ] ,
119
121
"ps" => [ "stdapi_sys_process_get_processes" ] ,
120
122
"reboot" => [ "stdapi_sys_power_exitwindows" ] ,
121
123
"reg" => [
@@ -403,6 +405,35 @@ def cmd_pkill_help
403
405
print_line @@ps_opts . usage
404
406
end
405
407
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
+
406
437
#
407
438
# validates an array of pids against the running processes on target host
408
439
# 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)
449
480
valid_pids
450
481
end
451
482
452
- def match_processes ( processes , args )
483
+ def match_processes ( processes , args , quiet : false )
453
484
454
485
search_proc = nil
455
486
search_user = nil
@@ -479,18 +510,18 @@ def match_processes(processes, args)
479
510
print_error "Enter an architecture"
480
511
processes = [ ]
481
512
else
482
- print_line "Filtering on arch '#{ val } "
513
+ print_line "Filtering on arch '#{ val } " if ! quiet
483
514
processes = processes . select do |p |
484
515
p [ 'arch' ] == val
485
516
end
486
517
end
487
518
when "-s"
488
- print_line "Filtering on SYSTEM processes..."
519
+ print_line "Filtering on SYSTEM processes..." if ! quiet
489
520
processes = processes . select do |p |
490
521
[ "NT AUTHORITY\\ SYSTEM" , "root" ] . include? p [ 'user' ]
491
522
end
492
523
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
494
525
current_shell_pid = client . sys . process . getpid
495
526
processes = processes . select do |p |
496
527
p [ 'ppid' ] == current_shell_pid
@@ -499,7 +530,7 @@ def match_processes(processes, args)
499
530
end
500
531
501
532
unless search_proc . nil?
502
- print_line "Filtering on '#{ search_proc } '"
533
+ print_line "Filtering on '#{ search_proc } '" if ! quiet
503
534
if exact_match
504
535
processes = processes . select do |p |
505
536
p [ 'name' ] == search_proc
@@ -513,7 +544,7 @@ def match_processes(processes, args)
513
544
end
514
545
515
546
unless search_user . nil?
516
- print_line "Filtering on user '#{ search_user } '"
547
+ print_line "Filtering on user '#{ search_user } '" if ! quiet
517
548
if exact_match
518
549
processes = processes . select do |p |
519
550
p [ 'user' ] == search_user
0 commit comments