forked from mandiant/GoReSym
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelf.go
317 lines (278 loc) · 7.91 KB
/
elf.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
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*Copyright (C) 2022 Mandiant, Inc. All Rights Reserved.*/
// Parsing of ELF executables (Linux, FreeBSD, and so on).
package objfile
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"github.com/mandiant/GoReSym/debug/dwarf"
"github.com/mandiant/GoReSym/debug/elf"
)
type elfFile struct {
elf *elf.File
}
func openElf(r io.ReaderAt) (rawFile, error) {
f, err := elf.NewFile(r)
if err != nil {
return nil, err
}
return &elfFile{f}, nil
}
func (f *elfFile) read_memory(VA uint64, size uint64) (data []byte, err error) {
for _, prog := range f.elf.Progs {
if prog.Vaddr <= VA && VA <= prog.Vaddr+prog.Filesz-1 {
n := prog.Vaddr + prog.Filesz - VA
if n > size {
n = size
}
data := make([]byte, n)
_, err := prog.ReadAt(data, int64(VA-prog.Vaddr))
if err != nil {
return nil, err
}
return data, nil
}
}
return nil, fmt.Errorf("Failed to read memory")
}
func (f *elfFile) symbols() ([]Sym, error) {
elfSyms, err := f.elf.Symbols()
if err != nil {
return nil, err
}
var syms []Sym
for _, s := range elfSyms {
sym := Sym{Addr: s.Value, Name: s.Name, Size: int64(s.Size), Code: '?'}
switch s.Section {
case elf.SHN_UNDEF:
sym.Code = 'U'
case elf.SHN_COMMON:
sym.Code = 'B'
default:
i := int(s.Section)
if i < 0 || i >= len(f.elf.Sections) {
break
}
sect := f.elf.Sections[i]
switch sect.Flags & (elf.SHF_WRITE | elf.SHF_ALLOC | elf.SHF_EXECINSTR) {
case elf.SHF_ALLOC | elf.SHF_EXECINSTR:
sym.Code = 'T'
case elf.SHF_ALLOC:
sym.Code = 'R'
case elf.SHF_ALLOC | elf.SHF_WRITE:
sym.Code = 'D'
}
}
if elf.ST_BIND(s.Info) == elf.STB_LOCAL {
sym.Code += 'a' - 'A'
}
syms = append(syms, sym)
}
return syms, nil
}
func (f *elfFile) pcln_scan() (candidates []PclntabCandidate, err error) {
// 1) Locate pclntab via symbols (standard way)
foundpcln := false
var pclntab []byte
if sect := f.elf.Section(".gopclntab"); sect != nil {
if pclntab, err = sect.Data(); err == nil {
foundpcln = true
}
}
// 2) if not found, byte scan for it
ExitScan:
for _, sec := range f.elf.Sections {
// first section is all zeros, skip
if sec.Type == elf.SHT_NULL {
continue
}
// malware can split the pclntab across multiple sections, re-merge
data := f.elf.DataAfterSection(sec)
if !foundpcln {
// https://github.com/golang/go/blob/2cb9042dc2d5fdf6013305a077d013dbbfbaac06/src/debug/gosym/pclntab.go#L172
pclntab_sigs := [][]byte{[]byte("\xFB\xFF\xFF\xFF\x00\x00"), []byte("\xFA\xFF\xFF\xFF\x00\x00"), []byte("\xF0\xFF\xFF\xFF\x00\x00"),
[]byte("\xFF\xFF\xFF\xFB\x00\x00"), []byte("\xFF\xFF\xFF\xFA\x00\x00"), []byte("\xFF\xFF\xFF\xF0\x00\x00")}
matches := findAllOccurrences(data, pclntab_sigs)
for _, pclntab_idx := range matches {
if pclntab_idx != -1 && pclntab_idx < int(sec.Size) {
pclntab = data[pclntab_idx:]
var candidate PclntabCandidate
candidate.Pclntab = pclntab
candidate.SecStart = uint64(sec.Addr)
candidate.PclntabVA = candidate.SecStart + uint64(pclntab_idx)
candidates = append(candidates, candidate)
// we must scan all signature for all sections. DO NOT BREAK
}
}
} else {
// 3) if we found it earlier, figure out which section base to return (might be wrong for packed things)
pclntab_idx := bytes.Index(data, pclntab)
if pclntab_idx != -1 && pclntab_idx < int(sec.Size) {
var candidate PclntabCandidate
candidate.Pclntab = pclntab
candidate.SecStart = uint64(sec.Addr)
candidate.PclntabVA = candidate.SecStart + uint64(pclntab_idx)
candidates = append(candidates, candidate)
break ExitScan
}
}
}
return candidates, nil
}
func (f *elfFile) pcln() (candidates []PclntabCandidate, err error) {
candidates, err = f.pcln_scan()
if err != nil {
return nil, err
}
// 4) symtab is completely optional, but try to find it
var symtab []byte
if sect := f.elf.Section(".gosymtab"); sect != nil {
symtab, err = sect.Data()
}
if err == nil {
for _, c := range candidates {
c.Symtab = symtab
}
}
return candidates, nil
}
func (f *elfFile) moduledata_scan(pclntabVA uint64, is64bit bool, littleendian bool, ignorelist []uint64) (secStart uint64, moduledataVA uint64, moduledata []byte, err error) {
foundsym := false
foundsec := false
foundmodule := false
syms, err := f.symbols()
if err == nil {
foundsym = false
for _, sym := range syms {
// TODO: handle legacy symbols ??
if sym.Name == "runtime.firstmoduledata" {
moduledataVA = sym.Addr
foundsym = true // annoyingly the elf symbols dont give section #, so we delay getting data to later, unlike in pe
break
}
}
}
scan:
for _, sec := range f.elf.Sections {
// first section is all zeros, skip
if sec.Type == elf.SHT_NULL {
continue
}
// malware can split the pclntab across multiple sections, re-merge
data := f.elf.DataAfterSection(sec)
if !foundsym {
// fall back to scanning for structure using address of pclntab, which is first value in struc
var pclntabVA_bytes []byte
if is64bit {
pclntabVA_bytes = make([]byte, 8)
if littleendian {
binary.LittleEndian.PutUint64(pclntabVA_bytes, pclntabVA)
} else {
binary.BigEndian.PutUint64(pclntabVA_bytes, pclntabVA)
}
} else {
pclntabVA_bytes = make([]byte, 4)
if littleendian {
binary.LittleEndian.PutUint32(pclntabVA_bytes, uint32(pclntabVA))
} else {
binary.BigEndian.PutUint32(pclntabVA_bytes, uint32(pclntabVA))
}
}
moduledata_idx := bytes.Index(data, pclntabVA_bytes)
if moduledata_idx != -1 && moduledata_idx < int(sec.Size) {
moduledata = data[moduledata_idx:]
moduledataVA = sec.Addr + uint64(moduledata_idx)
secStart = sec.Addr
// optionally consult ignore list, to skip past previous (bad) scan results
if ignorelist != nil {
for _, ignore := range ignorelist {
if ignore == secStart+uint64(moduledata_idx) {
continue scan
}
}
}
foundsec = true
foundmodule = true
break
}
} else {
if moduledataVA > sec.Addr && moduledataVA < sec.Addr+sec.Size {
sectionoffset := moduledataVA - sec.Addr
moduledata = data[sectionoffset:]
secStart = sec.Addr
foundsec = true
foundmodule = true
break
}
}
}
if !foundmodule {
return 0, 0, nil, fmt.Errorf("moduledata could not be located")
}
if !foundsec {
return 0, 0, nil, fmt.Errorf("moduledata containing section could not be located")
}
return secStart, moduledataVA, moduledata, nil
}
func (f *elfFile) text() (textStart uint64, text []byte, err error) {
sect := f.elf.Section(".text")
if sect == nil {
return 0, nil, fmt.Errorf("text section not found")
}
textStart = sect.Addr
text, err = sect.Data()
return
}
func (f *elfFile) rdata() (textStart uint64, text []byte, err error) {
sect := f.elf.Section(".rodata")
if sect == nil {
return 0, nil, fmt.Errorf("rdata section not found")
}
textStart = sect.Addr
text, err = sect.Data()
return
}
func (f *elfFile) rel_rdata() (textStart uint64, text []byte, err error) {
sect := f.elf.Section(".data.rel.ro")
if sect == nil {
return 0, nil, fmt.Errorf(".data.rel.ro section not found")
}
textStart = sect.Addr
text, err = sect.Data()
return
}
func (f *elfFile) goarch() string {
switch f.elf.Machine {
case elf.EM_386:
return "386"
case elf.EM_X86_64:
return "amd64"
case elf.EM_ARM:
return "arm"
case elf.EM_AARCH64:
return "arm64"
case elf.EM_PPC64:
if f.elf.ByteOrder == binary.LittleEndian {
return "ppc64le"
}
return "ppc64"
case elf.EM_S390:
return "s390x"
}
return ""
}
func (f *elfFile) loadAddress() (uint64, error) {
for _, p := range f.elf.Progs {
if p.Type == elf.PT_LOAD && p.Flags&elf.PF_X != 0 {
return p.Vaddr, nil
}
}
return 0, fmt.Errorf("unknown load address")
}
func (f *elfFile) dwarf() (*dwarf.Data, error) {
return f.elf.DWARF()
}