|
- #include <stdio.h>
- #include <WINDOWS.H>
- bool mapoep(LPCSTR szFilename)
- {
- //申请一个文件句柄
- HANDLE hfile;
- //申请一个镜像句柄
- HANDLE hmap;
- //打开文件
- if ( (hfile = CreateFile(szFilename,GENERIC_READ,FILE_SHARE_READ,
- 0,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,0)) == INVALID_HANDLE_VALUE)
- {
- printf("open file failed\n");
- return false;
- }
-
- //创建文件镜像
- if ( (hmap = CreateFileMapping(hfile,0,PAGE_READONLY|SEC_COMMIT,0,0,0) ) ==NULL)
- {
- printf("maping failed \n");
- CloseHandle(hfile);
- return false;
- }
- //申请一个无类型指针imagebase
- void* imagebase;
- //读取内存镜像获取镜像基址,赋值给imagedos
- if ( (imagebase = MapViewOfFile(hmap,FILE_MAP_READ,0,0,0)) == NULL)
- {
- printf("ERROR \n");
- return false;
- }
- //创建一个IMAGE_DOS_HEADER指针
- IMAGE_DOS_HEADER *idos;
- //把镜像基址转换成PIMAGE_DOS_HEADER结构复制给idos
- idos = (IMAGE_DOS_HEADER*)imagebase;
- //创建一个IMAGE_NT_HEADERS指针,并从idos中获取NTHEADER的基址,初始化imnt
- IMAGE_NT_HEADERS *imnt = (IMAGE_NT_HEADERS *)((ULONG)idos + idos->e_lfanew);
- //从imnt中获取AddressOfEntryPoint,并保存到一个新创建DWORD变量OEP中
- DWORD oep = imnt->OptionalHeader.AddressOfEntryPoint;
- //输出OEP16进制数
- printf("0x%X",oep);
- //卸下镜像
- UnmapViewOfFile(imagebase);
- CloseHandle(hmap);
- //关闭文件
- CloseHandle(hfile);
- return true;
- }
- void main()
- {
- LPCSTR filename;
- filename = "nimei.exe";
- mapoep(filename);
- system("pause");
- }
复制代码
|
|