-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (84 loc) · 2.13 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
package main
import (
"fmt"
"log"
"os"
"text/template"
"github.com/urfave/cli/v2"
"github.com/nick-jones/gost/pkg/scan"
)
const tmpl = `{{printf "%x: %q" .Addr .Value}} → {{range $i, $e := .Refs}}
{{- if le $i 5}}{{ printf "%s:%d " .File .Line }}{{end}}
{{- end}}
{{- if gt (len .Refs) 5}}... (truncated, {{len .Refs}} total){{- end -}}
`
func main() {
app := &cli.App{
Name: "gost",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "template",
Usage: "template string for printing the results (format is text/template)",
Value: tmpl,
},
&cli.StringFlag{
Name: "string-table",
Usage: `if symbols are missing, use values "guess" or "ignore" to enable more fuzzy matching`,
},
&cli.BoolFlag{
Name: "nulls",
Usage: "string candidates containing null characters will be included",
},
},
Action: run,
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func run(c *cli.Context) error {
filePath := c.Args().First()
format := c.String("template")
tmpl, err := template.New("format").Parse(format)
if err != nil {
return fmt.Errorf("failed to parse format flag: %w", err)
}
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
defer f.Close()
opts, err := parseFlags(c)
if err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
// run analysis
results, err := scan.Run(f, opts...)
if err != nil {
return fmt.Errorf("failed to search instructions: %w", err)
}
// print results
for _, res := range results {
if err := tmpl.Execute(os.Stdout, res); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
fmt.Println()
}
return nil
}
func parseFlags(c *cli.Context) ([]scan.Option, error) {
opts := make([]scan.Option, 0)
switch flag := c.String("string-table"); flag {
case "guess":
opts = append(opts, scan.WithStringTableGuessed())
case "ignore":
opts = append(opts, scan.WithStringTableIgnored())
case "":
default:
return nil, fmt.Errorf("invalid str-table flag value: %s", flag)
}
if c.Bool("nulls") {
opts = append(opts, scan.WithNullsPermitted())
}
return opts, nil
}