Advertisement
Open Source Projects by Phil Schwartz

From iptables logs to explainable alert rules

Writing a Python tool that analyzes iptables logs and generates alert rules turns weak firewall events into dependable operational evidence. The useful output is not a collection of DENY lines; it is a compact, reviewable signal that tells an operator what happened, why it matters, and which response is safe.

I approach this project with the same open-source principles used by tools such as DenyHosts and Kodos: readable code, portable dependencies, sensible defaults and documentation that explains the trade-offs. The analyser should help an Australian system administrator protect a server without making firewall management more hazardous than the threat itself.

Define the alert contract

Begin by defining what a useful alert contains. Each event needs a UTC timestamp, source address, destination address, protocol, port, firewall chain, action and original log reference. Optional fields can carry interface names, packet length, TCP flags and geographic metadata, but unknown values should remain unknown rather than being filled with misleading defaults.

Generated alert rules should have stable identifiers, severity, confidence, rationale and an expiry time. A portable JSON or YAML representation keeps the analyser independent of one monitoring product, while adapters can translate the result into formats understood by Wazuh, Grafana Alerting or a local ticketing system. Every alert must point back to supporting events so an administrator can inspect the evidence.

Parse logs without losing context

The parser can consume plain log files, command output, journal entries or standard input. It should follow file rotation, handle truncated final lines and preserve incomplete records until the next read. Regex matching works for normal key-value firewall messages, while a stricter tokenizer helps reject lines that contain similar text but lack the expected action and protocol fields.

Timestamps require careful normalisation because syslog records may omit the year and local machines can use different time zones. The parser should detect malformed and continuation lines, deduplicate replayed input and maintain a checkpoint without losing events during restart. It must also validate IPv4 and IPv6 addresses rather than accepting attacker-controlled strings that could later reach a command line.

Convert events into meaningful signals

Firewall logs become valuable only after related events are grouped. The analyser can maintain a sliding window for each source, destination port and protocol, then apply token-bucket thresholds to repeated connections, unusual port scans or bursts of dropped TCP packets. Counts alone are weak signals, so packet size, TCP flags and changes in destination should influence the rule’s confidence.

Baseline data reduces false positives from health checks, content delivery networks, monitoring systems and virtual private network gateways. NAT and carrier-grade NAT can compress many users behind one address, while dynamic addresses can transfer blame to a new tenant. Alert state therefore needs cooldowns, deduplication keys and clear suppression rules rather than generating a fresh notification for every matching packet.

Generate narrow, reversible responses

The tool should separate monitoring alerts from firewall actions. A generated monitoring rule might report suspicious SSH traffic, while a separate response template can propose a narrowly scoped block. Any command execution must use argument arrays, never a shell string, and must apply locking plus idempotent rule checks so concurrent workers cannot insert duplicate entries.

Blocking rules also need connection-state exceptions and a reliable removal path. A temporary deny is safer than a permanent blacklist because addresses change and shared networks can contain legitimate users. For Australian home and small-business servers connected through NBN fixed wireless, broadband or mobile backup links, a false positive could interrupt several services behind the same public address.

Account for Australian privacy and operations

Under the Privacy Act 1988 and the Australian Privacy Principles, an IP address can be personal information when it identifies or is reasonably linked to an individual. The default configuration should minimise stored addresses, restrict access to alert data and apply short, documented retention periods. Operators handling regulated records should also assess obligations with the Office of the Australian Information Commissioner.

Operational priorities vary across the Australian market. A water utility near Perth may need different escalation paths from a retail workload hosted in Sydney or Melbourne, especially when cloud services run alongside on-premises equipment. Alert routing can reflect Australian Signals Directorate guidance and sector playbooks without hard-coding assumptions that suit every organisation.

Test with realistic traffic

Unit tests should cover ordinary SSH connection failures, SYN scans, UDP reflection patterns, malformed records, IPv6 sources and daylight-saving transitions in Sydney and Melbourne. Property-based tests can generate unusual whitespace and hostile address strings, while replayed sanitised logs can verify aggregation without exposing production traffic to the test suite.

Integration tests belong inside disposable network namespaces or virtual machines. They should confirm that rule generation is deterministic, repeated runs do not duplicate entries and rollback restores the original ruleset. When a browser dashboard is added, principles from client-side caching can reduce repeated API calls, while authorisation and sensitive alert data remain on the server.

Package and maintain the project

The repository needs a small command-line interface, a versioned configuration file, sample logs and an explicit software licence. A standard-library-first implementation can run on minimal Debian and Ubuntu servers, while optional packages can provide richer enrichment. Documentation should identify supported Python versions and explain that log generation differs between traditional iptables and systems using nftables.

Continuous integration can run formatting, linting, type checks and tests across the supported Python releases. Releases should publish versioned archives and checksums, while the project page can report adoption through privacy-respecting download statistics. Clear issue templates, a security contact and reproducible examples keep the analyser useful alongside DenyHosts, Kodos and other open-source development utilities.