@@ -5,39 +5,74 @@ import (
55 "log"
66 "os"
77
8+ "github.com/caibirdme/yql"
89 "github.com/dustin-decker/threatseer/server/event"
10+ "github.com/fatih/structs"
911 yaml "gopkg.in/yaml.v2"
1012)
1113
12- type dynamicRule struct {
13- eventType string `yaml:"event_type"`
14- description string `yaml:"description"`
15- actions []string `yaml:"actions"`
16- query string `yaml:"query"`
17- score int `yaml:"score"`
14+ type DynamicRules []struct {
15+ Name string `yaml:"name"`
16+ Description string `yaml:"description"`
17+ EventType string `yaml:"event_type"`
18+ Query string `yaml:"query"`
19+ Actions []string `yaml:"actions"`
20+ IndicatorType string `yaml:"indicator_type"`
21+ Score int `yaml:"score"`
1822}
1923
20- type DynamicRulesEngine struct {
24+ // RulesEngine stores engine state
25+ type RulesEngine struct {
2126 Out chan event.Event
22- dynamicRules [] dynamicRule
27+ DynamicRules DynamicRules
2328}
2429
2530// Run initiates the engine on the pipeline
26- func (engine * DynamicRulesEngine ) Run (in chan event.Event ) {
31+ func (engine * RulesEngine ) Run (in chan event.Event ) {
2732 for {
33+ // incoming event from the pipeline
2834 e := <- in
29- // log.Print(e)
35+
36+ // convert struct to map[string]interface{}
37+ evnt := structs .Map (e )
38+
39+ for _ , rule := range engine .DynamicRules {
40+ if len (rule .Query ) > 0 {
41+ result , err := yql .Match (rule .Query , evnt )
42+ if err != nil {
43+ if err .Error () == "interface conversion: interface is nil, not antlr.ParserRuleContext" {
44+ log .Print ("incorrect syntax for dynamic engine rule, got: " , rule .Query )
45+ } else {
46+ log .Print ("dynamic engine got error while testing rule: " , err )
47+ }
48+ }
49+ if result {
50+ e .Indicators = append (
51+ e .Indicators ,
52+ event.Indicator {
53+ Engine : "dynamic" ,
54+ IndicatorType : rule .IndicatorType ,
55+ Description : rule .Description ,
56+ Score : rule .Score ,
57+ RuleName : rule .Name ,
58+ },
59+ )
60+ }
61+ }
62+ }
63+
64+ // make event available to the next pipeline engine
3065 engine .Out <- e
3166 }
3267}
3368
3469// NewDynamicRulesEngine returns engine with configs loaded
35- func NewDynamicRulesEngine () DynamicRulesEngine {
36- var e DynamicRulesEngine
70+ func NewDynamicRulesEngine () RulesEngine {
71+ var e RulesEngine
3772
3873 // load risky_process.yaml information
3974 filename := "config/dynamic_rules.yaml"
40- var dr [] dynamicRule
75+ var dr DynamicRules
4176 if _ , err := os .Stat (filename ); os .IsNotExist (err ) {
4277 log .Printf ("%s does not exist, not loading any data for that check" , filename )
4378 } else {
@@ -50,7 +85,7 @@ func NewDynamicRulesEngine() DynamicRulesEngine {
5085 log .Fatalf ("could not parse %s, got %s" , filename , err .Error ())
5186 }
5287 }
53- e .dynamicRules = dr
88+ e .DynamicRules = dr
5489 e .Out = make (chan event.Event , 0 )
5590
5691 return e
0 commit comments