-
Notifications
You must be signed in to change notification settings - Fork 27
/
yaya.go
444 lines (386 loc) · 10.1 KB
/
yaya.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// YAYA: Yet Another Yara Automaton
// Automatically curate open source yara rules and run scans
// Author: Cooper Quintin - @cooperq - [email protected]
package main
import (
"bufio"
"fmt"
"log"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"io/ioutil"
"github.com/go-git/go-git/v5"
"github.com/hillu/go-yara/v4"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
// Ruleset is a record for a yara ruleset
type Ruleset struct {
gorm.Model
Name string `gorm:"unique_index"`
URL string
Description string
Enabled bool `gorm:"default:true"`
Rules []Rule
}
func (ruleset *Ruleset) toggleEnabled() {
db := openDB()
defer db.Close()
ruleset.Enabled = !ruleset.Enabled
db.Save(&ruleset)
}
func (ruleset *Ruleset) getStatus() string {
var status string
if ruleset.Enabled {
status = "enabled"
} else {
status = "disabled"
}
return status
}
// Rule is an individual YARA rule
type Rule struct {
gorm.Model
Namespace string
Path string
Enabled bool `gorm:"default:true"`
Ruleset Ruleset
RulesetID uint
}
func (rule *Rule) toggleEnabled() {
db := openDB()
defer db.Close()
rule.Enabled = !rule.Enabled
db.Save(&rule)
}
// Collections
var rulesets []Ruleset
var rules []Rule
var scanResults = map[string][]yara.MatchRule{}
// Paths
var home, _ = os.UserHomeDir()
var configPath = path.Join(home, ".yaya")
var rulesetsPath = path.Join(configPath, "rulsets")
var dbPath = path.Join(configPath, "yaya.db")
func main() {
// Make config directories if they don't exist
os.MkdirAll(rulesetsPath, os.ModePerm)
if !(len(os.Args) >= 2) || os.Args[1] == "-h" {
usage()
}
db := openDB()
defer db.Close()
// Migrate the schema
db.AutoMigrate(&Rule{})
db.AutoMigrate(&Ruleset{})
command := os.Args[1]
var path string = ""
if len(os.Args) > 2 {
path = os.Args[2]
}
initYaya(db)
switch command {
case "update":
updateRules()
case "edit":
editRules()
case "add":
if path == "" {
log.Fatalln("You must specify a ruleset path or github url to add.")
}
addRuleset(path)
case "scan":
if path == "" {
log.Fatalln("You must specify a path to scan.")
}
runScan(path)
case "export":
if path == "" {
log.Fatalln("You must specify an output path.")
}
exportRules(path)
case "exportcompiled":
if path == "" {
log.Fatalln("You must specify an output path.")
}
exportRulesCompiled(path)
default:
fmt.Println("Command not recognized")
usage()
}
}
//initYara populates the ruleset database with repos from the awesomelist
func initYaya(db *gorm.DB) {
var count int
db.Table("rulesets").Count(&count)
if count > 1 {
return
}
fmt.Println("Running YAYA for the first time. Gathering initial rulesets.")
installDefaultRules()
updateRules()
os.Exit(0)
}
// updateRules checks git repostitories for any new rules that have been added
func updateRules() {
log.Println("Updating YARA Rules...")
pullRulesets()
findRules()
}
func loadRulesets(rulesets *[]Ruleset) {
db := openDB()
defer db.Close()
db.Where("enabled = ?", true).Find(&rulesets)
}
func loadAllRulesets(rulesets *[]Ruleset) {
db := openDB()
defer db.Close()
db.Find(&rulesets)
}
func pullRulesets() {
loadRulesets(&rulesets)
for _, ruleset := range rulesets {
pullRuleset(&ruleset)
}
}
func pullRuleset(ruleset *Ruleset) {
rulesetPath := path.Join(rulesetsPath, ruleset.Name)
pathExists, _ := Exists(rulesetPath)
if !pathExists {
log.Printf("git clone %q", ruleset.URL)
_, err := git.PlainClone(rulesetPath, false, &git.CloneOptions{
URL: ruleset.URL,
})
if err != nil {
log.Println(err)
}
} else {
log.Printf("git pull %s", ruleset.Name)
// We instantiate a new repository targeting the given path (the .git folder)
r, err := git.PlainOpen(rulesetPath)
Warning(err)
// Get the working directory for the repository
w, err := r.Worktree()
Warning(err)
// Pull the latest changes from the origin remote and merge into the current branch
err = w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil && !(strings.Contains(err.Error(), "already")) {
Warning(err)
}
}
}
func findRules() {
db := openDB()
defer db.Close()
loadRulesets(&rulesets)
for _, ruleset := range rulesets {
updateRulesetRules(&ruleset, db)
}
}
func updateRulesetRules(ruleset *Ruleset, db *gorm.DB) {
rulesetPath := path.Join(rulesetsPath, ruleset.Name)
pathExists, _ := Exists(rulesetPath)
if !ruleset.Enabled {
return
}
log.Printf("loading %q located at %s", ruleset.Name, rulesetPath)
if !pathExists {
Warning(fmt.Errorf("the ruleset path %s doesn't exist, disabling for now", rulesetPath))
ruleset.Enabled = false
db.Save(&ruleset)
return
}
// scan ruleset path for *.yar[a]?$
filepath.Walk(rulesetPath, func(path string, info os.FileInfo, e error) error {
if e != nil {
return e
}
// check if it is a regular file (not dir)
if info.Mode().IsRegular() {
matched, _ := filepath.Match(`*.yar*`, info.Name())
if matched {
// create struct Rule and append to ruleset
r := Rule{Path: path, RulesetID: ruleset.ID}
record := db.FirstOrCreate(&r, r)
rulename := strings.TrimSuffix(info.Name(), filepath.Ext(info.Name()))
r.Namespace = fmt.Sprintf("%s:%s-%d", ruleset.Name, rulename, r.ID)
db.Save(&r)
// validate yara rule
if r.Enabled {
c, _ := yara.NewCompiler()
f, err := os.Open(r.Path)
if err != nil {
log.Printf("Could not open rule file %s: %s\n", r.Path, err)
return nil
}
err = c.AddFile(f, r.Namespace)
f.Close()
if err != nil {
log.Printf("Could not parse rule file %s: %s", r.Path, err)
r.Enabled = false
db.Save(&r)
}
}
db.Model(&ruleset).Association("Rules").Append(record)
}
}
return nil
})
// DB update ruleset
db.Save(&ruleset)
}
// editRules presents a UI allowing the user to ban certain rulesets or individual rules
func editRules() {
loadAllRulesets(&rulesets)
printRulesets(rulesets)
fmt.Print("Enter ruleset indexes (space seperated) to toggle: ")
db := openDB()
defer db.Close()
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.TrimSuffix(text, "\n")
input := strings.Split(text, " ")
for _, next := range input {
var ruleset Ruleset
idx, err := strconv.Atoi(next)
if err != nil {
log.Panicf("Couldn't parse input: %s", err)
}
if db.First(&ruleset, idx).Error != nil {
fmt.Printf("Ruleset %d is not in the database\n", idx)
continue
}
ruleset.toggleEnabled()
fmt.Printf("Ruleset %d \"%s\" is now %s\n", ruleset.ID, ruleset.Name, ruleset.getStatus())
}
return
}
// addRuleset allows the user to add a ruleset from either a git repository or local file path
func addRuleset(path string) {
db := openDB()
defer db.Close()
fmt.Println("Adding YARA Rules from ", path)
match, _ := regexp.MatchString(`\.git$`, path)
if !match {
// Not a successfull match so lets continue to the next line
log.Panicln("not a git repository")
}
s := strings.TrimSuffix(path, ".git")
name := s[strings.LastIndex(s, "/")+1:]
description := "Custom yara rules"
ruleset := Ruleset{Name: name, URL: path, Description: description, Enabled: true}
fmt.Printf("creating ruleset %+v\n", ruleset)
// Create or update ruleset in db
db.Create(&ruleset)
pullRuleset(&ruleset)
updateRulesetRules(&ruleset, db)
}
// runScan Scan a path recursively with every rule in the database
func runScan(scanPath string) {
db := openDB()
defer db.Close()
filepath.Walk(scanPath, func(path string, info os.FileInfo, e error) error {
if e != nil {
return e
}
// check if it is a regular file (not dir)
if info.Mode().IsRegular() {
var m []yara.MatchRule
scanResults[path] = m
}
return nil
})
db.Where("enabled = ?", true).Find(&rulesets)
for _, ruleset := range rulesets {
c, err := yara.NewCompiler()
if err != nil {
log.Fatalf("Failed to initialize YARA compiler: %s", err)
}
db.Model(&ruleset).Where("enabled = ?", true).Related(&rules)
log.Printf("Scanning with %s. Compiling %d rules\n", ruleset.Name, len(rules))
for _, rule := range rules {
f, err := os.Open(rule.Path)
if err != nil {
log.Printf("Could not open rule file %s: %s\n", rule.Path, err)
break
}
err = c.AddFile(f, rule.Namespace)
f.Close()
if err != nil {
log.Printf("Could not parse rule file %s: %s", rule.Path, err)
break
}
}
rules, err := c.GetRules()
if err != nil {
log.Panicf("Failed to compile rules: %s", err)
}
for path, matches := range scanResults {
var results yara.MatchRules
err := rules.ScanFile(path, 0, 0, &results)
if err != nil {
Warning(err)
}
scanResults[path] = append(matches, results...)
}
}
printMatches(scanResults)
saveMatchesJSON(scanResults)
}
// Export rules in plaintext instead of compiled
func exportRules(outputPath string) {
db := openDB()
defer db.Close()
db.Where("enabled = ?", true).Find(&rulesets)
outFile, err := os.Create(outputPath)
if err != nil {
log.Printf("Could not open rule file %s: %s\n", outputPath, err)
}
defer outFile.Close()
for _, ruleset := range rulesets {
db.Model(&ruleset).Where("enabled = ?", true).Related(&rules)
for _, rule := range rules {
dat, err := ioutil.ReadFile(rule.Path)
if err != nil {
log.Printf("Could not open rule file %s: %s\n", rule.Path, err)
return
}
outFile.Write(dat)
outFile.WriteString("\n")
outFile.Sync()
}
}
}
func exportRulesCompiled(outputPath string) {
db := openDB()
defer db.Close()
db.Where("enabled = ?", true).Find(&rulesets)
c, err := yara.NewCompiler()
if err != nil {
log.Fatalf("Failed to initialize YARA compiler: %s", err)
}
for _, ruleset := range rulesets {
db.Model(&ruleset).Where("enabled = ?", true).Related(&rules)
for _, rule := range rules {
f, err := os.Open(rule.Path)
if err != nil {
log.Printf("Could not open rule file %s: %s\n", rule.Path, err)
}
err = c.AddFile(f, rule.Namespace)
f.Close()
if err != nil {
log.Printf("Could not parse rule file %s: %s", rule.Path, err)
break
}
}
}
mainRule, err := c.GetRules()
if err != nil {
log.Panicf("Failed to compile rules: %s", err)
}
mainRule.Save(outputPath)
}