forked from Velocidex/go-pe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imports.go
176 lines (139 loc) · 3.81 KB
/
imports.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
package pe
import "fmt"
func (self *IMAGE_NT_HEADERS) ImportDirectory(
rva_resolver *RVAResolver) []*IMAGE_IMPORT_DESCRIPTOR {
result := []*IMAGE_IMPORT_DESCRIPTOR{}
dir := self.DataDirectory(IMAGE_DIRECTORY_ENTRY_IMPORT)
Debug(dir)
offset, err := rva_resolver.GetFileAddress(dir.VirtualAddress())
if err != nil {
return nil
}
for offset > 0 {
desc := self.Profile.IMAGE_IMPORT_DESCRIPTOR(
self.Reader, int64(offset))
if desc.Name() == 0 && desc.Characteristics() == 0 {
break
}
Debug(desc)
result = append(result, desc)
if len(result) > MAX_IMPORT_TABLE_LENGTH {
break
}
offset += uint32(desc.Size())
}
return result
}
func (self *IMAGE_IMPORT_DESCRIPTOR) DLLName(rva_resolver *RVAResolver) string {
offset, err := rva_resolver.GetFileAddress(self.Name())
if err != nil || offset == 0 {
return ""
}
result := ParseTerminatedString(self.Reader, int64(offset))
return result
}
func (self *IMAGE_IMPORT_DESCRIPTOR) Functions32(rva_resolver *RVAResolver) []string {
result := []string{}
offset, err := rva_resolver.GetFileAddress(self.OriginalFirstThunk())
if err != nil {
return result
}
for {
thunk := self.Profile.IMAGE_THUNK_DATA32(self.Reader, int64(offset))
if thunk.Function() == 0 {
break
}
ordinal := thunk.Ordinal()
if ordinal&0x80000000 > 0 {
// If the import is by ordinal then encode it as hex.
result = append(result, fmt.Sprintf(
"%#x", 0xFFFFFF&thunk.Ordinal()))
} else {
file_address, err := rva_resolver.GetFileAddress(
uint32(thunk.AddressOfData()))
// If the thunk address is not found in the file skip this
// thunk.
if err != nil || file_address == 0 {
return result
}
import_by_name := self.Profile.IMAGE_IMPORT_BY_NAME(
self.Reader, int64(file_address))
name := import_by_name.Name()
if name != "" {
if import_by_name.Offset != 0 {
result = append(result, name)
}
}
}
offset += uint32(thunk.Size())
// Keep the size resonable
if len(result) > MAX_IMPORT_TABLE_LENGTH {
break
}
}
return result
}
func (self *IMAGE_IMPORT_DESCRIPTOR) Functions64(rva_resolver *RVAResolver) []string {
result := []string{}
offset, err := rva_resolver.GetFileAddress(self.OriginalFirstThunk())
if err != nil {
return result
}
for {
thunk := self.Profile.IMAGE_THUNK_DATA64(self.Reader, int64(offset))
if thunk.Function() == 0 {
break
}
ordinal := thunk.Ordinal()
if ordinal&0x8000000000000000 > 0 {
// If the import is by ordinal then encode it as hex.
result = append(result, fmt.Sprintf(
"%#x", 0xFFFFFF&thunk.Ordinal()))
} else {
file_address, err := rva_resolver.GetFileAddress(
uint32(thunk.AddressOfData()))
// If the thunk address is not found in the file skip this
// thunk.
if err != nil || file_address == 0 {
return result
}
Debug(thunk)
import_by_name := self.Profile.IMAGE_IMPORT_BY_NAME(
self.Reader, int64(file_address))
name := import_by_name.Name()
if name != "" {
if import_by_name.Offset != 0 {
result = append(result, name)
}
}
}
offset += uint32(thunk.Size())
if len(result) > MAX_IMPORT_TABLE_LENGTH {
break
}
}
return result
}
func GetImports(nt_header *IMAGE_NT_HEADERS, rva_resolver *RVAResolver) []string {
result := []string{}
for _, desc := range nt_header.ImportDirectory(rva_resolver) {
dll_name := desc.DLLName(rva_resolver)
// Is it valid to have an import with an empty dll name?
if dll_name == "" || len(dll_name) > 512 {
continue
}
if rva_resolver.Is64Bit {
for _, name := range desc.Functions64(rva_resolver) {
result = append(result, dll_name+"!"+name)
}
} else {
for _, name := range desc.Functions32(rva_resolver) {
result = append(result, dll_name+"!"+name)
}
}
if len(result) > MAX_IMPORT_TABLE_LENGTH {
break
}
}
return result
}