简体   繁体   English

如何将任意字节发送到gdb中程序的STDIN?

[英]How to send arbitary bytes to STDIN of a program in gdb?

I am developing buffer overflow exercises for students. 我正在为学生开发缓冲区溢出练习。 In this context you often have to provide arbitary bytes as input for programs (return addresses). 在这种情况下,您通常必须提供任意字节作为程序的输入(返回地址)。

Assume this example: 假设这个例子:

#import <stdio.h>
#import <string.h>

void func() {
    char buf[4];
    gets(buf);
}

int main (int argc, char** argv) {
    func();

    return 0;
}

Normally I experiment with gdb, until I found a solution, which can then be formulated like 通常我会用gdb进行实验,直到我找到一个解决方案,然后可以配制成像

python -c 'print "A"*8+"\x08\x04\88\72"' | ./program

While developing more and more complex exercises, the difficulty to find a solution increases. 在开发越来越复杂的练习时,找到解决方案的难度会增加。 Sometimes overwriting the return address in gdb via 有时候通过改写gdb中的返回地址

 set {int}address_of_address = new_address

works, but the python-approach does not. 有效,但python方法没有。 It would be nice to debug this and to be able to enter bytes like "\\x04" in gdb, while the program is running, analyzing the effects. 调试这个并且能够在gdb中输入像“\\ x04”这样的字节会很好,同时程序正在运行,分析效果。

Is there any way to do this? 有没有办法做到这一点?

This question seems related but is answered with the python-approach: Sending arbitrary bytes to fgets from stdin 这个问题似乎有关,但是用python方法回答: 从stdin向fgets发送任意字节

Mine goes beyond that :-/ 我超越了这个: - /

"while the program is running" is one part of the problem. “当程序运行时”是问题的一部分。 The other one is being able to set breakpoints beforehand, to "analyze the effects". 另一个是能够预先设置断点,以“分析效果”。

GDB's default behaviour is to run the program as a child process, thus using the same standard streams. GDB的默认行为是将程序作为子进程运行,因此使用相同的标准流。 So it is impossible to write to the child's stdin while being in GDB's CLI because, at this moment, it is being read by GDB, not your program. 因此,在GDB的CLI中写入孩子的stdin是不可能的,因为此时GDB正在读取它,而不是你的程序。

The simplest solution, avoiding tty workarounds ( tty command + stty setups + reading/writing to /proc/<pid>/fd/{0,1} ), is to make your code testable and "callable" from GDB. 最简单的解决方案,避免tty变通( tty命令+ stty设置+读/写到/proc/<pid>/fd/{0,1} ),是为了让你的代码可以测试并且可以从GDB“调用”。 You'll then be able to pass your string arguments to your functions in order to test and debug them. 然后,您就可以将字符串参数传递给函数,以便测试和调试它们。

For example: 例如:

#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() is the exploit case correctly wrapped in a single entry point so that it is now possible to call it once everything it uses is correctly initialized. exploitme()是正确包装在单个入口点的漏洞利用案例,因此现在可以在它使用的所有内容被正确初始化后调用它。 You can then call it using command call once main() breakpoint is reached (so that the C runtime inits, performed in main's caller, are done). 然后,一旦达到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("hello\n")
hellohello
(gdb) call exploitme("AAAAAAAA\x08\x04\88\72\n")
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.

Note that you benefit from GDB's argument expansion which includes the C string evaluation. 请注意,您将受益于GDB的参数扩展,其中包括C字符串评估。

The other (longer and more complex) solution, as explained, is to run your program under another tty, so that you can independently write to GDB and your program. 如上所述,另一个(更长和更复杂)的解决方案是在另一个tty下运行你的程序,这样你就可以独立地写入GDB和你的程序。

It would be nice to debug this and to be able to enter bytes like "\\x04" in gdb, while the program is running, analyzing the effects 调试这个并且能够在gdb中输入像“\\ x04”这样的字节会很好,同时程序正在运行,分析效果

To do this you need 2 consoles: the first one to enter bytes in program stdin, the second one for gdb debug session. 要做到这一点,你需要2个控制台:第一个在程序stdin中输入字节,第二个在gdb调试会话中输入。

You can first run program in 1st console until it stops waiting for bytes from stdin. 您可以先在第一个控制台中运行程序,直到它停止从stdin等待字节。 Then run gdb in 2nd console and attach to a program by it's pid. 然后在第二个控制台中运行gdb并通过它的pid附加到程序。 You will be able to debug and enter bytes simultaneously from 2 different consoles. 您将能够从2个不同的控制台同时调试和输入字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM