Securus Journal 43.65°N, 79.38°W

Reading logs for the first ten minutes

A Python log analyzer that reads SSH and web server logs and flags brute-force attempts, scan traces and probes of restricted URLs, and what a log can and cannot see.

Contents

In 1997 Marcus Ranum, who had built some of the first commercial firewalls, described a way of reading logs he called “artificial ignorance.” Nobody can read a busy system’s logs line by line, he argued, and nobody can write rules for every attack in advance. So do the reverse: write patterns for the lines known to be boring, throw those away, and read what is left.

The script described here takes the other half of that bargain and looks for lines known to be bad. It is a Python script that reads log files and flags patterns associated with common attack behaviour: brute-force login attempts, the traces of port scans, and probes against URLs that should not be accessible.

A real SIEM does far more. This automates the first ten minutes of looking at a log when something feels off, the part where you grep for failed logins and source IPs and stare at the patterns. That work is repetitive and easy to script.

What it handles

  • .log input from OpenSSH (auth.log) and Apache or Nginx (access.log)
  • Failed SSH logins, repeated requests from the same IP, sequences that look like port scans, requests against known-sensitive paths
  • Output aggregated by source IP and rule type
  • A regex-based rule table that is straightforward to extend
  • CLI invocation, suitable for cron or piping into anything else

How it works

The script reads the file line by line and applies a list of regex rules. Each rule is tagged with a threat type and a severity. Matched events are stored with the timestamp, the source IP when there is one, and the rule that fired. Results print to stdout and optionally to a file.

Rules live in a single dictionary at the top of the file, so a new detection is one line plus a regex.

What a log can see

A log is written by one service, about that service. auth.log knows what sshd saw; access.log knows what the web server saw. A port scan touches hundreds or thousands of ports, and on the ports where nothing is listening it leaves no line in any log at all. What it does leave is a trace on the ports that log: on SSH, connections that open and close without ever attempting a login, and on a web server, short bursts of requests for paths that do not exist. “Possible port scan” in the output below means that kind of trace from one source. To see the scan itself, the evidence has to come from the network, a firewall log or a capture.

Example output

[2025-06-01 21:31:44] WARNING - Repeated failed SSH login from 192.168.1.45
[2025-06-01 21:32:05] INFO    - Possible port scan detected from 103.21.244.0
[2025-06-01 21:34:09] ALERT   - Attempted access to /admin (403 Forbidden) from 45.67.89.123

The severities rise for a reason. A failed login is ordinary, and many from one address are a warning. A probe of /admin answered with 403 means someone went looking for an administrative page by name, which is a step past noise.

Usage

python log_analyzer.py --input /path/to/file.log --output report.txt

Flags:

  • --input path to the log file
  • --output write results to a file instead of stdout
  • --verbose include the surrounding lines for each match

What comes next

  • GeoIP enrichment, so the source country is visible on flagged IPs
  • Optional firewall integration, so a flagged IP can be pushed straight to a deny list
  • A small web view for browsing results, mostly for convenience
  • Configurable thresholds per rule, so a quiet environment does not generate the same severity as a busy one

The second item needs care. A script that turns a log line into a firewall rule hands some control of the firewall to whoever can put text into that log. A username crafted to look like an address can get the wrong host banned, and behind a proxy the address in the log may be the proxy’s own. fail2ban, which does exactly this job, limits the damage with bans that expire and a list of addresses it will never ban. A home-grown version needs both.

The current version covers enough to be useful for small environments and to act as a base for anything more ambitious. Real SIEMs do all of this and more, but they also cost money and need infrastructure. For a homelab or a single VPS, a short Python script is a reasonable starting point, and Ranum’s filter of known-boring lines is the next thing worth adding to it.

Sources

  • Ranum, Marcus J. “artificial ignorance: how-to guide.” Posted to the firewall-wizards mailing list, 1997.