在main函数之前跑代码的方法
方法: 手工找到程序入口点, 替换为我们自己的函数
写测试程序
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <crtdbg.h>
/// 程序入口点 00671140 >|? 55 push ebp
/// 用OD可知
extern "C" void mainCRTStartup(void);
int foo()
{
/**
00671020
*/
/// 执行不带CRT函数的代码
MessageBox(NULL, "foo before main", "test", MB_OK);
mainCRTStartup();
return 0;
}
int main(int argc, char* argv[])
{
/** ALT + 8
00671080
*/
foo();
printf("&main = %p\n", &main);
getchar();
return 0;
}
用DASM窗口记录foo函数的地址为 0x00671020
用WinHex打开PE文件, 找到程序入口点在0x100, 内容为0x1140, 修改程序入口点为0x1020.
手工查找程序入口点的方法: http://blog.csdn.net/lostspeed/article/details/49506193
运行效果
在程序中单步,看看效果
可以看到程序先运行到了foo, 而不是main.