Skip to content

Commit 1939d8b

Browse files
committed
better output
1 parent fef0fff commit 1939d8b

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

utils.go

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,40 @@ func Warning(err error) {
100100
}
101101
}
102102

103-
func printMatches(m []yara.MatchRule, err error) {
104-
if err == nil {
105-
if len(m) > 0 {
106-
for _, match := range m {
107-
log.Printf("- [%s] %s ", match.Namespace, match.Rule)
103+
// printMatches prints match results to the screen in a human readable way
104+
func printMatches(results map[string][]yara.MatchRule) {
105+
for filePath, matches := range results {
106+
log.Printf("%s:", filePath)
107+
if len(matches) > 0 {
108+
for _, match := range matches {
109+
log.Printf(" - [%s] %s ", match.Namespace, match.Rule)
108110
}
109111
} else {
110-
log.Print("no matches.")
112+
log.Print(" - no matches.")
111113
}
112-
} else {
113-
log.Printf("error: %s.", err)
114114
}
115115
}
116116

117+
// saveMatchesJSON saves match results to json file for later processing
118+
func saveMatchesJSON(results map[string][]yara.MatchRule) {
119+
outpath := "/tmp/yaya.json"
120+
121+
txt, err := json.Marshal(results)
122+
if err != nil {
123+
log.Panicf("Marshaling error: %s", err)
124+
}
125+
126+
f, err := os.Create(outpath)
127+
defer f.Close()
128+
if err != nil {
129+
fmt.Println(err)
130+
return
131+
}
132+
133+
f.Write(txt)
134+
log.Printf("json output written to %s", outpath)
135+
}
136+
117137
// usage prints help about the program
118138
func usage() {
119139
fmt.Print(""+

yaya.go

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,6 @@ type Ruleset struct {
3131
Rules []Rule
3232
}
3333

34-
// Rule is an individual YARA rule
35-
type Rule struct {
36-
gorm.Model
37-
Namespace string
38-
Path string
39-
Enabled bool `gorm:"default:true"`
40-
Ruleset Ruleset
41-
RulesetID uint
42-
}
43-
44-
func (rule *Rule) toggleEnabled() {
45-
db := openDB()
46-
defer db.Close()
47-
rule.Enabled = !rule.Enabled
48-
db.Save(&rule)
49-
}
50-
5134
func (ruleset *Ruleset) toggleEnabled() {
5235
db := openDB()
5336
defer db.Close()
@@ -65,8 +48,27 @@ func (ruleset *Ruleset) getStatus() string {
6548
return status
6649
}
6750

51+
// Rule is an individual YARA rule
52+
type Rule struct {
53+
gorm.Model
54+
Namespace string
55+
Path string
56+
Enabled bool `gorm:"default:true"`
57+
Ruleset Ruleset
58+
RulesetID uint
59+
}
60+
61+
func (rule *Rule) toggleEnabled() {
62+
db := openDB()
63+
defer db.Close()
64+
rule.Enabled = !rule.Enabled
65+
db.Save(&rule)
66+
}
67+
68+
// Collections
6869
var rulesets []Ruleset
6970
var rules []Rule
71+
var scanResults = map[string][]yara.MatchRule{}
7072

7173
// Paths
7274
var home, _ = os.UserHomeDir()
@@ -75,6 +77,9 @@ var rulesetsPath = path.Join(configPath, "rulsets")
7577
var dbPath = path.Join(configPath, "yaya.db")
7678

7779
func main() {
80+
// Make config directories if they don't exist
81+
os.MkdirAll(rulesetsPath, os.ModePerm)
82+
7883
if !(len(os.Args) >= 2) || os.Args[1] == "-h" {
7984
usage()
8085
}
@@ -304,17 +309,16 @@ func addRuleset(path string) {
304309
func runScan(scanPath string) {
305310
db := openDB()
306311
defer db.Close()
307-
var matches yara.MatchRules
308-
var scanPaths []string
312+
309313
filepath.Walk(scanPath, func(path string, info os.FileInfo, e error) error {
310314
if e != nil {
311315
return e
312316
}
313317

314318
// check if it is a regular file (not dir)
315319
if info.Mode().IsRegular() {
316-
317-
scanPaths = append(scanPaths, path)
320+
var m []yara.MatchRule
321+
scanResults[path] = m
318322
}
319323
return nil
320324
})
@@ -329,7 +333,7 @@ func runScan(scanPath string) {
329333

330334
db.Model(&ruleset).Where("enabled = ?", true).Related(&rules)
331335

332-
//fmt.Printf("Compiling %d rules\n", len(rules))
336+
log.Printf("Scanning with %s. Compiling %d rules\n", ruleset.Name, len(rules))
333337
for _, rule := range rules {
334338
f, err := os.Open(rule.Path)
335339
if err != nil {
@@ -346,12 +350,15 @@ func runScan(scanPath string) {
346350
if err != nil {
347351
log.Panicf("Failed to compile rules: %s", err)
348352
}
349-
for _, path := range scanPaths {
350-
log.Printf("Scanning file %s... ", path)
351-
matches, err = rules.ScanFile(path, 0, 0)
352-
printMatches(matches, err)
353+
for path, matches := range scanResults {
354+
results, err := rules.ScanFile(path, 0, 0)
355+
if err != nil {
356+
Warning(err)
357+
}
358+
scanResults[path] = append(matches, results...)
353359
}
354360

355361
}
356-
362+
printMatches(scanResults)
363+
saveMatchesJSON(scanResults)
357364
}

0 commit comments

Comments
 (0)