PeHelp.h
资源名称:PeEdit.rar [点击查看]
上传用户:polioc
上传日期:2022-08-03
资源大小:1956k
文件大小:21k
源码类别:
文件操作
开发平台:
Visual C++
- #include <windows.h>
- //全局函数用于保存各块的RVA和OFFSET和输入输出表信息
- DWORD DATARVA[20];//默认为20个块表
- DWORD DATAOFFSET[20];
- IMAGE_DATA_DIRECTORY Global_IDD[16];//数据目录表信息
- IMAGE_IMPORT_DESCRIPTOR iid[20]={0};//默认为调用20个DLL
- DWORD NUMDATA;//块数目
- BOOL CheckPeFile(FILE *ImageBase)//检查是否是PE文件
- {
- ::fseek(ImageBase,0,0);//每个函数使用前先文件定位
- if(NULL == ImageBase)
- {
- return 0;
- }
- IMAGE_DOS_HEADER DOS_HEADER;
- const DWORD PESYMBOL=0x00004550;
- const WORD DOSSYMBOL=0x5A4D;
- ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
- if(DOS_HEADER.e_magic == DOSSYMBOL)
- {
- ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);//文件定位
- DWORD pesymbol;
- ::fread(&pesymbol,sizeof(DWORD),1,ImageBase);
- if(pesymbol == PESYMBOL)
- return 1;
- else
- return 0;
- }
- else
- return 0;
- }
- //一些输出转换函数
- //WORD 转换为2个16进制数
- unsigned char WORDTOLI16(const unsigned char *old)//WORD的低8位
- {
- unsigned char LI16;
- LI16=old[0];
- return LI16;
- }
- unsigned char WORDTOHI16(const unsigned char *old)//WORD的高8位
- {
- unsigned char HI16;
- HI16=old[1];
- return HI16;
- }
- //DWORD 转换为4个16进制数
- unsigned char DWORDTO_0_16(const unsigned char *old)
- {
- unsigned char LI16;
- LI16=old[0];
- return LI16;
- }
- unsigned char DWORDTO_1_16(const unsigned char *old)
- {
- unsigned char LI16;
- LI16=old[1];
- return LI16;
- }
- unsigned char DWORDTO_2_16(const unsigned char *old)
- {
- unsigned char LI16;
- LI16=old[2];
- return LI16;
- }
- unsigned char DWORDTO_3_16(const unsigned char *old)
- {
- unsigned char LI16;
- LI16=old[3];
- return LI16;
- }
- void ShowPEDosHeader(FILE *ImageBase)//显示PE文件DOS头
- {
- ::fseek(ImageBase,0,0);
- IMAGE_DOS_HEADER DOS_HEADER;
- ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
- ::printf("------PE文件DOS头部数据-------n");
- ::printf("e_magic :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_magic),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_magic));
- ::printf("e_cblp :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cblp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cblp));
- ::printf("e_cp :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cp));
- ::printf("e_crlc :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_crlc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_crlc));
- ::printf("e_cparhdr :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cparhdr),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cparhdr));
- ::printf("e_minalloc :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_minalloc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_minalloc));
- ::printf("e_maxalloc :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_maxalloc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_maxalloc));
- ::printf("e_ss :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ss),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ss));
- ::printf("e_sp :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_sp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_sp));
- ::printf("e_csum :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_csum),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_csum));
- ::printf("e_ip :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ip),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ip));
- ::printf("e_cs :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cs),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cs));
- ::printf("e_lfarlc :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_lfarlc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_lfarlc));
- ::printf("e_ovno :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ovno),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ovno));
- ::printf("e_res :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_res),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_res));
- ::printf("e_oemid :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_oemid),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_oemid));
- ::printf("e_oeminfo :%02x%02x ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_oeminfo),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_oeminfo));
- ::printf("e_res2 :%02x%02x n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_res2),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_res2));
- ::printf("e_lfanew :%02x%02x%02x%02x n",DWORDTO_0_16((const unsigned char *)&DOS_HEADER.e_lfanew),DWORDTO_1_16((const unsigned char *)&DOS_HEADER.e_lfanew),DWORDTO_1_16((const unsigned char *)&DOS_HEADER.e_lfanew),DWORDTO_2_16((const unsigned char *)&DOS_HEADER.e_lfanew),DWORDTO_3_16((const unsigned char *)&DOS_HEADER.e_lfanew));
- ::printf("n");
- }
- void ShowPENTHeader(FILE *ImageBase)
- {
- ::fseek(ImageBase,0,0);
- IMAGE_DOS_HEADER DOS_HEADER;
- IMAGE_NT_HEADERS NT_HEADER;
- ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
- ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);
- ::fread(&NT_HEADER,sizeof(IMAGE_NT_HEADERS),1,ImageBase);
- ::printf("------PE文件NT头部数据-------n");
- const unsigned char *DWord=NULL;
- DWord=(const unsigned char *)&NT_HEADER.Signature;
- ::printf("Singature:%02x%02x%02x%02x n",DWord[0],DWord[1],DWord[2],DWord[3]);
- ::printf("n");
- ::printf("------PE文件NT头部IMAGE_FILE_HEADER数据-------n");
- IMAGE_FILE_HEADER FILE_HEADER;
- ::memcpy(&FILE_HEADER,&NT_HEADER.FileHeader,sizeof(IMAGE_FILE_HEADER));
- DWord=(const unsigned char *)&FILE_HEADER.Machine;
- ::printf("Machine:%02x%02x ",DWord[0],DWord[1]);
- DWord=(const unsigned char *)&FILE_HEADER.NumberOfSections;
- ::printf("NumberOfSections:%02X%02Xn",DWord[0],DWord[1]);
- ::printf("n");
- ::printf("------PE文件NT头部IMAGE_OPTIONAL_HEADER数据-------n");
- IMAGE_OPTIONAL_HEADER OPTIONAL_HEADER;
- ::memcpy(&OPTIONAL_HEADER,&NT_HEADER.OptionalHeader,sizeof(IMAGE_OPTIONAL_HEADER));
- DWord=(const unsigned char *)&OPTIONAL_HEADER.AddressOfEntryPoint;
- ::printf("AddressOfEntryPoint:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&OPTIONAL_HEADER.BaseOfCode;
- ::printf("BaseOfCode:%02x%02x%02x%02x n",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&OPTIONAL_HEADER.BaseOfData;
- ::printf("BaseOfData:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&OPTIONAL_HEADER.NumberOfRvaAndSizes;
- ::printf("NumberOfRvaAndSizes:%02x%02x%02x%02x n",DWord[0],DWord[1],DWord[2],DWord[3]);
- ::printf("n");
- ::printf("------PE文件NT头部数据目录表数据-------n");
- for(int i=0;i<16;i++)
- {
- if(0 != NT_HEADER.OptionalHeader.DataDirectory[i].Size)
- {
- DWord=(const unsigned char *)&NT_HEADER.OptionalHeader.DataDirectory[i].VirtualAddress;
- ::printf("序号:%2d VirtualAddress:%02x%02x%02x%02xn",i,DWord[0],DWord[1],DWord[2],DWord[3]);
- ::memcpy(&Global_IDD[i],&NT_HEADER.OptionalHeader.DataDirectory[i],sizeof(IMAGE_DATA_DIRECTORY));
- }
- }
- }
- void ShowSectionHeader(FILE *ImageBase)
- {
- ::fseek(ImageBase,0,0);
- const unsigned char *DWord=NULL;
- IMAGE_DOS_HEADER DOS_HEADER;
- IMAGE_NT_HEADERS NT_HEADER;
- IMAGE_SECTION_HEADER SECTION_HEADER[20];//默认为20个块表
- ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
- ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);
- ::fread(&NT_HEADER,sizeof(IMAGE_NT_HEADERS),1,ImageBase);
- ::fseek(ImageBase,DOS_HEADER.e_lfanew+sizeof(IMAGE_NT_HEADERS),0);
- if(NT_HEADER.FileHeader.NumberOfSections !=0 )
- {
- ::printf("------PE文件的块表信息-------n");
- for(int i=0;i<NT_HEADER.FileHeader.NumberOfSections;i++)
- {
- ::fread(&SECTION_HEADER[i],sizeof(IMAGE_SECTION_HEADER),1,ImageBase);
- ::printf("Name:%s ",SECTION_HEADER[i].Name);
- DWord=(const unsigned char *)&SECTION_HEADER[i].VirtualAddress;
- ::printf("VirtualAddress:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&SECTION_HEADER[i].PointerToRawData;
- ::printf("PointerToRawData:%02x%02x%02x%02xn",DWord[0],DWord[1],DWord[2],DWord[3]);
- DATARVA[i]=SECTION_HEADER[i].VirtualAddress;
- DATAOFFSET[i]=SECTION_HEADER[i].PointerToRawData;
- }
- NUMDATA=i;
- }
- }
- //找到相应的块
- DWORD RVATOOFFSET(DWORD addr,DWORD numdata)
- {
- for(DWORD i=0;i<numdata;i++)
- {
- if(DATARVA[i+1] != 0)
- {
- if(addr>=DATARVA[i] && addr<DATARVA[i+1])
- {
- return addr-DATARVA[i]+DATAOFFSET[i];
- }
- }
- else
- {
- if(addr>=DATARVA[i])
- {
- return addr-DATARVA[i]+DATAOFFSET[i];
- }
- }
- }
- }
- void ShowIID(FILE *fp)
- {
- ::fseek(fp,0,0);
- if(Global_IDD[1].Size != 0)
- {
- ::printf("------该PE文件输入表如下:--------n");
- DWORD addr=RVATOOFFSET(Global_IDD[1].VirtualAddress,NUMDATA);
- const unsigned char *DWord=NULL;
- fseek(fp,addr,0);
- DWORD isIID;
- ::fread(&isIID,sizeof(DWORD),1,fp);
- int i=0;
- while(isIID != 0)
- {
- ::fseek(fp,addr+i*sizeof(IMAGE_IMPORT_DESCRIPTOR),0);
- ::fread(&iid[i],sizeof(IMAGE_IMPORT_DESCRIPTOR),1,fp);
- i++;
- ::fread(&isIID,sizeof(DWORD),1,fp);
- }
- for(int p=0;p<i;p++)
- {
- DWord=(const unsigned char *)&iid[p].OriginalFirstThunk;
- ::printf("OriginalFirstThunk:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&iid[p].FirstThunk;
- ::printf("FirstThunk:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWORD nameaddr;
- nameaddr=RVATOOFFSET(iid[p].Name,NUMDATA);
- ::fseek(fp,nameaddr,0);
- ::printf("DLLName:");
- char ch=::fgetc(fp);
- while(ch != 0)
- {
- putchar(ch);
- ch=fgetc(fp);
- }
- ::printf("n");
- }
- ::printf("------该PE文件输入函数有:--------n");
- DWORD funaddr1;
- DWORD funaddr2;
- int numdll=0;
- while(iid[numdll].OriginalFirstThunk != 0)
- {
- funaddr1=RVATOOFFSET(iid[numdll].OriginalFirstThunk,NUMDATA);
- ::fseek(fp,funaddr1,0);
- ::fread(&funaddr2,sizeof(DWORD),1,fp);
- int numfun=0;
- while(funaddr2 != 0)
- {
- funaddr2=RVATOOFFSET(funaddr2,NUMDATA);
- ::fseek(fp,funaddr2,0);
- int i=0;
- char ch=::fgetc(fp);
- while(ch != 0 || i<2)
- {
- if(i>=2)//头两个字节是序号不输出
- {
- if(ch<=0)
- {
- ::printf("按序号导出的函数");
- break;
- }
- putchar(ch);
- }
- i++;
- ch=fgetc(fp);
- }
- ::printf("n");
- numfun++;
- ::fseek(fp,funaddr1+numfun*sizeof(DWORD),0);
- ::fread(&funaddr2,sizeof(DWORD),1,fp);
- }
- numdll++;
- }
- }
- else
- printf("-------该PE文件无输入表.--------n");
- }
- void ShowIED(FILE *fp)
- {
- ::fseek(fp,0,0);
- if(Global_IDD[0].Size != 0)
- {
- ::printf("------该PE文件输出表如下:--------n");
- DWORD addr=RVATOOFFSET(Global_IDD[0].VirtualAddress,NUMDATA);
- const unsigned char *DWord=NULL;
- IMAGE_EXPORT_DIRECTORY EXPORT_DIRECTORY;
- fseek(fp,addr,0);
- ::fread(&EXPORT_DIRECTORY,sizeof(IMAGE_EXPORT_DIRECTORY),1,fp);
- DWord=(const unsigned char *)&EXPORT_DIRECTORY.NumberOfFunctions;
- ::printf("NumOfFunction:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&EXPORT_DIRECTORY.NumberOfNames;
- ::printf("NumberOfNames:%02x%02x%02x%02x n",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfFunctions;
- ::printf("AddressOfFunctions:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfNames;
- ::printf("AddressOfNames:%02x%02x%02x%02x n",DWord[0],DWord[1],DWord[2],DWord[3]);
- DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfNameOrdinals;
- ::printf("AddressOfNameOrdinals:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- ::printf("导出表名字:");
- DWORD dllname;
- dllname=RVATOOFFSET(EXPORT_DIRECTORY.Name,NUMDATA);
- ::fseek(fp,dllname,0);
- char ch=::fgetc(fp);
- while(ch != 0)
- {
- putchar(ch);
- ch=fgetc(fp);
- }
- ::printf("n--------导出函数如下---------n");
- addr=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfNames,NUMDATA);
- ::fseek(fp,addr,0);
- DWORD addr2;
- ::fread(&addr2,sizeof(DWORD),1,fp);
- DWORD addr1=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfNameOrdinals,NUMDATA);
- ::fseek(fp,addr1,0);
- WORD Ordinal;
- ::fread(&Ordinal,sizeof(WORD),1,fp);
- DWORD funAddr=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfFunctions,NUMDATA);
- const DWORD FunOfAddr=funAddr;
- funAddr=funAddr+Ordinal*sizeof(DWORD);
- ::fseek(fp,funAddr,0);
- ::printf("虚拟地址 导出函数名称n");
- DWORD funAddr1;
- for(DWORD i=0;i<EXPORT_DIRECTORY.NumberOfNames;i++)
- {
- ::fread(&funAddr1,sizeof(DWORD),1,fp);
- DWord=(const unsigned char*)&funAddr1;
- ::printf("%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- addr2=RVATOOFFSET(addr2,NUMDATA);
- ::fseek(fp,addr2,0);
- char ch=::fgetc(fp);
- while(ch != 0)
- {
- putchar(ch);
- ch=fgetc(fp);
- }
- ::printf("n");
- ::fseek(fp,addr+sizeof(DWORD),0);
- addr=addr+sizeof(DWORD);
- ::fread(&addr2,sizeof(DWORD),1,fp);
- addr1=addr1+sizeof(WORD);
- ::fseek(fp,addr1,0);
- ::fread(&Ordinal,sizeof(WORD),1,fp);
- funAddr=FunOfAddr+Ordinal*sizeof(DWORD);
- ::fseek(fp,funAddr,0);
- }
- }
- else
- ::printf("------该PE文件无输出表:--------n");
- }
- void ShowReloCation(FILE *fp)
- {
- ::fseek(fp,0,0);
- if(Global_IDD[5].Size != 0)
- {
- ::printf("------该PE文件基址重定位表------n");
- IMAGE_BASE_RELOCATION BASE_RELOCATION;
- DWORD addr=RVATOOFFSET(Global_IDD[5].VirtualAddress,NUMDATA);
- unsigned char *DWord=NULL;
- int numTypeOffset=0;
- ::fseek(fp,addr,0);
- ::fread(&BASE_RELOCATION,sizeof(IMAGE_BASE_RELOCATION),1,fp);
- while(BASE_RELOCATION.VirtualAddress != 0)
- {
- DWord=(unsigned char*)&BASE_RELOCATION.VirtualAddress;
- ::printf("重定位基地址:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- numTypeOffset=(BASE_RELOCATION.SizeOfBlock-0x8)/0x2;
- ::printf("重定位项数量:%dn",numTypeOffset);
- WORD offset;
- for(int i=0,p=0;i<numTypeOffset;i++,p++)
- {
- if(p%10==0) printf("n");
- ::fread(&offset,sizeof(WORD),1,fp);
- offset=offset & 0x0fff;
- offset=offset + BASE_RELOCATION.VirtualAddress;
- DWord=(unsigned char*)&offset;
- ::printf("%02x%02x ",DWord[1],DWord[0]);
- }
- printf("n");
- ::fread(&BASE_RELOCATION,sizeof(IMAGE_BASE_RELOCATION),1,fp);
- }
- }
- else
- {
- ::printf("------该PE文件无基址重定位表------n");
- }
- }
- void FindResource_Directory(FILE *fp,DWORD addr,int cengci,const DWORD ResourceBase)
- {
- DWORD tempaddr=ResourceBase+addr;
- ::fseek(fp,tempaddr,0);
- IMAGE_RESOURCE_DIRECTORY RESOURCE_DIRECTORY;
- IMAGE_RESOURCE_DIRECTORY_ENTRY DIRECTORY_ENTRY[1000];//默认一个较大的资源表
- ::fread(&RESOURCE_DIRECTORY,sizeof(IMAGE_RESOURCE_DIRECTORY),1,fp);
- int NumOfDIRENTRY=RESOURCE_DIRECTORY.NumberOfIdEntries+RESOURCE_DIRECTORY.NumberOfNamedEntries;
- for(int i=0;i<NumOfDIRENTRY;i++)
- {
- ::fread(&DIRECTORY_ENTRY[i],sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY),1,fp);
- }
- unsigned char *DWord=NULL;
- for(int p=0;p<NumOfDIRENTRY;p++)
- {
- DWord=(unsigned char *)&DIRECTORY_ENTRY[p].Name;
- switch(cengci)
- {
- case 0:
- {
- DWORD name=DIRECTORY_ENTRY[p].Name;
- name=name & 0x80000000;
- if(name == 0x00000000)
- {
- ::printf("资源类型:%02x%02x%02x%02x n",DWord[3],DWord[2],DWord[1],DWord[0]);
- }
- else
- {
- IMAGE_RESOURCE_DIR_STRING_U resourcename;
- name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
- ::fseek(fp,ResourceBase+name,0);
- ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
- ::printf("资源名称:");
- char resname[100];
- ::fseek(fp,ResourceBase+name+2,0);
- ::fread(resname,resourcename.Length*2,1,fp);
- for(WORD i=0;i<resourcename.Length*2;i=i+2)
- {
- ::printf("%c",resname[i]);
- }
- }
- DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
- offset=offset & 0x80000000;
- if(offset == 0x80000000)
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- FindResource_Directory(fp,offset,cengci+1,ResourceBase);
- }
- else
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
- ::fseek(fp,offset+ResourceBase,0);
- ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
- DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
- ::printf("资源数据的RVA:%02X%02X%02X%02X ",DWord[3],DWord[2],DWord[1],DWord[0]);
- ::printf("资源长度%ld ",DATA_ENTRY.Size);
- DWord=(unsigned char *)&DATA_ENTRY.CodePage;
- ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
- }
- break;
- }
- case 1:
- {
- DWORD name=DIRECTORY_ENTRY[p].Name;
- name=name & 0x80000000;
- if(name == 0x00000000)
- {
- ::printf("资源ID:%u n",DIRECTORY_ENTRY[p].Name);
- }
- else
- {
- IMAGE_RESOURCE_DIR_STRING_U resourcename;
- name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
- ::fseek(fp,ResourceBase+name,0);
- ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
- ::printf("资源名称:");
- char resname[200];
- ::fseek(fp,ResourceBase+name+2,0);
- ::fread(resname,resourcename.Length*2,1,fp);
- for(WORD i=0;i<resourcename.Length*2;i=i+2)
- {
- ::printf("%c",resname[i]);
- }
- }
- DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
- offset=offset & 0x80000000;
- if(offset == 0x80000000)
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- FindResource_Directory(fp,offset,cengci+1,ResourceBase);
- }
- else
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
- ::fseek(fp,offset+ResourceBase,0);
- ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
- DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
- ::printf("资源数据的RVA:%02X%02X%02X%02X ",DWord[3],DWord[2],DWord[1],DWord[0]);
- ::printf("资源长度%ld ",DATA_ENTRY.Size);
- DWord=(unsigned char *)&DATA_ENTRY.CodePage;
- ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
- }
- break;
- }
- case 2:
- {
- DWORD name=DIRECTORY_ENTRY[p].Name;
- name=name & 0x80000000;
- if(name == 0x0000)
- {
- ::printf("资源代码页编号:%02x%02x%02x%02x n",DWord[3],DWord[2],DWord[1],DWord[0]);
- }
- else
- {
- IMAGE_RESOURCE_DIR_STRING_U resourcename;
- name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
- ::fseek(fp,ResourceBase+name,0);
- ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
- ::printf("资源名称:");
- char resname[100];
- ::fseek(fp,ResourceBase+name+2,0);
- ::fread(resname,resourcename.Length*2,1,fp);
- for(WORD i=0;i<resourcename.Length*2;i=i+2)
- {
- ::printf("%c",resname[i]);
- }
- }
- DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
- offset=offset & 0x80000000;
- if(offset == 0x80000000)
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- FindResource_Directory(fp,offset,cengci+1,ResourceBase);
- }
- else
- {
- offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
- IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
- ::fseek(fp,offset+ResourceBase,0);
- ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
- DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
- ::printf("资源数据的RVA:%02X%02X%02X%02X ",DWord[3],DWord[2],DWord[1],DWord[0]);
- ::printf("资源长度%ld ",DATA_ENTRY.Size);
- DWord=(unsigned char *)&DATA_ENTRY.CodePage;
- ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
- ::printf("n");
- }
- break;
- }
- default:
- {
- ::printf("超过了3层资源ID:%02x%02x%02x%02x n",DWord[3],DWord[2],DWord[1],DWord[0]);
- }
- }
- }
- }
- void ShowResource(FILE *fp)
- {
- ::fseek(fp,0,0);
- unsigned char *DWord=NULL;
- if(Global_IDD[2].Size != 0)
- {
- ::printf("-----该PE文件资源表如下------n");
- const DWORD ResourceBase=RVATOOFFSET(Global_IDD[2].VirtualAddress,NUMDATA);
- FindResource_Directory(fp,0,0,ResourceBase);
- }
- else
- printf("--------该PE文件无资源-------n");
- }
- void ShowTls(FILE *fp)
- {
- ::fseek(fp,0,0);
- unsigned char *DWord=NULL;
- if(Global_IDD[9].Size != 0)
- {
- ::printf("-----该PE的TLS表如下------n");
- IMAGE_TLS_DIRECTORY TLS;
- const DWORD TlsAddr=RVATOOFFSET(Global_IDD[9].VirtualAddress,NUMDATA);
- ::fseek(fp,TlsAddr,0);
- ::fread(&TLS,sizeof(IMAGE_TLS_DIRECTORY32),1,fp);
- DWord=(unsigned char *)&TLS.AddressOfCallBacks;
- ::printf("TLS.AddressofCallBacks:%02x%02x%02x%02x ",DWord[0],DWord[1],DWord[2],DWord[3]);
- }
- else
- printf("--------该PE文件无TLS-------n");
- }