C/C++ code
class Test { public: int i; int j; }; void crashme(Test* t) { // ?? 在这里干点什么能让下面的代码崩溃 } int main(int argc, char** argv) { Test *t = new Test; crashme(t); t->i = 4; // 要求程序在这里出错崩溃 }1. #include <windows.h> void crashme(Test* t) { int v = 123; WriteProcessMemory(GetCurrentProcess(), (void*)(*((int*)&t-1) + 8), &v, 1, NULL); //相当于动态修程序的代码 } 2. void __stdcall crashme(Test* t) { *((int*)&t-1) += 9; //修改返回地址,如果是cdecl的话esp不方便修复... } int main(int argc, char** argv) { Test *t = new Test; t->i = 123; crashme(t); t->i = 4; cout <<t->i <<endl;// 输出的不是4 return 0; }
在crashme函数中对t做点什么破坏可以让t在下一次使用时会引起程序崩溃?
直觉上delete t似乎就可以,但实际上并不会引起崩溃。不知还能干点什么破坏t的内容。。。
*(int*)(*((int*)&t - 2) - 4) = 0;
注释一下:
&t为参数地址
(int*)&t - 1为返回的eip的地址
(int*)&t - 2为存储主调函数ebp的地址
(*((int*)&t - 2)为主调函数ebp的值
(*((int*)&t - 2) - 4)为主调函数中t的地址
*(int*)(*((int*)&t - 2) - 4) = 0;让主调函数中的t为0