write464
Last updated
Was this helpful?
Last updated
Was this helpful?
today we are going to be tackling the 4th challenge on ROP Emporium which is a series of challenges to teach ROP Return Oriented Programming
download the x86 zip file and extract it using unzip, you will get 2 files a 32 bit executable and a flag.txt file which will be revealed if you exploit the binary
the executable is not stripped which makes reverse engeneering easier because it doesnt hide function names and the executable is dynamically linked so the libc library which contains many external functions like fprintf() are not loaded within the executable but they dynamically linked at the execution time
we have only one security configuration enabled which is NX No-Execute
also known as Data Execution Prevention or DEP
marks certain areas of the program as not executable, meaning that stored input or data cannot be executed as code. This is significant because it prevents attackers from being able to jump to custom shellcode that they've stored on the stack or in a global variable.
let's locate strings and functions imported in this binary using rabin2
using ghidra to do more reversing on this binary we find the print_file() function which takes the string "nonexsistent" as argument so our goal is to write the flag.txt string somewhere and call the print_file() function with the argument flag.txt so it can be printed
let's take a look at the sections and their sizes and permissions in order to write our string somewhere there
now we will pick one of this sections that meets those conditions :
enough size so we can write our string
the section should be writeable
but even if those conditions are meet there is high possibility if the section we have choosed contains other data this may corrupt the binary so using ghidra let's take a look on the sections and find which sections maybe interesting for our purpose , data section is empty so this makes it a good one to write in it our string flag.txt
we will use gdb-pwndbg and cyclic from pwntools to generate a pattern and overwrite RIP and then search the pattern that made into RIP to find the offset to the RIP
let's run the program and send this pattern as input, so faaaaaaa is the pattern that made it into the RIP
seaching the pattern and the offset is 40
using gdb-pwndbg list all the functions in the binary and we find a function called usefulgadgets
let's disassemble this function
mov QWORD PTR [r14],r15; ret; => mov r15 register into the memory location pointed by the r14 register and then return
so the idea here is to find a way to empty the r14 register and put the data section address in it and then put the 8 bytes of the string "flag.txt" in the r15 and then use this gadget mov QWORD PTR [r14],r15; ret;
to write "flag.txt" string that is in the r15 in the memory location pointed by the data section address (r14)
lets find the gadget that pops the r14 and the r15 using ropper
pop the r14 and put the data section address in it
pop the r15 and put "flag.txt" inside it
mov the r15 (flag.txt) to the memory location pointed by the r14 (data section)
pop rdi and put the data address in it
call print_file() function with the argument in the rdi
using ropper to find the pop rdi gadget
let's run the script and BOOM we've got the flag
another exploit using ROP class
let's run this script and BOOM we've got the flag