2017-02-02 48 views
0

最近我一直在阅读的书籍和文章关于黑客感兴趣,我发现黑客:开发的艺术仅仅是一个必须阅读的标题。我正在学习关于如何使用标准Linux工具和分析代码的基本教程(编程章节)。我不是编程的初学者,但使用Linux终端对我来说是非常新的。我正在使用最新版本的Kali Linux。(GDB)断点和拆卸

现在我的简单程序应该用来分析堆栈段的工作原理。

int main(){ 
    void stack_func(int a,int b, int c, int d){ 
    char first; 
    int second; 

    first = 'c'; 
    second = 220; 
    } 

    stack_func(1,2,3,4); 
    return 0; 
} 

第一个问题是我不能添加任何断点的内部功能。我的功能并不像stack_func()也不是函数库,如strcpy等。根据本书,待决断点应该解决。我被忽略,程序完成。

[email protected]:~/Folder# gdb -q ./stack 
Reading symbols from ./stack...done. 
(gdb) b stack_func 
Function "stack_func" not defined. 
Make breakpoint pending on future shared library load? (y or [n]) y 
Breakpoint 1 (stack_func) pending. 
(gdb) run 
Starting program: /root/Folder/stack 
[Inferior 1 (process 20421) exited normally] 
(gdb) 

的第二个问题是,拆机也没有对我的工作的功能。根据这本书,我应该能够看到我的函数stack_func()的汇编代码,但结果如下。

(gdb) disass stack_func() 
No symbol "stack_func" in current context. 
(gdb) 

我为文中的任何语法错误而道歉。 :)

+0

你无副作用功能可能只是由编译器取出,或至少内联。 –

+0

你有没有试过移动'main'之外的'stack_func'? –

+0

是的,我现在尝试了这样的错误信息。 '你不能这样做,没有一个过程来调试。' – Jacobe

回答

1

问题是你在另一个函数里面定义了stack_func。这称为nested function,它是GNU C中的gcc扩展。此函数有一点比您期望的其他符号名称。要找出它的确切符号名称可以使用nm工具:

[ tmp]$ nm a.out |grep stack_func 
00000000004004a6 t stack_func.1761 

并设置断点,在gdb拆卸:

[ tmp]$ gdb -q ./a.out 
Reading symbols from ./a.out...done. 
(gdb) b 'stack_func.1761' 
Breakpoint 1 at 0x4004ba: file 111.c, line 6. 
(gdb) disassemble 'stack_func.1761' 
Dump of assembler code for function stack_func: 
    0x00000000004004a6 <+0>: push %rbp 
    0x00000000004004a7 <+1>: mov %rsp,%rbp 
    0x00000000004004aa <+4>: mov %edi,-0x14(%rbp) 
    0x00000000004004ad <+7>: mov %esi,-0x18(%rbp) 
    0x00000000004004b0 <+10>: mov %edx,-0x1c(%rbp) 
    0x00000000004004b3 <+13>: mov %ecx,-0x20(%rbp) 
    0x00000000004004b6 <+16>: mov %r10,-0x28(%rbp) 
    0x00000000004004ba <+20>: movb $0x63,-0x1(%rbp) 
    0x00000000004004be <+24>: movl $0xdc,-0x8(%rbp) 
    0x00000000004004c5 <+31>: nop 
    0x00000000004004c6 <+32>: pop %rbp 
    0x00000000004004c7 <+33>: retq 
End of assembler dump. 
(gdb)