Blog | G5 Cyber Security

JMP ESP & CALL ESI Exploit Guide

TL;DR

This guide explains how to exploit a buffer overflow vulnerability using the JMP ESP and CALL ESI techniques. We’ll cover finding suitable addresses, crafting an exploit payload, and executing shellcode.

Prerequisites

1. Identify the Vulnerability

The target program must have a buffer overflow vulnerability, typically in functions that handle user input without proper bounds checking (e.g., strcpy, gets). Use debugging tools to locate this function.

2. Find JMP ESP Gadget

  1. What is a JMP ESP gadget? It’s an instruction sequence in the program or loaded libraries that ends with jmp esp. This allows us to redirect execution to the stack, where our shellcode will be located.
  2. Use a disassembler (e.g., objdump, IDA Pro) to search for jmp esp within the executable and its linked libraries.
  3. objdump -d vulnerable_program | grep 'jmp esp'
  4. Record the address of a reliable jmp esp gadget.

3. Find CALL ESI Gadget

  1. What is a CALL ESI gadget? This instruction sequence allows us to call an address pointed to by the ESI register. We’ll use this to execute our shellcode, which will be placed in memory and whose address stored in ESI.
  2. Use a disassembler to search for call esi within the executable and its linked libraries.
  3. objdump -d vulnerable_program | grep 'call esi'
  4. Record the address of a reliable call esi gadget.

4. Craft the Exploit Payload

  1. Shellcode: Prepare your shellcode (e.g., using Metasploit or hand-written assembly). Ensure it’s position-independent code if necessary.
  2. Padding: Determine the amount of padding needed to overwrite the return address on the stack. This is done by debugging and observing where the return address is located relative to the buffer.
  3. JMP ESP Address: Include the address of your jmp esp gadget in the payload, overwriting the original return address.
  4. CALL ESI Address: Place the address of your call esi gadget after the JMP ESP address on the stack.
  5. ESI Value: Put the address where you’ll place your shellcode in memory immediately after the CALL ESI address. This will be loaded into the ESI register by the CALL ESI instruction.

5. Example Payload Structure (Conceptual)

[Buffer] + [Padding] + [JMP ESP Address] + [CALL ESI Address] + [Shellcode Address]

6. Execute the Exploit

  1. Run the vulnerable program with your crafted payload as input.
  2. If successful, execution will jump to jmp esp, then execute call esi which calls the address of shellcode in memory.

7. Debugging and Troubleshooting

Exit mobile version