node.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package parser
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "github.com/antonmedv/expr"
  7. "github.com/logrusorgru/grokky"
  8. yaml "gopkg.in/yaml.v2"
  9. "github.com/antonmedv/expr/vm"
  10. "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
  11. "github.com/crowdsecurity/crowdsec/pkg/types"
  12. "github.com/davecgh/go-spew/spew"
  13. "github.com/prometheus/client_golang/prometheus"
  14. "github.com/sirupsen/logrus"
  15. log "github.com/sirupsen/logrus"
  16. )
  17. type Node struct {
  18. FormatVersion string `yaml:"format"`
  19. //Enable config + runtime debug of node via config o/
  20. Debug bool `yaml:"debug,omitempty"`
  21. //If enabled, the node (and its child) will report their own statistics
  22. Profiling bool `yaml:"profiling,omitempty"`
  23. //Name, author, description and reference(s) for parser pattern
  24. Name string `yaml:"name,omitempty"`
  25. Author string `yaml:"author,omitempty"`
  26. Description string `yaml:"description,omitempty"`
  27. Rerferences []string `yaml:"references,omitempty"`
  28. //if debug is present in the node, keep its specific Logger in runtime structure
  29. Logger *log.Entry `yaml:"-"`
  30. //This is mostly a hack to make writting less repetive.
  31. //relying on stage, we know which field to parse, and we
  32. //can as well promote log to next stage on success
  33. Stage string `yaml:"stage,omitempty"`
  34. //OnSuccess allows to tag a node to be able to move log to next stage on success
  35. OnSuccess string `yaml:"onsuccess,omitempty"`
  36. rn string //this is only for us in debug, a random generated name for each node
  37. //Filter is executed at runtime (with current log line as context)
  38. //and must succeed or node is exited
  39. Filter string `yaml:"filter,omitempty"`
  40. RunTimeFilter *vm.Program `yaml:"-" json:"-"` //the actual compiled filter
  41. ExprDebugger *exprhelpers.ExprDebugger `yaml:"-" json:"-"` //used to debug expression by printing the content of each variable of the expression
  42. //If node has leafs, execute all of them until one asks for a 'break'
  43. LeavesNodes []Node `yaml:"nodes,omitempty"`
  44. //Flag used to describe when to 'break' or return an 'error'
  45. EnrichFunctions []EnricherCtx
  46. /* If the node is actually a leaf, it can have : grok, enrich, statics */
  47. //pattern_syntax are named grok patterns that are re-utilised over several grok patterns
  48. SubGroks yaml.MapSlice `yaml:"pattern_syntax,omitempty"`
  49. //Holds a grok pattern
  50. Grok types.GrokPattern `yaml:"grok,omitempty"`
  51. //Statics can be present in any type of node and is executed last
  52. Statics []types.ExtraField `yaml:"statics,omitempty"`
  53. //Whitelists
  54. Whitelist types.Whitelist `yaml:"whitelist,omitempty"`
  55. Data []*types.DataSource `yaml:"data,omitempty"`
  56. }
  57. func (n *Node) validate(pctx *UnixParserCtx, ectx []EnricherCtx) error {
  58. //stage is being set automagically
  59. if n.Stage == "" {
  60. return fmt.Errorf("stage needs to be an existing stage")
  61. }
  62. /* "" behaves like continue */
  63. if n.OnSuccess != "continue" && n.OnSuccess != "next_stage" && n.OnSuccess != "" {
  64. return fmt.Errorf("onsuccess '%s' not continue,next_stage", n.OnSuccess)
  65. }
  66. if n.Filter != "" && n.RunTimeFilter == nil {
  67. return fmt.Errorf("non-empty filter '%s' was not compiled", n.Filter)
  68. }
  69. if n.Grok.RunTimeRegexp != nil || n.Grok.TargetField != "" {
  70. if n.Grok.TargetField == "" {
  71. return fmt.Errorf("grok's apply_on can't be empty")
  72. }
  73. if n.Grok.RegexpName == "" && n.Grok.RegexpValue == "" {
  74. return fmt.Errorf("grok needs 'pattern' or 'name'")
  75. }
  76. }
  77. for idx, static := range n.Statics {
  78. if static.Method != "" {
  79. if static.ExpValue == "" {
  80. return fmt.Errorf("static %d : when method is set, expression must be present", idx)
  81. }
  82. method_found := false
  83. for _, enricherCtx := range ectx {
  84. if _, ok := enricherCtx.Funcs[static.Method]; ok && enricherCtx.initiated {
  85. method_found = true
  86. break
  87. }
  88. }
  89. if !method_found {
  90. return fmt.Errorf("the method '%s' doesn't exist or the plugin has not been initialized", static.Method)
  91. }
  92. } else {
  93. if static.Meta == "" && static.Parsed == "" && static.TargetByName == "" {
  94. return fmt.Errorf("static %d : at least one of meta/event/target must be set", idx)
  95. }
  96. if static.Value == "" && static.RunTimeValue == nil {
  97. return fmt.Errorf("static %d value or expression must be set", idx)
  98. }
  99. }
  100. }
  101. return nil
  102. }
  103. func (n *Node) process(p *types.Event, ctx UnixParserCtx) (bool, error) {
  104. var NodeState bool
  105. clog := n.Logger
  106. clog.Tracef("Event entering node")
  107. if n.RunTimeFilter != nil {
  108. //Evaluate node's filter
  109. output, err := expr.Run(n.RunTimeFilter, exprhelpers.GetExprEnv(map[string]interface{}{"evt": p}))
  110. if err != nil {
  111. clog.Warningf("failed to run filter : %v", err)
  112. clog.Debugf("Event leaving node : ko")
  113. return false, nil
  114. }
  115. switch out := output.(type) {
  116. case bool:
  117. if n.Debug {
  118. n.ExprDebugger.Run(clog, out, exprhelpers.GetExprEnv(map[string]interface{}{"evt": p}))
  119. }
  120. if !out {
  121. clog.Debugf("Event leaving node : ko (failed filter)")
  122. return false, nil
  123. }
  124. default:
  125. clog.Warningf("Expr '%s' returned non-bool, abort : %T", n.Filter, output)
  126. clog.Debugf("Event leaving node : ko")
  127. return false, nil
  128. }
  129. NodeState = true
  130. } else {
  131. clog.Tracef("Node has not filter, enter")
  132. NodeState = true
  133. }
  134. if n.Name != "" {
  135. NodesHits.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  136. }
  137. isWhitelisted := false
  138. hasWhitelist := false
  139. var srcs []net.IP
  140. /*overflow and log don't hold the source ip in the same field, should be changed */
  141. /* perform whitelist checks for ips, cidr accordingly */
  142. /* TODO move whitelist elsewhere */
  143. if p.Type == types.LOG {
  144. if _, ok := p.Meta["source_ip"]; ok {
  145. srcs = append(srcs, net.ParseIP(p.Meta["source_ip"]))
  146. }
  147. } else if p.Type == types.OVFLW {
  148. for k, _ := range p.Overflow.Sources {
  149. srcs = append(srcs, net.ParseIP(k))
  150. }
  151. }
  152. for _, src := range srcs {
  153. if isWhitelisted {
  154. break
  155. }
  156. for _, v := range n.Whitelist.B_Ips {
  157. if v.Equal(src) {
  158. clog.Debugf("Event from [%s] is whitelisted by Ips !", src)
  159. isWhitelisted = true
  160. } else {
  161. clog.Tracef("whitelist: %s is not eq [%s]", src, v)
  162. }
  163. hasWhitelist = true
  164. }
  165. for _, v := range n.Whitelist.B_Cidrs {
  166. if v.Contains(src) {
  167. clog.Debugf("Event from [%s] is whitelisted by Cidrs !", src)
  168. isWhitelisted = true
  169. } else {
  170. clog.Tracef("whitelist: %s not in [%s]", src, v)
  171. }
  172. hasWhitelist = true
  173. }
  174. }
  175. if isWhitelisted {
  176. p.Whitelisted = true
  177. }
  178. /* run whitelist expression tests anyway */
  179. for eidx, e := range n.Whitelist.B_Exprs {
  180. output, err := expr.Run(e.Filter, exprhelpers.GetExprEnv(map[string]interface{}{"evt": p}))
  181. if err != nil {
  182. clog.Warningf("failed to run whitelist expr : %v", err)
  183. clog.Debugf("Event leaving node : ko")
  184. return false, nil
  185. }
  186. switch out := output.(type) {
  187. case bool:
  188. if n.Debug {
  189. e.ExprDebugger.Run(clog, out, exprhelpers.GetExprEnv(map[string]interface{}{"evt": p}))
  190. }
  191. if out {
  192. clog.Infof("Event is whitelisted by Expr !")
  193. p.Whitelisted = true
  194. isWhitelisted = true
  195. }
  196. hasWhitelist = true
  197. default:
  198. log.Errorf("unexpected type %t (%v) while running '%s'", output, output, n.Whitelist.Exprs[eidx])
  199. }
  200. }
  201. if isWhitelisted {
  202. p.WhiteListReason = n.Whitelist.Reason
  203. /*huglily wipe the ban order if the event is whitelisted and it's an overflow */
  204. if p.Type == types.OVFLW { /*don't do this at home kids */
  205. ips := []string{}
  206. for _, src := range srcs {
  207. ips = append(ips, src.String())
  208. }
  209. clog.Infof("Ban for %s whitelisted, reason [%s]", strings.Join(ips, ","), n.Whitelist.Reason)
  210. p.Overflow.Whitelisted = true
  211. }
  212. }
  213. //Process grok if present, should be exclusive with nodes :)
  214. gstr := ""
  215. if n.Grok.RunTimeRegexp != nil {
  216. clog.Tracef("Processing grok pattern : %s : %p", n.Grok.RegexpName, n.Grok.RunTimeRegexp)
  217. //for unparsed, parsed etc. set sensible defaults to reduce user hassle
  218. if n.Grok.TargetField == "" {
  219. clog.Fatalf("not default field and no specified on stage '%s'", n.Stage)
  220. } else {
  221. //it's a hack to avoid using real reflect
  222. if n.Grok.TargetField == "Line.Raw" {
  223. gstr = p.Line.Raw
  224. } else if val, ok := p.Parsed[n.Grok.TargetField]; ok {
  225. gstr = val
  226. } else {
  227. clog.Debugf("(%s) target field '%s' doesn't exist in %v", n.rn, n.Grok.TargetField, p.Parsed)
  228. NodeState = false
  229. //return false, nil
  230. }
  231. }
  232. var groklabel string
  233. if n.Grok.RegexpName == "" {
  234. groklabel = fmt.Sprintf("%5.5s...", n.Grok.RegexpValue)
  235. } else {
  236. groklabel = n.Grok.RegexpName
  237. }
  238. grok := n.Grok.RunTimeRegexp.Parse(gstr)
  239. if len(grok) > 0 {
  240. clog.Debugf("+ Grok '%s' returned %d entries to merge in Parsed", groklabel, len(grok))
  241. //We managed to grok stuff, merged into parse
  242. for k, v := range grok {
  243. clog.Debugf("\t.Parsed['%s'] = '%s'", k, v)
  244. p.Parsed[k] = v
  245. }
  246. // if the grok succeed, process associated statics
  247. err := n.ProcessStatics(n.Grok.Statics, p)
  248. if err != nil {
  249. clog.Fatalf("(%s) Failed to process statics : %v", n.rn, err)
  250. }
  251. } else {
  252. //grok failed, node failed
  253. clog.Debugf("+ Grok '%s' didn't return data on '%s'", groklabel, gstr)
  254. //clog.Tracef("on '%s'", gstr)
  255. NodeState = false
  256. }
  257. } else {
  258. clog.Tracef("! No grok pattern : %p", n.Grok.RunTimeRegexp)
  259. }
  260. //Iterate on leafs
  261. if len(n.LeavesNodes) > 0 {
  262. for _, leaf := range n.LeavesNodes {
  263. //clog.Debugf("Processing sub-node %d/%d : %s", idx, len(n.SuccessNodes), leaf.rn)
  264. ret, err := leaf.process(p, ctx)
  265. if err != nil {
  266. clog.Tracef("\tNode (%s) failed : %v", leaf.rn, err)
  267. clog.Debugf("Event leaving node : ko")
  268. return false, err
  269. }
  270. clog.Tracef("\tsub-node (%s) ret : %v (strategy:%s)", leaf.rn, ret, n.OnSuccess)
  271. if ret {
  272. NodeState = true
  273. /* if child is successful, stop processing */
  274. if n.OnSuccess == "next_stage" {
  275. clog.Debugf("child is success, OnSuccess=next_stage, skip")
  276. break
  277. }
  278. } else {
  279. NodeState = false
  280. }
  281. }
  282. }
  283. /*todo : check if a node made the state change ?*/
  284. /* should the childs inherit the on_success behaviour */
  285. clog.Tracef("State after nodes : %v", NodeState)
  286. //grok or leafs failed, don't process statics
  287. if !NodeState {
  288. if n.Name != "" {
  289. NodesHitsKo.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  290. }
  291. clog.Debugf("Event leaving node : ko")
  292. return NodeState, nil
  293. }
  294. if n.Name != "" {
  295. NodesHitsOk.With(prometheus.Labels{"source": p.Line.Src, "type": p.Line.Module, "name": n.Name}).Inc()
  296. }
  297. /*
  298. Please kill me. this is to apply statics when the node *has* whitelists that successfully matched the node.
  299. */
  300. if hasWhitelist && isWhitelisted && len(n.Statics) > 0 || len(n.Statics) > 0 && !hasWhitelist {
  301. clog.Debugf("+ Processing %d statics", len(n.Statics))
  302. // if all else is good in whitelist, process node's statics
  303. err := n.ProcessStatics(n.Statics, p)
  304. if err != nil {
  305. clog.Fatalf("Failed to process statics : %v", err)
  306. }
  307. } else {
  308. clog.Tracef("! No node statics")
  309. }
  310. if NodeState {
  311. clog.Debugf("Event leaving node : ok")
  312. log.Tracef("node is successful, check strategy")
  313. if n.OnSuccess == "next_stage" {
  314. idx := stageidx(p.Stage, ctx.Stages)
  315. //we're at the last stage
  316. if idx+1 == len(ctx.Stages) {
  317. clog.Debugf("node reached the last stage : %s", p.Stage)
  318. } else {
  319. clog.Debugf("move Event from stage %s to %s", p.Stage, ctx.Stages[idx+1])
  320. p.Stage = ctx.Stages[idx+1]
  321. }
  322. } else {
  323. clog.Tracef("no strategy on success (%s), continue !", n.OnSuccess)
  324. }
  325. } else {
  326. clog.Debugf("Event leaving node : ko")
  327. }
  328. clog.Tracef("Node successful, continue")
  329. return NodeState, nil
  330. }
  331. func (n *Node) compile(pctx *UnixParserCtx, ectx []EnricherCtx) error {
  332. var err error
  333. var valid bool
  334. valid = false
  335. dumpr := spew.ConfigState{MaxDepth: 1, DisablePointerAddresses: true}
  336. n.rn = seed.Generate()
  337. n.EnrichFunctions = ectx
  338. log.Tracef("compile, node is %s", n.Stage)
  339. /* if the node has debugging enabled, create a specific logger with debug
  340. that will be used only for processing this node ;) */
  341. if n.Debug {
  342. var clog = logrus.New()
  343. if err := types.ConfigureLogger(clog); err != nil {
  344. log.Fatalf("While creating bucket-specific logger : %s", err)
  345. }
  346. clog.SetLevel(log.DebugLevel)
  347. n.Logger = clog.WithFields(log.Fields{
  348. "id": n.rn,
  349. })
  350. n.Logger.Infof("%s has debug enabled", n.Name)
  351. } else {
  352. /* else bind it to the default one (might find something more elegant here)*/
  353. n.Logger = log.WithFields(log.Fields{
  354. "id": n.rn,
  355. })
  356. }
  357. /* display info about top-level nodes, they should be the only one with explicit stage name ?*/
  358. n.Logger = n.Logger.WithFields(log.Fields{"stage": n.Stage, "name": n.Name})
  359. n.Logger.Tracef("Compiling : %s", dumpr.Sdump(n))
  360. //compile filter if present
  361. if n.Filter != "" {
  362. n.RunTimeFilter, err = expr.Compile(n.Filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  363. if err != nil {
  364. return fmt.Errorf("compilation of '%s' failed: %v", n.Filter, err)
  365. }
  366. if n.Debug {
  367. n.ExprDebugger, err = exprhelpers.NewDebugger(n.Filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  368. if err != nil {
  369. log.Errorf("unable to build debug filter for '%s' : %s", n.Filter, err)
  370. }
  371. }
  372. }
  373. /* handle pattern_syntax and groks */
  374. for _, pattern := range n.SubGroks {
  375. n.Logger.Tracef("Adding subpattern '%s' : '%s'", pattern.Key, pattern.Value)
  376. if err := pctx.Grok.Add(pattern.Key.(string), pattern.Value.(string)); err != nil {
  377. if err == grokky.ErrAlreadyExist {
  378. n.Logger.Warningf("grok '%s' already registred", pattern.Key)
  379. continue
  380. }
  381. n.Logger.Errorf("Unable to compile subpattern %s : %v", pattern.Key, err)
  382. return err
  383. }
  384. }
  385. /* load grok by name or compile in-place */
  386. if n.Grok.RegexpName != "" {
  387. n.Logger.Tracef("+ Regexp Compilation '%s'", n.Grok.RegexpName)
  388. n.Grok.RunTimeRegexp, err = pctx.Grok.Get(n.Grok.RegexpName)
  389. if err != nil {
  390. return fmt.Errorf("Unable to find grok '%s' : %v", n.Grok.RegexpName, err)
  391. }
  392. if n.Grok.RunTimeRegexp == nil {
  393. return fmt.Errorf("Empty grok '%s'", n.Grok.RegexpName)
  394. }
  395. n.Logger.Tracef("%s regexp: %s", n.Grok.RegexpName, n.Grok.RunTimeRegexp.Regexp.String())
  396. valid = true
  397. } else if n.Grok.RegexpValue != "" {
  398. if strings.HasSuffix(n.Grok.RegexpValue, "\n") {
  399. n.Logger.Debugf("Beware, pattern ends with \\n : '%s'", n.Grok.RegexpValue)
  400. }
  401. n.Grok.RunTimeRegexp, err = pctx.Grok.Compile(n.Grok.RegexpValue)
  402. if err != nil {
  403. return fmt.Errorf("Failed to compile grok '%s': %v\n", n.Grok.RegexpValue, err)
  404. }
  405. if n.Grok.RunTimeRegexp == nil {
  406. // We shouldn't be here because compilation succeeded, so regexp shouldn't be nil
  407. return fmt.Errorf("Grok compilation failure: %s", n.Grok.RegexpValue)
  408. }
  409. n.Logger.Tracef("%s regexp : %s", n.Grok.RegexpValue, n.Grok.RunTimeRegexp.Regexp.String())
  410. valid = true
  411. }
  412. /* load grok statics */
  413. if len(n.Grok.Statics) > 0 {
  414. //compile expr statics if present
  415. for idx := range n.Grok.Statics {
  416. if n.Grok.Statics[idx].ExpValue != "" {
  417. n.Grok.Statics[idx].RunTimeValue, err = expr.Compile(n.Grok.Statics[idx].ExpValue,
  418. expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  419. if err != nil {
  420. return err
  421. }
  422. }
  423. }
  424. valid = true
  425. }
  426. /* compile leafs if present */
  427. if len(n.LeavesNodes) > 0 {
  428. for idx := range n.LeavesNodes {
  429. if n.LeavesNodes[idx].Name == "" {
  430. n.LeavesNodes[idx].Name = fmt.Sprintf("child-%s", n.Name)
  431. }
  432. /*propagate debug/stats to child nodes*/
  433. if !n.LeavesNodes[idx].Debug && n.Debug {
  434. n.LeavesNodes[idx].Debug = true
  435. }
  436. if !n.LeavesNodes[idx].Profiling && n.Profiling {
  437. n.LeavesNodes[idx].Profiling = true
  438. }
  439. n.LeavesNodes[idx].Stage = n.Stage
  440. err = n.LeavesNodes[idx].compile(pctx, ectx)
  441. if err != nil {
  442. return err
  443. }
  444. }
  445. valid = true
  446. }
  447. /* load statics if present */
  448. for idx := range n.Statics {
  449. if n.Statics[idx].ExpValue != "" {
  450. n.Statics[idx].RunTimeValue, err = expr.Compile(n.Statics[idx].ExpValue, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  451. if err != nil {
  452. n.Logger.Errorf("Statics Compilation failed %v.", err)
  453. return err
  454. }
  455. }
  456. valid = true
  457. }
  458. /* compile whitelists if present */
  459. for _, v := range n.Whitelist.Ips {
  460. n.Whitelist.B_Ips = append(n.Whitelist.B_Ips, net.ParseIP(v))
  461. n.Logger.Debugf("adding ip %s to whitelists", net.ParseIP(v))
  462. valid = true
  463. }
  464. for _, v := range n.Whitelist.Cidrs {
  465. _, tnet, err := net.ParseCIDR(v)
  466. if err != nil {
  467. n.Logger.Fatalf("Unable to parse cidr whitelist '%s' : %v.", v, err)
  468. }
  469. n.Whitelist.B_Cidrs = append(n.Whitelist.B_Cidrs, tnet)
  470. n.Logger.Debugf("adding cidr %s to whitelists", tnet)
  471. valid = true
  472. }
  473. for _, filter := range n.Whitelist.Exprs {
  474. expression := &types.ExprWhitelist{}
  475. expression.Filter, err = expr.Compile(filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  476. if err != nil {
  477. n.Logger.Fatalf("Unable to compile whitelist expression '%s' : %v.", filter, err)
  478. }
  479. expression.ExprDebugger, err = exprhelpers.NewDebugger(filter, expr.Env(exprhelpers.GetExprEnv(map[string]interface{}{"evt": &types.Event{}})))
  480. if err != nil {
  481. log.Errorf("unable to build debug filter for '%s' : %s", filter, err)
  482. }
  483. n.Whitelist.B_Exprs = append(n.Whitelist.B_Exprs, expression)
  484. n.Logger.Debugf("adding expression %s to whitelists", filter)
  485. valid = true
  486. }
  487. if !valid {
  488. /* node is empty, error force return */
  489. n.Logger.Infof("Node is empty: %s", spew.Sdump(n))
  490. n.Stage = ""
  491. }
  492. if err := n.validate(pctx, ectx); err != nil {
  493. return err
  494. //n.logger.Fatalf("Node is invalid : %s", err)
  495. }
  496. return nil
  497. }