gdb
拡張機能のpwndbgを使用している。
チートシート
情報参照
infoコマンド。iで省略可能。
| 省略 | 説明 |
|---|---|
i func | 関数の一覧 |
i var | 変数の一覧 |
i reg | レジスタの一覧 |
i proc all | プロセスのメモリマップ全体を俯瞰して |
i b | brakpointの一覧 |
i w | watchpointの一覧 |
breakpoints
| コマンド | 説明 |
|---|---|
b <line_num> | breakpointをline_numにセット |
b <func_name> | breakpointをfunc_nameにセット |
i b | brakpointの一覧 |
disable <breakpoint_num> | 指定されたbreakpointを無効化する |
enable <breakpoint_num> | 指定されたbreakpointを有効化する |
delete <breakpoint_num> | 指定されたbreakpointを削除する |
delete | すべてのbreakpointを削除する |
watch <val_name> | watchpointをセット |
i watch | watchpointの一覧 |
一通りやってみる
1. サンプルプログラム
#include <stdio.h>#include <stdlib.h>
void copy_arg(char *string){ char buffer[10]; strcpy(buffer, string); printf("%s\n", buffer); return 0;}
int main(int argc, char **argv){ printf("argv[1] is:\n"); copy_arg(argv[1]);}コンパイル
# デバッグ情報を埋め込む$ gcc -g -O0 -o overflow_no_symbol overflow.c
# デバッグ情報を埋め込まない$ gcc -O0 -o overflow_no_symbol overflow.c
# アセンブラを出力$ gcc -S -O0 overflow.c
# 一覧$ lsoverflow overflow.c overflow_no_symbol overflow.s2. gdbに読み込ませる
シンボル情報がある場合(-qでバナーを出さない)
$ gdb ./overflow -qpwndbg: loaded 198 commands. Type pwndbg [filter] for a list.pwndbg: created $rebase, $ida gdb functions (can be used with print/break)Reading symbols from ./overflow... <= シンボルを読み込んだよpwndbg>シンボル情報がない場合
$ gdb ./overflow_no_symbol -qpwndbg: loaded 198 commands. Type pwndbg [filter] for a list.pwndbg: created $rebase, $ida gdb functions (can be used with print/break)Reading symbols from ./overflow_no_symbol...(No debugging symbols found in ./overflow_no_symbol) <= シンボルがないよpwndbg>これ以降はシンボル情報がある場合(gdb ./overflow -q)で進める
3. breakpoint
breakpointを設定すると、その行の前で一旦停止できる
line 8にbreakpointをセットする
pwndbg> b 8Breakpoint 1 at 0x1168: file overflow.c, line 8.関数copy_arg()breakpointをセットする
pwndbg> b copy_argBreakpoint 2 at 0x1155: file overflow.c, line 7.関数mainにtemporary breakpointをセットする
pwndbg> tbreak mainTemporary breakpoint 3 at 0x1186: file overflow.c, line 14.条件がTRUEの場合に動作するbreakpointを作る
pwndbg> b main if <some conditions>セットされたbreakpointの一覧
pwndbg> info bNum Type Disp Enb Address What1 breakpoint keep y 0x0000000000001168 in copy_arg at overflow.c:82 breakpoint keep y 0x0000000000001155 in copy_arg at overflow.c:73 breakpoint del y 0x0000000000001186 in main at overflow.c:14breakpointを無効化する(削除はしない)
pwndbg> disable 1pwndbg> info bNum Type Disp Enb Address What1 breakpoint keep n 0x0000000000001168 in copy_arg at overflow.c:82 breakpoint keep y 0x0000000000001155 in copy_arg at overflow.c:73 breakpoint del y 0x0000000000001186 in main at overflow.c:14breakpointを有効化する
pwndbg> enable 1pwndbg> info bNum Type Disp Enb Address What1 breakpoint keep y 0x0000000000001168 in copy_arg at overflow.c:82 breakpoint keep y 0x0000000000001155 in copy_arg at overflow.c:73 breakpoint del y 0x0000000000001186 in main at overflow.c:14breakpointをひとつ削除する
pwndbg> delete 1Num Type Disp Enb Address What2 breakpoint keep y 0x0000000000001155 in copy_arg at overflow.c:73 breakpoint del y 0x0000000000001186 in main at overflow.c:14breakpointを全部削除する
pwndbg> info bNo breakpoints or watchpoints.watchpoint
watchpointを設定すると、その変数が変更された後で一旦停止できる
変数bufferにwatchpointをセットする
pwndbg> watch bufferWatchpoint 5: bufferセットされたwatchipointの一覧
pwndbg> info watchpointsNum Type Disp Enb Address What5 watchpoint keep y buffer