通过堆栈溢出重定向指令指针

时间:2011-10-21 22:27:01

标签: c overflow stack-overflow

我一直在阅读 The Shellcoder的手册(2e)并且一直在尝试重现第18-23页的堆栈溢出实验。

我有这段代码

void return_input (void)
{
    char array[30];
    gets (array);
    printf(“%s\n”, array);
}
main()
{
    return_input();
    return 0;
}

编译:{{1​​}}

转储函数main的汇编代码:

gcc -fno-stack-protector -o overflow overflow.c

我们可以使用0x080483ea <main+0>: push %ebp 0x080483eb <main+1>: mov %esp,%ebp 0x080483ed <main+3>: call 0x80483c4 <return_input> 0x080483f2 <main+8>: mov $0x0,%eax 0x080483f7 <main+13>: pop %ebp 0x080483f8 <main+14>: ret

的调用地址覆盖保存的返回地址
return_input()

因此,这会导致我们的输入被打印两次。但是,第二次没有提示我输入。第二次拨打$ printf "AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDD\xed\x83\x04\x08" | ./overflow AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDí AAAAAAAAAABBBBBBBBBBCCCCCCCCCCDDDDDDò 时,是否应该再次拨打return_input()

1 个答案:

答案 0 :(得分:0)

这可能与stdin()从stdin读取的内容有关。

程序略有改动:

#include <stdio.h>

int n = 1;

void return_input(void)
{
    char array[30];
    gets (array);
    printf("%s\n", array);
    if (n--) return_input();
}

int main(void)
{
    return_input();
    return 0;
}

如果我只是运行它,我可以键入2个短字符串(每个字符串后跟Enter键),如下所示:

C:\gets.exe
qwe
qwe
123
123

这里qwe和123都在屏幕上重复出现(第一次输入时,第二次打印时)。

当我使用echo命令在Windows上输入程序输入时,我得到以下内容而没有机会输入第二个字符串,gets()以某种方式设法在第二次调用时获取垃圾作为输入:

C:\echo qwe|gets.exe
qwe
№  ☺

因此,gets()读取管道输入并且与堆栈溢出无关,这是错误的。