-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnsrl.go
411 lines (353 loc) · 9.32 KB
/
nsrl.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"bytes"
"encoding/binary"
"encoding/csv"
"encoding/json"
"fmt"
"html/template"
"io"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/fatih/structs"
"github.com/gorilla/mux"
"github.com/malice-plugins/pkgs/database"
"github.com/malice-plugins/pkgs/database/elasticsearch"
"github.com/malice-plugins/pkgs/utils"
"github.com/parnurzeal/gorequest"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/willf/bloom"
)
var (
// Version stores the plugin's version
Version string
// BuildTime stores the plugin's build time
BuildTime string
// ErrorRate stores the bloomfilter desired error-rate
ErrorRate string
// HashType is the type of hash to use to build the bloomfilter
HashType string
hash string
// es is the elasticsearch database object
es elasticsearch.Database
)
const (
// NSRL fields
sha1 = 0
md5 = 1
crc32 = 2
fileName = 3
fileSize = 4
productCode = 5
opSystemCode = 6
specialCode = 7
)
const (
name = "nsrl"
category = "intel"
)
type pluginResults struct {
ID string `json:"id" gorethink:"id,omitempty"`
Data ResultsData `json:"nsrl" gorethink:"nsrl"`
}
// Nsrl json object
type Nsrl struct {
Results ResultsData `json:"nsrl"`
}
// ResultsData json object
type ResultsData struct {
Found bool `json:"found"`
Hash string `json:"hash"`
MarkDown string `json:"markdown,omitempty" structs:"markdown,omitempty"`
}
func assert(err error) {
if err != nil {
log.WithFields(log.Fields{
"plugin": name,
"category": category,
"hash": hash,
}).Fatal(err)
}
}
func generateMarkDownTable(n Nsrl) string {
var tplOut bytes.Buffer
t := template.Must(template.New("nsrl").Parse(tpl))
err := t.Execute(&tplOut, n)
if err != nil {
log.Println("executing template:", err)
}
return tplOut.String()
}
func lineCounter(r io.Reader) (uint64, error) {
buf := make([]byte, 32*1024)
var count uint64
lineSep := []byte{'\n'}
for {
c, err := r.Read(buf)
count += uint64(bytes.Count(buf[:c], lineSep))
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
}
}
func getNSRLFieldFromHashType() int {
switch strings.ToLower(HashType) {
case "sha1":
return 0
case "md5":
return 1
case "crc32":
return 2
case "filename":
return 3
case "filesize":
return 4
case "productcode":
return 5
case "opsystemcode":
return 6
case "specialcode":
return 7
default:
log.Fatal(fmt.Errorf("hash type %s not supported", HashType))
}
return -1
}
// build bloomfilter from NSRL database
func buildFilter() {
var err error
nsrlField := getNSRLFieldFromHashType()
// open NSRL database
nsrlDB, err := os.Open("NSRLFile.txt")
assert(err)
defer nsrlDB.Close()
// count lines in NSRL database
lines, err := lineCounter(nsrlDB)
assert(err)
log.Debugf("Number of lines in NSRLFile.txt: %d", lines)
// write line count to file LINECOUNT
buf := new(bytes.Buffer)
assert(binary.Write(buf, binary.LittleEndian, lines))
assert(ioutil.WriteFile("LINECOUNT", buf.Bytes(), 0644))
// Create new bloomfilter with size = number of lines in NSRL database
erate, err := strconv.ParseFloat(ErrorRate, 64)
assert(err)
filter := bloom.NewWithEstimates(uint(lines), erate)
// jump back to the begining of the file
_, err = nsrlDB.Seek(0, io.SeekStart)
assert(err)
log.Debug("Loading NSRL database into bloomfilter")
reader := csv.NewReader(nsrlDB)
// strip off csv header
_, _ = reader.Read()
for {
record, err := reader.Read()
if err == io.EOF {
break
}
assert(err)
// log.Debug(record)
filter.Add([]byte(record[nsrlField]))
}
bloomFile, err := os.Create("nsrl.bloom")
assert(err)
defer bloomFile.Close()
log.Debug("Writing bloomfilter to disk")
_, err = filter.WriteTo(bloomFile)
assert(err)
}
// lookUp queries the NSRL bloomfilter for a hash
func lookUp(hash string, timeout int) ResultsData {
var lines uint64
nsrlResults := ResultsData{}
// read line count from file LINECOUNT
lineCount, err := ioutil.ReadFile("LINECOUNT")
assert(err)
buf := bytes.NewReader(lineCount)
assert(binary.Read(buf, binary.LittleEndian, &lines))
log.Debugf("Number of lines in NSRLFile.txt: %d", lines)
// Create new bloomfilter with size = number of lines in NSRL database
erate, err := strconv.ParseFloat(ErrorRate, 64)
assert(err)
filter := bloom.NewWithEstimates(uint(lines), erate)
// load NSRL bloomfilter from file
f, err := os.Open("nsrl.bloom")
assert(err)
_, err = filter.ReadFrom(f)
assert(err)
// test of existance of hash in bloomfilter
nsrlResults.Found = filter.TestString(hash)
return nsrlResults
}
func webService() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/lookup/{hash}", webLookUp)
log.Info("web service listening on port :3993")
log.Fatal(http.ListenAndServe(":3993", router))
}
func webLookUp(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hash := vars["hash"]
if utils.StringInSlice(HashType, []string{"md5", "sha1"}) {
hashType, _ := utils.GetHashType(hash)
if !strings.EqualFold(hashType, strings.ToUpper(HashType)) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Please supply a proper %s hash to query", strings.ToUpper(HashType))
return
}
}
nsrl := Nsrl{Results: lookUp(strings.ToUpper(hash), 10)}
nsrl.Results.Hash = hash
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if nsrl.Results.Found {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
if err := json.NewEncoder(w).Encode(nsrl); err != nil {
panic(err)
}
}
func printStatus(resp gorequest.Response, body string, errs []error) {
fmt.Println(body)
}
func main() {
cli.AppHelpTemplate = utils.AppHelpTemplate
app := cli.NewApp()
app.Name = "nsrl"
app.Author = "blacktop"
app.Email = "https://github.com/blacktop"
app.Version = Version + ", BuildTime: " + BuildTime
app.Compiled, _ = time.Parse("20060102", BuildTime)
app.Usage = "Malice NSRL Plugin"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "verbose, V",
Usage: "verbose output",
},
}
app.Commands = []cli.Command{
{
Name: "web",
Aliases: []string{"w"},
Usage: "Create a NSRL lookup web service",
Action: func(c *cli.Context) error {
webService()
return nil
},
},
{
Name: "build",
Aliases: []string{"b"},
Usage: "Build bloomfilter from NSRL database",
Action: func(c *cli.Context) error {
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
buildFilter()
return nil
},
},
{
Name: "lookup",
Aliases: []string{"l"},
Usage: "Query NSRL for hash",
ArgsUsage: fmt.Sprintf("%s to query NSRL with", strings.ToUpper(HashType)),
Flags: []cli.Flag{
cli.StringFlag{
Name: "elasticsearch",
Value: "",
Usage: "elasticsearch url for Malice to store results",
EnvVar: "MALICE_ELASTICSEARCH_URL",
Destination: &es.URL,
},
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.IntFlag{
Name: "timeout",
Value: 10,
Usage: "malice plugin timeout (in seconds)",
EnvVar: "MALICE_TIMEOUT",
},
cli.BoolFlag{
Name: "table, t",
Usage: "output as Markdown table",
},
},
Action: func(c *cli.Context) error {
if c.Args().Present() {
hash = strings.ToUpper(c.Args().First())
if utils.StringInSlice(HashType, []string{"md5", "sha1"}) {
hashType, _ := utils.GetHashType(hash)
if !strings.EqualFold(hashType, strings.ToUpper(HashType)) {
log.Fatal(fmt.Errorf("please supply a valid %s hash to query NSRL with", strings.ToUpper(HashType)))
}
}
if c.GlobalBool("verbose") {
log.SetLevel(log.DebugLevel)
}
nsrl := Nsrl{Results: lookUp(hash, c.GlobalInt("timeout"))}
nsrl.Results.Hash = hash
nsrl.Results.MarkDown = generateMarkDownTable(nsrl)
// upsert into Database
if len(c.String("elasticsearch")) > 0 {
err := es.Init()
if err != nil {
return errors.Wrap(err, "failed to initalize elasticsearch")
}
err = es.StorePluginResults(database.PluginResults{
ID: utils.Getopt("MALICE_SCANID", hash),
Name: name,
Category: category,
Data: structs.Map(nsrl.Results),
})
if err != nil {
return errors.Wrapf(err, "failed to index malice/%s results", name)
}
}
if c.Bool("table") {
fmt.Println(nsrl.Results.MarkDown)
} else {
nsrl.Results.MarkDown = ""
nsrlJSON, err := json.Marshal(nsrl)
assert(err)
if c.Bool("post") {
request := gorequest.New()
if c.Bool("proxy") {
request = gorequest.New().Proxy(os.Getenv("MALICE_PROXY"))
}
request.Post(os.Getenv("MALICE_ENDPOINT")).
Set("X-Malice-ID", utils.Getopt("MALICE_SCANID", hash)).
Send(string(nsrlJSON)).
End(printStatus)
return nil
}
fmt.Println(string(nsrlJSON))
}
} else {
log.Fatal(fmt.Errorf("please supply a %s hash to query NSRL with", strings.ToUpper(HashType)))
}
return nil
},
},
}
err := app.Run(os.Args)
assert(err)
}