-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
242 lines (208 loc) · 5.15 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"www.velocidex.com/golang/binparsergen/reader"
"www.velocidex.com/golang/go-pe"
)
type PEFunction struct {
Host string `json:"Host"`
Functions []string `json:"Functions"`
}
var (
pePath string
reDirPath string
reType string
printDef string
printImpHash bool
printImports bool
printExports bool
printForwards bool
verbose bool
)
func init() {
log.SetPrefix("ERROR: ")
log.SetOutput(os.Stderr)
flag.StringVar(&printDef, "def", "", "Print .def file from a PEs imports of the given dllname")
flag.BoolVar(&printImpHash, "imphash", false, "Print ImpHash only")
flag.BoolVar(&printImports, "imports", false, "Print Imports only")
flag.BoolVar(&printExports, "exports", false, "Print Exports only")
flag.BoolVar(&printForwards, "forwards", false, "Print Forwards only")
flag.BoolVar(&verbose, "v", false, "Print additional fields")
flag.StringVar(&reDirPath, "dir", "", "Directory to recurse")
flag.StringVar(&reType, "type", "", "Use with --dir. Get [exe|dll]")
flag.Parse()
if (reDirPath != "" && reType == "") || (reDirPath == "" && reType != "") {
log.Print("\n-dir and -type must be used together\n\n")
if (reType != "dll" && reType != "exe") || reDirPath == "" {
log.Printf("\n-type must be 'dll' or 'exe'\n\n")
flag.Usage()
os.Exit(1)
}
flag.Usage()
os.Exit(1)
}
if flag.NArg() == 0 && reDirPath == "" {
log.Fatal("\nPath to PE required\n\n")
}
}
func main() {
if reDirPath != "" {
peType := fmt.Sprintf("*.%s", reType)
absDirPath, _ := filepath.Abs(reDirPath)
walkFunction := walkFunctionGenerator(peType)
filepath.WalkDir(absDirPath, walkFunction)
os.Exit(0)
}
pePath = flag.Arg(0)
info, err := os.Stat(pePath)
if err != nil {
log.Fatalf("cannot open %s %s", pePath, err)
}
var report *Report
if info.IsDir() {
report = newDirectoryReport(pePath)
jsPrint(report)
os.Exit(0)
}
report = newPEReport(pePath)
peFile, err := newPEFile(report.Path)
if err != nil {
log.Fatalf("%s %s\n", report.Path, err)
}
if printImpHash {
tyrian := peFile.ImpHash()
fmt.Println(tyrian)
os.Exit(0)
}
if printImports {
for _, data := range peFile.Imports() {
fmt.Println(data)
}
os.Exit(0)
}
if printExports {
for _, data := range peFile.Exports() {
fmt.Println(data)
}
os.Exit(0)
}
if printForwards {
for _, data := range peFile.Forwards() {
fmt.Println(data)
}
os.Exit(0)
}
err = populatePEReport(report, peFile)
if err != nil {
log.Fatalf("%s %s\n", report.Path, err)
}
if printDef != "" {
var defs []string
for _, imp := range report.Imports {
if strings.ToLower(imp.Host) == strings.ToLower(printDef) {
sansSuffix := strings.Replace(imp.Host, ".dll", "", 1)
for _, fn := range imp.Functions {
line := fmt.Sprintf("%s.%s", sansSuffix, fn)
defs = append(defs, line)
}
}
}
out := makeDepFile(defs)
fmt.Println(out)
os.Exit(0)
}
jsPrint(report)
}
func walkFunctionGenerator(pattern string) fs.WalkDirFunc {
// use a set to track if a report for a PE's parent directory
// has already been printed
printedParentDir := make(map[string]bool)
return func(path string, info os.DirEntry, err error) error {
if err != nil {
log.Printf("HUH? %s\n", err)
}
if info.IsDir() {
return nil
}
matched, err := filepath.Match(pattern, filepath.Base(path))
if err != nil {
log.Printf("#Match %s\n", err)
}
if matched {
parent := filepath.Dir(path)
if !printedParentDir[parent] {
// first time finding a PE in this directory
dirReport := newDirectoryReport(parent)
jsPrint(dirReport)
printedParentDir[parent] = true
}
report := newPEReport(path)
peFile, err := newPEFile(report.Path)
if err != nil {
log.Printf("#newPEFile - %s - %s\n", report.Path, err)
return nil
}
err = populatePEReport(report, peFile)
if err != nil {
log.Printf("#populateReport - %s\n", err)
return nil
}
jsPrint(report)
}
return nil
}
}
func newDirectoryReport(path string) *Report {
report := &Report{}
report.Name = filepath.Base(path)
report.Path, _ = filepath.Abs(path)
report.Type = "directory"
report.Dir = filepath.Dir(path)
err := handleDirPerms(report)
if err != nil {
return report
}
return report
}
func newPEReport(path string) *Report {
report := &Report{}
report.Name = filepath.Base(path)
report.Path, _ = filepath.Abs(path)
report.Type = "file"
return report
}
func newPEFile(path string) (pefile *pe.PEFile, err error) {
peFileH, err := os.OpenFile(path, os.O_RDONLY, 0600)
if err != nil {
return
}
peReader, err := reader.NewPagedReader(peFileH, 4096, 100)
if err != nil {
return
}
return pe.NewPEFile(peReader)
}
func makeDepFile(deps []string) string {
if len(deps) == 0 {
fmt.Fprintln(os.Stderr, "nothing to forward")
os.Exit(1)
}
base := strings.Split(deps[0], ".")[0]
var formatted []string
for _, dep := range deps {
fn := strings.Split(dep, ".")[1]
tmpl := fmt.Sprintf(" %s = %s", fn, dep)
formatted = append(formatted, tmpl)
}
template := `LIBRARY %s
EXPORTS
%s
`
return fmt.Sprintf(template, base, strings.Join(formatted, "\n"))
}