Format-string vulnerability: writing to memory via %n
%x only reads — but another printf specifier in C, %n, WRITES the number of characters outputted to the screen so far to the specified address (pointer!). This is a full arbitrary write primitive.
def printf_n_sim(fmt_parts, reg_args, stack_args, memory):
"""fmt_parts — [('literal', matn) | ('pad', N, argN) | ('n', argN)] ro'yxati."""
chiqqan = 0
for part in fmt_parts:
if part[0] == 'literal':
chiqqan += len(part[1])
elif part[0] == 'pad': # %<N>x, kengligi majburlangan
chiqqan += part[1]
elif part[0] == 'n': # %<pos>$n
pos = part[1]
addr = reg_args[pos-1] if pos <= 5 else stack_args[pos-6]
memory[addr] = chiqqan # <-- YOZISH!
Your format string first outputs a known text (literal) consisting of 31 characters. After that, you place %<W>x (width-forced hex output, outputs W characters, regardless of its value), and then you write with %<N>$n.
The memory address where the "verification code" (for example, the authorization flag) is located inside the program is equal to ONE OF the arguments given in the table below — but you must know which one is a pointer and which one is simply register/stack garbage:
| N (6+i) | Value |
|---|---|
6 | 6333003 |
7 | 1774609 |
8 | 2768853 |
9 | 660287 |
10 | 5762165 |
11 | 3425080 |
12 | 1585436 |
13 | 484564 |
In this system, real memory addresses (pointers) are always located in the range 0x00600000–0x0060FFFF (the ".data" segment). Other values — simply smaller numbers (register garbage) — never fall into this range.
You must write exactly the value 5035 to this address (the program will only continue executing with this value).
Calculate two things: (1) the required width W for %x (such that prefix_len + W = maqsad qiymat), (2) the argument number N holding the pointer (from the table above).
Submit the answer in the format BAYROQ{PWN_WRITE_W<W>_N<N>} (for example W12_N7).