csdn曾见到如下代码:
- #include <stdio.h>
- #include <stdlib.h>
- void HelloWorld()
- {
- printf("Hello World!/n");
- system("Pause");
- exit(0);
- }
- int main()
- {
- int arData[1] = {0};
- arData[2] = (int)HelloWorld;
- printf("not print/n");
- return 0;
- }
dev c++下输出如下:
not print
Hello World!
根据栈内数据布局的,显而易见数组越界修改了存在栈中eip值。
vs2005 debug下, 我这arData[3]才是eip的值,可能是编译选项的缘故吧。
下面代码也比较有意思:
-
- #include <iostream>
- #include <stdlib.h>
- #include <windows.h>
- using namespace std;
- #pragma pack(1)
- int main(int argc, char* argv[])
- {
- char pass[] = "1234";
- char pwd[5] = {0};
- cout<<sizeof(pwd)<<endl;
- cout<<int(pwd)<<endl;
- cout<<int(pass)<<endl;
- system("pause");
- while (1)
- {
- cout << "请输入密码:";
- cin >> pwd;
- if (strcmp(pwd,pass) == 0)
- {
- MessageBox(NULL,_T("OK"),_T("正确的密码"),MB_OK);
- break;
- }
- else
- {
- MessageBox(NULL,_T("Error"),_T("错误的密码"),MB_OK);
- }
- }
- system("pause");
- return 0;
- }
如果pass和pwd首地址相差8,也就是类似如下
5
1245028
1245036
那么输入
1111011111110
再输入
11110
就能弹出ok的对话框
利用是函数局部变量在栈中布局,根据pass和pwd地址,通过溢出修改pass的值,我在vs05 realse通过相差8的情况。
ps:上述两段代码来自网上,著作权属于原作者。