-
Notifications
You must be signed in to change notification settings - Fork 3
/
transform-alert-message.awk
executable file
·296 lines (277 loc) · 9.7 KB
/
transform-alert-message.awk
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/gawk -f
#
# Script to transform ModSecurity alert messages into JSON format.
# Copyright 2017, Christian Folini
#
# [2017-03-29 11:29:28.238891] [-:error] 127.0.0.1:58152 WNt@eH8AAQEAABoE82MAAAAA [client 127.0.0.1] ModSecurity: Warning. Matched phrase "etc/passwd" at ARGS:test. [file "/home/dune73/data/git/crs-official/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"] [line "448"] [id "932160"] [rev "1"] [msg "Remote Command Execution: Unix Shell Code Found"] [data "Matched Data: etc/passwd found within ARGS:test: /etc/passwd"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "8"] [tag "application-multi"] [tag "language-shell"] [tag "platform-unix"] [tag "attack-rce"] [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"] [hostname "localhost"] [uri "/index.html"] [unique_id "WNt@eH8AAQEAABoE82MAAAAA"]
# [client 127.0.0.1] ModSecurity: Warning. Matched phrase "etc/passwd" at ARGS:test. [file "/home/dune73/data/git/crs-official/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"] [line "448"] [id "932160"] [rev "1"] [msg "Remote Command Execution: Unix Shell Code Found"] [data "Matched Data: etc/passwd found within ARGS:test: /etc/passwd"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "8"] [tag "application-multi"] [tag "language-shell"] [tag "platform-unix"] [tag "attack-rce"] [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"] [tag "WASCTC/WASC-31"] [tag "OWASP_TOP_10/A1"] [tag "PCI/6.5.2"] [hostname "localhost"] [uri "/index.html"] [unique_id "WNt@eH8AAQEAABoE82MAAAAA"]
# [client 127.0.0.1]
# ModSecurity: Warning.
# Matched phrase "etc/passwd" at ARGS:test.
# [file "/home/dune73/data/git/crs-official/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf"]
# [line "448"]
# [id "932160"]
# [rev "1"]
# [msg "Remote Command Execution: Unix Shell Code Found"]
# [data "Matched Data: etc/passwd found within ARGS:test: /etc/passwd"]
# [severity "CRITICAL"]
# [ver "OWASP_CRS/3.0.0"]
# [maturity "1"]
# [accuracy "8"]
# [tag "application-multi"]
# [tag "language-shell"]
# [tag "platform-unix"]
# [tag "attack-rce"]
# [tag "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION"]
# [tag "WASCTC/WASC-31"]
# [tag "OWASP_TOP_10/A1"]
# [tag "PCI/6.5.2"]
# [hostname "localhost"]
# [uri "/index.html"]
# [unique_id "WNt@eH8AAQEAABoE82MAAAAA"]
# {
# "modsec-alert": {
# "client": "127.0.0.1",
# "description": "Warning. Matched phrase \"etc/passwd\" at ARGS:test",
# "file": "/home/dune73/data/git/crs-official/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf",
# "line": "448",
# "id": "932160",
# "rev": "1",
# "msg": "Remote Command Execution: Unix Shell Code Found",
# "data": "Matched Data: etc/passwd found within ARGS:test: /etc/passwd",
# "severity": "CRITICAL",
# "ver": "OWASP_CRS/3.0.0",
# "maturity": "1",
# "accuracy": "8",
# "tags": ["application-multi", "language-shell", "platform-unix", "attack-rce", "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION", "WASCTC/WASC-31", "OWASP_TOP_10/A1", "PCI/6.5.2"],
# "hostname": "localhost",
# "uri": "/index.html",
# "unique_id": "WNt@eH8AAQEAABoE82MAAAAA"
# }
# }
# {
# "modsec-alert": {
# "description": "Warning. Matched phrase \"etc/passwd\" at ARGS:test",
# "id": "932160",
# "client": "127.0.0.1",
# "hostname": "localhost",
# "uri": "/index.html",
# "unique_id": "WNt@eH8AAQEAABoE82MAAAAA"
# "msg": "Remote Command Execution: Unix Shell Code Found",
# "data": "Matched Data: etc/passwd found within ARGS:test: /etc/passwd",
# "severity": "CRITICAL",
# "tags": ["application-multi", "language-shell", "platform-unix", "attack-rce", "OWASP_CRS/WEB_ATTACK/COMMAND_INJECTION", "WASCTC/WASC-31", "OWASP_TOP_10/A1", "PCI/6.5.2"],
# "file": "/home/dune73/data/git/crs-official/rules/REQUEST-932-APPLICATION-ATTACK-RCE.conf",
# "line": "448",
# "rev": "1",
# "ver": "OWASP_CRS/3.0.0",
# "maturity": "1",
# "accuracy": "8",
# }
# }
BEGIN {
verbose = 0
}
{
n_tags = 0 # num of elements in tags array
Description = ""
json_out = ""
# This substition is done for PCRE error messages. For some reason the different sections of the
# error message, are not separated with a space between the closing and opening square brackets.
# Adding a space in those cases allows `awk` to properly split the different sections
gsub(/\]\[/,"] [");
for (i = 1; i <= NF; i++) {
# read values
switch (FN) {
case "client": Client = substr($i, 1, length($i) - 1); FN = ""; break
# description
case "file": File = substr($i, 2, length($i) - 3); FN = ""; break
case "line": Line = substr($i, 2, length($i) - 3); FN = ""; break
case "id": Id = substr($i, 2, length($i) - 3); FN = ""; break
case "rev": Rev = substr($i, 2, length($i) - 3); FN = ""; break
case "severity": Severity = substr($i, 2, length($i) - 3); FN = ""; break
case "ver": Ver = substr($i, 2, length($i) - 3); FN = ""; break
case "maturity": Maturity = substr($i, 2, length($i) - 3); FN = ""; break
case "accuracy": Accuracy = substr($i, 2, length($i) - 3); FN = ""; break
# tag
case "hostname": Hostname = substr($i, 2, length($i) - 3); FN = ""; break
case "uri": Uri = substr($i, 2, length($i) - 3); FN = ""; break
# This needs to remove 1 char more since it's the last entry and ends in "], instead of "]
case "unique_id": UniqueID = substr($i, 2, length($i) - 4); FN = ""; break
case "msg":
if ( substr($i, 1, 1) == "\"" ) {
Msg = substr($i, 2, length($i) - 1)
break
}
else {
if ( substr($i, length($i) - 1, 2) == "\"]" ) {
Msg = Msg " " substr($i, 1, length($i) - 2)
FN = ""
break
}
else {
Msg = Msg " " $i
break
}
}
case "data":
if ( substr($i, 1, 1) == "\"" ) {
Data = substr($i, 2, length($i) - 1)
if ( substr($i, length($i) - 1, 2) == "\"]" ) {
# New string, but it is already finished
FN = ""
}
break
}
else {
if ( substr($i, length($i) - 1, 2) == "\"]" ) {
# data string finished
Data = Data " " substr($i, 1, length($i) - 2)
FN = ""
break
}
else {
# data string that will continue
Data = Data " " $i
break
}
}
case "tag":
if ( substr($i , length($i) -1, 2) == "\"]" ) {
n_tags = n_tags + 1
if ( substr($i, 1, 1) == "\"") {
Tags[n_tags] = substr($i, 2, length($i) - 3)
}
else {
Tags[n_tags] = Tags[n_tags] " " substr($i, 1, length($i) -2)
}
FN= ""
break
}
else {
if ( substr($i, 1, 1) == "\"") {
Tags[n_tags+1] = substr($i, 2, length($i))
} else {
Tags[n_tags+1] = $i
}
}
default:
# examine keys
if ( i <= 2 ) {
# Timestamp
# FIXME: Make other timestamp formats also accessible
if ( i == 1 ) {
Timestamp = substr($i, 2, length($i) - 1)
}
else {
Timestamp = Timestamp " " substr($i, 1, length($i) - 1)
}
}
else {
switch ($i) {
case "[client": FN = "client"; break
case "[file": FN = "file"; break
case "[line": FN = "line"; break
case "[id": FN = "id"; break
case "[rev": FN = "rev"; break
case "[msg": FN = "msg"; break
case "[data": FN = "data"; break
case "[severity": FN = "severity"; break
case "[ver": FN = "ver"; break
case "[maturity": FN = "maturity"; break
case "[accuracy": FN = "accuracy"; break
case "[tag": FN = "tag"; break
case "[hostname": FN = "hostname"; break
case "[uri": FN = "uri"; break
case "[unique_id": FN = "unique_id"; break
}
if ( FN == "" ) {
# Description: Everything outside of square bracket pairs. FIXME: not done yet
if ( Description == "" && $i == "ModSecurity:" ) {
Description = $i
}
else {
if ( length(Description) != 0 ) {
Description = Description " " $i
}
}
}
}
}
}
# Escaping
gsub(/"/, "\\\"", Description)
gsub(/"/, "\\\"", Msg)
gsub(/"/, "\\\"", Data)
gsub(/"/, "\\\"", Uri)
if ( verbose ) {
print "Timestamp", Timestamp
print "Description", Description
print "Client", Client
print "File", File
print "Line", Line
print "Id", Id
print "Rev", Rev
print "Msg", Msg
print "Data", Data
print "Severity", Severity
print "Tags", get_tags(Tags)
print "Ver", Ver
print "Maturity", Maturity
print "Accuracy", Accuracy
print "Hostname", Hostname
print "Uri", Uri
print "UniqueID", UniqueID
}
if ( Id != "" ) {
json_out = json_out "{ "
json_out = json_out "\"modsec-alert\": {"
json_out = json_out "\"description\": \"" Description "\","
json_out = json_out "\"id\": " Id ","
json_out = json_out "\"client\": \"" Client "\","
json_out = json_out "\"hostname\": \"" Hostname "\","
json_out = json_out "\"uri\": \"" Uri "\","
json_out = json_out "\"uniqueID\": \"" UniqueID "\","
json_out = json_out "\"msg\": \"" Msg "\","
json_out = json_out "\"data\": \"" Data "\","
json_out = json_out "\"severity\": \"" Severity "\","
json_out = json_out "\"tags\": " get_tags(Tags) ","
json_out = json_out "\"file\": \"" File "\","
json_out = json_out "\"line\": " Line ","
json_out = json_out "\"rev\": \"" Rev "\","
json_out = json_out "\"ver\": \"" Ver "\","
json_out = json_out "\"maturity\": \"" Maturity "\","
json_out = json_out "\"rule_template\": \"# ModSec Rule Exclusion: " Id " : " Msg " (severity: " Maturity " " Severity ")\""
json_out = json_out "} }"
n_tags = 0
Description = ""
Id = ""
Client = ""
Hostname = ""
Uri = ""
UniqueID = ""
Msg = ""
Data = ""
Severity = ""
File = ""
Line = ""
Rev = ""
Ver = ""
Maturity = ""
print json_out "\n"
} else {
print $0 "\n"
}
}
END {
}
function get_tags (Tags) {
mytags = ""
for (i in Tags) {
if ( length(mytags) > 0 ) {
mytags = mytags ", "
}
mytags = mytags "\"" Tags[i] "\""
}
return "[" mytags "]"
}