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