forked from omarghader/pefile-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe.go
630 lines (548 loc) · 18.1 KB
/
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package pefile
/*
TODO: figure out how to detect endianess instead of forcing LittleEndian
*/
import (
"bytes"
"crypto/md5"
"encoding/binary"
"encoding/hex"
"errors"
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/Codehardt/go-pefile/lib"
mmap "github.com/Codehardt/mmap-go"
)
/* The representation of the PEFile with some helpful abstractions */
type PEFile struct {
Filename string
DosHeader *lib.DosHeader
NTHeader *lib.NTHeader
FileHeader *lib.FileHeader
OptionalHeader *lib.OptionalHeader
OptionalHeader64 *lib.OptionalHeader64
Sections []*lib.SectionHeader
ImportDescriptors []*lib.ImportDescriptor
ExportDirectory *lib.ExportDirectory
// Private Fields
data mmap.MMap
f *os.File
dataLen uint32
headerEnd uint32
}
func NewPEFile(filename string) (pe *PEFile, err error) {
pe = new(PEFile)
pe.Filename = filename
var offset = uint32(0)
pe.f, err = os.Open(pe.Filename)
if err != nil {
return nil, err
}
pe.data, err = mmap.Map(pe.f, mmap.RDONLY, 0)
if err != nil {
pe.f.Close()
return nil, err
}
pe.dataLen = uint32(len(pe.data))
pe.DosHeader = lib.NewDosHeader(uint32(0x0))
if err = pe.parseHeader(&pe.DosHeader.Data, offset, pe.DosHeader.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
if pe.DosHeader.Data.E_magic == IMAGE_DOSZM_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Probably a ZM Executable (not a PE file).")
}
if pe.DosHeader.Data.E_magic != IMAGE_DOS_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("DOS Header magic not found.")
}
if pe.DosHeader.Data.E_lfanew > pe.dataLen {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid e_lfanew value, probably not a PE file")
}
offset = pe.DosHeader.Data.E_lfanew
pe.NTHeader = lib.NewNTHeader(offset)
if err = pe.parseHeader(&pe.NTHeader.Data, offset, pe.NTHeader.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
if (0xFFFF & pe.NTHeader.Data.Signature) == IMAGE_NE_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid NT Headers signature. Probably a NE file")
} else if (0xFFFF & pe.NTHeader.Data.Signature) == IMAGE_LE_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid NT Headers signature. Probably a LE file")
} else if (0xFFFF & pe.NTHeader.Data.Signature) == IMAGE_LX_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid NT Headers signature. Probably a LX file")
} else if (0xFFFF & pe.NTHeader.Data.Signature) == IMAGE_TE_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid NT Headers signature. Probably a TE file")
} else if pe.NTHeader.Data.Signature != IMAGE_NT_SIGNATURE {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("Invalid NT Headers signature.")
}
offset += pe.NTHeader.Size
pe.FileHeader = lib.NewFileHeader(offset)
if err = pe.parseHeader(&pe.FileHeader.Data, offset, pe.FileHeader.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
lib.SetFlags(pe.FileHeader.Flags, lib.ImageCharacteristics, uint32(pe.FileHeader.Data.Characteristics))
offset += pe.FileHeader.Size
//log.Println("Size of OptionalHeader")
pe.OptionalHeader = lib.NewOptionalHeader(offset)
if err = pe.parseHeader(&pe.OptionalHeader.Data, offset, pe.OptionalHeader.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
lib.SetFlags(pe.OptionalHeader.Flags, lib.DllCharacteristics, uint32(pe.OptionalHeader.Data.DllCharacteristics))
if pe.OptionalHeader.Data.Magic == OPTIONAL_HEADER_MAGIC_PE_PLUS {
pe.OptionalHeader64 = lib.NewOptionalHeader64(offset)
if err = pe.parseHeader(&pe.OptionalHeader64.Data, offset, pe.OptionalHeader64.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
if pe.OptionalHeader64.Data.Magic != OPTIONAL_HEADER_MAGIC_PE_PLUS {
pe.data.Unmap()
pe.f.Close()
return nil, errors.New("No Optional Header found, invalid PE32 or PE32+ file")
}
lib.SetFlags(pe.OptionalHeader64.Flags, lib.DllCharacteristics, uint32(pe.OptionalHeader64.Data.DllCharacteristics))
}
// Windows 8 specific check
//
if pe.OptionalHeader.Data.AddressOfEntryPoint < pe.OptionalHeader.Data.SizeOfHeaders {
log.Println("Warning: SizeOfHeaders is smaller than AddressOfEntryPoint: this file cannot run under Windows 8")
}
// Section data
//MAX_ASSUMED_VALID_NUMBER_OF_RVA_AND_SIZES := 0x100
var numRvaAndSizes uint32
msg := "Suspicious NumberOfRvaAndSizes in the Optional Header."
msg += "Normal values are never larger than 0x10, the value is: 0x%x\n"
var dataDir map[string]*lib.DataDirectory
sectionOffset := offset + uint32(pe.FileHeader.Data.SizeOfOptionalHeader)
if pe.OptionalHeader64 != nil {
if pe.OptionalHeader64.Data.NumberOfRvaAndSizes > 0x10 {
//log.Printf(msg, pe.OptionalHeader64.Data.NumberOfRvaAndSizes)
}
numRvaAndSizes = pe.OptionalHeader64.Data.NumberOfRvaAndSizes
offset += pe.OptionalHeader64.Size
dataDir = pe.OptionalHeader64.DataDirs
} else {
if pe.OptionalHeader.Data.NumberOfRvaAndSizes > 0x10 {
//log.Printf(msg, pe.OptionalHeader.Data.NumberOfRvaAndSizes)
}
numRvaAndSizes = pe.OptionalHeader.Data.NumberOfRvaAndSizes
offset += pe.OptionalHeader.Size
dataDir = pe.OptionalHeader.DataDirs
}
for i := uint32(0); i < 0x7fffffff&numRvaAndSizes; i++ {
if pe.dataLen-offset == 0 {
break
}
dirEntry := lib.NewDataDirectory(offset)
if err = pe.parseHeader(&dirEntry.Data, offset, dirEntry.Size); err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
offset += dirEntry.Size
name, ok := lib.DirectoryEntryTypes[i]
dirEntry.Name = name
if !ok {
break
}
dataDir[dirEntry.Name] = dirEntry
// TODO: add skipped check at L2038
}
offset, err = pe.parseSections(sectionOffset)
if err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
pe.calculateHeaderEnd(offset)
if pe.getSectionByRva(pe.OptionalHeader.Data.AddressOfEntryPoint) != nil {
epOffset := pe.getOffsetFromRva(pe.OptionalHeader.Data.AddressOfEntryPoint)
if epOffset > pe.dataLen {
log.Printf("Possibly corrupt file. AddressOfEntryPoint lies outside the file. AddressOfEntryPoint: 0x%x", pe.OptionalHeader.Data.AddressOfEntryPoint)
}
} else {
log.Printf("AddressOfEntryPoint lies outside the sections' boundaries, AddressOfEntryPoint: 0x%x", pe.OptionalHeader.Data.AddressOfEntryPoint)
}
err = pe.parseDataDirectories()
if err != nil {
pe.data.Unmap()
pe.f.Close()
return nil, err
}
/*offset, err = pe.parseRichHeader()
if err != nil {
return nil, err
}*/
return pe, nil
}
func (self *PEFile) Close() error {
self.data.Unmap()
return self.f.Close()
}
type ByVAddr []*lib.SectionHeader
func (self ByVAddr) Len() int {
return len(self)
}
func (self ByVAddr) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (s ByVAddr) Less(i, j int) bool {
return s[i].Data.VirtualAddress < s[j].Data.VirtualAddress
}
func (self *PEFile) parseSections(offset uint32) (newOffset uint32, err error) {
newOffset = offset
for i := uint32(0); i < uint32(self.FileHeader.Data.NumberOfSections); i++ {
section := lib.NewSectionHeader(newOffset)
if err = self.parseHeader(§ion.Data, newOffset, section.Size); err != nil {
return 0, err
}
// TODO: More checks and error handling here from parseSections
// L2325-2376
lib.SetFlags(section.Flags, lib.SectionCharacteristics, uint32(section.Data.Characteristics))
// Suspecious check L2383 - L2395
self.Sections = append(self.Sections, section)
newOffset += section.Size
}
// Sort the sections by their VirtualAddress and add a field to each of them
// with the VirtualAddress of the next section. This will allow to check
// for potentially overlapping sections in badly constructed PEs.
sort.Sort(ByVAddr(self.Sections))
for idx, section := range self.Sections {
if idx == len(self.Sections)-1 {
section.NextHeaderAddr = 0
} else {
section.NextHeaderAddr = self.Sections[idx+1].Data.VirtualAddress
}
}
return newOffset, nil
}
func (self *PEFile) parseHeader(iface interface{}, offset, size uint32) (err error) {
if int(offset+size) >= len(self.data) {
return nil
}
buf := bytes.NewReader(self.data[offset : offset+size])
err = binary.Read(buf, binary.LittleEndian, iface)
if err != nil {
return err
}
return nil
}
func (self *PEFile) parseDataDirectories() error {
var dataDirs map[string]*lib.DataDirectory
funcMap := map[string]interface{}{
"IMAGE_DIRECTORY_ENTRY_IMPORT": self.parseImportDirectory,
"IMAGE_DIRECTORY_ENTRY_EXPORT": self.parseExportDirectory,
//"IMAGE_DIRECTORY_ENTRY_RESOURCE": self.parse_resources_directory,
// TODO at a later time
//"IMAGE_DIRECTORY_ENTRY_DEBUG": self.parseDebugDirectory,
//"IMAGE_DIRECTORY_ENTRY_BASERELOC": self.parseRelocationsDirectory,
//"IMAGE_DIRECTORY_ENTRY_TLS": self.parseDirectoryTls,
//"IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG": self.parseDirectoryLoadConfig,
//"IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT": self.parseDelayImportDirectory,
//"IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT": self.parseDirectoryBoundImports,
}
if self.OptionalHeader64 != nil {
dataDirs = self.OptionalHeader64.DataDirs
} else {
dataDirs = self.OptionalHeader.DataDirs
}
for name, dirEntry := range dataDirs {
if dirEntry.Data.VirtualAddress > 0 {
parser, ok := funcMap[name]
if !ok {
continue
}
err := parser.(func(uint32, uint32) error)(dirEntry.Data.VirtualAddress, dirEntry.Data.Size)
if err != nil {
return err
}
}
}
return nil
}
func (self *PEFile) getSectionByRva(rva uint32) *lib.SectionHeader {
for _, section := range self.Sections {
var size uint32
adjustedPointer := self.adjustFileAlignment(section.Data.PointerToRawData)
if self.dataLen-adjustedPointer < section.Data.SizeOfRawData {
size = section.Data.Misc
} else {
size = Max(section.Data.SizeOfRawData, section.Data.Misc)
}
vaddr := self.adjustSectionAlignment(section.Data.VirtualAddress)
if section.NextHeaderAddr != 0 && section.NextHeaderAddr > section.Data.VirtualAddress && vaddr+size > section.NextHeaderAddr {
size = section.NextHeaderAddr - vaddr
}
if vaddr <= rva && rva < (vaddr+size) {
return section
}
}
return nil
}
func (self *PEFile) getSectionByOffset(offset uint32) *lib.SectionHeader {
for _, section := range self.Sections {
if section.Data.PointerToRawData == 0 {
continue
}
adjustedPointer := self.adjustFileAlignment(section.Data.PointerToRawData)
if adjustedPointer <= offset && offset < (adjustedPointer+section.Data.SizeOfRawData) {
return section
}
}
return nil
}
func (self *PEFile) getRvaFromOffset(offset uint32) uint32 {
section := self.getSectionByOffset(offset)
minAddr := ^uint32(0)
if section == nil {
if len(self.Sections) == 0 {
return offset
}
for _, section := range self.Sections {
vaddr := self.adjustSectionAlignment(section.Data.VirtualAddress)
if vaddr < minAddr {
minAddr = vaddr
}
}
// Assume that offset lies within the headers
// The case illustrating this behavior can be found at:
// http://corkami.blogspot.com/2010/01/hey-hey-hey-whats-in-your-head.html
// where the import table is not contained by any section
// hence the RVA needs to be resolved to a raw offset
if offset < minAddr {
return offset
}
log.Println("data at Offset can't be fetched. Corrupt header?")
return ^uint32(0)
}
sectionAlignment := self.adjustSectionAlignment(section.Data.VirtualAddress)
fileAlignment := self.adjustFileAlignment(section.Data.PointerToRawData)
return offset - fileAlignment + sectionAlignment
}
func (self *PEFile) getOffsetFromRva(rva uint32) uint32 {
section := self.getSectionByRva(rva)
if section == nil {
if rva < self.dataLen {
return rva
}
log.Println("data at RVA can't be fetched. Corrupt header?")
return ^uint32(0)
}
sectionAlignment := self.adjustSectionAlignment(section.Data.VirtualAddress)
fileAlignment := self.adjustFileAlignment(section.Data.PointerToRawData)
return rva - sectionAlignment + fileAlignment
}
// According to http://corkami.blogspot.com/2010/01/parce-que-la-planche-aura-brule.html
// if PointerToRawData is less that 0x200 it's rounded to zero. Loading the test file
// in a debugger it's easy to verify that the PointerToRawData value of 1 is rounded
// to zero. Hence we reproduce the behavior
//
// According to the document:
// [ Microsoft Portable Executable and Common Object File Format Specification ]
// "The alignment factor (in bytes) that is used to align the raw data of sections in
// the image file. The value should be a power of 2 between 512 and 64 K, inclusive.
// The default is 512. If the SectionAlignment is less than the architecture's page
// size, then FileAlignment must match SectionAlignment."
//
// The following is a hard-coded constant if the Windows loader
func (self *PEFile) adjustFileAlignment(pointer uint32) uint32 {
fileAlignment := self.OptionalHeader.Data.FileAlignment
if fileAlignment > FILE_ALIGNMENT_HARDCODED_VALUE {
// If it's not a power of two, report it:
if !PowerOfTwo(fileAlignment) {
log.Printf("If FileAlignment > 0x200 it should be a power of 2. Value: %x", fileAlignment)
}
}
if fileAlignment < FILE_ALIGNMENT_HARDCODED_VALUE {
return pointer
}
return (pointer / 0x200) * 0x200
}
// According to the document:
// [ Microsoft Portable Executable and Common Object File Format Specification ]
// "The alignment (in bytes) of sections when they are loaded into memory. It must be
// greater than or equal to FileAlignment. The default is the page size for the
// architecture."
//
func (self *PEFile) adjustSectionAlignment(pointer uint32) uint32 {
sectionAlignment := self.OptionalHeader.Data.SectionAlignment
fileAlignment := self.OptionalHeader.Data.FileAlignment
if fileAlignment < FILE_ALIGNMENT_HARDCODED_VALUE {
if fileAlignment != sectionAlignment {
log.Printf("If FileAlignment(%x) < 0x200 it should equal SectionAlignment(%x)", fileAlignment, sectionAlignment)
}
}
if sectionAlignment < 0x1000 { // page size
sectionAlignment = fileAlignment
}
// else if sectionAlignment < 0x80 {
// 0x200 is the minimum valid FileAlignment according to the documentation
// although ntoskrnl.exe has an alignment of 0x80 in some Windows versions
// sectionAlignment = 0x80
//}
if sectionAlignment != 0 && (pointer%sectionAlignment) != 0 {
return sectionAlignment * (pointer / sectionAlignment)
}
return pointer
}
func (self *PEFile) getDataBounds(rva, length uint32) (start, size uint32) {
var end uint32
var offset uint32
section := self.getSectionByRva(rva)
if length > 0 {
end = rva + length
} else {
end = self.dataLen
}
if section == nil {
if rva < self.headerEnd {
end = Min(end, self.headerEnd)
}
// Before we give up we check whether the file might
// contain the data anyway. There are cases of PE files
// without sections that rely on windows loading the first
// 8291 bytes into memory and assume the data will be
// there
// A functional file with these characteristics is:
// MD5: 0008892cdfbc3bda5ce047c565e52295
// SHA-1: c7116b9ff950f86af256defb95b5d4859d4752a9
if rva < self.dataLen {
return rva, end
}
return ^uint32(0), ^uint32(0)
}
pointer := self.adjustFileAlignment(section.Data.PointerToRawData)
vaddr := self.adjustSectionAlignment(section.Data.VirtualAddress)
if rva == 0 {
offset = pointer
} else {
offset = (rva - vaddr) + pointer
}
if length != 0 {
end = offset + length
} else {
end = offset + section.Data.SizeOfRawData
}
if end > pointer+section.Data.SizeOfRawData {
end = section.Data.PointerToRawData + section.Data.SizeOfRawData
}
return offset, end
}
// Get an ASCII string from within the data at an RVA considering
// section
func (self *PEFile) getStringAtRva(rva uint32) []byte {
start, _ := self.getDataBounds(rva, 0)
return self.getStringFromData(start)
}
// Get an ASCII string from within the data.
func (self *PEFile) getStringFromData(offset uint32) []byte {
if offset > self.dataLen {
return []byte{}
}
end := offset
for end < self.dataLen {
// fmt.Printf("%d ", self.data[end])
if self.data[end] == 0 {
break
}
end += 1
}
// fmt.Println("offset", offset, "end", end)
return self.data[offset:end]
}
// OC Patch:
// There could be a problem if there are no raw data sections
// greater than 0
// fc91013eb72529da005110a3403541b6 example
// Should this throw an exception in the minimum header offset
// can't be found?
func (self *PEFile) calculateHeaderEnd(offset uint32) {
var rawDataPointers []uint32
for _, section := range self.Sections {
prd := section.Data.PointerToRawData
if prd > uint32(0x0) {
rawDataPointers = append(rawDataPointers, self.adjustFileAlignment(prd))
}
}
minSectionOffset := uint32(0x0)
if len(rawDataPointers) > 0 {
minSectionOffset = rawDataPointers[0]
for _, pointer := range rawDataPointers {
if pointer < minSectionOffset {
minSectionOffset = pointer
}
}
}
if minSectionOffset == 0 || minSectionOffset < offset {
self.headerEnd = offset
} else {
self.headerEnd = minSectionOffset
}
}
func (self *PEFile) GetImpHash() string {
impstrs := []string{}
exts := []string{"ocx", "sys", "dll"}
for _, entry := range self.ImportDescriptors {
libname := strings.ToLower(string(entry.Dll))
parts := strings.Split(libname, ".")
if len(parts) > 1 && stringInSlice(parts[1], exts) {
libname = parts[0]
}
for _, imp := range entry.Imports {
var funcname string
if len(imp.Name) == 0 {
funcname = OrdLookup(string(entry.Dll), uint64(imp.Ordinal), true)
} else {
funcname = string(imp.Name)
}
impstrs = append(impstrs, fmt.Sprintf("%s.%s", strings.ToLower(libname), strings.ToLower(funcname)))
// log.Println(funcname)
}
}
h := md5.New()
libnames := strings.Join(impstrs, ",")
h.Write([]byte(libnames))
md5_hash := hex.EncodeToString(h.Sum(nil))
return md5_hash
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func (self *PEFile) GetData(section *lib.SectionHeader) []byte {
// if section.
start, end := self.getDataBounds(section.Data.VirtualAddress, section.Data.SizeOfRawData)
if end > self.dataLen {
end = self.dataLen
}
return self.data[start:end]
}