关于文件内存影像PE:高手请进。。。。。。。。。。。。。。。(100分)

  • 主题发起人 主题发起人 5i1zhou
  • 开始时间 开始时间
5

5i1zhou

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi写一PE文件分析。。在C++Builder中完全成功可是在delphi中得到错误的结果。。请看我的程序:<br>//=====delphi==========<br>procedure TFormMain.PEdump(FileName: AnsiString);<br>type<br>&nbsp; PImageHeader = ^TImageHeader;<br>&nbsp; TImageHeader = packed record<br>&nbsp; &nbsp;PEDosHead &nbsp; &nbsp; &nbsp; : TImageDosHeader;<br>&nbsp; &nbsp; Signature &nbsp; &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp; FileHeader &nbsp; &nbsp; : TImageFileHeader;<br>&nbsp; &nbsp; OptionalHeader : TImageOptionalHeader;<br>&nbsp; &nbsp; &nbsp;PESectionHead :array of TImageSectionHeader;<br>&nbsp;end;<br>var<br>&nbsp; hFile,hMapping:THandle;<br>&nbsp; basepointer,basepointer1:Pointer;<br>&nbsp; PEDosHead:pImageDosHeader;<br>&nbsp; IsDos:word;<br>&nbsp; PImageHeader1:PImageHeader &nbsp;;<br>&nbsp; FileSize:integer;<br>begin<br><br>&nbsp; hFile := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_SHARE_READ, nil, OPEN_EXISTING,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FILE_FLAG_SEQUENTIAL_SCAN, 0);<br>&nbsp; if hFile=INVALID_HANDLE_VALUE then //安全检查<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp;ShowMessage('error');<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; hMapping:=CreateFileMapping(hFile,nil,PAGE_READWRITE,0,0,nil); //创建视图<br>&nbsp; if &nbsp;hMapping=0 then &nbsp; //安全检查<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('CreateFileMapping error');<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; end;<br>&nbsp; basepointer:=MapViewOfFile(hMapping,FILE_MAP_ALL_ACCESS,0,0,0); //创建影像<br>&nbsp; if basepointer=nil then//安全检查<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowMessage('error MapViewofFile');<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hMapping);<br>&nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end; &nbsp;<br>&nbsp; PImageHeader1:=basepointer;<br>&nbsp; IsDos:=PImageHeader1^.PEDosHead.e_magic;<br>&nbsp; if IsDos&lt;&gt;IMAGE_DOS_SIGNATURE then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;ShowMessage('不是Win32');<br>&nbsp; &nbsp; &nbsp;UnmapViewOfFile(basepointer);<br>&nbsp; &nbsp; &nbsp; FileClose(hFile);<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end; &nbsp;<br><br>&nbsp; &nbsp; &nbsp; LabeledEdit1.Text:=IntToHex(int64(basepointer),10);//@@@@得到错误结果<br>&nbsp; &nbsp; &nbsp; LabeledEdit2.Text:=Format('%10x',[PImageHeader1^.PEDosHead._lfanew]);//正确结果<br>&nbsp; &nbsp; &nbsp; LabeledEdit3.Text:=Format('%10d',[PImageHeader1^.FileHeader.NumberOfSections] );//@@@@@大错而特错结果<br><br>&nbsp; UnmapViewOfFile(basepointer);<br>&nbsp; end;<br>//=======c++ builder程序========== &nbsp; 一路OK<br>TFormMain::PEDump(AnsiString FileName)<br>{<br>&nbsp; HANDLE hFile, hMapping;<br>&nbsp; hFile = CreateFile(FileName.c_str() , GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);<br>&nbsp; if(hFile==INVALID_HANDLE_VALUE)<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp;ShowMessage("error open");<br>&nbsp; &nbsp; &nbsp;return 0;<br>&nbsp; &nbsp; &nbsp;}<br>&nbsp; hMapping = CreateFileMapping(hFile, 0, PAGE_READONLY | SEC_COMMIT, 0, 0, 0);<br>&nbsp; &nbsp;if(!hMapping )<br>&nbsp; &nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp;ShowMessage("CreateFileMapping Error");<br>&nbsp; &nbsp; &nbsp;return 0;<br>&nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp;//====<br>&nbsp; &nbsp; &nbsp;void *basepointer;<br>&nbsp; &nbsp; &nbsp;basepointer = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);<br>&nbsp; &nbsp; //=========<br>&nbsp; &nbsp; &nbsp;dos_head =(IMAGE_DOS_HEADER *)basepointer;<br>&nbsp; &nbsp; &nbsp; &nbsp;LabeledEdit1-&gt;Text=IntToHex(__int64(basepointer),10);<br>&nbsp; &nbsp; &nbsp; &nbsp;LabeledEdit2-&gt;Text=IntToHex(__int64(dos_head-&gt;e_lfanew),10) ;<br>&nbsp; &nbsp; &nbsp;peHeader * header;<br>&nbsp; &nbsp; &nbsp;header = (peHeader *)((char *)dos_head + dos_head-&gt;e_lfanew);//得到PE文件头<br>&nbsp; &nbsp; if (header-&gt;signature!=IMAGE_NT_SIGNATURE)<br>&nbsp; &nbsp; &nbsp;{ CloseHandle(hMapping);<br>&nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp; &nbsp;UnmapViewOfFile(basepointer);<br>&nbsp; &nbsp; &nbsp; &nbsp;ShowMessage("error PE header");<br>&nbsp; &nbsp; &nbsp; &nbsp;return 0;<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; LabeledEdit3-&gt;Text=IntToHex(__int64(header-&gt;_head.NumberOfSections),10);<br><br>&nbsp; &nbsp; //===========<br><br>&nbsp; &nbsp; &nbsp;CloseHandle(hMapping);<br>&nbsp; &nbsp; &nbsp;CloseHandle(hFile);<br>&nbsp; &nbsp; &nbsp;UnmapViewOfFile(basepointer);<br>
 
&nbsp;PImageDosHeader = ^TImageDosHeader;<br>&nbsp; &nbsp; {EXTERNALSYM _IMAGE_DOS_HEADER}<br>&nbsp; _IMAGE_DOS_HEADER = packed record &nbsp; &nbsp; &nbsp;{ DOS .EXE header &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_magic: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Magic number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_cblp: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Bytes on last page of file &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_cp: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Pages in file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_crlc: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Relocations &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_cparhdr: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { Size of header in paragraphs &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_minalloc: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Minimum extra paragraphs needed &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_maxalloc: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Maximum extra paragraphs needed &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_ss: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Initial (relative) SS value &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_sp: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Initial SP value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_csum: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Checksum &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_ip: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Initial IP value &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_cs: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Initial (relative) CS value &nbsp; &nbsp; &nbsp;}<br>&nbsp; &nbsp; &nbsp; e_lfarlc: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ File address of relocation table }<br>&nbsp; &nbsp; &nbsp; e_ovno: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Overlay number &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_res: array [0..3] of Word; &nbsp; &nbsp; &nbsp; { Reserved words &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_oemid: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { OEM identifier (for e_oeminfo) &nbsp; }<br>&nbsp; &nbsp; &nbsp; e_oeminfo: Word; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { OEM information; e_oemid specific}<br>&nbsp; &nbsp; &nbsp; e_res2: array [0..9] of Word; &nbsp; &nbsp; &nbsp;{ Reserved words &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; _lfanew: LongInt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ File address of new exe header &nbsp; }<br>&nbsp; end;<br>&nbsp; TImageDosHeader = _IMAGE_DOS_HEADER;<br>&nbsp; {$EXTERNALSYM IMAGE_DOS_HEADER}<br>&nbsp; IMAGE_DOS_HEADER = _IMAGE_DOS_HEADER;<br><br><br>&nbsp; PImageFileHeader = ^TImageFileHeader;<br>&nbsp; _IMAGE_FILE_HEADER = packed record<br>&nbsp; &nbsp; Machine: Word;<br>&nbsp; &nbsp; NumberOfSections: Word;<br>&nbsp; &nbsp; TimeDateStamp: DWORD;<br>&nbsp; &nbsp; PointerToSymbolTable: DWORD;<br>&nbsp; &nbsp; NumberOfSymbols: DWORD;<br>&nbsp; &nbsp; SizeOfOptionalHeader: Word;<br>&nbsp; &nbsp; Characteristics: Word;<br>&nbsp; end;<br>
 
后退
顶部