To improve your Logstash filter to collect as much useful data as possible from your ModSecurity logs coming through Filebeat, you want to:
Correct current Grok patterns.
Parse the entire ModSecurity log line (with all its key/value pairs).
Parse the multiline messages properly.
Capture all relevant fields: file, line, id, msg, severity, tags, hostname, URI, unique_id, etc.
Optionally parse the RESPONSE_BODY value (truncated) in data field.
Issues with current config:
Multiline filter pattern ^\[.*\] does not match your log line (which starts with ---LefEh04f---H--).
Grok pattern is syntactically incorrect (\[%DATA:component}\] -> missing { and %DATA should be %{DATA}).
Grok pattern is tailored for logs starting with [, but your example does not.
No parsing of the detailed fields inside the message.
Proposed improved Logstash configuration
Multiline filter
The ModSecurity log example is a single line that contains a huge message, but it could sometimes be multiline. You need to adjust multiline pattern properly.
From your sample log, lines don't start with timestamps in square brackets. They start with a custom marker like ---LefEh04f---H--.
If Filebeat is shipping multiline logs correctly, you don't need Logstash multiline filter, but if not, better to fix that at Filebeat multiline.
Let's assume multiline is already handled (recommended). Otherwise, your multiline pattern should be something like matching lines starting with ---:
conf
filter {
if [message] =~ /^---.*---H--/ {
# this line appears to be the start of a modsec log entry
# you can use multiline here if multiline is needed
}
}
I would recommend handling multiline in Filebeat for best results.
Grok parsing detailed ModSecurity log
ModSecurity logs have many key=value pairs embedded in square brackets:
You can extract these fields with a combination of grok and kv filters.
Example approach:
Extract the main message before the bracketed key=values (e.g. "ModSecurity: Access denied with code 403 (phase 4). Matched ... variable RESPONSE_BODY (Value: ...)" ).
Extract key=value pairs in the message inside [] using kv filter with custom field split.
Complete example filter:
conf
filter {
# If multiline is handled in filebeat, do not process here.
# Extract the ID marker at start (---LefEh04f---H--), optionally capture if needed
if [message] =~ /^---.*---H--/ {
grok {
match => {
"message" => "^%{DATA:id_marker}ModSecurity: %{GREEDYDATA:modsec_msg}(\s+\[file \"%{DATA:file}\"])?(\s+\[line \"%{NUMBER:line}\"])?(\s+\[id \"%{NUMBER:id}\"])?(\s+\[rev \"%{DATA:rev}\"])?(\s+\[msg \"%{DATA:msg}\"])?(\s+\[data \"%{DATA:data}\"])?(\s+\[severity \"%{DATA:severity}\"])?(\s+\[ver \"%{DATA:ver}\"])?(\s+\[maturity \"%{DATA:maturity}\"])?(\s+\[accuracy \"%{DATA:accuracy}\"])?(?:\s+\[tag \"%{DATA:tag}\"])?(?:\s+\[hostname \"%{IPORHOST:hostname}\"])?(?:\s+\[uri \"%{DATA:uri}\"])?(?:\s+\[unique_id \"%{DATA:unique_id}\"])?(?:\s+\[ref \"%{DATA:ref}\"])?"
}
# If you want to ignore if it doesn't match:
# tag_on_failure => []
}
}
# Some fields (like 'tag') may appear multiple times, and kv can be used for general parsing instead.
# Alternatively, to parse the entire message for key=value pairs enclosed in brackets:
kv {
source => "message"
field_split => "]["
value_split => " "
trim_key => " []"
trim_value => "\""
include_brackets => false
target => "modsec_fields"
# This will parse occurrences like [id "180003"] to modsec_fields.id => 180003
}
# Clean up the keys parsed by kv if nested in modsec_fields:
# you can also copy them to root, or access them by [modsec_fields][id], etc.
}
Explanation
The grok extracts the initial parts of the message and some of the main fields explicitly.
The kv filter processes all bracketed key=value elements automatically, capturing whatever keys are present.
Using field_split => "][" handles separated bracketed elements like [id "180003"][msg "appinfo leak"].
value_split => " " is a bit tricky here because key and value are separated by a space (e.g. id "180003"), but this will work if you trim quotes.
Further improvement
For your specific example, you might want a more flexible grok, combined with kv like this:
conf
filter {
# Remove leading id marker from message for clarity
mutate {
gsub => ["message", "^---.*---H--\s*", ""]
}
# Extract main message part before the first bracket
grok {
match => {
"message" => "^(?<modsec_main_msg>ModSecurity: .*?\.)\s*(?<bracketed>.*)$"
}
tag_on_failure => []
}
# Parse key=value pairs inside brackets
kv {
source => "bracketed"
field_split => "]["
value_split => " "
trim_key => " []"
trim_value => "\""
target => "modsec_fields"
}
# Optionally, copy fields from modsec_fields root for convenience
ruby {
code => "
if event.get('modsec_fields')
event.get('modsec_fields').each { |k,v| event.set(k,v) }
event.remove('modsec_fields')
end
"
}
}