Controlling EIP
Overwrite the return address.
Controlling EIP is a free Ethical Hacking Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ethical Hacking Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Goal
You have the offset, so you can place any four bytes into EIP. The question now is: what address should you put there?
You want EIP to point at your shellcode. But you cannot just hardcode the stack address; you need a reliable jump.
Why Not Hardcode the Stack Address?
The stack address where your shellcode sits can shift between runs and systems. Even small environment changes move it.
Instead, you use a stable instruction already in memory that redirects execution to wherever ESP currently points.
The JMP ESP Trick
If ESP points at your shellcode at crash time, you can set EIP to the address of a JMP ESP instruction. When EIP loads that address, the CPU executes jmp esp and jumps into your shellcode.
This indirection makes the exploit reliable regardless of the exact stack address.
jmp esp ; opcode FF E4Finding a JMP ESP
You search loaded modules (preferably ones without ASLR) for the opcode bytes FF E4. The module's base address stays constant, so this gadget is dependable.
Tools like mona.py find suitable instructions automatically.
!mona jmp -r esp -cpb "\x00"Avoiding Bad Characters
Some byte values break the input before it reaches the buffer intact. The classic one is the null byte 0x00, which terminates C strings. Others include 0x0a (newline) and 0x0d (carriage return).
The JMP ESP address must not contain any of these bad characters.
Identifying Bad Characters
To find which bytes are bad, send all byte values 0x00-0xff in the buffer and inspect memory in the debugger. Bytes that are mangled or missing are bad characters.
You will exclude these from both the return address and the shellcode.
!mona bytearray -cpb "\x00"Building the EIP Overwrite
Now assemble the input: padding to the offset, then the JMP ESP address in little-endian order.
If the JMP ESP lives at 0x625011AF, the bytes written are reversed.
buf = "A"*offset + "\xaf\x11\x50\x62" # 0x625011AF, little-endianVerifying the Jump
Set a breakpoint on the JMP ESP address in the debugger and run the exploit. When EIP reaches it and steps into ESP, you have confirmed reliable redirection.
At this point EIP points where your shellcode will go.
Making Room for Shellcode
After the return address, the remaining buffer (where ESP points) holds your shellcode. If space is tight, you may need a short jump or to relocate.
A NOP sled (a run of 0x90 no-op bytes) before the shellcode adds tolerance so the jump can land anywhere in the sled.
buf = "A"*offset + ret + "\x90"*16 + shellcodePutting It Together
The full exploit structure is now clear:
- Padding up to the offset.
- The JMP ESP address (no bad chars, little-endian).
- A NOP sled.
- The shellcode.
You are one step from a shell.
Alternatives to JMP ESP
JMP ESP is the classic redirect, but it is not the only one. If a different register points at your buffer, you can use jmp eax, call esp, or a push esp; ret gadget.
The principle is the same: find a stable instruction that transfers control to where your shellcode lives.
Quick Check
Recall why JMP ESP is preferred over a hardcoded stack address.
Recap
You now control execution flow:
- Overwrite EIP with the address of a JMP ESP gadget instead of a fragile stack address.
- Find the gadget in a non-ASLR module, avoiding bad characters like
0x00. - Write the address in little-endian and add a NOP sled before shellcode.
Next you will generate shellcode and pop a shell.
Frequently asked questions
Is the “Controlling EIP” lesson free?
Yes — the full text of “Controlling EIP” is free to read here on the web, and the Ethical Hacking Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ethical Hacking Academy course, upgrade to CoddyKit PRO.
What will I learn in “Controlling EIP”?
Overwrite the return address. You practise Ethical Hacking Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ethical Hacking Academy?
No prior experience is required. Ethical Hacking Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Controlling EIP” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ethical Hacking Academy lesson?
Yes. Every Ethical Hacking Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.