Final chain: leak + canary + ROP
The final challenge combines ALL the techniques learned in this section: leaking via format-string, correctly placing the stack canary and calling a 2-argument function via a ROP chain.
For simplicity, in ALL examples in this section, addresses/registers are considered to be 4-byte (32-bit), little-endian — in real 64-bit systems they are usually 8 bytes, but the idea itself (offset, ROP, canary, GOT...) remains the same.
Stage 1: leaking the canary
def printf_sim(pos, reg_args, stack_args):
"""Haqiqiy printf(fmt, ...) ning %N$x semantikasi:
N-argument (1-indeksli) — agar N <= 5 bo'lsa registrdan
(reg_args[N-1]), aks holda stekdan (stack_args[N-6]) olinadi."""
if pos <= 5:
return reg_args[pos - 1]
return stack_args[pos - 6]
The attacker sent "%11$x". The table below lists the arguments — ONE of them is actually the canary value itself (in decimal):
| N (6+i) | Value (one is the CANARY!) |
|---|---|
6 | 12340179 |
7 | 14147159 |
8 | 4886957 |
9 | 2855826 |
10 | 14419694 |
11 | 10649761 |
12 | 4010238 |
13 | 5731670 |
The canary is always 4 bytes and its LAST byte is 0x00 (little-endian: at the position of the MOST significant byte).
Stage 2: full payload
Buffer size: 12 bytes. After the canary comes a 4-byte saved register (value is not important), and then instead of the return address — the ROP chain begins.
The "calling convention" in this challenge is a simple model invented for educational purposes: the function takes its first argument from the R0 register, the second from R1, and if needed the third from R2 (in the real x86-64 System V ABI this happens via rdi, rsi, rdx — the idea is the same, the names are simplified).
Goal: to call maxfiy_funksiya(R0=178, R1=247) (address 0x00403086). Gadgets:
| Address | Instruction (gadget) |
|---|---|
0x00401910 | POP R2; RET |
0x00401DF8 | RET |
0x00401208 | POP R3; RET |
0x004014B0 | POP R1; RET |
0x00401FE0 | NOP; RET |
0x004018F8 | POP R0; RET |
Construct the full payload in the following order: 'A'*buffer + canary(1-bosqichdan, 4 bayt, kichik-endian) + 4 bayt ixtiyoriy + ROP zanjiri (gadget_R0, R0-qiymat, gadget_R1, R1-qiymat, maqsad_manzil — har biri 4 bayt).
Submit the answer in the format BAYROQ{PWN_FINAL_<to'liq payload, uzluksiz hex satr>}.