【HexWS】自己写一个PE文件 _蹭课学习成果

环境:Hex Workshop

先解决DOS头。

在弹出的对话框中选择十六进制,字节数量填40。点确定,40h就是DOS头的大小。


光标插到第一字节前,输入4D5A,对应ASCII码MZ,这是DOS可执行文件标志。如图,最后四个字节用来指示PE文件头开始的地址,我们的PE头将从00000040位置开始,所以把最后四个字节改为40 00 00 00(低低高高对应关系)。

DOS头完成了,是不是很简单啊!

―――――――――――――――――――――――――――――――――――

下面就是PE头,不过它比较复杂,在讲PE头之前我们先看几个重要的概念:文件偏移地址和相对虚似地址,上图黄色框中所示的就是文件偏移地址。即某个数据位置相对于文件头的偏移量。

文件执行过程中会把文件映射到内存中,在内存中文件头被映射到某一位置。其它字节的位置相对于头部映射的位置被称为相对虚似地址(RVA)。

文件从磁盘到内存映射过程中发生了一些变化。也就是怎样“对齐”的概念。

比如,一本书如果用32K的纸来印刷的话,假设一页32K的纸能容纳100个字,假设第一章有113个字,那么需要一页多一点的纸张(但下一章必须从新的一页开始),多出的地方就是空白(相当于文件中用0填充),所以第一章会占两页32K的纸。

假如现在用8K的纸来印刷的话,假设一页8K的纸能容纳400个字,那么第一章只用一页8K纸可以了。(因为第二章将从新一页开始),没字的部分就是空白(相当于文件中用0填充)。

在文件中“1页纸”一般200字节,在内存中一般1000字节。

 

―――――――――――――――――――――――――――――――――――

让我们继续吧!
PE头我们分成二块来讲。
第一部分:在空白的地方如前面的方法,插入18H个字节的0。需要修改的字节如图所示:


①处4个字节PE文件头的标志对应ASCII:PE\0\0;②处2个字节说明运行平台,014c表示intel处理器;③区块个数,相当于一本书多少章;④PE头第二部分的大小(可选头文件大小)⑤文件属性,普通exe文件一般是010f。
第二部分:插入E0h个字节。需要修改的字节如图所示:


①标志字,普通可执行映像一般是010B。②程序入口RVA,对应.text块的RVA,一般是1000。③文件装入内存的起始地址,一般是00400000。④内存对齐,1000,很重要。⑤磁盘对齐,是200,很重要。⑥要求最低子系统版本的主版本号,一设为0004对应win32子系统。⑦文件装入内存后的总大小,内存对齐是1000。DOS头+PE头+块表占1000字节;.text、.rdata、.data块分别占1000字节。所以这儿填4000。⑧DOS头+PE头+块表在磁盘中所占大小,磁盘对齐是200,这儿填00 02 00 00⑨这个可执行文件所期待的子系统,02为图形接口子系统。⑩ 对应最下面红框中数据的个数(8字节一组),PE文件基本都是10h。
―――――――――――――――――――――――――――――――
下面是块表
块表是对块的位置、大小以及一些属性的说明。
每个块表一共占28h个字节,一共3个块,新插入78h.。


我们以.text块表为例,①处共8个字节是块名,以“.”开头,后面填充0。 ②区块的实际大小,这儿是26h(请改成27),后面我们会知道他是怎么得出的。 ③.text块的RAV是1000,对应前面讲的程序入口地址。(后面的.rdata区块是2000,.data区块是3000) ④ 在磁盘文件上大小,200h(后面的.rdata和.data区块都是200) ⑤在磁盘文件上偏移,200(后面的.rdata区块是400,.data区块是600) ⑥块属性,具体请有兴趣的参考相关资料。

 

前言与目录结束了,还有最后一件事,DOS头+PE头+块表在磁盘中所占大小是以200字节对齐的,而现在还没到200字节,所以后面我们还要填50h字节的0。

 

下面是三个区块,首先是200字节的.text区。填充200字节的0

再填充200字节的0,作为.rdata。

再填充200字节的0,作为.data。

 

.data块主要用来存放数据,我们一共有二个字符串要存储。就是MessageBox的标题和上面显示的文字。


记住它们首地址偏移分别是00403000和00403017一会有用。(这些都是RVA,前面讲PE头时,我们设文件装入内存的起始地址是00400000,.data块的RVA是3000,下面都会算了吧^-^)


因为我们要显示一个消息框,所以要从外部导入user32.dll库中的MessageBox函数,程序退出要导入kernel32.dll库中的ExitProcess函数。怎么导入呢,这就和输入表有关啦。前面讲PE头第二部分有个标A的地方没讲,还有印像吗?那8个字节的数据就和输入表有关,前4个字节是输入表地址,后面是大小。
每导入一个DLL,我们就需要一个导入表。这个程序我们从2个DLL中导入函数,所以要2个输入表。那第一个输入表是不是从.rdata块首地址开始呢?不是这样的,这里有个规律,我们要导入多少个函数,那么就要空出8乘以导入函数的个数,这儿就空出16个字节,PE头中我们定义输入表的RAV是00 00 20 10对应。
如图每个输入表占14h个字节,最后以一个空表作为结束标志。


我们以输入表1为例来分析下输入表,输入表前4个字节所表示的地址指向一组指针,这些指针指向我们引用的所有引用的函数的名称。这些指针位于输入表空表之后,偏移为0000044c的位置,这个是磁盘偏移,转化为RAV为0000204c。从044c位置空出4个字节写指针(上面所知,该程序一个DLL对应一个函数),空出4个字节置0,作为结束的标志。输入表2和输入表1一样,也会占用8个字节。从044c开始再空出10h的字节,这就到了045c了。从这儿开始输入我们要调用的函数名和对应该的DLL名称,函数名前空出两个字节。结果如图所示!!字符串写好后,就可以把044c处的指针填上了!
输入表1的②四个字节对应该输入表DLL名称的RAV,如图,在磁盘上偏移是046A 这里是user32.dll,将这儿046A转化为RAV为206A,所以这儿填6A 20 00 00。③处三个字节同①一样,指向一组指针,这些指针指向我们引用的所有引用的函数的名称。这些指针就放在我们前面刚空出的10h个字节。


―――――――――――――――――――――――――――――――
现在解决.text区块了
代码如下:
压参数:
Push 0
Push 00403000
Push 00403013
Push 0
Call dword ptr ds:[402008]
Push 0
Call dword ptr ds:[402000]
jmp dword ptr [402008]
jmp dword ptr [402000]




附上HEX程序码

00000000 4D 5A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 MZ..............
00000010 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000030 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 00 ............@...
00000040 50 45 00 00 4C 01 03 00 00 00 00 00 00 00 00 00 PE..L...........
00000050 00 00 00 00 E0 00 0F 01 0B 01 00 00 00 00 00 00 ................
00000060 00 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 ................
00000070 00 00 00 00 00 00 40 00 00 10 00 00 00 02 00 00 ......@.........
00000080 00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ................
00000090 00 40 00 00 00 02 00 00 00 00 00 00 02 00 00 00 .@..............
000000A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000B0 00 00 00 00 10 00 00 00 00 00 00 00 00 00 00 00 ................
000000C0 10 20 00 00 3C 00 00 00 00 00 00 00 00 00 00 00 . ..<...........
000000D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000000F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000100 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000110 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000120 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000130 00 00 00 00 00 00 00 00 2E 74 65 78 74 00 00 00 .........text...
00000140 26 00 00 00 00 10 00 00 00 02 00 00 00 02 00 00 &...............
00000150 00 00 00 00 00 00 00 00 00 00 00 00 20 00 00 60 ............ ..`
00000160 2E 72 64 61 74 61 00 00 92 00 00 00 00 20 00 00 .rdata....... ..
00000170 00 02 00 00 00 04 00 00 00 00 00 00 00 00 00 00 ................
00000180 00 00 00 00 40 00 00 40 2E 64 61 74 61 00 00 00 ....@..@.data...
00000190 3E 00 00 00 00 30 00 00 00 02 00 00 00 06 00 00 >....0..........
000001A0 00 00 00 00 00 00 00 00 00 00 00 00 40 00 00 C0 ............@...
000001B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000001F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000200 6A 00 68 00 30 40 00 68 11 30 40 00 6A 00 FF 15 j.h.0@.h.0@.j...
00000210 08 20 40 00 6A 00 FF 15 00 20 40 00 FF 25 08 20 . @.j.... @..%. 
00000220 40 00 FF 25 00 20 40 00 00 00 00 00 00 00 00 00 @..%. @.........
00000230 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000240 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000250 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000260 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000270 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000280 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000290 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000002F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000300 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000310 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000320 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000330 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000340 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000350 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000370 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000380 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000390 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000003F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000400 5C 20 00 00 00 00 00 00 78 20 00 00 00 00 00 00 \ ......x ......
00000410 4C 20 00 00 00 00 00 00 00 00 00 00 6A 20 00 00 L ..........j ..
00000420 00 20 00 00 54 20 00 00 00 00 00 00 00 00 00 00 . ..T ..........
00000430 86 20 00 00 08 20 00 00 00 00 00 00 00 00 00 00 . ... ..........
00000440 00 00 00 00 00 00 00 00 00 00 00 00 5C 20 00 00 ............\ ..
00000450 00 00 00 00 78 20 00 00 00 00 00 00 80 00 45 78 ....x ........Ex
00000460 69 74 50 72 6F 63 65 73 73 00 6B 65 72 6E 65 6C itProcess.kernel
00000470 33 32 2E 64 6C 6C 00 00 9D 01 4D 65 73 73 61 67 32.dll....Messag
00000480 65 42 6F 78 41 00 75 73 65 72 33 32 2E 64 6C 6C eBoxA.user32.dll
00000490 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000004F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000500 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000510 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000520 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000530 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000540 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000550 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000560 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000570 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000580 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000590 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000005F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000600 49 63 7A 65 6C 69 6F 6E 20 54 75 74 6F 72 69 61 Iczelion Tutoria
00000610 6C 20 4E 6F 2E 32 00 57 69 6E 33 32 20 41 73 73 l No.2.Win32 Ass
00000620 65 6D 62 6C 79 20 69 73 20 47 72 65 61 74 21 00 embly is Great!.
00000630 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000640 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000650 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000660 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000670 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000680 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000690 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000006F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000700 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000710 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000720 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000730 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000740 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000750 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000760 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000770 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000780 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00000790 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007A0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007B0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007C0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007E0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
000007F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................



### 回答1: Hex Workshop是一款强大的十六进制编辑器和数据分析工具。它主要用于处理二进制文件、磁盘映像和内存快照等底层数据,适用于软件开发、逆向工程、数字取证等领域。 Hex Workshop具有强大的特性和功能,可以让用户轻松地浏览、编辑和分析十六进制数据。它提供了直观的用户界面,用户可以通过打开文件或拖放文件来开始工作。编辑器窗口中显示的是文件的十六进制视图和ASCII视图,用户可以在两者之间进行切换,以便更方便地编辑和查看数据。 同时,Hex Workshop还提供了许多高级功能。例如,它支持对数据进行搜索和替换操作,用户可以指定搜索条件并进行批量替换。此外,它还具有数据插入、删除和复制等基本编辑功能,用户可以对数据进行精确的操作。另外,Hex Workshop还支持计算校验和、生成Checksums、数据分析等功能,为用户提供了更多数据处理的选项。 除了编辑功能外,Hex Workshop还具有数据分析工具,如结构视图和扩展核视图,可以帮助用户识别数据结构和进行数据解析。用户可以定义自定义结构,并使用颜色和注释来标记数据,方便数据分析和理解。 总之,Hex Workshop是一款功能丰富的十六进制编辑器和数据分析工具,它提供了强大的编辑、搜索、替换和分析功能,适用于各种数据处理任务。无论是开发人员、逆向工程师还是数字取证专家,都可以通过Hex Workshop轻松而高效地处理十六进制数据。 ### 回答2: Hex Workshop是一款强大的十六进制编辑器软件,用于查看和修改二进制文件。这里将为你简要介绍Hex Workshop的基本功能和使用方法。 首先,打开Hex Workshop后,你将看到一个分为两个部分的界面。左边是十六进制视图,以字节为单位显示文件内容;右边是字符视图,将每个字节翻译成可读格式。你可以通过点击或拖动来选择要修改的字节。 Hex Workshop有强大的搜索和替换功能,可以帮助你快速定位到特定的十六进制值或字符。你可以使用CTRL+F来打开搜索对话框,并设置搜索条件。另外,你还可以使用CTRL+H来打开替换对话框,对文件中的特定值进行替换。 Hex Workshop还提供了许多实用的工具和功能。例如,你可以使用"结构注册表"来定义复杂的数据结构,以便更方便地分析和修改文件内容。还可以使用"差异比较"来比较不同版本的文件,并找出其中的差异。另外,Hex Workshop还支持Python脚本编,以便扩展软件功能。 除了基本的编辑功能外,Hex Workshop还能支持多种文件类型,例如exe文件、图像文件、音频文件等。它还具有强大的数据解析能力,可以自动分析和解释文件中的特定数据格式。 总结一下,Hex Workshop是一款非常实用的十六进制编辑器软件,它提供了丰富的功能和工具,能够帮助你查看、编辑和分析二进制文件。无论是进行编程、系统调试还是逆向工程,Hex Workshop都是一款不可多得的工具。希望这个简要教程能帮助你更好地使用Hex Workshop。 ### 回答3: Hex Workshop是一款强大的二进制文件编辑器和数据分析工具。它被广泛应用于软件开发、逆向工程、数字取证等领域。以下是Hex Workshop的教程: 首先,在Hex Workshop的界面中,我们可以看到主要分为“编辑窗口”和“结构窗口”两个主要部分。编辑窗口是用于查看和编辑二进制文件内容的地方,而结构窗口则用于解析文件的内部结构。 接下来,我们通过点击工具栏中的"打开"按钮来加载要编辑的二进制文件。一旦文件被加载,我们就可以在编辑窗口中查看其十六进制和ASCII码表示。 在编辑窗口中,我们可以使用许多功能来编辑文件。例如,可以使用编辑菜单中的剪切、复制和粘贴命令来编辑选定的数据。同时,也可以使用查找和替换功能来在文件中进行关键字的搜索和替换。 另一个重要的功能是Hex Workshop的结构窗口。通过右键单击结构窗口,我们可以选择加载现有的文件结构,也可以通过定义新的结构来解析文件。这样,可以将文件的内部结构以一种易于理解的方式呈现出来,从而更好地分析和编辑文件的内容。 此外,Hex Workshop还提供了非常有用的解析器插件,例如:网络数据包解析器、图像解析器等,这些插件可以帮助我们更好地分析和理解不同类型文件的内部结构。 最后,当我们完成编辑和分析工作后,即可将所做的更改保存到源文件中,或者生成一个新的二进制文件。此外,Hex Workshop还支持比较工具,可以帮助我们比较两个文件的差异。 总而言之,Hex Workshop是一款功能强大的二进制文件编辑器和数据分析工具,具有丰富的功能和易用的界面。通过学习和使用Hex Workshop,我们可以更好地理解和编辑二进制文件,提高工作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值