To send the ModSecurity audit log file (/var/log/modsecurity/audit.log) which is in JSON format to a Logstash TCP input on a Linux machine, you can use various methods. The goal is to:
- Tail or continuously read the audit log file.
- Send each JSON log entry to Logstash via TCP.
Here's a step-by-step example using filebeat and alternatively using netcat or a simple Python script.
Option 1: Using Filebeat (recommended for production)
Filebeat is a lightweight log shipper from Elastic that supports reading JSON logs and sending them to Logstash over TCP.
Steps:
- Install Filebeat
On Debian/Ubuntu:
sudo apt-get update
sudo apt-get install filebeat
On RHEL/CentOS:
sudo yum install filebeat
- Configure Filebeat
Edit the Filebeat config file (usually /etc/filebeat/filebeat.yml):
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/modsecurity/audit.log
json.keys_under_root: true
json.add_error_key: true
json.message_key: message # if your JSON logs have a "message" field; else omit
output.logstash:
hosts: ["logstash-hostname:5044"] # Change to your Logstash TCP receiver host and port
Note: Replace logstash-hostname and 5044 with your Logstash TCP receiver address and port.
Filebeat can parse JSON logs natively (json.keys_under_root: true) which flattens the JSON fields to top-level fields.
- Start & enable Filebeat
sudo systemctl enable filebeat
sudo systemctl start filebeat
- Configure Logstash input
In your Logstash pipeline config (/etc/logstash/conf.d/your-pipeline.conf), define TCP input:
input {
beats {
port => 5044
}
}
filter {
# Optional: Add filters if needed
}
output {
stdout { codec => rubydebug }
# or send to Elasticsearch or other outputs
}
Option 2: Using nc (netcat) to send logs via TCP (simple method)
If you just want to forward contents over TCP, you can do:
tail -F /var/log/modsecurity/audit.log | nc logstash-hostname 5000
Where:
tail -F reads appended lines from the file continuously.
nc (netcat) sends them to your logstash TCP port (e.g., 5000).
On the Logstash side, define TCP input:
input {
tcp {
port => 5000
codec => json_lines
}
}
output {
stdout { codec => rubydebug }
}
Note: This assumes your JSON entries are line-delimited (one JSON object per line). If the audit log contains multi-line JSON entries, this approach will most likely not work correctly.
Option 3: Use a simple Python script to parse and send JSON over TCP
If the audit log entries are multi-line JSON objects, you need a parser to:
- Detect each JSON entry
- Send them as a single JSON message over TCP
Here is a simple Python script example:
import socket
import time
import json
LOGFILE = '/var/log/modsecurity/audit.log'
LOGSTASH_HOST = 'logstash-hostname'
LOGSTASH_PORT = 5000
def tail_f(filename):
with open(filename, 'r') as f:
f.seek(0, 2) # Go to end of file
while True:
line = f.readline()
if not line:
time.sleep(0.1)
continue
yield line
def main():
buffer = ''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((LOGSTASH_HOST, LOGSTASH_PORT))
for line in tail_f(LOGFILE):
buffer += line
try:
# Try to parse buffer as JSON
record = json.loads(buffer)
# If successful, send to logstash and clear buffer
sock.sendall((json.dumps(record) + "\n").encode())
buffer = ''
except json.JSONDecodeError:
# Not a complete JSON yet; keep buffering
continue
if __name__ == '__main__':
main()
- This script reads the file continuously.
- Buffers lines until the JSON object is complete.
- Sends JSON strings to Logstash over TCP, one per line.
- On the Logstash side, use
tcp input with json_lines codec as above.
Summary
| Method | Use Case | Pros | Cons |
|---|
| Filebeat | Production ready, JSON logs | Reliable, easy config, decoding | Requires installation/setup |
nc (netcat) | Quick testing, simple logs | Very simple and lightweight | Fails if multiline JSON logs |
| Python script | Multiline JSON logs | Full control over parsing | Requires custom scripting |
If you provide details about:
- Exact JSON format in your audit.log (one JSON per line or multiline object)
- Your Logstash host/port configuration
I can help tailor the solution further.