-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubenricher-crs.go
139 lines (116 loc) · 3.05 KB
/
subenricher-crs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"fmt"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/corazawaf/coraza/v2"
"github.com/telkomindonesia/httpmsg-enricher/ecs"
ecsx "github.com/telkomindonesia/httpmsg-enricher/ecs/custom"
)
var _ subEnricher = &crsSubEnricher{}
type crsSubEnricher struct {
tx *coraza.Transaction
}
func (erc *crsSubEnricher) Close() error {
erc.tx.ProcessLogging()
return erc.tx.Clean()
}
func (erc *crsSubEnricher) requestBodyWriter() io.WriteCloser {
return nopCloser{erc.tx.RequestBodyBuffer}
}
func (erc *crsSubEnricher) processRequest(req *http.Request) (err error) {
tx := erc.tx
client, port := "", 0
spl := strings.Split(req.RemoteAddr, ":")
if len(spl) > 0 {
client = strings.Join(spl[0:len(spl)-1], "")
}
if len(spl) > 1 {
port, _ = strconv.Atoi(spl[len(spl)-1])
}
tx.ProcessConnection(client, port, "", 0)
tx.ProcessURI(req.URL.String(), req.Method, req.Proto)
for k, vr := range req.Header {
for _, v := range vr {
tx.AddRequestHeader(k, v)
}
}
if req.Host != "" {
tx.AddRequestHeader("Host", req.Host)
}
if len(req.TransferEncoding) > 0 {
tx.AddRequestHeader("Transfer-Encoding", strings.Join(req.TransferEncoding, ","))
}
tx.ProcessRequestHeaders()
if _, err := tx.ProcessRequestBody(); err != nil {
return fmt.Errorf("error processing request: %w", err)
}
return
}
func (erc *crsSubEnricher) responseBodyWriter() io.WriteCloser {
return nopCloser{erc.tx.ResponseBodyBuffer}
}
func (erc *crsSubEnricher) processResponse(res *http.Response) (err error) {
tx := erc.tx
for k, v := range res.Header {
tx.AddResponseHeader(k, strings.Join(v, ","))
}
if len(res.TransferEncoding) > 0 {
tx.AddRequestHeader("Transfer-Encoding", strings.Join(res.TransferEncoding, ","))
}
tx.ProcessResponseHeaders(res.StatusCode, res.Proto)
if _, err := tx.ProcessResponseBody(); err != nil {
return fmt.Errorf("error processing response body: %w", err)
}
return
}
func (erc *crsSubEnricher) enrich(doc *ecsx.Document, msg *httpRecordedMessage) (err error) {
doc.CRS = &ecsx.CRS{
Scores: *ecsx.NewScores(erc.tx),
}
if doc.Threat == nil {
doc.Threat = &ecs.Threat{}
}
for _, rule := range erc.tx.MatchedRules {
idc := ecs.ThreatIndicator{
Description: rule.ErrorLog(0),
IP: net.ParseIP(rule.ClientIPAddress),
Provider: rule.Rule.Version,
Type: "network-traffic",
}
match := &ecs.ThreatEnrichmentMatch{
Type: "indicator_match_rule",
Atomic: Truncate(rule.MatchedData.Value, 200),
}
atk := false
for _, tag := range rule.Rule.Tags {
atk = atk || strings.HasPrefix(tag, "attack-")
pl := strings.TrimPrefix(tag, "paranoia-level/")
if pl == "" {
continue
}
i, _ := strconv.ParseInt(pl, 10, 8)
switch i {
case 1:
idc.Confidence = "High"
case 2:
idc.Confidence = "Medium"
case 3, 4:
idc.Confidence = "Low"
default:
idc.Confidence = "Not Specified"
}
}
if !atk {
continue
}
doc.Threat.Enrichments = append(doc.Threat.Enrichments, ecs.ThreatEnrichments{
Indicator: idc,
Match: match,
})
}
return nil
}