⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pe-tut5.html

📁 在DOS下编程因为实模式的限制
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  .endif <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke UnmapViewOfFile, pMapping <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .else <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
  invoke MessageBox, 0, addr FileMappingError, addr AppName, MB_OK+MB_ICONERROR 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.endif <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke CloseHandle,hMapping 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .else <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke MessageBox, 0, 
  addr FileOpenMappingError, addr AppName, MB_OK+MB_ICONERROR <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .endif <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke CloseHandle, hFile <br>
  &nbsp;&nbsp;&nbsp;&nbsp;.else <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke MessageBox, 0, addr FileOpenError, 
  addr AppName, MB_OK+MB_ICONERROR <br>
  &nbsp;&nbsp;&nbsp; .endif <br>
  &nbsp;&nbsp;.endif <br>
  &nbsp;&nbsp;invoke ExitProcess, 0 <br>
  &nbsp;&nbsp;invoke InitCommonControls <br>
  start endp <br>
  <br>
  SEHHandler proc uses edx pExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD 
  <br>
  &nbsp;&nbsp; mov edx,pFrame <br>
  &nbsp;&nbsp; assume edx:ptr SEH <br>
  &nbsp;&nbsp;&nbsp;mov eax,pContext <br>
  &nbsp;&nbsp; assume eax:ptr CONTEXT <br>
  &nbsp;&nbsp; push [edx].SafeOffset <br>
  &nbsp;&nbsp; pop [eax].regEip <br>
  &nbsp;&nbsp; push [edx].PrevEsp <br>
  &nbsp;&nbsp; pop [eax].regEsp <br>
  &nbsp;&nbsp; push [edx].PrevEbp <br>
  &nbsp;&nbsp; pop [eax].regEbp <br>
  &nbsp;&nbsp; mov ValidPE, FALSE <br>
  &nbsp;&nbsp; mov eax,ExceptionContinueExecution <br>
  &nbsp;&nbsp; ret <br>
  SEHHandler endp <br>
  <br>
  DlgProc proc uses edi esi hDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD 
  <br>
  &nbsp;&nbsp; LOCAL lvc:LV_COLUMN <br>
  &nbsp;&nbsp; LOCAL lvi:LV_ITEM <br>
  &nbsp;&nbsp; .if uMsg==WM_INITDIALOG <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov esi, lParam <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.imask,LVCF_FMT or LVCF_TEXT or LVCF_WIDTH 
  or LVCF_SUBITEM <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.fmt,LVCFMT_LEFT <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.lx,80 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.iSubItem,0 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset SectionName <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,0,addr 
  lvc inc lvc.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.fmt,LVCFMT_RIGHT <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset VirtualSize <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,1,addr 
  lvc <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvc.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset VirtualAddress <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,2,addr 
  lvc <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvc.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset SizeOfRawData <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,3,addr 
  lvc <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvc.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset RawOffset <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,4,addr 
  lvc <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvc.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvc.pszText,offset Characteristics <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTCOLUMN,5,addr 
  lvc <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov ax, NumberOfSections <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; movzx eax,ax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov edi,eax &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.imask,LVIF_TEXT <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.iItem,0 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assume esi:ptr IMAGE_SECTION_HEADER <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .while edi>0 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.iSubItem,0 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke RtlZeroMemory,addr buffer,9 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invoke lstrcpyn,addr buffer,addr 
  [esi].Name1,8 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke wsprintf,addr buffer,addr 
  template,[esi].Misc.VirtualSize <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_SETITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke wsprintf,addr buffer,addr 
  template,[esi].VirtualAddress <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_SETITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke wsprintf,addr buffer,addr 
  template,[esi].SizeOfRawData <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_SETITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke wsprintf,addr buffer,addr 
  template,[esi].PointerToRawData <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_SETITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke wsprintf,addr buffer,addr 
  template,[esi].Characteristics <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iSubItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_SETITEM,0,addr 
  lvi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inc lvi.iItem <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dec edi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add esi, sizeof IMAGE_SECTION_HEADER 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .endw <br>
  &nbsp;&nbsp; .elseif <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uMsg==WM_CLOSE <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke EndDialog,hDlg,NULL 
  <br>
  &nbsp;&nbsp; .else <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov eax,FALSE <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret <br>
  &nbsp;&nbsp;&nbsp;.endif <br>
  &nbsp;&nbsp; mov eax,TRUE <br>
  &nbsp;&nbsp; ret <br>
  DlgProc endp <br>
  <br>
  ShowSectionInfo proc uses edi <br>
  &nbsp;&nbsp; mov edi, pMapping <br>
  &nbsp;&nbsp; assume edi:ptr IMAGE_DOS_HEADER <br>
  &nbsp;&nbsp; add edi, [edi].e_lfanew <br>
  &nbsp;&nbsp; assume edi:ptr IMAGE_NT_HEADERS <br>
  &nbsp;&nbsp; mov ax,[edi].FileHeader.NumberOfSections <br>
  &nbsp;&nbsp; movzx eax,ax <br>
  &nbsp;&nbsp; mov NumberOfSections,eax <br>
  &nbsp;&nbsp; add edi,sizeof IMAGE_NT_HEADERS <br>
  &nbsp;&nbsp; invoke DialogBoxParam, hInstance, IDD_SECTIONTABLE,NULL, addr DlgProc, 
  edi<br>
  &nbsp;&nbsp; ret <br>
  ShowSectionInfo endp <br>
  end start </font></p>
<h3><font face="Arial, Helvetica, sans-serif">Analysis:</font></h3>
<p><font face="MS Sans Serif" size="-1">This example reuses the code of the example 
  in PE tutorial 2. After it verifies that the file is a valid PE, it calls a 
  function, ShowSectionInfo.</font></p>
<p><font face="Fixedsys">ShowSectionInfo proc uses edi <br>
  &nbsp;&nbsp; mov edi, pMapping <br>
  &nbsp;&nbsp; assume edi:ptr IMAGE_DOS_HEADER <br>
  &nbsp;&nbsp; add edi, [edi].e_lfanew</font><font face="Fixedsys"><br>
  &nbsp;&nbsp; assume edi:ptr IMAGE_NT_HEADERS</font></p>
<p><font face="MS Sans Serif" size="-1">We use edi as the pointer to the data 
  in the PE file. At first, we initialize it to the value of pMapping which is 
  the address of the DOS header. Then we add the value in <font color="#FFFFCC"><b>e_lfanew</b></font> 
  to it so it now contains the address of the PE header.</font></p>
<p><font face="Fixedsys"> &nbsp;&nbsp; mov ax,[edi].FileHeader.NumberOfSections<br>
  &nbsp;&nbsp; mov NumberOfSections,ax </font></p>
<p><font face="MS Sans Serif" size="-1">Since we need to walk the section table, 
  we must obtain the number of sections in this file. That's the value in NumberOfSections 
  member of the file header. Don't forget that this member is of word size.</font></p>
<p><font face="Fixedsys"> &nbsp;&nbsp; add edi,sizeof IMAGE_NT_HEADERS </font></p>
<p><font face="MS Sans Serif" size="-1">Edi currently contains the address of 
  the PE header. Adding the size of the PE header to it will make it point at 
  the section table.</font></p>
<p><font face="Fixedsys"> &nbsp;&nbsp; invoke DialogBoxParam, hInstance, IDD_SECTIONTABLE,NULL, 
  addr DlgProc, edi</font></p>
<p><font face="MS Sans Serif" size="-1">Call <font color="#FFFFCC"><b>DialogBoxParam</b></font> 
  to show the dialog box containing the listview control. Note that we pass the 
  address of the section table as its last parameter. This value will be available 
  in lParam during <font color="#CCFFCC"><b>WM_INITDIALOG</b></font> message. 
  </font></p>
<p><font face="MS Sans Serif" size="-1">In the dialog box procedure, in response 
  to WM_INITDIALOG message, we store the value of lParam (address of the section 
  table) in esi, the number of sections in edi and then dress up the listview 
  control. When everything is ready, we enter a loop which will insert the info 
  about each section into the listview control. This part is very simple.</font></p>
<p><font face="Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .while edi>0 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.iSubItem,0 </font></p>
<p><font face="MS Sans Serif" size="-1">Put this string in the first column.</font></p>
<p><font face="Fixedsys"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke 
  RtlZeroMemory,addr buffer,9 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invoke lstrcpyn,addr buffer,addr 
  [esi].Name1,8 <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lea eax,buffer <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mov lvi.pszText,eax </font></p>
<p><font face="MS Sans Serif" size="-1">We will display the name of the section 
  but we must convert it to an ASCIIZ string first.</font></p>
<p><font face="Fixedsys"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; invoke 
  SendDlgItemMessage,hDlg,IDC_SECTIONLIST,LVM_INSERTITEM,0,addr lvi </font></p>
<p><font face="MS Sans Serif" size="-1">Then we display it in the first column.<br>
  We continue with this scheme until the last value we want to display for this 
  section is displayed. Then we must move to the next structure.</font> </p>
<p><font face="Fixedsys">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dec 
  edi <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; add esi, sizeof IMAGE_SECTION_HEADER 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .endw </font></p>
<p><font face="MS Sans Serif" size="-1">We decrement the value in edi for each 
  section processed. And we add the size of <font color="#CCFFCC"><b>IMAGE_SECTION_HEADER</b></font> 
  to esi so it contains the address of the next <font color="#CCFFCC"><b>IMAGE_SECTION_HEADER</b></font> 
  structure.</font></p>
<p><font face="MS Sans Serif" size="-1">The steps in walking the section table 
  are:</font></p>
<ol>
  <li><font face="MS Sans Serif" size="-1">Verify that the file is a valid PE</font></li>
  <li><font face="MS Sans Serif" size="-1">Go to the beginning of the PE header</font></li>
  <li><font face="MS Sans Serif" size="-1">Obtain the number of sections from 
    <font color="#FFFFCC"> <b>NumberOfSections</b></font> field in the file header.</font></li>
  <li><font face="MS Sans Serif" size="-1">Go to the section table either by adding 
    <font color="#FFFFCC"> <b>ImageBase</b></font> to <font color="#FFFFCC"><b>SizeOfHeaders</b></font> 
    or by adding the address of the PE header to the size of the PE header. (The 
    section table immediately follows the PE header). If you don't use file mapping, 
    you need to move the file pointer to the section table using <font color="#FFFFCC"><b>SetFilePointer</b></font>. 
    The file offset of the section table is in <font color="#FFFFCC"><b>SizeOfHeaders</b></font>.(<font color="#FFFFCC"><b>SizeOfHeaders</b></font> 
    is a member of <font color="#CCFFCC"><b>IMAGE_OPTIONAL_HEADER</b></font>)</font></li>
  <li><font face="MS Sans Serif" size="-1">Process each <font color="#CCFFCC"><b>IMAGE_SECTION_HEADER</b></font> 
    structure.</font></li>
</ol>
<hr>
<p align="center"><b><font face="MS Sans Serif" size="-1">[<a href="http://win32asm.cjb.net">Iczelion's 
  Win32 Assembly Homepage</a>]</font></b></p>
<p>&nbsp;</p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -