Exploit 1: Stack buffer overflow.
Exploit 1: Stack buffer overflow.
Program
File: Elf x86_64
Porection: NX
Info
You can download the binary here: vuln
Process
It is always important to use the 'file' command to know the type of file going
under analyze. We can see that it is an ELF x64 program.
We will run the program in our sandbox and see what happens.
It seems that the program read the word we entered and send us a Hello, with our
word as argument.
Using r2 we can observe that the main program allocates a buffer, takes input using
the read function and then print Hello and input as printf argument.
The logic next step when analyzing program is to have an overview of the different
functions. We see the shell function who is really weird.
The shell function seems to call execve and pass the argument /bin/bash. But what
is execve a quick look at the man pages can tell us the truth about this function.
We can see that execve is a syscall who take the path of what should be executed as
argument. In our case /bin/bash. This clearly means that we can have a shell if we
are able to execute the shell function.
Wait... The binary file is has the setuid bit set, this means by exploiting it we
could gain root privileges. Since the file will run under the privileges of his
owner in our case root. This will so call execve who will execute /bin/bash and
this will drop us a root shell.
Before doing anything we will analyze current protections. I will use checksec tool
for this.
Perfect, seems the only protection is NX who stands for No-Executable Stack, since
we will not executing injected shellcode we dont care about this one for now.
Let's try to enter a enormous amount of A to trigger a seg fault by overflowing the
buffer.
Input buffer
Return address
Address of shell function
The Return address minus the start of the input buffer will give us the offset and
we will overwrite the return address with the address of the shell funciton.
The offset is obtained by calculating the return address minus the input buffer
address.
Its then needed to convert the hexadecimal in decimal you can use rax2 who do an
awesome work. I greatly advise you to add rax2 in your arsenal.
Offset = 108
Exploit
Time to craft our exploit!
We will use iPython for this with pwntools, this way will be very fast and
straightforward.
We use the p64() cause this will reverse the address to little-endian and 8 bytes
address format such as 0x8f11400000000000, if you not use p64() from pwntools be
sure to remember the correct formatting.
Finally we will interact with the process to be able to use the shell dropped.
We are now root! Congrats we exploited our first stack buffer overflow on x86_64
elf.
Attention
Please remove the program when you finished the exploitation its really dangerous
to keep them in your system since you were able to exploit this one, an attacker
could abuse the setuid binary and owning root in your own system!
Conclusion
We sucessfully exploit our first C program, this was quite straightforward but we
learned the basics. In the next post of this exploitation serie we will exploit a
protected program.