Analysis of notepad process loading

因为上一篇博客PE文件格式学习笔记 ,我萌生了分析Windows7系统自带记事本程序的process loading过程的念头。正好,自己也亲自计算一下其中的细节,毕竟这都是逆向的基本功呀。当然文中可能有些许错误,恳请大家指正 😃

①Headers

the DOS Header is parsed.
the PE Header is parsed.
	(its offset is DOS Header's e_lfanew)
the Optional Header is parsed.
	(it follows the PE header)

由起始两bytes判断DOS Header ==> 由DOS Header的最后4 bytes确定PE Header位置 ==> 紧挨在PE Header后面的是Optional Header,但其大小不确定,需要由PE Header中的SizeofOptionalHeader告知

Headers

②Section table

Section table里的条目个数不是确定的,由PE Header里的第6、7个字节(从第0个字节数起)告知。由上图可知为 00 04,即有四个section。
Section table紧接在Optional Header后面,每一个条目占用40 bytes.
.text

可将四个条目分析出来:
Section table

③Mapping

要将PE文件从磁盘映射到内存里,首先得知道PE file section和内存之间的映射关系。这里可能会疑惑,为啥要映射呢?因为PE文件的对齐规则和内存的对齐规则是不一样的,因此不能说直接把file拷贝进内存就可以了,而必须通过一定的地址换算,这个换算过程就是映射。Optional Header里的FileFileAlignment、SectionAlignment参数分别对应了PE file、内存的对齐规则。

FileFileAlignment represents where sections should start on file.
SectionAlignmenet represents where sections should start in memory.

Alignment参数

映射示意图如下,这里只画了.text section的映射部分:
映射

由上图的映射可知一个关系等式:
某section的某处相对于该section起始处的offset == 加载到内存后该section的该处相对于该section起始处的offset
对了,这里顺便也po一张好图:
映射


凭借这个等式我们可以计算一些东西。
比如说,想要知道PE file的ImportTable的具体内容,步骤如下:
由Optional Header可知ImportTable(RVA):A0 48 和ImportTable占用的大小SizeofExportTable:01 2C
由上述运算法则计算ImportTable相对于PE file起始处的offset:

首先把RVA地址A0 48加上imagebase值01 00 00 00,得01 00 A0 48,由此可判断ImportTable in memory位于image的.text setion区域
由等式【01 00 A0 48 - 01 00 10 00 == ImportTable相对于PE file起始处的offset - 00 00 04 00】可求出:
	ImportTable相对于PE file起始处的offset为94 48

查看PE file的94 48处:
ImportTable

黄色部分为对应的Import条目的Name的RVA。通过类似于上述的计算可得,RVA 00 00 A2 24处对应的相对于PE file起始处的offset为【01 00 A2 24 - 01 00 10 00 + 00 00 04 00 = 96 24】,对应上图中的ADVAPI32.dll;RVA 00 00 A2 14处对应的相对于PE file起始处的offset为96 14,对应上图中的KERNEL32.dll;......

④Imports

结合③中的计算:
DataDirectories are parsed.
ImportTable are parsed.
	each descriptor specifies a DLL name
		this DLL is loaded in memory
		INT and IAT are parsed simultaneously
		for each API in INT:
			its address is written in the IAT entry

IAT

具体的IAT输入过程如下:
INT to IAT

⑤Excecution

Code is called at the EntryPoint.
the calls of the code go via the IAT to the APIs.
posted @ 2017-12-01 11:17  T_1  阅读(144)  评论(0编辑  收藏  举报