ret2syscall
Set register values => call "int 0x80" (x86) or "syscall" (x86_64)
Theory
If the binary itself contains int 0x80 (x86) or syscall (x86_64) as well as necessary ROP gadgets, you might want to try ret2syscall to get a shell. To find such gadgets, use ROPgadget or Ropper. With this method, you simply build an ROP chain to set all relevant registers to some proper state and call int 0x80 (x86) or syscall (x86_64) when everything is ready. This idea is the same as an execve() shellcode.
Below is a quick cheat sheet. This cheat sheet provides a general idea of how ret2syscall is exploited, but it is definitely NOT the only way of doing it. If any gadget is missing from the binary, use your creativity to build equivalent ROP chains.
32-bit ret2syscall
Calling Convention
syscall number:
$eax1st parameter:
$ebx2nd parameter:
$ecx3rd parameter:
$edx4th parameter:
$esi5th parameter:
$edi6th parameter:
$ebpcall
int 0x80when everything is ready.
Calling execve("/bin/sh", 0, 0)
execve("/bin/sh", 0, 0)Set
$eaxto0xbROPgadget --binary vuln --only "pop|ret" | grep eax
Set
$ebxto the address of the string"/bin/sh"ROPgadget --binary vuln --only "pop|ret" | grep ebxROPgadget --binary vuln --string "/bin/sh"
Set
$ecxto0ROPgadget --binary vuln --only "pop|ret" | grep ecx
Set
$edxto0ROPgadget --binary vuln --only "pop|ret" | grep edx
Call
int 0x80(opcode for syscall on x86)ROPgadget --binary vuln --only "int"
64-bit ret2syscall
Calling Convention
syscall number:
$rax1st parameter:
$rdi2nd parameter:
$rsi3rd parameter:
$rdx4th parameter:
$r105th parameter:
$r86th parameter:
$r9call
syscallwhen everything is ready.
Note: If there exists more arguments, the extra ones will be stored on the stack.
Calling execve("/bin/sh", 0, 0)
execve("/bin/sh", 0, 0)Set
$raxto0x3bROPgadget --binary vuln --only "pop|ret" | grep rax
Set
$rdito the address of the string"/bin/sh"ROPgadget --binary vuln --only "pop|ret" | grep rdiROPgadget --binary vuln --string "/bin/sh"
Set
$rsito0ROPgadget --binary vuln --only "pop|ret" | grep rsi
Set
$rdxto0ROPgadget --binary vuln --only "pop|ret" | grep rdx
Find
syscallROPgadget --binary vuln --only "syscall"
Nuance
Oftentimes, the binary does not contain the string "/bin/sh". If that is the case, we should pass "/bin/sh" to the .bss section before exploiting ret2syscall. The address of .bss can be easily found using Pwntools: bss = elf.bss(). The binary may contain some input function, such as gets(). We can utilize input functions such as gets() for initializing a STDIN session and then input the string "/bin/sh" in this STDIN session.
Last updated
Was this helpful?