utils.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package wafacquisition
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/crowdsecurity/coraza/v3/experimental"
  6. "github.com/crowdsecurity/crowdsec/pkg/types"
  7. "github.com/crowdsecurity/crowdsec/pkg/waf"
  8. "github.com/prometheus/client_golang/prometheus"
  9. )
  10. func EventFromRequest(r waf.ParsedRequest) (types.Event, error) {
  11. evt := types.Event{}
  12. //we might want to change this based on in-band vs out-of-band ?
  13. evt.Type = types.LOG
  14. evt.ExpectMode = types.LIVE
  15. //def needs fixing
  16. evt.Stage = "s00-raw"
  17. evt.Process = true
  18. evt.Parsed = map[string]string{
  19. "source_ip": r.ClientIP,
  20. "target_host": r.Host,
  21. "target_uri": r.URI,
  22. "method": r.Method,
  23. "req_uuid": r.Tx.ID(),
  24. "source": "coraza",
  25. //TBD:
  26. //http_status
  27. //user_agent
  28. }
  29. evt.Line = types.Line{
  30. Time: time.Now(),
  31. //should we add some info like listen addr/port/path ?
  32. Labels: map[string]string{"type": "coraza-waf"},
  33. Process: true,
  34. Module: "waf",
  35. Src: "waf",
  36. Raw: "dummy-waf-data", //we discard empty Line.Raw items :)
  37. }
  38. evt.Waap = []map[string]interface{}{}
  39. return evt, nil
  40. }
  41. func LogWaapEvent(evt *types.Event) {
  42. /*log.WithFields(log.Fields{
  43. "module": "waf",
  44. "source": evt.Parsed["source_ip"],
  45. "target_uri": evt.Parsed["target_uri"],
  46. }).Infof("%s triggered %d rules [%+v]", evt.Parsed["source_ip"], len(evt.Waap), evt.Waap.GetRuleIDs())*/
  47. //log.Infof("%s", evt.Waap)
  48. }
  49. func (r *WafRunner) AccumulateTxToEvent(tx experimental.FullTransaction, kind string, evt *types.Event) error {
  50. //log.Infof("tx addr: %p", tx)
  51. if tx.IsInterrupted() {
  52. r.logger.Infof("interrupted() = %t", tx.IsInterrupted())
  53. r.logger.Infof("interrupted.action = %s", tx.Interruption().Action)
  54. if evt.Meta == nil {
  55. evt.Meta = map[string]string{}
  56. }
  57. evt.Parsed["interrupted"] = "true"
  58. evt.Parsed["action"] = tx.Interruption().Action
  59. //log.Infof("action: %s", tx.Interruption().Action)
  60. evt.Meta["waap_interrupted"] = "1"
  61. evt.Meta["waap_action"] = tx.Interruption().Action
  62. }
  63. r.logger.Infof("variables addr in AccumulateTxToEvent: %p", tx.Variables())
  64. //log.Infof("variables: %s", spew.Sdump(tx.Variables()))
  65. //log.Infof("tx variables: %+v", tx.Collection(variables.TX))
  66. //log.Infof("TX %s", spew.Sdump(tx.MatchedRules()))
  67. for _, rule := range tx.MatchedRules() {
  68. if rule.Message() == "" {
  69. continue
  70. }
  71. WafRuleHits.With(prometheus.Labels{"rule_id": fmt.Sprintf("%d", rule.Rule().ID()), "type": kind}).Inc()
  72. corazaRule := map[string]interface{}{
  73. "id": rule.Rule().ID(),
  74. "uri": evt.Parsed["uri"],
  75. "rule_type": kind,
  76. "method": evt.Parsed["method"],
  77. "disruptive": rule.Disruptive(),
  78. "tags": rule.Rule().Tags(),
  79. "file": rule.Rule().File(),
  80. "file_line": rule.Rule().Line(),
  81. "revision": rule.Rule().Revision(),
  82. "secmark": rule.Rule().SecMark(),
  83. "accuracy": rule.Rule().Accuracy(),
  84. "msg": rule.Message(),
  85. "severity": rule.Rule().Severity().String(),
  86. }
  87. evt.Waap = append(evt.Waap, corazaRule)
  88. }
  89. return nil
  90. }