c++读取pe格式文件

来源:互联网 发布:软件项目管理注意事项 编辑:程序博客网 时间:2024/06/05 04:02
#include <stdio.h>#include <windows.h>int main(int argc,char *argv[]){FILE *fp;char filename[MAX_PATH];IMAGE_DOS_HEADER DOS_header;//DOS头结构IMAGE_NT_HEADERS nt_header;//PE头结构IMAGE_SECTION_HEADER *psection_header;//节表结构指针printf("请输入文件名:");gets(filename);fp=fopen(filename,"rb");if(fp==NULL){printf("\nError:打开文件出错,请重试!\n");getchar();exit(0);}system("cls");printf("\n当前文件:%s\n\n",filename);//检查前两个字节是否为MZchar ch; ch=fgetc(fp);if(ch != 'M'){printf("\n-->->->该文件不是有效的PE文件!\n");exit(0);}else{ch=fgetc(fp);if(ch != 'Z'){printf("\n-->->->该文件不是有效的PE文件!\n");exit(0);}else{printf("\n-->->->该文件通过第一重有效性检验!\n");}}// 判断是否被感染,因为在一般情况下这个位置都应该不变,为0x00fseek(fp,0x7f,0);ch=fgetc(fp);if(ch!=0x00){printf("\n-->->->该文件已经被感染!\n");fclose(fp);exit(0);}rewind(fp);printf("\n------------------文件信息-------------------------\n");fread(&DOS_header,sizeof(struct _IMAGE_DOS_HEADER),1,fp);printf("\nPE文件头偏移:%8X h\n",DOS_header.e_lfanew);fseek(fp,DOS_header.e_lfanew,0);fread(&nt_header,sizeof(struct _IMAGE_NT_HEADERS),1,fp);if(nt_header.Signature != 0x00004550){printf("\n-->->->该文件没有通过第二重有效性检验!\n");fclose(fp);exit(0);}printf("\n-->->->该文件通过第二重有效性检验!\n");printf("\n包含节的个数:%8X h\n",nt_header.FileHeader.NumberOfSections);printf("\n程序入口地址:%8X h\n",nt_header.OptionalHeader.AddressOfEntryPoint);printf("\n优先虚拟地址:%8X h\n",nt_header.OptionalHeader.ImageBase);printf("\n内存文件映像尺寸:0x%X\n\n\n",nt_header.OptionalHeader.SizeOfImage);system("pause");system("cls");printf("\n------------------各节详尽分析-----------------------\n\n\n");psection_header = new IMAGE_SECTION_HEADER[nt_header.FileHeader.NumberOfSections];fread(psection_header,nt_header.FileHeader.NumberOfSections*sizeof(struct _IMAGE_SECTION_HEADER),1,fp);for(int i=0;i<nt_header.FileHeader.NumberOfSections;i++){printf("\n第 %d 节的节表名称:%s\n",i+1,psection_header[i].Name);printf("\n第 %d 节的文件偏移:%Xh\n",i+1,psection_header[i].PointerToRawData);printf("\n第 %d 节的内存偏移:%Xh\n",i+1,psection_header[i].VirtualAddress);printf("\n第 %d 节的实际大小:%XH\n",i+1,psection_header[i].Misc.VirtualSize);printf("\n第 %d 节对齐后大小:%XH\n",i+1,psection_header[i].SizeOfRawData);printf("\n第 %d 节的相关属性:%XH\n\n\n",i+1,psection_header[i].Characteristics);}fclose(fp);getchar();return 0;}

原创粉丝点击