以下是引用wp231957在2012-11-29 14:24:13的发言:
你们弄错了 我才是楼主
我的目标就是解析pe 而不是写病毒 木马 更不去做黑客
你们弄错了 我才是楼主
我的目标就是解析pe 而不是写病毒 木马 更不去做黑客
哦,是错了。我说的是猪猪侠。
授人以渔,不授人以鱼。
#include <stdio.h> #include <windows.h> //判断是否是一个pe文件 BOOL ispefile(LPVOID imagebase) { PIMAGE_DOS_HEADER pdh=NULL; PIMAGE_NT_HEADERS pnth=NULL; if(!imagebase) return FALSE; pdh=(PIMAGE_DOS_HEADER)imagebase; if(pdh->e_magic!=IMAGE_DOS_SIGNATURE) return FALSE; pnth=(PIMAGE_NT_HEADERS32)((DWORD)pdh+pdh->e_lfanew); if(pnth->Signature !=IMAGE_NT_SIGNATURE) return FALSE; return TRUE; } //获取IMAGE_NT_HEADERS指针 PIMAGE_NT_HEADERS getntheaders(LPVOID imagebase) { PIMAGE_DOS_HEADER pdh=NULL; PIMAGE_NT_HEADERS pnth=NULL; if(!ispefile(imagebase)) return NULL; pdh=(PIMAGE_DOS_HEADER)imagebase; pnth=(PIMAGE_NT_HEADERS32)((DWORD)pdh+pdh->e_lfanew); return pnth; } //获取IMAGE_FILE_HEADER指针 PIMAGE_FILE_HEADER WINAPI getfileheader(LPVOID imagebase) { PIMAGE_NT_HEADERS pnth=NULL; PIMAGE_FILE_HEADER pfh=NULL; pnth=getntheaders(imagebase); if(!pnth) return NULL; pfh=&pnth->FileHeader; return pfh; } //获取IAMGE_OPTIONAL_HEADER指针 PIMAGE_OPTIONAL_HEADER WINAPI getoptionalheader(LPVOID imagebase) { PIMAGE_OPTIONAL_HEADER poh=NULL; PIMAGE_NT_HEADERS pnth=NULL; pnth=getntheaders(imagebase); if(!pnth) return NULL; poh=&pnth->OptionalHeader ; return poh; } //主程序入口处//使用命令行参数获取待检测的pe文件名 int main(int argc,char *argv[]) { LPVOID lpMsgBuf; TCHAR szBuf[128]; DWORD dw; PIMAGE_FILE_HEADER pfh=NULL; PIMAGE_OPTIONAL_HEADER poh=NULL; if (argc<2) return 0; //使用createfile、createfilemapping 、MapViewOfFile获取imagebase HANDLE hFile = CreateFile(argv[1],GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ, NULL,OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN,NULL); if (hFile == INVALID_HANDLE_VALUE) { dw=GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,dw,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL); wsprintf(szBuf,("%s 出错信息 (出错码=%d): %s"),("CreateFile"), dw, lpMsgBuf); printf("%s",szBuf); LocalFree(lpMsgBuf); return 0; } HANDLE hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READONLY, 0, 0, NULL); if (hFileMapping == NULL) { dw=GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,dw,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL); wsprintf(szBuf,("%s 出错信息 (出错码=%d): %s"),("CreateFileMapping"), dw, lpMsgBuf); printf("%s",szBuf); LocalFree(lpMsgBuf); return 0; } LPVOID lpMemory = MapViewOfFile(hFileMapping,FILE_MAP_READ,0, 0, 0); if (lpMemory==NULL) { dw=GetLastError(); FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,dw,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf,0, NULL); wsprintf(szBuf,("%s 出错信息 (出错码=%d): %s"),("MapViewOfFile"), dw, lpMsgBuf); printf("%s",szBuf); LocalFree(lpMsgBuf); return 0; } if(!ispefile(lpMemory)) printf("%s不是一个有效的pe文件\n",argv[1]); else { //传入的文件是一个有效的PE文件 pfh=getfileheader(lpMemory); if(!pfh) return 0; //获取FILEHEADER部分信息 printf("machine:%X\n",pfh->Machine); printf("numberofsections:%X\n",pfh->NumberOfSections); printf("timedatestamp:%X\n",pfh->TimeDateStamp ); poh=getoptionalheader(lpMemory); if(!poh) { printf("获取optionalheader失败\n"); return 0; } //获取OPTIONALHEADER部分信息 printf("imagebase=:%X\n",poh->ImageBase ); printf("addressofentrypoint=:%X\n",poh->AddressOfEntryPoint ); printf("baseofcode=:%X\n",poh->BaseOfCode ); printf("subsystem=:%X\n",poh->Subsystem ); } // 释放文件内核对象 UnmapViewOfFile(lpMemory); CloseHandle(hFileMapping); CloseHandle(hFile); return 0; }