Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. ret-overwrite
7 min read

Return Overwrite

This challenge takes our overwrite further and performs a Buffer Overflow. Let's look at the challenge description:

ret-overwrite
$ ./challYour goal is to modify the return pointer @ 0x7ffe00e7a128to a function called win() in the binary.Hints: Use objdump or gdb!0x7ffe00e7a100: 0x0000000000000000 0x00000000000000000x7ffe00e7a110: 0x0000000000000000 0x00000000000000000x7ffe00e7a120: 0x0000000000000001 0x00007f5ebb243ca8Enter input:

We need to overwrite the return pointer. First, let's understand what the return pointer does.

What is the return pointer?
When functions are called during runtime, the Instruction Pointer must move elsewhere to execute those instructions. However, it must remind itself where to return once it's done with the called function. That is what the return pointer is for; it is an address on the stack that the instruction pointer returns to once the called function is finished.

Something about this doesn't seem right. We shouldn't be allowed to modify the return pointer without restriction. This is due to a vulnerability in the program known as a buffer overflow or stack smashing. To understand the vulnerability, we must dive into GDB.

Analysis with GDB

I will skip right to the disassembly of main; however, I recommend following the standard procedure of running info functions first to see what else might be valuable.

We take a close look at the fgets call, which gets our input:

nasm
10x00000000004015da <+228>:   mov    rdx,QWORD PTR [rip+0x2a8f]        # 0x404070 <stdin@GLIBC_2.2.5>
20x00000000004015e1

Here, we notice something super interesting. We start writing our data at rbp-0x20 (please ensure you know why!), but we can write 0x32 bytes. This means we're allowed to write more bytes than space allows! This is the buffer overflow vulnerability. We can write more data than the space allotted, meaning we can overwrite critical information for the binary.

So, what's there to overwrite? To answer this, we must understand how function calls work.

Binaries start at a defined function called _start. This is called the binary's entry point. We often ignore this entry point in favor of main, which we typically consider the entry point. _start calls main, so this isn't a horrible assumption to make. When the call function is used, two vital things happen:

  1. The address of the next instruction is pushed on the stack. This is the return pointer, which tells the instruction pointer where to return once the called function is finished.
  2. The instruction pointer jumps to the address passed into the call statement.

Pushing the return pointer is essential to understanding the layout of our current stack frame. Once this happens, any method with a stack frame will have the following header:

nasm
14014f6:       55                      push   rbp
24014f7:       48 89 e5                mov    rbp,rsp
34014fa:       48 83 ec 20             sub    rsp,0x20

The value in the sub statement changes based on the method. This is the size of the stack frame created. We also see before that, rbp is pushed on the stack. We noticed from the first two challenges that all stack variables are referenced relative to rbp, meaning rbp's location is very important. This makes our stack frame:

| --------- stack space --------- | rbp | return address |

The inverse of this happens at the end of the method:

nasm
1401641:       b8 00 00 00 00          mov    eax,0x0
2401646:       c9                      leave
3401647:       c3                      ret

rax serves as the return register, meaning the calling function will look at rax after the function call to check for returns. leave is a keyword for mov rsp, rbp; pop rbp , which inverts the operations done at the top of the method. Then we have ret.

ret takes whatever is at the top of the stack at the time and goes there. This is a logical operation since the setup and tear-down of stack frames leave the return address on the top of the stack as expected. However, if we stack smash, we can change this address and force the instruction pointer to go there. It's important to note that ret does not validate that the value wasn't changed; it blindly goes there.

Therefore, if we can overwrite all the data leading up to the return pointer and then overwrite the return pointer, we can hijack execution and go elsewhere. This is what buffer overflow vulnerabilities are all about. They aren't much different than the variable overwrite but are more dangerous.

Crafting our Payload

Our payload will look similar to the payload from the last exercise. The first thing we need is our padding, or the distance from where we start writing until the return pointer. The visualization gives us that hint, but let's use the Assembly to confirm our guess.

Here is the fgets call we're interested in:

nasm
10x00000000004015da <+228>:   mov    rdx,QWORD PTR [rip+0x2a8f]        # 0x404070 <stdin@GLIBC_2.2.5>
20x00000000004015e1

From this, we gather that we start writing at rbp-0x20. Now, where is the return pointer? Using our stack frame visualization, we see it's the next block after rbp. Since rbp is 8 bytes long, the return pointer is at rbp+0x8. This makes our padding (rbp+0x8)−(rbp−0x20)=0x28(\text{rbp}+0x8)-(\text{rbp}-0x20)=0x28(rbp+0x8)−(rbp−0x20)=0x28. Now, we need to figure out where to go. We're instructed to go to win(), which has an address in memory. There are three ways to figure that out:

  • readelf: This displays information about ELF (Linux executable) files. The -s flag provides section headers. Therefore, we can use readelf -s chall | grep win to filter for the win() function.
  • gdb: We can use info functions win inside gdb to filter for the win() function. Or, if we don't want to go inside GDB, use echo "info functions win" | gdb chall.
  • objdump: This provides the disassembly. We can filter for the win() function here, too, using objdump -d chall | grep win.

We see all of these yield the same address 0x4014e0:

$ readelf -s chall | grep win    38: 00000000004014e0    22 FUNC    GLOBAL DEFAULT   14 win$ objdump -d chall | grep '<win>:'00000000004014e0 <win>:$ echo "info functions win" | gdb -q chall0x00000000004014e0  win

We now need to pack the address in 64-bit little-endian format. If you're using pwntools, use p64(0x4014e0) to pack it. Otherwise, we can pack by hand: \xe0\x14\x40\x00\x00\x00\x00\x00. Finally, we put the payload together and send it to the binary:

solution.py
$ python3 solution.py[+] Starting local process './chall': pid 35827[*] Switching to interactive modeYour goal is to modify the return pointer @ 0x7fffa1c347a8to a function called win() in the binary.Hints: Use objdump or gdb!0x7fffa1c34780: 0x0000000000000000 0x00000000000000000x7fffa1c34790: 0x0000000000000000 0x00000000000000000x7fffa1c347a0: 0x0000000000000001 0x00007fedd78b4ca8Enter input:0x7fffa1c34780: 0x4141414141414141 0x41414141414141410x7fffa1c34790: 0x4141414141414141 0x41414141414141410x7fffa1c347a0: 0x4141414141414141 0x00000000004014e0Congrats! You successfully changed execution flow.[*] Got EOF while reading in interactive$[*] Process './chall' stopped with exit code -11 (SIGSEGV) (pid 35827)
Why does the program stop with SIGSEGV after our exploit?
This means that the binary crashed! This happens once we finish win(), the instruction pointer will pull whatever's on top of the stack for the return pointer. Since we jumped from main(), which doesn't have a stack frame below it, there's no return pointer for win() to use. The instruction pointer ends up taking a random value and jumping there, then recognizes there's no valid instruction to execute, and crashes.
<
+
235
>
: lea
rax
,
[
rbp
-
0x20
]
30x00000000004015e5 <+239>: mov esi,0x32
40x00000000004015ea <+244>: mov rdi,rax
50x00000000004015ed <+247>: call 0x401070 <fgets@plt>
<
+
235
>
: lea
rax
,
[
rbp
-
0x20
]
30x00000000004015e5 <+239>: mov esi,0x32
40x00000000004015ea <+244>: mov rdi,rax
50x00000000004015ed <+247>: call 0x401070 <fgets@plt>