node.go 16 KB

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