Ret2syscall
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
This is similar to Ret2lib, however, in this case we won't be calling a function from a library. In this case, everything will be prepared to call the syscall sys_execve
with some arguments to execute /bin/sh
. This technique is usually performed on binaries that are compiled statically, so there might be plenty of gadgets and syscall instructions.
In order to prepare the call for the syscall it's needed the following configuration:
rax: 59 Specify sys_execve
rdi: ptr to "/bin/sh" specify file to execute
rsi: 0 specify no arguments passed
rdx: 0 specify no environment variables passed
So, basically it's needed to write the string /bin/sh
somewhere and then perform the syscall
(being aware of the padding needed to control the stack). For this, we need a gadget to write /bin/sh
in a known area.
Another interesting syscall to call is mprotect
which would allow an attacker to modify the permissions of a page in memory. This can be combined with ret2shellcode.
Let's start by finding how to control those registers:
With these addresses it's possible to write the content in the stack and load it into the registers.
First you need to find a writable place in the memory
Then you need to find a way to write arbitrary content in this address
The following command creates a full sys_execve
ROP chain given a static binary when there are write-what-where gadgets and syscall instructions:
If you are lacking gadgets, for example to write /bin/sh
in memory, you can use the SROP technique to control all the register values (including RIP and params registers) from the stack:
https://guyinatuxedo.github.io/07-bof_static/dcquals19_speedrun1/index.html
64 bits, no PIE, nx, write in some memory a ROP to call execve
and jump there.
https://guyinatuxedo.github.io/07-bof_static/bkp16_simplecalc/index.html
64 bits, nx, no PIE, write in some memory a ROP to call execve
and jump there. In order to write to the stack a function that performs mathematical operations is abused
https://guyinatuxedo.github.io/07-bof_static/dcquals16_feedme/index.html
64 bits, no PIE, nx, BF canary, write in some memory a ROP to call execve
and jump there.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)