-
Notifications
You must be signed in to change notification settings - Fork 4
/
team-cymru.go
201 lines (176 loc) · 4.69 KB
/
team-cymru.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
package main
import (
"encoding/json"
"fmt"
"log"
"net"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/crackcomm/go-clitable"
"github.com/levigross/grequests"
"github.com/parnurzeal/gorequest"
"github.com/urfave/cli"
)
// Version stores the plugin's version
var Version string
// BuildTime stores the plugin's build time
var BuildTime string
// TeamCymru json object
type TeamCymru struct {
Results ResultsData `json:"team-cymru"`
}
// ResultsData json object
type ResultsData struct {
Found bool `json:"found"`
LastSeen string `json:"lastseen"` // last known GMT timestamp associated with that hash in unix epoch
Detection string `json:"detection"` // detection percentage across a mix of AV packages
}
type notFound struct {
Found bool `json:"found"`
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func hashType(hash string) *grequests.RequestOptions {
if match, _ := regexp.MatchString("([a-fA-F0-9]{32})", hash); match {
return &grequests.RequestOptions{
Params: map[string]string{
"md5": hash,
},
}
} else if match, _ := regexp.MatchString("([a-fA-F0-9]{40})", hash); match {
return &grequests.RequestOptions{
Params: map[string]string{
"sha1": hash,
},
}
} else if match, _ := regexp.MatchString("([a-fA-F0-9]{64})", hash); match {
return &grequests.RequestOptions{
Params: map[string]string{
"sha256": hash,
},
}
} else if match, _ := regexp.MatchString("([a-fA-F0-9]{128})", hash); match {
return &grequests.RequestOptions{
Params: map[string]string{
"sha512": hash,
},
}
} else {
return &grequests.RequestOptions{ //, fmt.Errorf("%s is not a valid hash", hash)
}
}
}
func parseLookupHashOutput(lookupout []string) ResultsData {
lookup := ResultsData{Found: false}
if len(lookupout) > 0 {
fields := strings.Fields(lookupout[0])
if len(fields) > 0 {
lookup.Detection = fields[1] + "%"
s, _ := strconv.ParseInt(fields[0], 10, 64)
lookup.LastSeen = time.Unix(s, 0).Format(time.DateOnly)
lookup.Found = true
} else {
log.Fatal(fmt.Errorf("unable to parse LookupHashOutput: %#v", lookupout))
}
}
return lookup
}
// LookupHash retreieves the TeamCymru - Malware Hash Registry for the given hash
func LookupHash(hash string) ResultsData {
answers, _ := net.LookupTXT(hash + ".malware.hash.cymru.com")
// fmt.Printf("answers: %#v\n", answers)
// fmt.Printf("err: %#v\n", err)
tcResult := parseLookupHashOutput(answers)
// fmt.Printf("%#v", tcResult)
return tcResult
}
func printStatus(resp gorequest.Response, body string, errs []error) {
fmt.Println(resp.Status)
}
func printMarkDownTable(ss TeamCymru) {
fmt.Println("#### TeamCymru")
if ss.Results.Found {
table := clitable.New([]string{"Found", "Detection", "LastSeen"})
table.AddRow(map[string]interface{}{
"Found": ss.Results.Found,
"Detection": ss.Results.Detection,
"LastSeen": ss.Results.LastSeen,
})
table.Markdown = true
table.Print()
} else {
fmt.Println(" - Not found")
}
}
var appHelpTemplate = `Usage: {{.Name}} {{if .Flags}}[OPTIONS] {{end}}COMMAND [arg...]
{{.Usage}}
Version: {{.Version}}{{if or .Author .Email}}
Author:{{if .Author}}
{{.Author}}{{if .Email}} - <{{.Email}}>{{end}}{{else}}
{{.Email}}{{end}}{{end}}
{{if .Flags}}
Options:
{{range .Flags}}{{.}}
{{end}}{{end}}
Commands:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}}
Run '{{.Name}} COMMAND --help' for more information on a command.
`
func main() {
cli.AppHelpTemplate = appHelpTemplate
app := cli.NewApp()
app.Name = "team-cymru"
app.Author = "blacktop"
app.Email = "https://github.com/blacktop"
app.Version = Version + ", BuildTime: " + BuildTime
app.Compiled, _ = time.Parse("20060102", BuildTime)
app.Usage = "Malice TeamCymru - Malware Hash Registry Plugin"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "post, p",
Usage: "POST results to Malice webhook",
EnvVar: "MALICE_ENDPOINT",
},
cli.BoolFlag{
Name: "proxy, x",
Usage: "proxy settings for Malice webhook endpoint",
EnvVar: "MALICE_PROXY",
},
cli.BoolFlag{
Name: "table, t",
Usage: "output as Markdown table",
},
}
app.ArgsUsage = "MD5/SHA1 hash of file"
app.Action = func(c *cli.Context) {
if c.Args().Present() {
ssReport := LookupHash(c.Args().First())
ss := TeamCymru{Results: ssReport}
if c.Bool("table") {
printMarkDownTable(ss)
} else {
ssJSON, err := json.Marshal(ss)
assert(err)
fmt.Println(string(ssJSON))
}
} else {
log.Fatal(fmt.Errorf("please supply a MD5/SHA1 hash to query"))
}
}
err := app.Run(os.Args)
assert(err)
}