?? lion-petut-c03.htm
字號:
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=gb_2312-80">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Iczelion的PE教程3: File Header (文件頭)</title>
</head>
<body bgcolor="#003366" text="#FFFFFF" link="#FFFFCC"
vlink="#FFCCCC" alink="#CCFFCC">
<h1 align="center"><font color="#FFFFCC"
face="Arial, Helvetica, sans-serif">PE</font><font
color="#FFFFCC">教程</font><font color="#FFFFCC"
face="Arial, Helvetica, sans-serif">3: File Header </font><font
color="#FFFFCC">(文件頭)</font></h1>
<p><font size="2">本課我們將要研究 </font><font size="2"
face="MS Sans Serif">PE header </font><font size="2">的 </font><font
size="2" face="MS Sans Serif">file header</font><font size="2">(文件頭)部分。</font></p>
<p><font size="2">至此,我們已經學到了哪些東東,先簡要回顧一下</font><font
size="2" face="MS Sans Serif">:</font></p>
<ul>
<li><font size="2" face="MS Sans Serif">DOS MZ header </font><font
size="2">又命名為</font><font color="#CCFFCC"
size="2"><b> </b></font><font color="#CCFFCC" size="2"
face="MS Sans Serif"><b>IMAGE_DOS_HEADER</b></font><font
size="2" face="MS Sans Serif">.</font><font size="2">。其中只有兩個域比較重要</font><font
size="2" face="MS Sans Serif">: </font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>e_magic</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">包含字符串</font><font
size="2" face="MS Sans Serif">"MZ"</font><font
size="2">,</font><font color="#FFFFCC" size="2"
face="MS Sans Serif"><b>e_lfanew</b></font><font size="2"
face="MS Sans Serif"> </font><font size="2">包含</font><font
size="2" face="MS Sans Serif">PE header</font><font
size="2">在文件中的偏移量。</font></li>
<li><font size="2">比較</font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b>e_magic</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">是否為</font><font
color="#CCFFCC" size="2" face="MS Sans Serif"><b>IMAGE_DOS_SIGNATURE</b></font><font
size="2">以驗證是否是有效的</font><font size="2"
face="MS Sans Serif">DOS header</font><font size="2">。比對符合則認為文件擁有一個有效的</font><font
size="2" face="MS Sans Serif">DOS header</font><font
size="2">。</font></li>
<li><font size="2">為了定位</font><font size="2"
face="MS Sans Serif">PE header</font><font size="2">,移動文件指針到</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>e_lfanew</b></font><font
size="2">所指向的偏移。</font></li>
<li><font size="2" face="MS Sans Serif">PE header</font><font
size="2">的第一個雙字包含字符串</font><font
size="2" face="MS Sans Serif">"PE\0\0"</font><font
size="2">。該雙字與</font><font color="#CCFFCC"
size="2" face="MS Sans Serif"><b>IMAGE_NT_SIGNATURE</b></font><font
size="2">比對,符合則認為</font><font size="2"
face="MS Sans Serif">PE header</font><font size="2">有效。</font></li>
</ul>
<p><font size="2">本課我們繼續探討關于 </font><font
size="2" face="MS Sans Serif">PE header </font><font size="2">的知識。
</font><font size="2" face="MS Sans Serif">PE header </font><font
size="2">的正式命名是 </font><font color="#CCFFCC" size="2"
face="MS Sans Serif"><b>IMAGE_NT_HEADERS</b></font><font size="2">。再來回憶一下這個結構。</font></p>
<blockquote>
<p><font color="#999900" size="2" face="MS Sans Serif"><b>IMAGE_NT_HEADERS
STRUCT <br>
Signature dd ? <br>
FileHeader IMAGE_FILE_HEADER <> <br>
OptionalHeader IMAGE_OPTIONAL_HEADER32 <> <br>
IMAGE_NT_HEADERS ENDS </b></font></p>
</blockquote>
<p><font color="#FFFFCC" size="2" face="MS Sans Serif"><b>Signature</b></font><font
size="2" face="MS Sans Serif"> PE</font><font size="2">標記,值為</font><font
size="2" face="MS Sans Serif">50h, 45h, 00h, 00h</font><font
size="2">(</font><font size="2" face="MS Sans Serif">PE\0\0</font><font
size="2">)。 <br>
</font><font color="#FFFFCC" size="2" face="MS Sans Serif"><b>FileHeader</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">該結構域包含了關于</font><font
size="2" face="MS Sans Serif">PE</font><font size="2">文件物理分布的一般信息。<br>
</font><font color="#FFFFCC" size="2" face="MS Sans Serif"><b>OptionalHeader
</b></font><font size="2">該結構域包含了關于</font><font
size="2" face="MS Sans Serif">PE</font><font size="2">文件邏輯分布的信息。</font></p>
<p><font size="2">最有趣的東東在</font><font
color="#FFFFCC" size="2"><b> </b></font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b>OptionalHeader</b></font><font
size="2"> 里。不過,</font><font color="#FFFFCC" size="2"
face="MS Sans Serif"><b>FileHeader</b></font><font size="2"
face="MS Sans Serif"> </font><font size="2">里的一些域也很重要。本課我們將學習</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>FileHeader</b></font><font
color="#FFFFCC" size="2"><b>,</b></font><font size="2">下一課研究</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>OptionalHeader</b></font><font
size="2">。</font></p>
<p><font color="#009933" size="2" face="MS Sans Serif"><b>IMAGE_FILE_HEADER
STRUCT <br>
Machine WORD ? <br>
NumberOfSections WORD ? <br>
TimeDateStamp dd ? <br>
PointerToSymbolTable dd ? <br>
NumberOfSymbols dd ? <br>
SizeOfOptionalHeader WORD ? <br>
Characteristics WORD ? <br>
IMAGE_FILE_HEADER ENDS </b></font></p>
<table border="1" cellpadding="2">
<tr>
<th bgcolor="#006666"><font size="2" face="MS Sans Serif"><b>Field
name</b></font></th>
<th bgcolor="#006666"><font size="2" face="MS Sans Serif">Meanings</font></th>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>Machine</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">該文件運行所要求的</font><font
size="2" face="MS Sans Serif">CPU</font><font size="2">。對于</font><font
size="2" face="MS Sans Serif">Intel</font><font size="2">平臺,該值是</font><font
color="#CCFFCC" size="2" face="MS Sans Serif"><b>IMAGE_FILE_MACHINE_I386</b></font><font
size="2" face="MS Sans Serif"> (14Ch)</font><font
size="2">。我們嘗試了</font><font size="2"
face="MS Sans Serif">LUEVELSMEYER</font><font size="2">的</font><font
size="2" face="MS Sans Serif">pe.txt</font><font size="2">聲明的</font><font
size="2" face="MS Sans Serif">14Dh</font><font size="2">和</font><font
size="2" face="MS Sans Serif">14Eh</font><font size="2">,但</font><font
size="2" face="MS Sans Serif">Windows</font><font
size="2">不能正確執行??雌饋?,除了禁止程序執行之外,本域對我們來說用處不大。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>NumberOfSections </b></font></td>
<td align="center" bgcolor="#006666"><font size="2">文件的節數目。如果我們要在文件中增加或刪除一個節,就需要修改這個值。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>TimeDateStamp</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">文件創建日期和時間。我們不感興趣。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>PointerToSymbolTable</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">用于調試。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>NumberOfSymbols</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">用于調試。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>SizeOfOptionalHeader</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">指示緊隨本結構之后的
</font><font color="#FFFFCC" size="2"
face="MS Sans Serif"><b>OptionalHeader</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">結構大小,必須為有效值。</font></td>
</tr>
<tr>
<td align="center" bgcolor="#006666"><font size="2"
face="MS Sans Serif"><b>Characteristics</b></font></td>
<td align="center" bgcolor="#006666"><font size="2">關于文件信息的標記,比如文件是</font><font
size="2" face="MS Sans Serif">exe</font><font size="2">還是</font><font
size="2" face="MS Sans Serif">dll</font><font size="2">。</font></td>
</tr>
</table>
<p><font size="2">簡言之,只有三個域對我們有一些用</font><font
size="2" face="MS Sans Serif">:</font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b> Machine</b></font><font
size="2" face="MS Sans Serif">, </font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b>NumberOfSections</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">和</font><font
size="2" face="MS Sans Serif"> </font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b>Characteristics</b></font><font
size="2">。通常不會改變</font><font size="2"
face="MS Sans Serif"> </font><font color="#FFFFCC" size="2"
face="MS Sans Serif"><b>Machine</b></font><font size="2"
face="MS Sans Serif"> </font><font size="2">和</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>Characteristics</b></font><font
size="2" face="MS Sans Serif"> </font><font size="2">的值,但如果要遍歷節表就得使用</font><font
size="2" face="MS Sans Serif"> </font><font color="#FFFFCC"
size="2" face="MS Sans Serif"><b>NumberOfSections</b></font><font
color="#FFFFCC" size="2"><b>。</b></font><font size="2"
face="MS Sans Serif"><br>
</font><font size="2">為了更好闡述 </font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>NumberOfSections
</b></font><font size="2">的用處,這里簡要介紹一下節表。</font></p>
<p><font size="2">節表是一個結構數組,每個結構包含一個節的信息。因此若有</font><font
size="2" face="MS Sans Serif">3</font><font size="2">個節,數組就有</font><font
size="2" face="MS Sans Serif">3</font><font size="2">個成員。
我們需要</font><font color="#FFFFCC" size="2"
face="MS Sans Serif"><b>NumberOfSections</b></font><font size="2">值來了解該數組中到底有幾個成員。</font><font
size="2" face="MS Sans Serif"> </font><font size="2">也許您會想檢測結構中的全</font><font
size="2" face="MS Sans Serif">0</font><font size="2">成員起到同樣效果。</font><font
size="2" face="MS Sans Serif">Windows</font><font size="2">確實采用了這種方法。為了證明這一點,可以增加</font><font
size="2" face="MS Sans Serif">NumberOfSections</font><font
size="2">的值,</font><font size="2" face="MS Sans Serif">Windows</font><font
size="2">仍然可以正常執行文件。據我們的觀察,</font><font
size="2" face="MS Sans Serif">Windows</font><font size="2">讀取</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>NumberOfSections</b></font><font
size="2">的值然后檢查節表里的每個結構,如果找到一個全</font><font
size="2" face="MS Sans Serif">0</font><font size="2">結構就結束搜索,否則一直處理完</font><font
color="#FFFFCC" size="2" face="MS Sans Serif"><b>NumberOfSections</b></font><font
size="2">指定數目的結構。</font><font size="2"
face="MS Sans Serif"> </font><font size="2">為什么我們不能忽略</font><font
size="2" face="MS Sans Serif">NumberOfSections</font><font
size="2">的值</font><font size="2" face="MS Sans Serif">? </font><font
size="2">有幾個原因。</font><font size="2"
face="MS Sans Serif">PE</font><font size="2">說明中沒有指定節表必須以全</font><font
size="2" face="MS Sans Serif">0</font><font size="2">結構結束。</font><font
size="2" face="MS Sans Serif">Thus there may be a situation where
the last array member is contiguous to the first section, without
empty space at all. Another reason has to do with bound imports.
The new-style binding puts the information immediately following
the section table's last structure array member. </font><font
size="2">因此您仍然需要</font><font size="2"
face="MS Sans Serif">NumberOfSections</font><font size="2">。</font></p>
<hr>
<p align="center"><font size="2"><b>翻譯:</b></font><font
size="2" face="MS Sans Serif"><b>iamgufeng [</b></font><a
href="http://win32asm.cjb.net/"><font size="2"
face="MS Sans Serif"><b>Iczelion's Win32 Assembly Homepage</b></font></a><font
size="2" face="MS Sans Serif"><b>]</b><strong>[</strong></font><a
href="http://asm.yeah.net"><font size="2" face="MS Sans Serif"><strong>LuoYunBin's
Win32 ASM Page</strong></font></a><font size="2"
face="MS Sans Serif"><strong>]</strong></font></p>
<p> </p>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -