utils.go 3.6 KB

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