本文会演示两个发生栈溢出的例子,并用OD工具演示整个过程。
案例一
案例代码
// dishui_nx.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
void HelloWord()
{
printf("Hello World");
getchar();
}
void Fun()
{
int arr[5] = {1,2,3,4,5};
//覆盖返回地址 Fun函数执行完就会Helloworld函数
arr[6] = (int)HelloWord;
}
int main(int argc, char* argv[])
{
Fun();
// printf("Hello World!\n");
return 0;
}
案例分析
1.使用OD载入, 运行到Fun函数内部,在堆栈中可以看到Fun函数的下一行汇编指令的地址和EBP的值
2.当运行到0040D7B2 pop edi ,发现0012FF30 处的值变为dishui_n.0040100F,意味着Fun的返回地址被修改。
3.接着运行HelloWord函数,当运行到该函数的末尾时,发现返回地址是7C 930738.
4.HelloWord函数运行完后,程序跳转到7C930738处。而不是main函数处。
案例二
案例代码
#include "stdafx.h"
void Fun()
{
int i;
int arr[5] = {0};
for(i=0;i<=5;i++)
{
arr[i] = 0;
printf("Hello World!\n");
}
}
int main(int argc, char* argv[])
{
Fun();
// printf("Hello World!\n");
return 0;
}
案例分析
1.运行到Fun函数内部,执行循环结构
2.发现变量i 的值存放在[ebp-4]中,当i为5时,[ebp
edx*4-18]就变为[ebp-4],那么i的值就被覆盖。
3.那么i的值一直不超过5,导致无限循环