1.若干命令速查
-
file <文件名>:加載被調試的可執行程序文件。因為一般都在被調試程序所在目錄下執行GDB,因而文本名不需要帶路徑。示例:(gdb) file gdb-sample
-
r:Run的簡寫,運行被調試的程序,也是重啟程序運行。如果此前沒有下過斷點,則執行完整個程序;如果有斷點,則程序暫停在第一個可用斷點處。
-
c:Continue的簡寫,繼續執行被調試程序,直至下一個斷點或程序結束。
-
b <行號>: b <函數名稱>/ b *<函數名稱>/b *<代碼地址>,b: Breakpoint的簡寫,設置斷點。兩可以使用“行號”“函數名稱”“執行地址”等方式指定斷點位置。其中在函數名稱前面加“*”符號表示將斷點設置在“由編譯器生成的prolog代碼處”。如果不了解匯編,可以不予理會此用法。
-
set args [parameter]:db調試的時候,設置命令行參數。
-
bt:bt可以去查看堆棧信息。down和up和一個一個打印堆棧信息。bt直接打印全部堆棧信息。
-
n:單步調試.執行完后顯示的是下一條命令。
-
s單步進入
-
finish:如果想讓程序執行到當前函數返回之后停止,用finish,當前函數的剩余語句將會正常運行。
- 保存斷點:save breakpoint fig8.3.bp;輸入斷點:gdb fig8.3 -x fig8.3.bp(-x參數)
- gcc 編譯有源碼情況,調試帶源碼。gcc x.c -o x -g
- nm可以查看程序函數固定偏移和符號表情況
- gdb attach pid:attach已經運行的進程。i locals 查看變量值。
- readelf -a xx;查看xx內存數據分布情況
- 內存映射: i proc m (info proc mappings 的簡寫)核查零是不是有效地址
- gdb高級用法:http://blog.jobbole.com/107759/
- gdb執行文件中的命令(批處理):source -v file.txt
- 查看pe映射:cat /proc/28124/maps。相當於vmmap更全。
-
解決gdb調試分頁問題:type return to continue,or q <return> to quit:set pagination off
2.查看程序安全特性:
- peda命令:checksec,可以看到NX是否啟用。
- linux系統查看是否開啟aslr:cat /proc/sys/kernel/randomize_va_space 2
- 查看程序加載機制,或者分布圖,三種方法:
- info sharedlibrary
- peda:vmmap
- pmap -x pid:更全面
1.gdbserver遠程調試
gdbserver 192.168.16.1:7678 ./over//若有參數,加上參數。
//這是gdbserver啟動程序運行。另外一種是attach方式:./gdbserver :12345 --attach 3334 &//&表明后台運行
gdb ./over
target remote 192.168.16.1:7678
continue
info sharedlibrary :查看加載的模塊
以調試lampp中apache httpd為例:
(1).gdbserver單進程啟動httpd:gdbserver :12345 ./httpd -X
(2).gdb掛上:gdb ./httpd
(gdb) target remote 127.0.0.1:12345
Remote debugging using 127.0.0.1:12345
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading symbols from target:/lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug/.build-id/e7/d1cfa2253c89a2f4ad9588f8db4f1c1fc58467.debug...done.
done.//這里可以看到是有符號文件的。
0x00007ffff7dd9cc0 in _start () from target:/lib64/ld-linux-x86-64.so.2
(gdb) b ap_process_request
(gdb) continue
Reading /opt/lampp/lib/libpcre.so.0 from remote target...//注意gdb是從遠程讀取so文件
Reading /opt/lampp/lib/libaprutil-1.so.0 from remote target...
(gdb) info sharedlibrary
From To Syms Read Shared Object Library
0x00007ffff7dd9ac0 0x00007ffff7df53c0 Yes target:/lib64/ld-linux-x86-64.so.2
0x00007ffff7bbd230 0x00007ffff7bd3ec8 Yes (*) target:/opt/lampp/lib/libpcre.so.0
0x00007ffff799f030 0x00007ffff79b50c8 Yes (*) target:/opt/lampp/lib/libaprutil-1.so.0
0x00007ffff776d9f0 0x00007ffff7789568 Yes (*) target:/opt/lampp/lib/libexpat.so.1
0x00007ffff7487370 0x00007ffff749c618 Yes (*) target:/opt/lampp/lib/libiconv.so.2
0x00007ffff7262f60 0x00007ffff727ca68 Yes (*) target:/opt/lampp/lib/libapr-1.so.0
0x00007ffff7051100 0x00007ffff7053ecf Yes target:/lib/x86_64-linux-gnu/librt.so.1
2.其他命令
nm - list symbols from object files
objdump -tT libgd.so//查看無符號情況
轉:http://blog.csdn.net/zhangmiaoping23/article/details/41009053
條件斷點
設置一個條件斷點,條件由cond指定;在gdb每次執行到此斷點時,cond都被計算。當cond的值為非零時,程序在斷點處停止。
用法:break [break-args] if (condition)
例如:
break main if argc > 1
break 180 if (string == NULL && i < 0)
break test.c:34 if (x & y) == 1
break myfunc if i % (j+3) != 0
break 44 if strlen(mystring) == 0
b 10 if ((int)$gdb_strcmp(a,"chinaunix") == 0)
b 10 if ((int)aa.find("dd",0) == 0)
condition
可以在我們設置的條件成立時,自動停止當前的程序,先使用break(或者watch也可以)設置斷點,然后用condition來修改這個斷點的停止(就是斷)的條件。
用法:condition <break_list> (conditon)
例如:
cond 3 i == 3
condition 2 ((int)strstr($r0,".plist") != 0)
ignore
如果我們不是想根據某一條件表達式來停止,而是想斷點自動忽略前面多少次的停止,從某一次開始才停止,這時ignore就很有用了。
用法:ignore <break_list> count。上面的命令行表示break_list所指定的斷點號將被忽略count次。例如:ignore 1 100,表示忽略斷點1的前100次停止
為斷點設置命令列表
設置一個斷點並且在上面中斷后,我們必須會查詢一些變量或者做一些其他動作。如果這些動作可以一起呵成,豈不妙哉!使用命令列表(commands)就能實現這個功能。
步驟:
1.建立斷點。2.使用commands命令
用法:
commands <break_list>
例如:
(gdb) commands 1
Type commands for when breakpoint 1 is hit,one per line.
End with a line saying just "end".
>silent
>print "n= %d \n",n
>continue
>end
文件記錄 :斷點2在open函數開頭
(gdb) commands 2
Type commands for when breakpoint 2 is hit,one per line.
End with a line saying just "end".
>x/s $r0
>continue
>end
如果遇到指針情況,一定要先轉化,例如push [ebp-0x24];
則為x/s *(int *)(ebp-0x24)
---------------------------------------------------------------------------
4. GDB寄存器和內存 :轉:http://blog.chinaunix.net/uid-22315114-id-99972.html
- 查看寄存器
(gdb) i r
(gdb) i r a # 查看所有寄存器(包括浮點、多媒體)
(gdb) i r esp
(gdb) i r pc
- 查看內存
(gdb) x /wx 0x80040000 # 以16進制顯示指定地址處的數據
(gdb) x /8x $esp
(gdb) x /16x $esp+12
(gdb) x /16s 0x86468700 # 以字符串形式顯示指定地址處的數據
(gdb) x /24i 0x8048a51 # 以指令形式顯示指定地址處的數據(24條)
- 修改寄存器的值
(gdb) set $v0 = 0x004000000
(gdb) set $epc = 0xbfc00000
- 修改內存的值
(gdb) set {unsigned int}0x8048a51=0x0
(gdb) set *(unsigned int*)0x8048a54=0x55aa55aa
- 內存搜索
Usage: find <start> <end> <count> <value>
(gdb) define find
set $ptr = $arg0
set $cnt = 0
while ( ($ptr<=$arg1) && ($cnt<$arg2) )
if ( *(unsigned int *)$ptr == $arg3 )
x /wx $ptr
set $cnt = $cnt + 1
end
set $ptr = $ptr + 4
end
end
- 斷點、監測點
(gdb) b *0x80400000
(gdb) watch *(unsigned int *)0xbffff400==0x90909090
- gdb調試有參數的程序:下面可以使用兩種方法輸入命令行參數
1)run 命令行參數
2)set args 命令行參數
- gdb查看main函數:
(gdb) b main
Breakpoint 1 at 0x40072a
(gdb) x/16i main
fork多進程調試一般有一下3種方法:
轉:http://blog.csdn.net/fingding/article/details/46459095
1. follow-fork-mode
- void debug_wait(char *tag)
- {
- while(1)
- {
- if (tag存在) <span style="color:#ff0000;">// tag可以是一個環境變量,也可以是一個文件等</span>
- 睡眠一段時間;
- else
- break;
- }
- }
peda
PEDA - Python Exploit Development Assistance for GDB
Key Features:
- Enhance the display of gdb: colorize and display disassembly codes, registers, memory information during debugging.
- Add commands to support debugging and exploit development (for a full list of commands use
peda help
):aslr
-- Show/set ASLR setting of GDBchecksec
-- Check for various security options of binarydumpargs
-- Display arguments passed to a function when stopped at a call instructiondumprop
-- Dump all ROP gadgets in specific memory rangeelfheader
-- Get headers information from debugged ELF fileelfsymbol
-- Get non-debugging symbol information from an ELF filelookup
-- Search for all addresses/references to addresses which belong to a memory rangepatch
-- Patch memory start at an address with string/hexstring/intpattern
-- Generate, search, or write a cyclic pattern to memoryprocinfo
-- Display various info from /proc/pid/pshow
-- Show various PEDA options and other settingspset
-- Set various PEDA options and other settingsreadelf
-- Get headers information from an ELF fileropgadget
-- Get common ROP gadgets of binary or libraryropsearch
-- Search for ROP gadgets in memorysearchmem|find
-- Search for a pattern in memory; support regex searchshellcode
-- Generate or download common shellcodes.skeleton
-- Generate python exploit code templatevmmap
-- Get virtual mapping address ranges of section(s) in debugged processxormem
-- XOR a memory region with a key
Installation
git clone https://github.com/longld/peda.git ~/peda
echo "source ~/peda/peda.py" >> ~/.gdbinit
echo "DONE! debug your program with gdb and enjoy"