PicoCTF Buffer Overflow -part 2

PicoCTF Buffer Overflow -part 2

The last article was not complete, it had some knowledge gaps and partial results, we will be removing those inconveniences today.

The summary of yesterday's article is that we tried to overflow the binary, but the prod version did not overflow.

Let's get back to constructive problem solving, taking obvious steps that lead us to a solution.

We need to overflow the BUFSIZE variable, which is a 32-byte buffer. Let's try a sample payload of 32 random characters.

It did not change the return address. Let's try to add 4 more letters and try. Repeat this process until you get a change in value of the result. This approach is called fuzzing, usually it's automated.

Article content

Something changed while we tried 44 letters. This means at that point the overflow happened.

When we tried 48 it returned bask the ascii values of the last 4 letters in the results.(in reverse order)

Article content
0x6161616c stands for 'aaal' in ASCII

So what happened here?

In a stack frame, memory is structured like this :

[ Local Variables (e.g., buffer[32]) ][ Saved EBP ][ Return Address (EIP) ]        

Saved EBP - 4 bytes

Return Address EIP - 4 bytes

So, our payload completely overflowed the EIP. There will be some function specific padding used that's why the numbers didn't add up.

Now we need to get the address of the win function. Let's try readelf command.

Article content

It displays the symbol table of the ELF (Executable and Linkable Format) binary file vuln.

➜  Downloads readelf -s vuln | grep win
    63: 080491f6   139 FUNC    GLOBAL DEFAULT   13 win        

Now, let's replace the the 'laaa' with the address of win function 0x080491f6. We need to convert it into little endian first.


The byte \xf6 corresponds to the ASCII character ö in Latin-1 encoding. When printed directly, it may display as ö instead of the raw byte \xf6. To solve this, use sys.stdout.buffer.write() to write raw bytes (bypassing string encoding).

Article content

Now we got the our dummy flag correctly. Now lets try to access the prod flag (actual one).

python3 -c "import sys; sys.stdout.buffer.write(b'aaaabaaacaaadaaaeaaafaaagaaahaaaiaaajaaakaaa\xf6\x91\x04\x08\n')" | nc saturn.picoctf.net 63690        

Add a \n to the stdout value to get the pipe working.

Article content

Thanks for the read. Happy hacking.


Try out LiveAPI. Get your backend APIs documented automatically. It supports 90+ backend frameworks and 20+ programming languages. Please give it a try and provide your feedback.

To view or add a comment, sign in

More articles by Sreedeep CV

  • Hackthebox Legacy

    This is one of the old retired boxes, We need to get good at attacking Windows boxes, so let's target this. Let's start…

  • HacktheBox Included

    This is the last remaining box in the starting point track. Let's dive in.

  • HackTheBox Markup

    This box is one of the last starting point boxes. Let's dive in and figure out the box, starting with an nmap scan.

  • Hackthebox Vaccine

    This is one of the boxes in tier 3, the starting point, which is quite difficult. I was stuck at the root flag, but…

  • HackTheBox Chemistry - Part 2

    This is the second part of the Chemistry Box in htb. Since we got a shell in the previous attempt, we will continue…

  • HackTheBox Chemistry - Part 1

    As always, start with an Nmap scan. There are two open ports: 22 (SSH) and 5000, which is likely hosting an HTTP server.

  • Automate YouTube Insights Into Obsidian - No Code, Just n8n

    N8n is an open-source workflow automation tool. This can help you create automation for your day-to-day tasks…

    2 Comments
  • Prompt Injection Techniques

    This article is based on a white paper by Sander Schulhoff on AI prompt engineering. He conducted a competition in…

  • Use Tmux to save your Terminals

    Managing terminals is super important if you are a hacker. You often do a lot of stuff and usually forget to document…

  • PicoCTF SSTI challenges

    SSTI are a quite intresting bug class. I have been tracking this for a while, here are some lab scenerios you can try…

Insights from the community

Others also viewed

Explore topics