pwnable.kr(4)

pwnable.krの4記事目。
linuxの仮想環境立てるのがめんどくさくておくれてしまいました。

pwnable.kr

まず、添付されてるフラグをダウンロードする。

[b1u3 Downloads]$ sudo u+x ./flag
[b1u3 Downloads]$ ./flag
I will malloc() and strcpy the flag there. take it.

オォン。

[b1u3 Downloads]$ objdump -d flag

flag:     ファイル形式 elf64-x86-64

!!!??。
stringsやってもなんかめぼしいのが出なかった。

こっからは調べました!!!

参考にしたのはgoogleで検索してgithubにまとめてあったやつ。

github.com

なんで逆アセンブルできなかったんだろうっていう疑問がここで解決した。
UPXっていうフリーのパッカーを使ってるようだ。
UPXの問題はセキュリティコンテストチャレンジブックに載ってたと思う。

http://amzn.asia/c0mKeYM

美しき策謀のほうだったかな。どっちかわすれました。

UPXをインストールして、解凍する。

[b1u3 Downloads]$ sudo pacman -S upx
[b1u3 Downloads]$ upx -d flag -o flag_decompressed
[b1u3@localhost]$ readelf -s flag_decompressed |grep main
    51: 00000000006c27c0  2184 OBJECT  LOCAL  DEFAULT   26 main_arena
   395: 00000000006c5580     8 OBJECT  LOCAL  DEFAULT   27 _nl_loaded_domains
   916: 0000000000494f60   214 FUNC    GLOBAL DEFAULT    7 _nl_unload_domain
   934: 000000000041dac0  5900 FUNC    GLOBAL DEFAULT    6 _nl_load_domain
  1017: 00000000006c60e0     8 OBJECT  GLOBAL DEFAULT   27 _nl_domain_bindings
  1193: 0000000000430b80    49 FUNC    GLOBAL DEFAULT    6 _IO_switch_to_main_wget_a
  1301: 000000000041d830   645 FUNC    GLOBAL DEFAULT    6 _nl_find_domain
  1600: 0000000000401164    61 FUNC    GLOBAL DEFAULT    6 main
  1719: 00000000004ae998     5 OBJECT  GLOBAL DEFAULT   10 _libc_intl_domainname
  1760: 0000000000494f10    73 FUNC    GLOBAL DEFAULT    7 _nl_finddomain_subfreeres
  1938: 00000000004011b0   590 FUNC    GLOBAL DEFAULT    6 __libc_start_main
  2043: 0000000000402d80    43 FUNC    GLOBAL DEFAULT    6 _IO_switch_to_main_get_ar

mainシンボルがちゃんとある。
こっから、

[b1u3 Downloads]$ gdb flag_decompressed -q
Reading symbols from flag_decompressed...(no debugging symbols found)...done.
(gdb) r
Starting program: /home/b1u3/Downloads/flag_decompressed 
I will malloc() and strcpy the flag there. take it.
[Inferior 1 (process 30254) exited normally]
(gdb) disass
No frame selected.
(gdb) disass main
Dump of assembler code for function main:
   0x0000000000401164 <+0>:	push   %rbp
   0x0000000000401165 <+1>:	mov    %rsp,%rbp
   0x0000000000401168 <+4>:	sub    $0x10,%rsp
   0x000000000040116c <+8>:	mov    $0x496658,%edi
   0x0000000000401171 <+13>:	callq  0x402080 <puts>
   0x0000000000401176 <+18>:	mov    $0x64,%edi
   0x000000000040117b <+23>:	callq  0x4099d0 <malloc>
   0x0000000000401180 <+28>:	mov    %rax,-0x8(%rbp)
   0x0000000000401184 <+32>:	mov    0x2c0ee5(%rip),%rdx        # 0x6c2070 <flag>
   0x000000000040118b <+39>:	mov    -0x8(%rbp),%rax
   0x000000000040118f <+43>:	mov    %rdx,%rsi
   0x0000000000401192 <+46>:	mov    %rax,%rdi
   0x0000000000401195 <+49>:	callq  0x400320
   0x000000000040119a <+54>:	mov    $0x0,%eax
   0x000000000040119f <+59>:	leaveq 
   0x00000000004011a0 <+60>:	retq   
End of assembler dump.
(gdb) x/10xb
Argument required (starting display address).
(gdb) x/10xb 0x6c2070
0x6c2070 <flag>:	0x28	0x66	0x49	0x00	0x00	0x00	0x00	0x00
0x6c2078:	0x00	0x00
(gdb) e/s 0x496628
Ambiguous command "e/s 0x496628": .
(gdb) x/s 0x496628

ところどころコマンドミスってるけど気にしないでほしい。

コマンドの説明すると、

gdbを-qで起動時の余計な出力を省いて起動する。
一度実行 r(run) する。
mainフレームを逆アセンブルする。
謎のコメントがあるので、そのアドレスに格納されている値を調べる x(examine)/10(10個)x(16進数で)b(バイト単位で)。
0x6c2070の0x28 0x66 0x49をリトルエンディアンのアドレスとして(0x496628)、x(examine)/x(stringで) 調べる。

これで、フラグが出た。やったぜ!!!