Sigreturn Oriented Programming
Small Boi CSAW 2019
Last updated
Was this helpful?
Small Boi CSAW 2019
Last updated
Was this helpful?
Running the file command shows that the challenge is a 64-bit elf executable.
Using the radare2 suite to check the strings within the binary. It becomes apparent the string "/bin/sh" is present within the read only data section.
Checking the memory protections of the binary shows the only exploit mitigation in place is Data Execution Prevention.
The program is taking the address of RBP-0x20
and then stores it within the RSI register. This means the allocated space for the buffer is 0x20 or 32 in decimal.
The syscall number for SYS_READ is stored in the RAX register.
The file descriptor is set to STDIN within the RDI register.
Defines the amount of bytes the SYS_READ syscall will read.
Due to the amount of bytes read being greater than the allocated space the application suffers from a Buffer Overflow vulnerability.
To confirm the vulnerability, the program was given 16 bytes more than allocated on the stack, causing the first 8 bytes to overwrite the stack frame's stored RBP whereas the last 8 bytes overwrite the stack frame's Return Address.
Before diving further in, there are a few restrictions this binary presents.
Data Execution Prevention is enabled, thus we cannot return to something such as shellcode.
Neither is regular Return Oriented Programming a valid method of exploitation due to the lack of control over the RDI Register which is needed for the first argument of a syscall.
However, the binary does include a "POP RAX" gadget as well as the "syscall" instruction. To conclude the conditions met.
A very large overflow with no shortage of space.
Control over the RAX register which allows the specification of a syscall number.
Access to a syscall instruction.
The string "/bin/sh" is present within the binary.
Control over the Stack Frame's saved return address; this translates to control over the stack itself.
The combination of the conditions above allows implementation of Sigreturn Oriented Programming.
Before performing the sigreturn syscall, the following Signal Frame needs to be present on the stack.
The first step of exploitation is to redirect the binary into performing a SYS_SIGRETURN syscall, then to return into a curated signal frame which results in a shell.
In order accomplish this initial syscall, the following must take place:
Provide enough input to have the following bytes overwrite the return address which redirects code execution.
POP the currently stored value out of the EAX register to prepare for a value to be given.
Assign the Syscall number for SYS_Sigreturn to EAX register.
Use the syscall instruction to perform the SYS_Sigreturn Syscall.
Using the pwntools library, generating a Sigreturn Frame resulting in a shell is relatively straightforward. To begin, specifying the Architecture is necessary. This can be accomplished with.
Using the following code the registers can be assigned values to prepare the registers for an SYS_EXECVE syscall.
The previous code snippets combined result in the following completed exploit.