Ret2syscall

Basic Information

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.

Register gadgets

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.

Write string

Writable memory

First you need to find a writable place in the memory

Write String in memory

Then you need to find a way to write arbitrary content in this address

Automate ROP chain

The following command creates a full sys_execve ROP chain given a static binary when there are write-what-where gadgets and syscall instructions:

32 bits

64 bits

Lacking Gadgets

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:

SROP - Sigreturn-Oriented Programming

Exploit Example

Other Examples & References

Last updated