1.要测试的程序源码
我们将一个占用32bytes空间的字符串拷贝到只分配了16bytes空间的字符数组中,由于字符数组所占空间不能满足要拷贝的字符串大小,因此会产生缓冲区溢出的情况。
#include <stdio.h>
#include <string.h>
#define ATTACK_BUFF_LEN 1024
char attackStr[ATTACK_BUFF_LEN] = "01234567890123456789========ABCD"; // 32 bytes
void overflow()
{
char buff[16]; // 最多容纳16 bytes
strcpy (buff, attackStr);
}
int main(int argc, char * argv[])
{
overflow();
return 0;
}
2. 运行时的栈
要了解缓冲区溢出攻击原理,我们必须先要了解C程序函数运行时的栈存储情况。我们以下面代码为例观察运行时栈的存储情况:
int cal(int a,int b)
{
int x = 2;
int y = 3;
return x*a + y*b;
}
void main()
{
int a = 1;
int b