Pwnable.kr - flag
We continue our beginner series on Binary Exploitation with pwnable.kr which appears to have a reasonable curve of difficulty making it ideal for learning.
Let’s go for this 4rd challenge: flag.
Challenge Description
Flag
Papa brought me a packed present! let’s open it.
Download : http://pwnable.kr/bin/flag
This is reversing task. all you need is binary
Test & Analysis
This time we can retrieve the binary but not its source code.
1 | [hg8@archbook ~]$ wget [http://pwnable.kr/bin/flag](http://pwnable.kr/bin/flag) |
The program doesn’t seem to take input:
1 | [hg8@archbook ~]$ ./flag |
Malloc
The binary output is taking about malloc()
, what is that exactly ? Let’s take a look at the manual page:
1 | [hg8@archbook ~]$ man malloc |
Malloc stands for “memory allocation”, it is to allocate memory dynamically during program execution. When you call malloc()
, it reserves a block of memory of a specified size and returns a pointer to the first byte of this memory block. This memory is then typically used for creating dynamic data structures like arrays, linked lists, or other objects.
Binary analysis
Since we don’t have the source code, let’s see what informations we can retrieve from the binary itself, running file
indicate the
1 | [hg8@archbook ~]$ file flag |
Bummer, no section header
means that all the debugging info, code, data, symbols, and other metadata have been stripped out.
Let’s now use strings
to see if we can find some interesting strings in the binary:
1 | [hg8@archbook ~]$ file flag |
UPX is a name I am familiar with, it stands for “Ultimate Packer for Executables”. It is a compression utility designed to compress and decompress executable files.
This kind of tools are often referred as “packer”. It’s primarily used for reducing the size of executable files to save storage space and improve execution times, but it can also be employed for security-related tasks.
A good news with UPX, is that under normal conditions, it’s quite easy to decompress a packed executable. Let’s give a try by downloading and executing UPX:
1 | [hg8@archbook ~]$ upx -d flag |
Dynamic Analysis
Now that we unpacked the binary, we can start with the usual dynamic analysis using gdb
to understand what is happening the program is running.
Let’s start by disassembling the main()
:
1 | [hg8@archbook ~]$ gdb flag-unpacked |
Seems like a comment is indicating a address of the flag: # 0x6c2070 <flag>
Let’s print out the content of 0x6c2070
:
1 | pwndbg> x/1s *0x6c2070 |
Bingo! We got our flag.
Turn out that since there is obfuscations we could also have used strings
again to retrieve the flag:
1 | [hg8@archbook ~]$ strings ./flag-unpacked | grep "delivery" |
But wait, what’s the link with Malloc ?
Let’s do a proper analysis. First let’s set a breakpoint in main:
1 | [hg8@archbook ~]$ gdb flag-unpacked |
Then run the program with r
and run the binary step by step to see what’s happening in memory (ni
).
After a few steps we can see malloc being called with 0x64 as argument (RDI
), that 100.
And a few instruction further we can see the being send to the memory area reserved by malloc:
That’s it folks! I hope you enjoyed this series on Buffer Overflow.
As always do not hesitate to contact me for any questions or feedback!
See you next time ;)
-hg8