2013-03-05 77 views
1

我正在学习使用GDB为安全类导致缓冲区溢出。我有一个输入文件,成功地使一个程序通过写入缓冲区溢出跳转到未经授权的功能时,我给它输入这样的:有没有办法关闭gdb的缓冲区检查输入?

sh myFile.txt | ./myProgram 

现在我要检查使用GDB的授权的功能。但是当我使用tty command or using <将myFile作为输入提供给GDB时,GDB只需要我的输入的中间20个字节填充20字节的缓冲区。看起来GDB正在“检查”输入的缓冲区大小。

  1. gdb在做什么?
  2. 如果是这样,是否有办法关闭它?

C代码如下所示:

char one[20]; 
    char two[20]; 

    printf("..."); fflush(stdout); 
    gets(one); //only takes 20 bytes from the middle of printf "morethan20bytes..." from input file 
    printf("..."); fflush(stdout); 
    gets(two); //only takes 20 bytes from the middle of printf "morethan20bytes..." from input file 
+1

你如何确定这种行为? – 2013-03-05 14:13:57

+0

@OliCharlesworth我使用x命令检查缓冲区1和缓冲区2的比特级内容​​。然后我转换十六进制我看到ascii,并可以告诉,只是从中间输入20个字符。 – bernie2436 2013-03-05 14:24:09

+0

内存内容可能已被覆盖,但由于GDB知道阵列大小,它只会显示该内容。您可以使用超出范围的索引来检查数组外部,或者转换为指针并添加偏移量。 – 2013-03-05 14:43:27

回答

2

GDB不 “取” 什么。它只是假设你只想看到“one”的内容而已。

您是否知道用于在调试器中打印变量的{type} expr @ num符号?例如,看到“一”过去在缓冲区20指数的内容:

(gdb) next 
...10  gets(one); //only takes last 20 bytes of printf "morethan20bytes..." from input file 
(gdb) next 
BLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH # <== simulating input 
11 printf("..."); fflush(stdout); 

(gdb) print one 
$2 = "BLAHBLAHBLAHBLAHBLAH" 

以上,看来,“一”只中有20个字符。但那是因为gdb假设你只想看20个字节。

现在让我们打印出来的前40个字符是开始于“一”

(gdb) print {char}[email protected] 
$3 = "BLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH" 

的内存地址,您可以清楚地看到,它进入了缓冲区长度

(gdb) print two 
$4 = "BLAHBLAHBLAH\000\000\000\000\000\000\000" 

,你可以看到溢出也写入“两个”。

(gdb) x one 
0x7fffffffe750: 0x48414c42 
(gdb) print {char}[email protected] 
$6 = "BLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAHBLAH" 

上面你可以看到我们可以用内存地址做同样的事情。

相关问题