Skip to content

Commit b11f941

Browse files
author
kernelsmith
committed
cleaned up at validate_pids conversion, fixed YARD doc
in validate_pids no longer need dup as conversion to ints was cleaned up to use map. Which also improved readability and allowed adding uniq and compact, thanks egypt. YARD doc on cmd_suspend was incorrectly organized
1 parent 92e8def commit b11f941

File tree

1 file changed

+21
-26
lines changed
  • lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi

1 file changed

+21
-26
lines changed

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

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,17 @@ def cmd_kill(*args)
282282
end
283283

284284
# validate all the proposed pids first so we can bail if one is bogus
285-
clean_pids = validate_pids(args)
285+
valid_pids = validate_pids(args)
286286
args.uniq!
287-
diff = args - clean_pids.map {|e| e.to_s}
287+
diff = args - valid_pids.map {|e| e.to_s}
288288
if not diff.empty? # then we had an invalid pid
289289
print_error("The following pids are not valid:#{diff.join(", ").to_s}, quitting")
290290
return false
291291
end
292292

293293
# kill kill kill
294-
print_line("Killing: #{clean_pids.join(", ").to_s}")
295-
client.sys.process.kill(*(clean_pids.map { |x| x }))
294+
print_line("Killing: #{valid_pids.join(", ").to_s}")
295+
client.sys.process.kill(*(valid_pids.map { |x| x }))
296296
return true
297297
end
298298

@@ -320,8 +320,7 @@ def cmd_kill_help
320320
def validate_pids(arr_pids, allow_pid_0 = false, allow_session_pid = false)
321321

322322
return [] if (arr_pids.class != Array or arr_pids.empty?)
323-
pids = arr_pids.dup
324-
clean_pids = []
323+
valid_pids = []
325324
# to minimize network traffic, we only get host processes once
326325
host_processes = client.sys.process.get_processes
327326
if host_processes.length < 1
@@ -332,28 +331,22 @@ def validate_pids(arr_pids, allow_pid_0 = false, allow_session_pid = false)
332331
# get the current session pid so we don't suspend it later
333332
mypid = client.sys.process.getpid.to_i
334333

335-
# we convert to integers here separately because we want to uniq this array first so we
336-
# can avoid redundant lookups later
337-
pids.each_with_index do |pid,idx|
338-
next if pid.nil?
339-
pids[idx] = pid.to_i
340-
end
341-
# uniq'ify
342-
pids.uniq!
334+
# remove nils & redundant pids, conver to int
335+
clean_pids = pids.compact.uniq.map{|x| x.to_i}
343336
# now we look up the pids & remove bad stuff if nec
344-
pids.delete_if do |p|
337+
clean_pids.delete_if do |p|
345338
( (p == 0 and not allow_pid_0) or (p == mypid and not allow_session_pid) )
346339
end
347-
pids.each do |pid|
340+
clean_pids.each do |pid|
348341
# find the process with this pid
349342
theprocess = host_processes.select {|x| x["pid"] == pid}.first
350343
if ( theprocess.nil? )
351344
next
352345
else
353-
clean_pids << pid
346+
valid_pids << pid
354347
end
355348
end
356-
return clean_pids
349+
return valid_pids
357350
end
358351

359352
#
@@ -740,12 +733,14 @@ def cmd_shutdown(*args)
740733
end
741734

742735
#
743-
# @param args [Array] Suspends a list of one or more pids
736+
# Suspends or resumes a list of one or more pids
744737
# args can optionally be -c to continue on error or -r to resume instead of suspend,
745738
# followed by a list of one or more valid pids
746739
# A suspend which will accept process names will be added later
747-
# @return [Boolean] Returns true if command was successful, else false
748740
#
741+
# @param args [Array] List of one of more pids
742+
# @return [Boolean] Returns true if command was successful, else false
743+
749744
def cmd_suspend(*args)
750745
# give'em help if they want it, or seem confused
751746
if ( args.length == 0 or (args.length == 1 and args[0].strip == "-h") )
@@ -757,9 +752,9 @@ def cmd_suspend(*args)
757752
resume = args.delete ("-r") || false
758753

759754
# validate all the proposed pids first so we can bail if one is bogus
760-
clean_pids = validate_pids(args)
755+
valid_pids = validate_pids(args)
761756
args.uniq!
762-
diff = args - clean_pids.map {|e| e.to_s}
757+
diff = args - valid_pids.map {|e| e.to_s}
763758
if not diff.empty? # then we had an invalid pid
764759
print_error("The following pids are not valid:#{diff.join(", ").to_s}")
765760
if continue
@@ -773,9 +768,9 @@ def cmd_suspend(*args)
773768
#client.sys.process.kill(*(args.map { |x| x.to_i }))
774769
targetprocess = nil
775770
if resume
776-
print_status("Resuming: #{clean_pids.join(", ").to_s}")
771+
print_status("Resuming: #{valid_pids.join(", ").to_s}")
777772
begin
778-
clean_pids.each do |pid|
773+
valid_pids.each do |pid|
779774
print_status("Targeting process with PID #{pid}...")
780775
targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
781776
targetprocess.thread.each_thread do |x|
@@ -791,9 +786,9 @@ def cmd_suspend(*args)
791786
return false unless continue
792787
end
793788
else # suspend
794-
print_status("Suspending: #{clean_pids.join(", ").to_s}")
789+
print_status("Suspending: #{valid_pids.join(", ").to_s}")
795790
begin
796-
clean_pids.each do |pid|
791+
valid_pids.each do |pid|
797792
print_status("Targeting process with PID #{pid}...")
798793
targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
799794
targetprocess.thread.each_thread do |x|

0 commit comments

Comments
 (0)