-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary.go
154 lines (139 loc) · 3.64 KB
/
binary.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
package gopwn
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"regexp"
"github.com/ianlancetaylor/demangle"
)
type BinaryReader interface {
Read(p []byte) (n int, err error)
ReadAt(b []byte, off int64) (n int, err error)
Seek(offset int64, whence int) (int64, error)
}
type fileBytes struct {
raw []byte
addrToOffset func(addr uint64) (uint64, error)
}
// Write copies data to the raw data at the specified virtual address
func (f *fileBytes) Write(data []byte, addr uint64) error {
offset, err := f.addrToOffset(addr)
if err != nil {
return err
}
copy(f.raw[offset:offset+uint64(len(data))], data)
return nil
}
// Read reads up to n bytes from the raw data at the specified virtual address
func (f *fileBytes) Read(addr uint64, n int) ([]byte, error) {
offset, err := f.addrToOffset(addr)
if err != nil {
return nil, err
}
buf := make([]byte, n)
r := bytes.NewReader(f.raw)
if _, err := r.ReadAt(buf, int64(offset)); err != nil {
return nil, err
}
return buf, nil
}
// Save saves the raw bytes to a specified file path
func (f *fileBytes) Save(filePath string, fileMode os.FileMode) error {
return ioutil.WriteFile(filePath, f.raw, fileMode)
}
type Cave struct {
SectionName string
SectionOffset uint64
SectionSize uint64
Begin int
End int
Size int
Addr int
Infos string
}
func (c *Cave) Dump() {
fmt.Println("\n[+] CAVE DETECTED!")
fmt.Printf("[!] Section Name: %s\n", c.SectionName)
fmt.Printf("[!] Section Offset: %#x\n", c.SectionOffset)
fmt.Printf("[!] Section Size: %#x (%d bytes)\n", c.SectionSize, int(c.SectionSize))
fmt.Printf("[!] Section Flags: %s\n", c.Infos)
fmt.Printf("[!] Virtual Address: %#x\n", c.Addr)
fmt.Printf("[!] Cave Begin: %#x\n", c.Begin)
fmt.Printf("[!] Cave End: %#x\n", c.End)
fmt.Printf("[!] Cave Size: %#x (%d bytes)\n", c.Size, c.Size)
}
func searchCaves(name string, data []byte, offset, addr, size uint64, infos string, caveSize int) []Cave {
caveBytes := []byte("\x00")
var caves []Cave
caveCount := 0
for currentOffset := 0; currentOffset < len(data); currentOffset++ {
currentByte := data[currentOffset]
if bytes.Contains([]byte{currentByte}, caveBytes) {
caveCount++
} else {
if caveCount >= caveSize {
caves = append(caves, Cave{
SectionName: name,
SectionOffset: offset,
SectionSize: size,
Size: caveCount,
Addr: int(addr) + currentOffset - caveCount,
Begin: int(offset) + currentOffset - caveCount,
End: int(offset) + currentOffset,
Infos: infos,
})
}
caveCount = 0
}
}
return caves
}
type dataReader interface {
Data() ([]byte, error)
}
type StringsOptions struct {
Min int
Max int
Regex func(min, max int) *regexp.Regexp
Sections []string
Demangle bool
}
func parseStrings(sections []dataReader, optFns ...func(o *StringsOptions)) []string {
options := StringsOptions{
Min: 4,
Max: 100,
Demangle: false,
Regex: func(min, max int) *regexp.Regexp {
return regexp.MustCompile(fmt.Sprintf("([\x20-\x7E]{%d}[\x20-\x7E]*)", min))
},
}
for _, fn := range optFns {
fn(&options)
}
validString := options.Regex(options.Min, options.Max)
var strs []string
for _, s := range sections {
b, err := s.Data()
if err != nil {
continue
}
var slice [][]byte
if slice = bytes.Split(b, []byte("\x00")); slice == nil {
return nil
}
for _, b := range slice {
if len(b) == 0 || len(b) > options.Max {
continue
}
str := string(b)
if validString.MatchString(str) {
if options.Demangle {
str = demangle.Filter(str)
}
strs = append(strs, str)
}
}
}
return strs
}