This section of challenges introduces a new vulnerability stemming from the format string: the format write. Before solving this challenge, let's discuss how to use this vulnerability.
The Arbitrary Write is an additional modifier to the printf format string list: %n. %n is a unique format specifier that, rather than writing data from a parameter to standard output, it writes data to the parameter. %n writes the number of bytes written thus far to the parameter of choice. Consider the following code:
1#include <stdio.h>
2
3int main()
4{
5 int bytes;
6This still prints Hello World!, but also moves the number of bytes written (12) to bytes.
We can use this format specifier to change the value of an address in memory, so long as the address of that location is present on the stack. This is the arbitrary write vulnerability, and a very powerful one!
The challenge indicates we must overwrite a variable currently valued at 0xdeadbeef:
./challYour goal is to modify the variable @ 0x7ffda83f79c8to be any other value.This exercise is to modify the variable using the %n // %hn // %hhn specifier.0x7ffda83f79b0: 0x0000000000000000 0x00000000000000000x7ffda83f79c0: 0x0000000000000000 0x00000000deadbeef0x7ffda83f79d0: 0x0000000000000001 0x00007fca9a4d8ca8Enter input:We notice there is a clear format string vulnerability:
10x00000000004015d4 <+222>: lea rax,[rbp-0x20]
20x00000000004015d8 <+226>:Our goal is to take advantage of the format write. In this case, we only have 0x18 bytes of input. This challenge only tasks us with changing the input; we can easily clear it to 0 only using a few bytes.
The order of arbitrary writes is the following:
The most difficult task with format writes is determining what parameter to use. We'll need to remember what number we might use as we leave a placeholder for that value. If you're confused, bear with me here.
First, we must write the number of bytes. In this case, we can simply write 0 bytes as an easy way to change the value. No padding needed.
Then, we need to write the format specifier. This will be in the form %x$[size]n, where x is the argument to use. This takes four bytes. We must pad this to the size of an address block, so we need four bytes of padding after this. This means the start of our payload is one address block long. This is essential for later.
lln: Writing a long long (useful for clearing out a block)ln: Writing a longhn: Writing a shorthhn: Writing a byte (most used specifier, used to change one byte at a time)Finally, we need where to write. This address is leaked for us, so we'll need to parse for this address.
Now that we have the entire payload, we can retroactively compute the argument numbers. Looking at gdb, we're writing to rbp-0x20. When the stack frame is initialized, it was initialized to size 0x20. Therefore, we start writing at rsp. Using our knowledge of computing the argument offset to our buffer (or any desired value), we can easily recognize the buffer starts writing at argument 6. Since the front half of the payload is one address block long, the address of where we're writing (the back half of the payload) must be at argument 7. We'll use lln to ensure the entire block gets cleared. Although n will almost always do this naturally, it's good to enforce 8 bytes be written and not leave it to the Runtime.
Therefore, this makes the payload:
%7$llnAA\x88\xfa\x70\x6e\xff\x7f\x00\x00To write an exploit, we need the leak first.
1print(p.recvuntil(b'variable @ ').decode())
2leak = int(p.recvline(), 16)Then, we write the arbitrary-write payload. We make use of ljust to make this payload a bit cleaner. Rather than including the padding, we can simply use this to pad to our desired size.
1payload = b'%7$lln'.ljust(8, b'\x00')
2payload += p64(leak)With this, it gets sent to the binary!
p64(leak) pads to the size of an address using 0x00, a null-byte for strings. This null byte would trigger printf to stop printing, and thus the format specifier doesn't get resolved. This nullifies the exploit.python3 solution.py[*] './chall' Arch: amd64-64-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x400000) Stripped: No[+] Starting local process './chall': pid 150635[*] Switching to interactive modeto be any other value.This exercise is to modify the variable using the %n // %hn // %hhn specifier.0x7ffde1f11860: 0x0000000000000000 0x00000000000000000x7ffde1f11870: 0x0000000000000000 0x00000000deadbeef0x7ffde1f11880: 0x0000000000000001 0x00007fc5a5ce4ca8Enter input: [*] Process './chall' stopped with exit code 0 (pid 150635)0x7ffde1f11860: 0x00006e6c6c243725 0x00007ffde1f118780x7ffde1f11870: 0x000000000000000a 0x00000000000000000x7ffde1f11880: 0x0000000000000001 0x00007fc5a5ce4ca8