<分区>
首先,我运行此程序的计算机是在 32 位堆栈上运行 Linux 的 Intel 系统。我的教授在类里面向我们提出了一个挑战问题。
这是我提问前的代码
// funWithFooAndBar.c
#include <stdio.h>
#include <stdlib.h>
void bar()
{
printf("Now inside bar()!\n");
}
void foo()
{
void *addr[1];
printf("Now inside foo()!\n");
// this is where I need to modify my code,
//I was given the hint that it will only be two lines of code
// So something like:
addr[1] = bar;
addr[5] = addr[4];
addr[4] = bar;;
}
int main (int argc, char *argv[])
{
foo();
printf("Back in main\n");
return 0;
}
目标是通过超出数组末尾的写入来粉碎堆栈,并通过它覆盖返回地址,以便对 foo () 的函数调用在返回 main 的过程中返回到 bar ()。所以我的输出应该是这样的:
现在在 foo() 里面!
现在在 bar() 里面!
回到主线
为了做到这一点,我必须溢出数组,以便返回地址被 bar 的地址覆盖。我很确定它必须涉及函数 bar() 的地址,这等同于&bar()
他提出的问题是我们可以添加哪两行代码(我注释的地方)来使输出如上所示。
谢谢!
编辑:我更希望得到解释而不是直接回答,我知道我应该做什么,只是不知道如何将其转换为 C 代码。
编辑:尝试