gdb调试若干问题

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//查看无符号情况

3.gdb断点高级技巧

转: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
>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

用法:set follow-fork-mode [parent|child] 
进入gdb后,直接设置,默认是parent
所以如果想要调试子进程,进入gdb后设置set follow-fork-mode child,然后设置子进程的断点
可用使用show follow-fork-mode 来查询当前fork模式
使用follow-fork-mode,只能调试一个进程,不能同时调试父子进程
 
2. detach-on-fork mode
用法:set detach-on-fork [on|off]
on: 只调试父进程或子进程的其中一个(根据follow-fork-mode来决定),这是默认的模式。
off: 父子进程都在gdb的控制之下,其中一个进程正常调试(根据follow-fork-mode来决定),另一个进程会被设置为暂停状态。
如果设置了set detach-on-fork off且follow-fork-mode为parent,fork后子进程并不运行,而是处于暂停状态。
注意,最好使用GDB 6.6或以上版本,如果你使用的是GDB6.4,就只有follow-fork-mode模式。
 
3. attach后多进程调试
    上述方法还是不能同时调试两个进程,假设调试主进程,fork后,子进程已经不知道运行到何处了,所以这就需要让子进程先暂停一下。暂停的方法有很多
    1> 最简单的就是sleep一下
    else if(fpid == 0)  {// child process
          sleep(10); // 给你足够的时间去find子进程id(pstree -ap |  grep a.out),然后attach 
    }
  
    2> 加一段等待代码
  1. void debug_wait(char *tag)  
  2. {  
  3.     while(1)  
  4.     {  
  5.         if (tag存在) <span style="color:#ff0000;">// tag可以是一个环境变量,也可以是一个文件等</span>  
  6.             睡眠一段时间;  
  7.         else  
  8.             break;  
  9.     }  

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 GDB
    • checksec -- Check for various security options of binary
    • dumpargs -- Display arguments passed to a function when stopped at a call instruction
    • dumprop -- Dump all ROP gadgets in specific memory range
    • elfheader -- Get headers information from debugged ELF file
    • elfsymbol -- Get non-debugging symbol information from an ELF file
    • lookup -- Search for all addresses/references to addresses which belong to a memory range
    • patch -- Patch memory start at an address with string/hexstring/int
    • pattern -- Generate, search, or write a cyclic pattern to memory
    • procinfo -- Display various info from /proc/pid/
    • pshow -- Show various PEDA options and other settings
    • pset -- Set various PEDA options and other settings
    • readelf -- Get headers information from an ELF file
    • ropgadget -- Get common ROP gadgets of binary or library
    • ropsearch -- Search for ROP gadgets in memory
    • searchmem|find -- Search for a pattern in memory; support regex search
    • shellcode -- Generate or download common shellcodes.
    • skeleton -- Generate python exploit code template
    • vmmap -- Get virtual mapping address ranges of section(s) in debugged process
    • xormem -- 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"

Screenshot

start

pattern arg

patts

原文地址:https://www.cnblogs.com/studyskill/p/6609530.html