@@ -45,6 +45,9 @@ class Console::CommandDispatcher::Stdapi::Sys
45
45
"-r" => [ true , "The remote machine name to connect to (with current process credentials" ] ,
46
46
"-w" => [ false , "Set KEY_WOW64 flag, valid values [32|64]." ] )
47
47
48
+ #
49
+ # Options for the 'ps' command.
50
+ #
48
51
@@ps_opts = Rex ::Parser ::Arguments . new (
49
52
"-h" => [ false , "Help menu." ] ,
50
53
"-S" => [ true , "Filters processes on the process name using the supplied RegEx" ] ,
@@ -262,20 +265,46 @@ def cmd_clearev(*args)
262
265
# Kills one or more processes.
263
266
#
264
267
def cmd_kill ( *args )
265
- if ( args . length == 0 )
266
- print_line (
267
- "Usage: kill pid1 pid2 pid3 ...\n \n " +
268
- "Terminate one or more processes." )
268
+ # give'em help if they want it, or seem confused
269
+ if ( args . length == 0 or ( args . length == 1 and args [ 0 ] . strip == "-h" ) )
270
+ cmd_kill_help
269
271
return true
270
272
end
271
273
272
- print_line ( "Killing: #{ args . join ( ", " ) } " )
274
+ # validate all the proposed pids first so we can bail if one is bogus
275
+ args . each do |arg |
276
+ if not is_valid_pid? ( arg )
277
+ print_error ( "#{ arg } is not a valid pid" )
278
+ cmd_kill_help
279
+ return false
280
+ end
281
+ end
273
282
283
+ # kill kill kill
284
+ print_line ( "Killing: #{ args . join ( ", " ) } " )
274
285
client . sys . process . kill ( *( args . map { |x | x . to_i } ) )
275
-
276
286
return true
277
287
end
278
288
289
+ #
290
+ # help for the kill command
291
+ #
292
+ def cmd_kill_help
293
+ print_line ( "Usage: kill pid1 pid2 pid3 ...\n \n Terminate one or more processes." )
294
+ end
295
+
296
+ #
297
+ # Checks if +pid+ is a valid looking pid
298
+ #
299
+ def is_valid_pid? ( pid )
300
+ # in lieu of checking server side for pid validity at the moment, we just sanity check here
301
+ pid . strip!
302
+ return false if pid . strip =~ /^-/ # invalid if it looks "negative"
303
+ return true if pid == "0" # allow them to kill pid 0, otherwise false
304
+ # cuz everything returned from .to_i that's not an int returns 0, we depend on the statement above
305
+ return true if pid . to_i > 0
306
+ end
307
+
279
308
#
280
309
# Lists running processes.
281
310
#
0 commit comments