Skip to content

Commit b326682

Browse files
author
kernelsmith
committed
Addressed egypt's comments
-changed the suspend/resume loop logic to reduce code duplication. -fixed up some print_*'s to remove embedded \n's -changed formatting on some error messages -switched comment to a TODO: -change host_processes.select (blah} to use .find instead -adjusted code due to remvoal of the pids.dup, resulting in arr_pids disappearing
1 parent b11f941 commit b326682

File tree

1 file changed

+37
-47
lines changed
  • lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi

1 file changed

+37
-47
lines changed

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

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def cmd_kill(*args)
286286
args.uniq!
287287
diff = args - valid_pids.map {|e| e.to_s}
288288
if not diff.empty? # then we had an invalid pid
289-
print_error("The following pids are not valid:#{diff.join(", ").to_s}, quitting")
289+
print_error("The following pids are not valid: #{diff.join(", ").to_s}. Quitting")
290290
return false
291291
end
292292

@@ -300,26 +300,27 @@ def cmd_kill(*args)
300300
# help for the kill command
301301
#
302302
def cmd_kill_help
303-
print_line("Usage: kill pid1 pid2 pid3 ...\n\nTerminate one or more processes.")
303+
print_line("Usage: kill pid1 pid2 pid3 ...")
304+
print_line("Terminate one or more processes.")
304305
end
305306

306307
#
307308
# validates an array of pids against the running processes on target host
308-
# behavior can be controlled to allow/deny proces 0 and the session's process
309-
# the pids:
310-
# - are converted to integers
311-
# - have had pid 0 removed unless allow_pid_0
312-
# - have had current session pid removed unless allow_session_pid (to protect the session)
313-
# - have redundant entries removed
309+
# behavior can be controlled to allow/deny proces 0 and the session's process
310+
# the pids:
311+
# - are converted to integers
312+
# - have had pid 0 removed unless allow_pid_0
313+
# - have had current session pid removed unless allow_session_pid (to protect the session)
314+
# - have redundant entries removed
314315
#
315316
# @param pids [Array<String>] The pids to validate
316317
# @param allow_pid_0 [Boolean] whether to consider a pid of 0 as valid
317318
# @param allow_session_pid [Boolean] whether to consider a pid = the current session pid as valid
318319
# @return [Array] Returns an array of valid pids
319320

320-
def validate_pids(arr_pids, allow_pid_0 = false, allow_session_pid = false)
321+
def validate_pids(pids, allow_pid_0 = false, allow_session_pid = false)
321322

322-
return [] if (arr_pids.class != Array or arr_pids.empty?)
323+
return [] if (pids.class != Array or pids.empty?)
323324
valid_pids = []
324325
# to minimize network traffic, we only get host processes once
325326
host_processes = client.sys.process.get_processes
@@ -339,7 +340,7 @@ def validate_pids(arr_pids, allow_pid_0 = false, allow_session_pid = false)
339340
end
340341
clean_pids.each do |pid|
341342
# find the process with this pid
342-
theprocess = host_processes.select {|x| x["pid"] == pid}.first
343+
theprocess = host_processes.find {|x| x["pid"] == pid}
343344
if ( theprocess.nil? )
344345
next
345346
else
@@ -734,9 +735,9 @@ def cmd_shutdown(*args)
734735

735736
#
736737
# Suspends or resumes a list of one or more pids
737-
# args can optionally be -c to continue on error or -r to resume instead of suspend,
738-
# followed by a list of one or more valid pids
739-
# A suspend which will accept process names will be added later
738+
# args can optionally be -c to continue on error or -r to resume instead of suspend,
739+
# followed by a list of one or more valid pids
740+
# TODO: A suspend which will accept process names, much of that code is done
740741
#
741742
# @param args [Array] List of one of more pids
742743
# @return [Boolean] Returns true if command was successful, else false
@@ -756,7 +757,7 @@ def cmd_suspend(*args)
756757
args.uniq!
757758
diff = args - valid_pids.map {|e| e.to_s}
758759
if not diff.empty? # then we had an invalid pid
759-
print_error("The following pids are not valid:#{diff.join(", ").to_s}")
760+
print_error("The following pids are not valid: #{diff.join(", ").to_s}.")
760761
if continue
761762
print_status("Continuing. Invalid args have been removed from the list.")
762763
else
@@ -769,40 +770,28 @@ def cmd_suspend(*args)
769770
targetprocess = nil
770771
if resume
771772
print_status("Resuming: #{valid_pids.join(", ").to_s}")
772-
begin
773-
valid_pids.each do |pid|
774-
print_status("Targeting process with PID #{pid}...")
775-
targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
776-
targetprocess.thread.each_thread do |x|
777-
targetprocess.thread.open(x).resume
778-
end
779-
end
780-
rescue ::Rex::Post::Meterpreter::RequestError => e
781-
print_error "Error resuming the process threads: #{e.to_s}. " +
782-
"Try migrating to a process with the same owner as the target process"
783-
"Also consider running the win_privs post module and confirm SeDebug priv."
784-
ensure
785-
targetprocess.close if targetprocess
786-
return false unless continue
787-
end
788-
else # suspend
773+
else
789774
print_status("Suspending: #{valid_pids.join(", ").to_s}")
790-
begin
791-
valid_pids.each do |pid|
792-
print_status("Targeting process with PID #{pid}...")
793-
targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
794-
targetprocess.thread.each_thread do |x|
795-
targetprocess.thread.open(x).suspend
796-
end
775+
end
776+
begin
777+
valid_pids.each do |pid|
778+
print_status("Targeting process with PID #{pid}...")
779+
targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
780+
targetprocess.thread.each_thread do |x|
781+
if resume
782+
targetprocess.thread.open(x).resume
783+
else
784+
targetprocess.thread.open(x).suspend
785+
end
797786
end
798-
rescue ::Rex::Post::Meterpreter::RequestError => e
799-
print_error "Error suspending the process threads: #{e.to_s}. " +
800-
"Try migrating to a process with the same owner as the target process"
801-
"Also consider running the win_privs post module and confirm SeDebug priv."
802-
ensure
803-
targetprocess.close if targetprocess
804-
return false unless continue
805787
end
788+
rescue ::Rex::Post::Meterpreter::RequestError => e
789+
print_error "Error acting on the process: #{e.to_s}. " +
790+
"Try migrating to a process with the same owner as the target process"
791+
"Also consider running the win_privs post module and confirm SeDebug priv."
792+
return false unless continue
793+
ensure
794+
targetprocess.close if targetprocess
806795
end
807796
return true
808797
end
@@ -811,7 +800,8 @@ def cmd_suspend(*args)
811800
# help for the suspend command
812801
#
813802
def cmd_suspend_help
814-
print_line("Usage: suspend [options] pid1 pid2 pid3 ...\n\nSuspend one or more processes.")
803+
print_line("Usage: suspend [options] pid1 pid2 pid3 ...")
804+
print_line("Suspend one or more processes.")
815805
print @@suspend_opts.usage
816806
end
817807

0 commit comments

Comments
 (0)