/
exe_mz_pe.go
300 lines (268 loc) · 12 KB
/
exe_mz_pe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package exe
// PE/COFF exe (Win32, Win64)
// http://wiki.osdev.org/PE
// https://en.wikibooks.org/wiki/X86_Disassembly/Windows_Executable_Files
// https://www.microsoft.com/whdc/system/platform/firmware/PECOFF.mspx
// STATUS: 50%
import (
"fmt"
"log"
"os"
"github.com/martinlindhe/formats/parse"
)
var (
peHeaderLen = int64(24)
peSectionHeaderLen = int64(40)
peOptHeaderMainLen = int64(96)
peTypes = map[uint16]string{
0x107: "ROM",
0x10b: "PE32",
0x20b: "PE32+ (64-bit)",
}
peMachines = map[uint16]string{
0x14c: "intel 386",
0x162: "MIPS R3000",
0x168: "MIPS R10000",
0x169: "MIPS little endian WCI v2",
0x183: "old Alpha AXP",
0x184: "Alpha AXP",
0x1a2: "Hitachi SH3",
0x1a3: "Hitachi SH3 DSP",
0x1a6: "Hitachi SH4",
0x1a8: "Hitachi SH5",
0x1c0: "ARM little endian",
0x1c2: "ARM Thumb",
0x1d3: "Matsushita AM33",
0x1f0: "PowerPC little endian",
0x1f1: "PowerPC with floating point support",
0x200: "Intel IA64",
0x266: "MIPS16",
0x268: "Motorola 68000 series",
0x284: "Alpha AXP 64-bit",
0x366: "MIPS with FPU",
0x466: "MIPS16 with FPU",
0xebc: "EFI Byte Code",
0x8664: "AMD64",
0x9041: "Mitsubishi M32R little endian",
0xc0ee: "clr pure MSIL",
}
peSubsystems = map[uint16]string{
1: "Native",
2: "Windows GUI",
3: "Windows console",
5: "OS/2 console",
7: "POSIX",
8: "Native Windows",
9: "Windows CE",
10: "EFI application",
11: "EFI boot service device",
12: "EFI runtime driver",
13: "EFI rom",
14: "XBox",
16: "Boot application",
}
peRvaChunks = map[int64]string{
0: "exports",
1: "imports",
2: "resources",
5: "base reolcations",
9: "thread local storage",
12: "import address table",
14: "CLR header",
}
)
// parses 32/64-bit Windows executables
func parseMzPeHeader(file *os.File, pos int64) ([]parse.Layout, error) {
optHeaderSize, _ := parse.ReadUint16le(file, pos+20)
numberOfSections, _ := parse.ReadUint16le(file, pos+6)
machineName, _ := parse.ReadToMap(file, parse.Uint16le, pos+4, peMachines)
res := []parse.Layout{{
Offset: pos,
Length: peHeaderLen,
Info: "external header = PE",
Type: parse.Group,
Childs: []parse.Layout{
{Offset: pos, Length: 4, Info: "identifier", Type: parse.ASCIIZ},
{Offset: pos + 4, Length: 2, Info: "machine = " + machineName, Type: parse.Uint16le},
{Offset: pos + 6, Length: 2, Info: "number of sections", Type: parse.Uint16le},
{Offset: pos + 8, Length: 4, Info: "timestamp", Type: parse.DOSDateTime},
{Offset: pos + 12, Length: 4, Info: "symbol table offset", Type: parse.Uint32le},
{Offset: pos + 16, Length: 4, Info: "symbol table entries", Type: parse.Uint32le},
{Offset: pos + 20, Length: 2, Info: "optional header size", Type: parse.Uint16le},
{Offset: pos + 22, Length: 2, Info: "characteristics", Type: parse.Uint16le, Masks: []parse.Mask{
{Low: 0, Length: 1, Info: "relocs stripped", Spec: "IMAGE_FILE_RELOCS_STRIPPED"},
{Low: 1, Length: 1, Info: "executable", Spec: "IMAGE_FILE_EXECUTABLE_IMAGE"},
{Low: 2, Length: 1, Info: "line nums stripped", Spec: "IMAGE_FILE_LINE_NUMS_STRIPPED"},
{Low: 3, Length: 1, Info: "local syms stripped", Spec: "IMAGE_FILE_LOCAL_SYMS_STRIPPED"},
{Low: 4, Length: 1, Info: "aggressive ws trim", Spec: "IMAGE_FILE_AGGRESSIVE_WS_TRIM"},
{Low: 5, Length: 1, Info: "large address aware", Spec: "IMAGE_FILE_LARGE_ADDRESS_ AWARE"},
{Low: 6, Length: 1, Info: "reserved lo", Spec: ""},
{Low: 7, Length: 1, Info: "reversed", Spec: "IMAGE_FILE_BYTES_REVERSED_LO"},
{Low: 8, Length: 1, Info: "32bit", Spec: "IMAGE_FILE_32BIT_MACHINE"},
{Low: 9, Length: 1, Info: "debug stripped", Spec: "IMAGE_FILE_DEBUG_STRIPPED"},
{Low: 10, Length: 1, Info: "removable run from swap", Spec: "IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP"},
{Low: 11, Length: 1, Info: "net run from swap", Spec: "IMAGE_FILE_NET_RUN_FROM_SWAP"},
{Low: 12, Length: 1, Info: "file system", Spec: "IMAGE_FILE_SYSTEM"},
{Low: 13, Length: 1, Info: "dll", Spec: "IMAGE_FILE_DLL"},
{Low: 14, Length: 1, Info: "uniprocessor", Spec: "IMAGE_FILE_UP_SYSTEM_ONLY"},
{Low: 15, Length: 1, Info: "reserved hi", Spec: "IMAGE_FILE_BYTES_REVERSED_HI"},
}},
}}}
pos += peHeaderLen
if optHeaderSize > 0 {
optHeader := parsePEOptHeader(file, pos, optHeaderSize)
res = append(res, optHeader)
pos += optHeader.Length
}
res = append(res, parsePESections(file, pos, numberOfSections)...)
return res, nil
}
func parsePESections(file *os.File, pos int64, numberOfSections uint16) []parse.Layout {
res := []parse.Layout{}
sectionHeader := parse.Layout{
Offset: pos,
Length: int64(numberOfSections) * peSectionHeaderLen,
Info: "section header",
Type: parse.Group}
for i := 0; i < int(numberOfSections); i++ {
sectionName, _, _ := parse.ReadZeroTerminatedASCIIUntil(file, pos, 256)
rawDataSize, _ := parse.ReadUint32le(file, pos+16)
rawDataOffset, _ := parse.ReadUint32le(file, pos+20)
if rawDataSize > 0 {
res = append(res, parse.Layout{
Offset: int64(rawDataOffset),
Length: int64(rawDataSize),
Info: "section " + sectionName,
Type: parse.Group,
Childs: []parse.Layout{
{Offset: int64(rawDataOffset), Length: int64(rawDataSize), Info: "data", Type: parse.Bytes},
}})
}
chunk := []parse.Layout{
{Offset: pos, Length: 8, Info: "name", Type: parse.ASCIIZ},
{Offset: pos + 8, Length: 4, Info: "virtual size", Type: parse.Uint32le},
{Offset: pos + 12, Length: 4, Info: "virtual address", Type: parse.Uint32le},
{Offset: pos + 16, Length: 4, Info: "raw data size", Type: parse.Uint32le},
{Offset: pos + 20, Length: 4, Info: "raw data offset", Type: parse.Uint32le},
{Offset: pos + 24, Length: 4, Info: "reallocations offset", Type: parse.Uint32le},
{Offset: pos + 28, Length: 4, Info: "linenumbers offset", Type: parse.Uint32le},
{Offset: pos + 32, Length: 2, Info: "reallocations count", Type: parse.Uint16le},
{Offset: pos + 34, Length: 2, Info: "linenumbers count", Type: parse.Uint16le},
{Offset: pos + 36, Length: 4, Info: "flags", Type: parse.Uint32le, Masks: []parse.Mask{
{Low: 0, Length: 3, Info: "reserved"},
{Low: 3, Length: 1, Info: "no padding", Spec: "IMAGE_SCN_TYPE_NO_PAD"},
{Low: 4, Length: 1, Info: "reserved"},
{Low: 5, Length: 1, Info: "code", Spec: "IMAGE_SCN_CNT_CODE"},
{Low: 6, Length: 1, Info: "initialized data", Spec: "IMAGE_SCN_CNT_INITIALIZED_DATA"},
{Low: 7, Length: 1, Info: "uninitialized data", Spec: "IMAGE_SCN_CNT_UNINITIALIZED_DATA"},
{Low: 8, Length: 1, Info: "reserved", Spec: "IMAGE_SCN_LNK_OTHER"},
{Low: 9, Length: 1, Info: "info", Spec: "IMAGE_SCN_LNK_INFO"},
{Low: 10, Length: 1, Info: "reserved", Spec: ""},
{Low: 11, Length: 1, Info: "remove", Spec: "IMAGE_SCN_LNK_REMOVE"},
{Low: 12, Length: 1, Info: "COMDAT data", Spec: "IMAGE_SCN_LNK_COMDAT"},
{Low: 13, Length: 4, Info: "reserved"},
{Low: 17, Length: 1, Info: "data referenced through the global pointer", Spec: "IMAGE_SCN_GPREL"},
{Low: 18, Length: 2, Info: "reserved"},
{Low: 20, Length: 4, Info: "align x-bytes", Spec: "IMAGE_SCN_ALIGN_xBYTES"},
{Low: 24, Length: 1, Info: "extended relocations", Spec: "IMAGE_SCN_LNK_NRELOC_OVFL"},
{Low: 25, Length: 1, Info: "discardable", Spec: "IMAGE_SCN_MEM_DISCARDABLE"},
{Low: 26, Length: 1, Info: "cacheable", Spec: "IMAGE_SCN_MEM_NOT_CACHED"},
{Low: 27, Length: 1, Info: "pageable", Spec: "IMAGE_SCN_MEM_NOT_PAGED"},
{Low: 28, Length: 1, Info: "shared", Spec: "IMAGE_SCN_MEM_SHARED"},
{Low: 29, Length: 1, Info: "executable", Spec: "IMAGE_SCN_MEM_EXECUTE"},
{Low: 30, Length: 1, Info: "readable", Spec: "IMAGE_SCN_MEM_READ"},
{Low: 31, Length: 1, Info: "writeable", Spec: "IMAGE_SCN_MEM_WRITE"},
}}}
sectionHeader.Childs = append(sectionHeader.Childs, chunk...)
pos += peSectionHeaderLen
}
res = append(res, sectionHeader)
return res
}
func parsePEOptHeader(file *os.File, pos int64, size uint16) parse.Layout {
typeName, _ := parse.ReadToMap(file, parse.Uint16le, pos, peTypes)
subsystemName, _ := parse.ReadToMap(file, parse.Uint16le, pos+68, peSubsystems)
numberOfRva, _ := parse.ReadUint32le(file, pos+92)
optHeader := parse.Layout{
Offset: pos,
Info: "PE optional header",
Type: parse.Group,
Childs: []parse.Layout{
{Offset: pos, Length: 2, Info: "type = " + typeName, Type: parse.Uint16le},
{Offset: pos + 2, Length: 2, Info: "linker version", Type: parse.MajorMinor16le},
{Offset: pos + 4, Length: 4, Info: "size of code", Type: parse.Uint32le},
{Offset: pos + 8, Length: 4, Info: "size of initialized data", Type: parse.Uint32le},
{Offset: pos + 12, Length: 4, Info: "size of uninitialized data", Type: parse.Uint32le},
{Offset: pos + 16, Length: 4, Info: "address of entry point", Type: parse.Uint32le},
{Offset: pos + 20, Length: 4, Info: "base of code", Type: parse.Uint32le},
{Offset: pos + 24, Length: 4, Info: "base of data", Type: parse.Uint32le},
{Offset: pos + 28, Length: 4, Info: "image base", Type: parse.Uint32le},
{Offset: pos + 32, Length: 4, Info: "section alignment", Type: parse.Uint32le},
{Offset: pos + 36, Length: 4, Info: "file alignment", Type: parse.Uint32le},
{Offset: pos + 40, Length: 4, Info: "os version", Type: parse.MajorMinor32le},
{Offset: pos + 44, Length: 4, Info: "image version", Type: parse.MajorMinor32le},
{Offset: pos + 48, Length: 4, Info: "subsystem version", Type: parse.MajorMinor32le},
{Offset: pos + 52, Length: 4, Info: "win32 version", Type: parse.MajorMinor32le},
{Offset: pos + 56, Length: 4, Info: "size of image", Type: parse.Uint32le},
{Offset: pos + 60, Length: 4, Info: "size of headers", Type: parse.Uint32le},
{Offset: pos + 64, Length: 4, Info: "checksum", Type: parse.Uint32le},
{Offset: pos + 68, Length: 2, Info: "subsystem = " + subsystemName, Type: parse.Uint16le},
{Offset: pos + 70, Length: 2, Info: "dll characteristics", Type: parse.Uint16le},
{Offset: pos + 72, Length: 4, Info: "size of stack reserve", Type: parse.Uint32le},
{Offset: pos + 76, Length: 4, Info: "size of stack commit", Type: parse.Uint32le},
{Offset: pos + 80, Length: 4, Info: "size of heap reserve", Type: parse.Uint32le},
{Offset: pos + 84, Length: 4, Info: "size of heap commit", Type: parse.Uint32le},
{Offset: pos + 88, Length: 4, Info: "loader flags", Type: parse.Uint32le},
{Offset: pos + 92, Length: 4, Info: "number of rva and sizes", Type: parse.Uint32le},
}}
ddLen := int64(8)
optHeader.Length = peOptHeaderMainLen + (int64(numberOfRva) * ddLen)
if optHeader.Length != int64(size) {
if optHeader.Length > int64(size) {
log.Fatalf("optHeader is too big, got 0x%04x, expected at most 0x%04x", optHeader.Length, size)
}
padLength := int64(size) - optHeader.Length
// mark remaining bytes as unknown, TODO figure out what it is
optHeader.Childs = append(optHeader.Childs, parse.Layout{
Offset: pos + 96,
Length: padLength,
Info: "unknown",
Type: parse.Bytes,
})
optHeader.Length += padLength
}
pos += peOptHeaderMainLen
for i := int64(0); i < int64(numberOfRva); i++ {
info := "data directory " + fmt.Sprintf("%d", i)
if val, ok := peRvaChunks[i]; ok {
info = val
}
optHeader.Childs = append(optHeader.Childs, []parse.Layout{
{Offset: pos, Length: 4, Info: info + " RVA", Type: parse.Uint32le},
{Offset: pos + 4, Length: 4, Info: info + " size", Type: parse.Uint32le},
}...)
pos += 8
}
return optHeader
}
/* XXX
public long FileOffsetFromVirtualAddress(long va)
{
if (this.sections.Count == 0) {
Log("no sections - ERROR");
return va;
//throw new Exception("no sections");
}
// Log("translate VA " + va.ToString("x8")+ " to file offset");
foreach (var section in this.sections) {
if (va >= section.virtualOffset && (va < section.virtualOffset + section.length)) {
long res = (va - section.virtualOffset) + section.realOffset;
// Log("translated to " + res.ToString("x8"));
return res;
}
}
Log("FATAL ERROR not found for va " + va.ToString("x8"));
return va;
//throw new Exception("not found for va " + va.ToString("x8"));
}
*/