c-如何在gdb中向程序的STDIN发送任意字节



我正在为学生开发缓冲区溢出练习。在这种情况下,您通常必须提供任意字节作为程序的输入(返回地址)。

假设这个例子:

#import <stdio.h>
#import <string.h>
void func() {
char buf[4];
gets(buf);
}
int main (int argc, char** argv) {
func();
return 0;
}

通常情况下,我会用gdb进行实验,直到我找到一个解决方案,然后可以像一样进行配制

python -c 'print "A"*8+"x08x048872"' | ./program

在进行越来越复杂的练习的同时,找到解决方案的难度也增加了。有时通过覆盖gdb中的返回地址

set {int}address_of_address = new_address

有效,但python方法不行。调试它并能够在程序运行时在gdb中输入类似"\x04"的字节,分析效果,这将是一件很好的事情。

有办法做到这一点吗?

这个问题似乎是相关的,但可以用python方法来回答:从stdin 向fgets发送任意字节

我的不止于此:-/

如果能调试它并能够输入如下字节,那就太好了"\x04"在gdb中,当程序运行时,分析的影响

要做到这一点,您需要两个控制台:第一个控制台在程序stdin中输入字节,第二个控制台用于gdb调试会话。

您可以首先在第一个控制台中运行程序,直到它停止等待来自stdin的字节。然后在第二个控制台中运行gdb,并通过它的pid连接到程序。您将能够从两个不同的控制台同时调试和输入字节。

"当程序运行时"是问题的一部分。另一个是能够预先设置断点,以"分析效果"。

GDB的默认行为是将程序作为子进程运行,从而使用相同的标准流。因此,在GDB的CLI中写入孩子的stdin是不可能的,因为此时,它是由GDB读取的,而不是您的程序。

避免tty解决方案(tty命令+stty设置+对/proc/<pid>/fd/{0,1}的读取/写入)的最简单解决方案是使代码可从GDB进行测试和"调用"。然后,您就可以将字符串参数传递给函数,以便对它们进行测试和调试。

例如:

#include <stdio.h>
#include <unistd.h>
void exploitme(char* str)
{
printf(str);
}
int main()
{
while (1)
{
char str[10];
fgets(str, sizeof (str), stdin);
exploitme(str);
}
return 0;
}

exploitme()是一个被正确封装在单个入口点中的漏洞利用案例,因此一旦它使用的所有内容都被正确初始化,现在就可以调用它。一旦到达main()断点,就可以使用命令call调用它(这样就完成了在main的调用者中执行的C运行时inits)。

~/test $ gdb ./a.out                                                                                       
(gdb) call exploitme("hello")
You can't do that without a process to debug.
(gdb) b main
Breakpoint 1 at 0x4005ae: file helloworld.c, line 14.
(gdb) r
Starting program: /home/julio/test/a.out 
Breakpoint 1, main () at helloworld.c:14
14          fgets(str, sizeof (str), stdin);
(gdb) call exploitme("hello")
(gdb) call exploitme("hellon")
hellohello
(gdb) call exploitme("AAAAAAAAx08x048872n")
AAAAAAA�:
(gdb) b exploitme 
Breakpoint 2 at 0x400592: file helloworld.c, line 6.
(gdb) call exploitme("foo")
Breakpoint 2, exploitme (str=0x602010 "foo") at helloworld.c:6
6         printf(str);
The program being debugged stopped while in a function called from GDB.
Evaluation of the expression containing the function
(exploitme) will be abandoned.
When the function is done executing, GDB will silently stop.

请注意,您可以从GDB的参数扩展中获益,其中包括C字符串求值。

另一个(更长、更复杂)的解决方案,如前所述,是在另一个tty下运行程序,这样您就可以独立地编写GDB和程序。

最新更新