解題 pwnable 第 6 章より。
gdb を使って heap 領域を中心にメモリの中身を見る。
- start: いいところでブレークしてくれる。run だとブレークポイントの設定が必要。
- s: step in. 関数の中にも入る。
- n: next. 関数の中には入らない。
- Ctrl+C: プログラムをいったん止める。continue で再開。
- info proc mappings: メモリ割り当て状態を俯瞰。
- x/21000x 0x606000: 0x606000 から 0x21000 分のメモリの中身を表示する。
- info variables ^cage$: グローバル変数のうち、正規表現 “^cage$” にヒットするものを表示する。
まずはプログラムを動かす。
$ gdb -q birdcage
Reading symbols from birdcage...
(No debugging symbols found in birdcage)
(gdb) start
Temporary breakpoint 1 at 0x402321
Starting program: /home/ubuntu/kaidai-pwnable/birdcage/birdcage
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Temporary breakpoint 1, 0x0000000000402321 in main ()
(gdb) s
Single stepping until exit from function main,
which has no line number information.
Birdcage
capture <index> cock|owl|parrot
sing <index>
release <index>
list
exit
> capture 0 parrot
Talk to: parrot1
> capture 1 parrot
Talk to: parrot2
> ^C
Program received signal SIGINT, Interrupt.
0x00007ffff7914852 in __GI___libc_read (fd=0, buf=0x7ffff7a1ab23 <_IO_2_1_stdin_+131>, nbytes=1) at ../sysdeps/unix/sysv/linux/read.c:26
26 ../sysdeps/unix/sysv/linux/read.c: No such file or directory.
次にメモリの中身を見ていく。
(gdb) info proc mappings
process 11259
Mapped address spaces:
Start Addr End Addr Size Offset Perms objfile
0x400000 0x405000 0x5000 0x0 r-xp /home/ubuntu/kaidai-pwnable/birdcage/birdcage
0x604000 0x605000 0x1000 0x4000 r--p /home/ubuntu/kaidai-pwnable/birdcage/birdcage
0x605000 0x606000 0x1000 0x5000 rw-p /home/ubuntu/kaidai-pwnable/birdcage/birdcage
0x606000 0x627000 0x21000 0x0 rw-p [heap]
0x7ffff7800000 0x7ffff7828000 0x28000 0x0 r--p /usr/lib/x86_64-linux-gnu/libc.so.6
~~~
(gdb) x/21000x 0x606000
~~~
0x617ea0: 0x00000000 0x00000000 0x00000031 0x00000000
0x617eb0: 0x00604d08 0x00000000 0x00617ec8 0x00000000
0x617ec0: 0x00000000 0x00000000 0x72726170 0x0031746f
0x617ed0: 0x00000000 0x00000000 0x00000031 0x00000000
0x617ee0: 0x00604d08 0x00000000 0x00617ef8 0x00000000
0x617ef0: 0x00000000 0x00000000 0x72726170 0x0032746f
0x617f00: 0x00000000 0x00000000 0x0000f101 0x00000000
~~~
(gdb) info variables ^cage$
All variables matching regular expression "^cage$":
Non-debugging symbols:
0x0000000000605380 cage
(gdb) x/8x 0x605380
0x605380 <cage>: 0x00617eb0 0x00000000 0x00617ee0 0x00000000
0x605390 <cage+16>: 0x00000000 0x00000000 0x00000000 0x00000000
chunk size が 0x31、sing の vtable のアドレスが 0x00604d08 であることがわかる。
0x72726170 0x0031746f は parrot1 を示す。