Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
noblogs-composer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Container Registry
Model registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Noblogs
noblogs-composer
Commits
755c43f4
Commit
755c43f4
authored
2 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Run mod_security logs through a JSON converter
parent
cd4e199a
No related branches found
No related tags found
1 merge request
!181
Rewrite mod_security output to structured logging
Pipeline
#38439
passed
2 years ago
Stage: build
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Dockerfile
+6
-0
6 additions, 0 deletions
Dockerfile
docker/conf/apache2/conf-available/modsecurity-custom.conf
+2
-0
2 additions, 0 deletions
docker/conf/apache2/conf-available/modsecurity-custom.conf
modsec_logger.go
+67
-0
67 additions, 0 deletions
modsec_logger.go
with
75 additions
and
0 deletions
Dockerfile
+
6
−
0
View file @
755c43f4
FROM
golang:1.19
AS
gobuild
COPY
modsec_logger.go /src/modsec_logger.go
WORKDIR
/src
RUN
go build
-tags
netgo
-o
modsec_logger modsec_logger.go
FROM
composer:2.2.9
as
build
ADD
. /build
...
...
@@ -15,6 +20,7 @@ COPY docker/wp-config.php /opt/noblogs/www/wp-config.php
COPY
docker/wp-cache-config.php /opt/noblogs/www/wp-content/wp-cache-config.php
COPY
docker/conf /tmp/conf
COPY
docker/build.sh /tmp/build.sh
COPY
--from=gobuild /src/modsec_logger /usr/local/bin/modsec_logger
RUN
/tmp/build.sh
&&
rm
/tmp/build.sh
...
...
This diff is collapsed.
Click to expand it.
docker/conf/apache2/conf-available/modsecurity-custom.conf
+
2
−
0
View file @
755c43f4
...
...
@@ -6,5 +6,7 @@
SecRuleEngine
Off
</
Location
>
ErrorLog
"|/usr/local/bin/modsec_logger"
</
IfModule
>
</
IfModule
>
This diff is collapsed.
Click to expand it.
modsec_logger.go
0 → 100644
+
67
−
0
View file @
755c43f4
// Tool to rewrite mod_security2 logs (very difficult to parse
// although they are in semi-structured format) to JSON.
//
package
main
import
(
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"regexp"
)
var
(
outerRx
=
regexp
.
MustCompile
(
`\[[^\]]+]`
)
innerRx
=
regexp
.
MustCompile
(
`\[([^ ]+) \"?(.*)\"\]$`
)
needle
=
[]
byte
(
"ModSecurity: "
)
)
func
parseModSec
(
w
io
.
Writer
,
line
[]
byte
)
bool
{
if
!
bytes
.
Contains
(
line
,
needle
)
{
return
false
}
fields
:=
make
(
map
[
string
]
interface
{})
var
tags
[]
string
for
_
,
inner
:=
range
outerRx
.
FindAll
(
line
,
-
1
)
{
for
_
,
matches
:=
range
innerRx
.
FindAllSubmatch
(
inner
,
-
1
)
{
field
:=
string
(
matches
[
1
])
value
:=
string
(
matches
[
2
])
switch
field
{
case
"tag"
:
tags
=
append
(
tags
,
value
)
case
"client"
,
"unique_id"
,
"file"
,
"line"
:
// Suppress these tags.
default
:
fields
[
field
]
=
value
}
}
}
if
len
(
fields
)
==
0
{
return
false
}
if
len
(
tags
)
>
0
{
fields
[
"tag"
]
=
tags
}
data
,
_
:=
json
.
Marshal
(
fields
)
fmt
.
Fprintf
(
w
,
"@cee:{
\"
modsec
\"
:%s}
\n
"
,
data
)
return
true
}
func
main
()
{
outw
:=
bufio
.
NewWriter
(
os
.
Stdout
)
defer
outw
.
Flush
()
scanner
:=
bufio
.
NewScanner
(
os
.
Stdin
)
for
scanner
.
Scan
()
{
line
:=
scanner
.
Bytes
()
if
!
parseModSec
(
outw
,
line
)
{
outw
.
Write
(
line
)
io
.
WriteString
(
outw
,
"
\n
"
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment