text
stringlengths
0
1.99k
binary as a parameter, here is the command I came up with:
ROPgadget --binary libc.so.6 --all --nojop --badbytes 0A
I marked 0A (newline) as a bad byte because the code is reading our input
with gets(), which cuts off at newline - so anything after that would just
be ignored.
Next, we are looking for two gadgets that are exactly x pages apart from
each other. For example, take a look at these:
1. 0x0015b456 : pop ebx ; pop esi ; pop edi ; pop ebp ; ret
2. 0x0015c456 : ret
Then we will use one of those gadgets in our buffer overflow - for example,
the second one. We will pair it with a libc base address guess, taken from
one of the program's previous runs.
For simplicity, let's go with a guessed libc base of 0xf0000000.
Here is what our stack looks like during the overflow:
+----------------------------------------+
| AAAAAAAAAA | AAAAAAAAAAAA | 0xf015c456 |
+----------------------------------------+
^
the return address --------------+
We are overwriting the return address with our gadget at 0xf015c456, sitting
exactly one page above the first ROP gadget inside our guessed libc.
And here is how the relevant chunk of memory in libc looks:
+----------------+
| |
| |
| |
| |
libc base address --> |----------------| 0xf0000000
| . |
| . |
| . |
|----------------|
| First gadget | 0xf015b456
| . |
|----------------|
relevant gadget --> | Second gadget | 0xf015c456
| . |
| . |
| . |
end of libc --> |----------------|
| |
| |
+----------------+
Now, if the program actually maps libc at our guessed address (0xf0000000)
then our second ROP gadget will get triggered right away. But what about
the first ROP gadget? How does it help?
Here is the trick: since libc is always loaded with page alignment, there is
an equal chance the base address might land at 0xf0001000, just one page off
from our original guess. In that case, the memory layout shifts, and looks
like this:
+----------------+
| |
| |
| |
| |
libc base address --> |----------------| 0xf0001000
| . |
| . |
| . |
|----------------|
relevant gadget --> | First gadget | 0xf015c456
| . |
|----------------|
| Second gadget | 0xf015d456
| . |
| . |
| . |
end of libc --> |----------------|
| |
| |
+----------------+
So now, if libc loads one page higher, our overwritten return address will
still point into libc - but this time it hits the first gadget instead of
the second. That way, we have doubled our chances of a successful hit,
without changing the payload!
In order to successfully execute arbitrary code in both cases, we need to
know which ROP gadget was triggered - which tells us what the libc base
address is. Once we figure that out, we can continue crafting the rest of
our chain accordingly.
You might have noticed that in both of our gadgets, we change the stack
pointer. However, each ROP gadget adjusts it by a different amount. In our
example, the first gadget pops four more values than the second. This subtle
difference is key: it lets us identify which ROP gadget actually ran based
on where the stack pointer ends up at.