Finding the Offset
Write a unique pattern of bytes to consecutive memory addresses to determine which offset overwrites the return address. The pattern value at the return address location reveals the exact offset.
Before an attacker can exploit a buffer overflow, they need to know exactly how many bytes of input reach the return address. The technique: write a unique pattern (1, 2, 3, 4, 5...) into the buffer and read back what value landed at the return address. If the return address contains '4', then the 4th byte of input overwrites it — meaning the offset is 3 (the buffer is 3 bytes). This is the real-world equivalent of using tools like pattern_create in Metasploit.
Simulator
Walkthrough Guide
- 1
Set up the return address and pattern
Do this: Step through the setup: return address 15 at address 203, pattern start at address 200
Watch for: Address 203 = 15 (return address). R0 = 200 (start), R1 = 1 (pattern start), R2 = 6 (count)
We set a known return address (15) at address 203 and prepare to write 6 unique pattern bytes starting from address 200. More bytes than the buffer can hold ensures we overwrite past the buffer.
- 2
Write the unique pattern
Do this: Step through the pattern-writing loop
Watch for: Addresses 200-205 receive values 1, 2, 3, 4, 5, 6. Address 203 changes from 15 to 4
Each address gets a unique value: addr[200]=1, addr[201]=2, addr[202]=3, addr[203]=4, addr[204]=5, addr[205]=6. The pattern is designed so that each position is identifiable by its value.
- 3
Read back the return address location
Do this: Step through LOAD R5, [R3]
Watch for: R5 = 4. The pattern value 4 was found at the return address location
R5 = 4 tells us that the 4th byte of input landed on the return address. Therefore the buffer is 3 bytes long (offsets 0, 1, 2) and the return address is at offset 3. The attacker now knows exactly where to place their target address in the payload.
Objectives
- •Watch pattern values 1,2,3,4,5,6 written to addresses 200-205
- •See address 203 (return address) get overwritten with pattern value 4
- •Verify R5 = 4, revealing that the 4th byte of input overwrites the return address
- •Understand how this technique maps input offset to critical memory locations
Hints
- Each pattern byte is unique, so the value found at the return address tells you exactly which byte position in the input reached it