-
Notifications
You must be signed in to change notification settings - Fork 5
/
macho.go
71 lines (61 loc) · 1.33 KB
/
macho.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
package gopwn
import (
"bytes"
"debug/macho"
"fmt"
"os"
)
type MACHO struct {
file *macho.File
}
func NewMACHO(path string) (*MACHO, error) {
fh, err := os.Open(path)
if err != nil {
return nil, err
}
return NewMACHOFromReader(fh)
}
func NewMACHOFromBytes(b []byte) (*MACHO, error) {
r := bytes.NewReader(b)
return NewMACHOFromReader(r)
}
func NewMACHOFromReader(r BinaryReader) (*MACHO, error) {
f, err := macho.NewFile(r)
if err != nil {
return nil, err
}
return &MACHO{
file: f,
}, nil
}
func (m *MACHO) Close() error {
return m.file.Close()
}
func (m *MACHO) Caves(caveSize int) []Cave {
var caves []Cave
for _, s := range m.file.Sections {
data, _ := s.Data()
caves = append(caves, searchCaves(fmt.Sprintf("%s.%s", s.Seg, s.Name), data, uint64(s.Offset), s.Addr, s.Size, parseMACHOFlags(s.Flags), caveSize)...)
}
return caves
}
func parseMACHOFlags(f uint32) string {
return "TODO"
}
func (m *MACHO) Strings(optFns ...func(o *StringsOptions)) []string {
options := StringsOptions{}
for _, fn := range optFns {
fn(&options)
}
var sections []dataReader
if len(options.Sections) > 0 {
for _, name := range options.Sections {
sections = append(sections, m.file.Section(name))
}
} else {
for _, s := range m.file.Sections {
sections = append(sections, s)
}
}
return parseStrings(sections)
}