C语言编程获取PE文件File_Header内容
2016-02-02 22:34
483 查看
#include <windows.h> #include <stdio.h> #include <tchar.h> void viewImageFileCharacteristics(WORD); int _tmain(int argc, TCHAR *argv[]) { PIMAGE_DOS_HEADER pImageDosHeader; PIMAGE_NT_HEADERS pImageNtHeaders; PIMAGE_FILE_HEADER pImageFileHeader; HANDLE hFile; HANDLE hMapObject; PUCHAR uFileMap; if(argc<2) return -1; if(!(hFile=CreateFile(argv[1],GENERIC_READ,0,NULL,OPEN_EXISTING,0,0))) return -1; if (!(hMapObject=CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL))) return -1; if(!(uFileMap=MapViewOfFile(hMapObject,FILE_MAP_READ,0,0,0))) return -1; pImageDosHeader=(PIMAGE_DOS_HEADER)uFileMap; if (pImageDosHeader->e_magic != IMAGE_DOS_SIGNATURE) return -1; pImageNtHeaders=(PIMAGE_NT_HEADERS)((PUCHAR)uFileMap+pImageDosHeader->e_lfanew); if (pImageNtHeaders->Signature!=IMAGE_NT_SIGNATURE) return -1; pImageFileHeader=(PIMAGE_FILE_HEADER) &(pImageNtHeaders->FileHeader); printf("Machine: 0x%04X",pImageFileHeader->Machine); ((pImageFileHeader->Machine == IMAGE_FILE_MACHINE_I386) ?printf("(I386) \n") :printf(" (?) \n")); printf("NumberOfSections: 0x%04X\n",pImageFileHeader->NumberOfSections); printf("TimeDateStamp: 0x%08X\n",pImageFileHeader->TimeDateStamp); printf("PointerToSymbolTable: 0x08X\n",pImageFileHeader->PointerToSymbolTable); printf("NumberOfSymbols: 0x%08X\n",pImageFileHeader->NumberOfSymbols); printf("SizeOfOptionalHeader: 0x%04X\n",pImageFileHeader->SizeOfOptionalHeader); printf("Characteristics: 0x%04X\n",pImageFileHeader->Characteristics); viewImageFileCharacteristics(pImageFileHeader->Characteristics); UnmapViewOfFile(uFileMap); CloseHandle(hMapObject); CloseHandle(hFile); return 0; } void viewImageFileCharacteristics(WORD wCharacteristics) { BYTE szCharacteristics[100]; memset(szCharacteristics,0,100); szCharacteristics[0]='('; if (wCharacteristics & 0x0001) strcat(szCharacteristics,"RELOCS_STRIPPED|"); if (wCharacteristics & 0x0002) strcat(szCharacteristics,"EXECUTABLE_IMAGE|"); if (wCharacteristics & 0x0004) strcat(szCharacteristics,"LINE_NUMS_STRIPPED|"); if (wCharacteristics & 0x0100) strcat(szCharacteristics,"32BIT_MACHINE|"); if (wCharacteristics & 0x0200) strcat(szCharacteristics,"DEBUG_STRIPPED|"); if (wCharacteristics & 0x1000) strcat(szCharacteristics,"FILE_SYSTEM|"); if (wCharacteristics & 0x2000) strcat(szCharacteristics,"FILE_DLL|"); szCharacteristics[strlen(szCharacteristics)-1]=')'; szCharacteristics[strlen(szCharacteristics)]='\0'; printf(" %s\n",szCharacteristics); }
相关文章推荐
- c++转换构造函数和类型转换函数
- C语言编程获取PE文件DOS头
- c++中的数据类型
- C++ 通用编程
- C语言 求一个数列的前n项之和,保留两位小数。
- [土狗之路]coursera上C语言进阶习题 括号匹配
- [土狗之路]coursera C语言进阶练习题 排队游戏
- C语言求斐波拉切数列第n项
- 一起talk C栗子吧(第一百一十九回:C语言实例--线程死锁三)
- 小蚂蚁学习C语言(38)——题目——求字符串是否回文
- (C++) 分隔符匹配
- C++ Primer 笔记(一)cin输入的一些用法和注意事项(未完成)
- 设计模式C++实现:管理者模式
- asm基础——asm函数和c++函数之间的相互调用
- sqlite3对数据库加密用c语言开源项目wxSQLite3实现
- 用C++ Builder实现一组数组的排序与插入操作
- C++引用计数(reference counting)技术简介(3)
- C++引用计数(reference counting)技术简介(3)
- 学习C++
- C/C++: Inline function, calloc vs malloc