PeHelp.h
上传用户:polioc
上传日期:2022-08-03
资源大小:1956k
文件大小:21k
源码类别:

文件操作

开发平台:

Visual C++

  1. #include <windows.h>
  2. //全局函数用于保存各块的RVA和OFFSET和输入输出表信息
  3. DWORD DATARVA[20];//默认为20个块表
  4. DWORD DATAOFFSET[20];
  5. IMAGE_DATA_DIRECTORY Global_IDD[16];//数据目录表信息
  6. IMAGE_IMPORT_DESCRIPTOR iid[20]={0};//默认为调用20个DLL
  7. DWORD NUMDATA;//块数目
  8. BOOL CheckPeFile(FILE *ImageBase)//检查是否是PE文件
  9. {
  10. ::fseek(ImageBase,0,0);//每个函数使用前先文件定位
  11.    if(NULL == ImageBase)
  12.    {
  13.    return 0;
  14.    }
  15.    IMAGE_DOS_HEADER DOS_HEADER;
  16.    const DWORD PESYMBOL=0x00004550;
  17.    const WORD DOSSYMBOL=0x5A4D;
  18.    ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
  19.    if(DOS_HEADER.e_magic == DOSSYMBOL)
  20.    {
  21.    ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);//文件定位
  22.        DWORD pesymbol;
  23.    ::fread(&pesymbol,sizeof(DWORD),1,ImageBase);
  24.       if(pesymbol == PESYMBOL)
  25.   return 1;
  26.   else 
  27.   return 0;
  28.    }
  29.    else
  30.     return 0;
  31. }
  32. //一些输出转换函数
  33. //WORD 转换为2个16进制数
  34. unsigned char WORDTOLI16(const unsigned char *old)//WORD的低8位
  35. {
  36.      unsigned char LI16;
  37.  LI16=old[0];
  38.  return LI16;
  39. }
  40. unsigned char WORDTOHI16(const unsigned char *old)//WORD的高8位
  41. {
  42.       unsigned char HI16;
  43.   HI16=old[1];
  44.       return HI16;
  45. }
  46. //DWORD 转换为4个16进制数
  47. unsigned char DWORDTO_0_16(const unsigned char *old)
  48. {
  49.      unsigned char LI16;
  50.  LI16=old[0];
  51.  return LI16;
  52. }
  53. unsigned char DWORDTO_1_16(const unsigned char *old)
  54. {
  55.      unsigned char LI16;
  56.  LI16=old[1];
  57.  return LI16;
  58. }
  59. unsigned char DWORDTO_2_16(const unsigned char *old)
  60. {
  61.      unsigned char LI16;
  62.  LI16=old[2];
  63.  return LI16;
  64. }
  65. unsigned char DWORDTO_3_16(const unsigned char *old)
  66. {
  67.      unsigned char LI16;
  68.  LI16=old[3];
  69.  return LI16;
  70. }
  71. void ShowPEDosHeader(FILE *ImageBase)//显示PE文件DOS头
  72. {
  73. ::fseek(ImageBase,0,0);
  74.     IMAGE_DOS_HEADER DOS_HEADER;
  75. ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
  76. ::printf("------PE文件DOS头部数据-------n");
  77. ::printf("e_magic    :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_magic),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_magic));
  78. ::printf("e_cblp     :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cblp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cblp));
  79. ::printf("e_cp       :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cp));
  80. ::printf("e_crlc     :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_crlc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_crlc));
  81.     ::printf("e_cparhdr  :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cparhdr),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cparhdr));
  82. ::printf("e_minalloc :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_minalloc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_minalloc));
  83. ::printf("e_maxalloc :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_maxalloc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_maxalloc));
  84. ::printf("e_ss       :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ss),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ss));
  85. ::printf("e_sp       :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_sp),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_sp));
  86. ::printf("e_csum     :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_csum),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_csum));
  87. ::printf("e_ip       :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ip),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ip));
  88. ::printf("e_cs       :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_cs),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_cs));
  89. ::printf("e_lfarlc   :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_lfarlc),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_lfarlc));
  90. ::printf("e_ovno     :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_ovno),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_ovno));
  91. ::printf("e_res      :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_res),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_res));
  92. ::printf("e_oemid    :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_oemid),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_oemid));
  93. ::printf("e_oeminfo  :%02x%02x   ",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_oeminfo),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_oeminfo));
  94. ::printf("e_res2     :%02x%02x   n",WORDTOLI16((const unsigned char *)&DOS_HEADER.e_res2),WORDTOHI16((const unsigned char *)&DOS_HEADER.e_res2));
  95. ::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));
  96.     ::printf("n");
  97. }
  98. void ShowPENTHeader(FILE *ImageBase)
  99. {
  100.     ::fseek(ImageBase,0,0);
  101. IMAGE_DOS_HEADER DOS_HEADER;
  102.     IMAGE_NT_HEADERS NT_HEADER;
  103.     ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
  104. ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);
  105. ::fread(&NT_HEADER,sizeof(IMAGE_NT_HEADERS),1,ImageBase);
  106. ::printf("------PE文件NT头部数据-------n");
  107.     const unsigned char *DWord=NULL;
  108. DWord=(const unsigned char *)&NT_HEADER.Signature;
  109.     ::printf("Singature:%02x%02x%02x%02x  n",DWord[0],DWord[1],DWord[2],DWord[3]);
  110. ::printf("n");
  111. ::printf("------PE文件NT头部IMAGE_FILE_HEADER数据-------n");
  112. IMAGE_FILE_HEADER FILE_HEADER;
  113. ::memcpy(&FILE_HEADER,&NT_HEADER.FileHeader,sizeof(IMAGE_FILE_HEADER));
  114. DWord=(const unsigned char *)&FILE_HEADER.Machine;
  115. ::printf("Machine:%02x%02x  ",DWord[0],DWord[1]);
  116. DWord=(const unsigned char *)&FILE_HEADER.NumberOfSections;
  117. ::printf("NumberOfSections:%02X%02Xn",DWord[0],DWord[1]);
  118. ::printf("n");
  119. ::printf("------PE文件NT头部IMAGE_OPTIONAL_HEADER数据-------n");
  120. IMAGE_OPTIONAL_HEADER OPTIONAL_HEADER;
  121.     ::memcpy(&OPTIONAL_HEADER,&NT_HEADER.OptionalHeader,sizeof(IMAGE_OPTIONAL_HEADER));
  122.     DWord=(const unsigned char *)&OPTIONAL_HEADER.AddressOfEntryPoint;
  123. ::printf("AddressOfEntryPoint:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  124.     DWord=(const unsigned char *)&OPTIONAL_HEADER.BaseOfCode;
  125.     ::printf("BaseOfCode:%02x%02x%02x%02x  n",DWord[0],DWord[1],DWord[2],DWord[3]);
  126.     DWord=(const unsigned char *)&OPTIONAL_HEADER.BaseOfData;
  127.     ::printf("BaseOfData:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  128. DWord=(const unsigned char *)&OPTIONAL_HEADER.NumberOfRvaAndSizes;
  129.     ::printf("NumberOfRvaAndSizes:%02x%02x%02x%02x  n",DWord[0],DWord[1],DWord[2],DWord[3]);
  130. ::printf("n");
  131.     ::printf("------PE文件NT头部数据目录表数据-------n");
  132. for(int i=0;i<16;i++)
  133. {
  134. if(0 != NT_HEADER.OptionalHeader.DataDirectory[i].Size)
  135. {
  136. DWord=(const unsigned char *)&NT_HEADER.OptionalHeader.DataDirectory[i].VirtualAddress;
  137. ::printf("序号:%2d  VirtualAddress:%02x%02x%02x%02xn",i,DWord[0],DWord[1],DWord[2],DWord[3]);
  138. ::memcpy(&Global_IDD[i],&NT_HEADER.OptionalHeader.DataDirectory[i],sizeof(IMAGE_DATA_DIRECTORY));
  139. }
  140. }
  141. }
  142. void ShowSectionHeader(FILE *ImageBase)
  143. {
  144.     ::fseek(ImageBase,0,0);
  145. const unsigned char *DWord=NULL;
  146.     IMAGE_DOS_HEADER DOS_HEADER;
  147.     IMAGE_NT_HEADERS NT_HEADER;
  148. IMAGE_SECTION_HEADER SECTION_HEADER[20];//默认为20个块表
  149.     ::fread(&DOS_HEADER,sizeof(IMAGE_DOS_HEADER),1,ImageBase);
  150.     ::fseek(ImageBase,DOS_HEADER.e_lfanew,0);
  151. ::fread(&NT_HEADER,sizeof(IMAGE_NT_HEADERS),1,ImageBase);
  152.     ::fseek(ImageBase,DOS_HEADER.e_lfanew+sizeof(IMAGE_NT_HEADERS),0);
  153. if(NT_HEADER.FileHeader.NumberOfSections !=0 )
  154. {
  155. ::printf("------PE文件的块表信息-------n");
  156.   for(int i=0;i<NT_HEADER.FileHeader.NumberOfSections;i++)
  157.   {
  158. ::fread(&SECTION_HEADER[i],sizeof(IMAGE_SECTION_HEADER),1,ImageBase);
  159.         ::printf("Name:%s   ",SECTION_HEADER[i].Name);
  160.         DWord=(const unsigned char *)&SECTION_HEADER[i].VirtualAddress;
  161. ::printf("VirtualAddress:%02x%02x%02x%02x   ",DWord[0],DWord[1],DWord[2],DWord[3]);
  162. DWord=(const unsigned char *)&SECTION_HEADER[i].PointerToRawData;
  163.         ::printf("PointerToRawData:%02x%02x%02x%02xn",DWord[0],DWord[1],DWord[2],DWord[3]);
  164. DATARVA[i]=SECTION_HEADER[i].VirtualAddress;
  165. DATAOFFSET[i]=SECTION_HEADER[i].PointerToRawData;
  166.   }
  167.   NUMDATA=i;
  168. }
  169. }
  170. //找到相应的块
  171. DWORD RVATOOFFSET(DWORD addr,DWORD numdata)
  172. {
  173.    for(DWORD i=0;i<numdata;i++)
  174.    {
  175.    if(DATARVA[i+1] != 0)
  176.    {
  177.    if(addr>=DATARVA[i] && addr<DATARVA[i+1])
  178.    {
  179.    return addr-DATARVA[i]+DATAOFFSET[i]; 
  180.    }
  181.    }
  182.    else
  183.    {
  184.    if(addr>=DATARVA[i])
  185.    {
  186.                return addr-DATARVA[i]+DATAOFFSET[i]; 
  187.    }
  188.    }
  189.    }   
  190. }
  191. void ShowIID(FILE *fp)
  192. {
  193. ::fseek(fp,0,0);
  194. if(Global_IDD[1].Size != 0)
  195. {
  196. ::printf("------该PE文件输入表如下:--------n");
  197. DWORD addr=RVATOOFFSET(Global_IDD[1].VirtualAddress,NUMDATA);
  198. const unsigned char *DWord=NULL;
  199.         fseek(fp,addr,0);
  200. DWORD isIID;
  201.     ::fread(&isIID,sizeof(DWORD),1,fp);
  202. int i=0;
  203. while(isIID != 0)
  204. ::fseek(fp,addr+i*sizeof(IMAGE_IMPORT_DESCRIPTOR),0);
  205. ::fread(&iid[i],sizeof(IMAGE_IMPORT_DESCRIPTOR),1,fp);
  206. i++;
  207.             ::fread(&isIID,sizeof(DWORD),1,fp);
  208. }
  209. for(int p=0;p<i;p++)
  210. {
  211.                DWord=(const unsigned char *)&iid[p].OriginalFirstThunk;
  212.    ::printf("OriginalFirstThunk:%02x%02x%02x%02x   ",DWord[0],DWord[1],DWord[2],DWord[3]);
  213.                DWord=(const unsigned char *)&iid[p].FirstThunk;
  214.    ::printf("FirstThunk:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  215.    DWORD nameaddr;
  216.    nameaddr=RVATOOFFSET(iid[p].Name,NUMDATA);
  217.    ::fseek(fp,nameaddr,0);
  218.    ::printf("DLLName:");
  219.    char ch=::fgetc(fp);
  220.    while(ch != 0)
  221.    {
  222.    putchar(ch);
  223.    ch=fgetc(fp);
  224.    }
  225.    ::printf("n");
  226. }
  227. ::printf("------该PE文件输入函数有:--------n");
  228.         DWORD funaddr1;
  229. DWORD funaddr2;
  230. int numdll=0;
  231. while(iid[numdll].OriginalFirstThunk != 0)
  232. {
  233.         funaddr1=RVATOOFFSET(iid[numdll].OriginalFirstThunk,NUMDATA);
  234. ::fseek(fp,funaddr1,0);
  235. ::fread(&funaddr2,sizeof(DWORD),1,fp);
  236. int numfun=0;
  237. while(funaddr2 != 0)
  238. {
  239. funaddr2=RVATOOFFSET(funaddr2,NUMDATA);
  240. ::fseek(fp,funaddr2,0);
  241. int i=0;
  242. char ch=::fgetc(fp);
  243. while(ch != 0 || i<2)
  244. {
  245. if(i>=2)//头两个字节是序号不输出
  246. if(ch<=0)
  247. {
  248.    ::printf("按序号导出的函数");
  249.    break;
  250. }
  251.   putchar(ch);  
  252. }          
  253. i++;
  254. ch=fgetc(fp);
  255. }
  256. ::printf("n");
  257. numfun++;
  258. ::fseek(fp,funaddr1+numfun*sizeof(DWORD),0);
  259. ::fread(&funaddr2,sizeof(DWORD),1,fp);
  260.             }
  261. numdll++;
  262. }
  263. }
  264. else
  265. printf("-------该PE文件无输入表.--------n");
  266. }
  267. void ShowIED(FILE *fp)
  268. {
  269. ::fseek(fp,0,0);
  270.     if(Global_IDD[0].Size != 0)
  271. {
  272. ::printf("------该PE文件输出表如下:--------n");
  273.         DWORD addr=RVATOOFFSET(Global_IDD[0].VirtualAddress,NUMDATA);
  274. const unsigned char *DWord=NULL;
  275. IMAGE_EXPORT_DIRECTORY EXPORT_DIRECTORY;
  276. fseek(fp,addr,0);
  277. ::fread(&EXPORT_DIRECTORY,sizeof(IMAGE_EXPORT_DIRECTORY),1,fp);
  278.         
  279. DWord=(const unsigned char *)&EXPORT_DIRECTORY.NumberOfFunctions;
  280. ::printf("NumOfFunction:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  281. DWord=(const unsigned char *)&EXPORT_DIRECTORY.NumberOfNames;
  282.         ::printf("NumberOfNames:%02x%02x%02x%02x  n",DWord[0],DWord[1],DWord[2],DWord[3]);
  283. DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfFunctions;
  284.         ::printf("AddressOfFunctions:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  285. DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfNames;
  286.         ::printf("AddressOfNames:%02x%02x%02x%02x  n",DWord[0],DWord[1],DWord[2],DWord[3]);
  287. DWord=(const unsigned char *)&EXPORT_DIRECTORY.AddressOfNameOrdinals;
  288. ::printf("AddressOfNameOrdinals:%02x%02x%02x%02x  ",DWord[0],DWord[1],DWord[2],DWord[3]);
  289.         
  290. ::printf("导出表名字:");
  291. DWORD dllname;
  292. dllname=RVATOOFFSET(EXPORT_DIRECTORY.Name,NUMDATA);
  293. ::fseek(fp,dllname,0);
  294.         char ch=::fgetc(fp);
  295. while(ch != 0)
  296. {
  297. putchar(ch);
  298. ch=fgetc(fp);
  299. }
  300. ::printf("n--------导出函数如下---------n");
  301.        
  302.         addr=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfNames,NUMDATA);
  303.         ::fseek(fp,addr,0);
  304. DWORD addr2;
  305. ::fread(&addr2,sizeof(DWORD),1,fp);
  306. DWORD  addr1=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfNameOrdinals,NUMDATA);
  307. ::fseek(fp,addr1,0);
  308.         WORD Ordinal;
  309. ::fread(&Ordinal,sizeof(WORD),1,fp);
  310. DWORD funAddr=RVATOOFFSET(EXPORT_DIRECTORY.AddressOfFunctions,NUMDATA);
  311. const DWORD FunOfAddr=funAddr;
  312. funAddr=funAddr+Ordinal*sizeof(DWORD);
  313. ::fseek(fp,funAddr,0);
  314.         ::printf("虚拟地址    导出函数名称n");
  315. DWORD funAddr1;
  316. for(DWORD i=0;i<EXPORT_DIRECTORY.NumberOfNames;i++)
  317. {          
  318. ::fread(&funAddr1,sizeof(DWORD),1,fp);
  319.             DWord=(const unsigned char*)&funAddr1;
  320. ::printf("%02x%02x%02x%02x    ",DWord[0],DWord[1],DWord[2],DWord[3]);
  321. addr2=RVATOOFFSET(addr2,NUMDATA);
  322. ::fseek(fp,addr2,0);
  323. char ch=::fgetc(fp);
  324.             while(ch != 0)
  325. {
  326.     putchar(ch);
  327.     ch=fgetc(fp);
  328. }
  329. ::printf("n");
  330.             ::fseek(fp,addr+sizeof(DWORD),0);
  331. addr=addr+sizeof(DWORD);
  332.             ::fread(&addr2,sizeof(DWORD),1,fp);
  333.               
  334. addr1=addr1+sizeof(WORD);
  335. ::fseek(fp,addr1,0);
  336.             ::fread(&Ordinal,sizeof(WORD),1,fp);
  337. funAddr=FunOfAddr+Ordinal*sizeof(DWORD);
  338.     ::fseek(fp,funAddr,0);
  339. }
  340. }
  341. else
  342.         ::printf("------该PE文件无输出表:--------n");
  343. }
  344. void ShowReloCation(FILE *fp)
  345. {
  346. ::fseek(fp,0,0);
  347. if(Global_IDD[5].Size != 0)
  348. {
  349. ::printf("------该PE文件基址重定位表------n");
  350.         IMAGE_BASE_RELOCATION BASE_RELOCATION;
  351. DWORD addr=RVATOOFFSET(Global_IDD[5].VirtualAddress,NUMDATA);
  352. unsigned char *DWord=NULL;
  353. int numTypeOffset=0;
  354. ::fseek(fp,addr,0);
  355. ::fread(&BASE_RELOCATION,sizeof(IMAGE_BASE_RELOCATION),1,fp);
  356. while(BASE_RELOCATION.VirtualAddress != 0)
  357. {
  358. DWord=(unsigned char*)&BASE_RELOCATION.VirtualAddress;
  359. ::printf("重定位基地址:%02x%02x%02x%02x   ",DWord[0],DWord[1],DWord[2],DWord[3]);
  360. numTypeOffset=(BASE_RELOCATION.SizeOfBlock-0x8)/0x2;
  361. ::printf("重定位项数量:%dn",numTypeOffset);
  362. WORD offset;
  363. for(int i=0,p=0;i<numTypeOffset;i++,p++)
  364. {
  365. if(p%10==0) printf("n");
  366. ::fread(&offset,sizeof(WORD),1,fp);
  367. offset=offset & 0x0fff;
  368. offset=offset + BASE_RELOCATION.VirtualAddress;
  369. DWord=(unsigned char*)&offset;
  370. ::printf("%02x%02x  ",DWord[1],DWord[0]);
  371. }
  372. printf("n");
  373. ::fread(&BASE_RELOCATION,sizeof(IMAGE_BASE_RELOCATION),1,fp);
  374. }
  375. }
  376. else
  377. {
  378. ::printf("------该PE文件无基址重定位表------n");
  379. }    
  380. }
  381. void FindResource_Directory(FILE *fp,DWORD addr,int cengci,const DWORD ResourceBase)
  382. {
  383. DWORD tempaddr=ResourceBase+addr;
  384. ::fseek(fp,tempaddr,0);
  385.     IMAGE_RESOURCE_DIRECTORY RESOURCE_DIRECTORY;
  386. IMAGE_RESOURCE_DIRECTORY_ENTRY DIRECTORY_ENTRY[1000];//默认一个较大的资源表
  387.     ::fread(&RESOURCE_DIRECTORY,sizeof(IMAGE_RESOURCE_DIRECTORY),1,fp);
  388.     int NumOfDIRENTRY=RESOURCE_DIRECTORY.NumberOfIdEntries+RESOURCE_DIRECTORY.NumberOfNamedEntries;
  389. for(int i=0;i<NumOfDIRENTRY;i++)
  390. {
  391. ::fread(&DIRECTORY_ENTRY[i],sizeof(IMAGE_RESOURCE_DIRECTORY_ENTRY),1,fp);
  392. }
  393. unsigned char *DWord=NULL;
  394. for(int p=0;p<NumOfDIRENTRY;p++)
  395. {
  396. DWord=(unsigned char *)&DIRECTORY_ENTRY[p].Name;
  397. switch(cengci)
  398. {
  399. case 0:
  400. {
  401. DWORD name=DIRECTORY_ENTRY[p].Name;
  402. name=name & 0x80000000;
  403. if(name == 0x00000000)
  404. {
  405.    ::printf("资源类型:%02x%02x%02x%02x  n",DWord[3],DWord[2],DWord[1],DWord[0]);
  406. }
  407. else
  408. {
  409.                    IMAGE_RESOURCE_DIR_STRING_U resourcename;
  410.    name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
  411.    ::fseek(fp,ResourceBase+name,0);
  412.    ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
  413.    ::printf("资源名称:");   
  414.    char resname[100];
  415.    ::fseek(fp,ResourceBase+name+2,0);
  416.    ::fread(resname,resourcename.Length*2,1,fp);
  417.    for(WORD i=0;i<resourcename.Length*2;i=i+2)
  418.    {
  419.    ::printf("%c",resname[i]);
  420.    }
  421. }
  422.                 DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
  423. offset=offset & 0x80000000;
  424. if(offset == 0x80000000)
  425. {
  426.    offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  427.                    FindResource_Directory(fp,offset,cengci+1,ResourceBase);
  428. }
  429. else
  430. {
  431.    offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  432.                    IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
  433.    ::fseek(fp,offset+ResourceBase,0);
  434.    ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
  435.    DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
  436.    ::printf("资源数据的RVA:%02X%02X%02X%02X   ",DWord[3],DWord[2],DWord[1],DWord[0]);
  437.                    ::printf("资源长度%ld  ",DATA_ENTRY.Size);
  438.    DWord=(unsigned char *)&DATA_ENTRY.CodePage;
  439.                    ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
  440. }
  441. break;
  442. }
  443. case 1:
  444. {
  445. DWORD name=DIRECTORY_ENTRY[p].Name;
  446. name=name & 0x80000000;
  447. if(name == 0x00000000)
  448. {
  449. ::printf("资源ID:%u  n",DIRECTORY_ENTRY[p].Name);
  450.                 }
  451. else
  452. {
  453. IMAGE_RESOURCE_DIR_STRING_U resourcename;
  454. name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
  455. ::fseek(fp,ResourceBase+name,0);
  456. ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
  457. ::printf("资源名称:");
  458. char resname[200];
  459. ::fseek(fp,ResourceBase+name+2,0);
  460. ::fread(resname,resourcename.Length*2,1,fp);
  461. for(WORD i=0;i<resourcename.Length*2;i=i+2)
  462. {
  463. ::printf("%c",resname[i]);
  464. }
  465. }
  466.                 DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
  467. offset=offset & 0x80000000;
  468. if(offset == 0x80000000)
  469. {
  470. offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  471. FindResource_Directory(fp,offset,cengci+1,ResourceBase);
  472. }
  473. else
  474. {
  475. offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  476. IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
  477. ::fseek(fp,offset+ResourceBase,0);
  478. ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
  479. DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
  480. ::printf("资源数据的RVA:%02X%02X%02X%02X   ",DWord[3],DWord[2],DWord[1],DWord[0]);
  481. ::printf("资源长度%ld  ",DATA_ENTRY.Size);
  482. DWord=(unsigned char *)&DATA_ENTRY.CodePage;
  483. ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
  484. }
  485. break;
  486. }
  487. case 2:
  488. {
  489. DWORD name=DIRECTORY_ENTRY[p].Name;
  490. name=name & 0x80000000;
  491. if(name == 0x0000)
  492. {
  493. ::printf("资源代码页编号:%02x%02x%02x%02x  n",DWord[3],DWord[2],DWord[1],DWord[0]);
  494.                 }
  495. else
  496. {
  497. IMAGE_RESOURCE_DIR_STRING_U resourcename;
  498. name=DIRECTORY_ENTRY[p].Name & 0x0000ffff;
  499. ::fseek(fp,ResourceBase+name,0);
  500. ::fread(&resourcename,sizeof(IMAGE_RESOURCE_DIR_STRING_U),1,fp);
  501. ::printf("资源名称:");
  502. char resname[100];
  503. ::fseek(fp,ResourceBase+name+2,0);
  504. ::fread(resname,resourcename.Length*2,1,fp);
  505. for(WORD i=0;i<resourcename.Length*2;i=i+2)
  506. {
  507. ::printf("%c",resname[i]);
  508. }
  509. }
  510.                 DWORD offset=DIRECTORY_ENTRY[p].OffsetToData;
  511. offset=offset & 0x80000000;
  512. if(offset == 0x80000000)
  513. {
  514. offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  515. FindResource_Directory(fp,offset,cengci+1,ResourceBase);
  516. }
  517. else
  518. {
  519. offset=DIRECTORY_ENTRY[p].OffsetToData & 0x0000ffff;
  520. IMAGE_RESOURCE_DATA_ENTRY DATA_ENTRY;
  521. ::fseek(fp,offset+ResourceBase,0);
  522. ::fread(&DATA_ENTRY,sizeof(IMAGE_RESOURCE_DATA_ENTRY),1,fp);
  523. DWord=(unsigned char *)&DATA_ENTRY.OffsetToData;
  524. ::printf("资源数据的RVA:%02X%02X%02X%02X   ",DWord[3],DWord[2],DWord[1],DWord[0]);
  525. ::printf("资源长度%ld  ",DATA_ENTRY.Size);
  526. DWord=(unsigned char *)&DATA_ENTRY.CodePage;
  527. ::printf("资源数据的代码页:%02X%02X%02X%02X n ",DWord[3],DWord[2],DWord[1],DWord[0]);
  528. ::printf("n");
  529. }
  530. break;
  531. }
  532. default:
  533. {
  534. ::printf("超过了3层资源ID:%02x%02x%02x%02x  n",DWord[3],DWord[2],DWord[1],DWord[0]);
  535. }
  536. }
  537. }
  538. }
  539. void ShowResource(FILE *fp)
  540. {
  541. ::fseek(fp,0,0);
  542. unsigned char *DWord=NULL;
  543. if(Global_IDD[2].Size != 0)
  544. {
  545. ::printf("-----该PE文件资源表如下------n");
  546. const DWORD ResourceBase=RVATOOFFSET(Global_IDD[2].VirtualAddress,NUMDATA);
  547. FindResource_Directory(fp,0,0,ResourceBase);
  548. }
  549. else
  550. printf("--------该PE文件无资源-------n");
  551. }
  552. void ShowTls(FILE *fp)
  553. {
  554. ::fseek(fp,0,0);
  555.     unsigned char *DWord=NULL;
  556. if(Global_IDD[9].Size != 0)
  557. {
  558. ::printf("-----该PE的TLS表如下------n");
  559.         IMAGE_TLS_DIRECTORY TLS;
  560.         const DWORD TlsAddr=RVATOOFFSET(Global_IDD[9].VirtualAddress,NUMDATA);
  561. ::fseek(fp,TlsAddr,0);
  562. ::fread(&TLS,sizeof(IMAGE_TLS_DIRECTORY32),1,fp);
  563. DWord=(unsigned char *)&TLS.AddressOfCallBacks;
  564.         ::printf("TLS.AddressofCallBacks:%02x%02x%02x%02x   ",DWord[0],DWord[1],DWord[2],DWord[3]);
  565. }
  566. else
  567. printf("--------该PE文件无TLS-------n");
  568. }