forked from goretk/gore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe.go
153 lines (135 loc) · 3.92 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
// This file is part of GoRE.
//
// Copyright (C) 2019-2021 GoRE Authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package gore
import (
"debug/gosym"
"debug/pe"
"encoding/binary"
"fmt"
"os"
)
func openPE(fp string) (peF *peFile, err error) {
// Parsing by the file by debug/pe can panic if the PE file is malformed.
// To prevent a crash, we recover the panic and return it as an error
// instead.
go func() {
if r := recover(); r != nil {
err = fmt.Errorf("error when processing PE file, probably corrupt: %s", r)
}
}()
osFile, err := os.Open(fp)
if err != nil {
err = fmt.Errorf("error when opening the file: %w", err)
return
}
f, err := pe.NewFile(osFile)
if err != nil {
err = fmt.Errorf("error when parsing the PE file: %w", err)
return
}
peF = &peFile{file: f, osFile: osFile}
return
}
type peFile struct {
file *pe.File
osFile *os.File
pclntabAddr uint64
imageBase uint64
}
func (p *peFile) getFile() *os.File {
return p.osFile
}
func (p *peFile) getPCLNTab() (*gosym.Table, error) {
addr, pclndat, err := searchFileForPCLNTab(p.file)
if err != nil {
return nil, err
}
pcln := gosym.NewLineTable(pclndat, uint64(p.file.Section(".text").VirtualAddress)+p.imageBase)
p.pclntabAddr = uint64(addr) + p.imageBase
return gosym.NewTable(make([]byte, 0), pcln)
}
func (p *peFile) Close() error {
err := p.file.Close()
if err != nil {
return err
}
return p.osFile.Close()
}
func (p *peFile) getRData() ([]byte, error) {
section := p.file.Section(".rdata")
if section == nil {
return nil, ErrSectionDoesNotExist
}
return section.Data()
}
func (p *peFile) getCodeSection() ([]byte, error) {
section := p.file.Section(".text")
if section == nil {
return nil, ErrSectionDoesNotExist
}
return section.Data()
}
func (p *peFile) moduledataSection() string {
return ".data"
}
func (p *peFile) getPCLNTABData() (uint64, []byte, error) {
b, d, e := searchFileForPCLNTab(p.file)
return p.imageBase + uint64(b), d, e
}
func (p *peFile) getSectionDataFromOffset(off uint64) (uint64, []byte, error) {
for _, section := range p.file.Sections {
if section.Offset == 0 {
// Only exist in memory
continue
}
if p.imageBase+uint64(section.VirtualAddress) <= off && off < p.imageBase+uint64(section.VirtualAddress+section.Size) {
data, err := section.Data()
return p.imageBase + uint64(section.VirtualAddress), data, err
}
}
return 0, nil, ErrSectionDoesNotExist
}
func (p *peFile) getSectionData(name string) (uint64, []byte, error) {
section := p.file.Section(name)
if section == nil {
return 0, nil, ErrSectionDoesNotExist
}
data, err := section.Data()
return p.imageBase + uint64(section.VirtualAddress), data, err
}
func (p *peFile) getFileInfo() *FileInfo {
fi := &FileInfo{ByteOrder: binary.LittleEndian, OS: "windows"}
if p.file.Machine == pe.IMAGE_FILE_MACHINE_I386 {
fi.WordSize = intSize32
optHdr := p.file.OptionalHeader.(*pe.OptionalHeader32)
p.imageBase = uint64(optHdr.ImageBase)
fi.Arch = Arch386
} else {
fi.WordSize = intSize64
optHdr := p.file.OptionalHeader.(*pe.OptionalHeader64)
p.imageBase = optHdr.ImageBase
fi.Arch = ArchAMD64
}
return fi
}
func (p *peFile) getBuildID() (string, error) {
data, err := p.getCodeSection()
if err != nil {
return "", fmt.Errorf("failed to get code section: %w", err)
}
return parseBuildIDFromRaw(data)
}