Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: tests

on:
push:
branches: [main]
pull_request:

permissions:
contents: read

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
python-version: ["3.10", "3.12", "3.13"]
include:
- os: macos-latest
python-version: "3.9"
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
with:
persist-credentials: false
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5
with:
python-version: ${{ matrix.python-version }}
- name: Unit tests
run: python -m unittest -v
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

## 1.1.0

- `--json` output
- ISO-8601 timestamps (rsyslog high precision, `journalctl -o short-iso`)
- `--by-subnet` groups failures by /24 or /64 to expose distributed attacks
- `--ignore IP_OR_CIDR` leaves your own hosts out
- Reports successful root logins, password logins and pre-auth drops
- Installable with pip (`authlog-summary` command), CI on Linux and macOS

## 1.0.0

Initial release.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Read-only and standard library only. It reports; it never blocks anything.
- Most targeted usernames
- Failures by hour of day
- IPs that **logged in successfully after failing** (worth investigating)
- Successful **root** logins and how many logins used a password instead of a key
- Pre-auth drops (`Connection closed by invalid user ... [preauth]`) per IP
- With `--by-subnet`: failures grouped by /24 (IPv4) or /64 (IPv6), which exposes attacks spread over many addresses
- With `--suggest`: IPs over the threshold, one per line, for *your* review before
adding them to a firewall or fail2ban list

Expand All @@ -18,12 +21,19 @@ Read-only and standard library only. It reports; it never blocks anything.
python3 authlog_summary.py /var/log/auth.log
python3 authlog_summary.py /var/log/auth.log --threshold 10 --suggest
zcat /var/log/auth.log.2.gz | python3 authlog_summary.py -
python3 authlog_summary.py /var/log/auth.log --by-subnet --ignore 192.0.2.0/24 --ignore 2001:db8::1
python3 authlog_summary.py /var/log/auth.log --json
journalctl -u ssh -o short-iso | python3 authlog_summary.py -
# or: pip install git+https://github.com/TPOALOS/authlog-summary && authlog-summary /var/log/auth.log
```

`--ignore` leaves your own addresses or networks (monitoring, jump hosts) out of every count.

Exit codes: `0` nothing over threshold, `1` suspicious IPs found, `2` unreadable file.

Recognized sshd messages: `Failed password|publickey|keyboard-interactive`,
`Invalid user`, and `Accepted ...`. IPv4 and IPv6 sources are supported.
`Invalid user`, `Accepted ...` and pre-auth `Connection closed` / `Disconnected` lines. Classic syslog timestamps
and ISO-8601 ones (rsyslog high precision, `journalctl -o short-iso`) are understood. IPv4 and IPv6 sources are supported.

## Tests

Expand Down
132 changes: 118 additions & 14 deletions authlog_summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,139 @@
"""
import argparse
import collections
import ipaddress
import json
import re
import sys

__version__ = "1.0.0"
__version__ = "1.1.0"

TS = re.compile(r"^[A-Z][a-z]{2}\s+\d+\s+(\d{2}):\d{2}:\d{2}\s")
TS_ISO = re.compile(r"^\d{4}-\d{2}-\d{2}[T ](\d{2}):\d{2}:\d{2}") # rsyslog high-precision / journalctl -o short-iso
FAILED = re.compile(r"sshd\[\d+\]: Failed (?:password|publickey|keyboard-interactive/pam) "
r"for (?:invalid user )?(\S+) from (\S+) port \d+")
INVALID = re.compile(r"sshd\[\d+\]: Invalid user (\S*) from (\S+)")
ACCEPTED = re.compile(r"sshd\[\d+\]: Accepted \S+ for (\S+) from (\S+) port \d+")
ACCEPTED = re.compile(r"sshd\[\d+\]: Accepted (\S+) for (\S+) from (\S+) port \d+")
PREAUTH = re.compile(r"sshd\[\d+\]: (?:Connection closed by|Disconnected from) (?:authenticating user|invalid user) "
r"(\S+) (\S+) port \d+ \[preauth\]")


def analyze(lines):
"""Return a stats dict from an iterable of log lines."""
def parse_ignores(items):
"""CIDR/IP strings -> networks. Raises ValueError on a bad entry."""
return [ipaddress.ip_network(i, strict=False) for i in items]


def is_ignored(ip, nets):
if not nets:
return False
try:
addr = ipaddress.ip_address(ip)
except ValueError:
return False
return any(addr.version == n.version and addr in n for n in nets)


def subnet_of(ip):
"""/24 for IPv4, /64 for IPv6; the input itself when it is not an IP address."""
try:
addr = ipaddress.ip_address(ip)
except ValueError:
return ip
return str(ipaddress.ip_network("%s/%d" % (addr, 24 if addr.version == 4 else 64), strict=False))


def hour_of(line):
m = TS.match(line) or TS_ISO.match(line)
return int(m.group(1)) if m else None


def analyze(lines, ignore=()):
"""Return a stats dict from an iterable of log lines (``ignore``: networks to leave out)."""
fails = collections.Counter()
users_by_ip = collections.defaultdict(set)
user_targets = collections.Counter()
by_hour = collections.Counter()
first_fail_seen = set()
success_after_fail = {}
successes = 0
root_logins, password_logins, preauth = [], 0, collections.Counter()
for line in lines:
m = FAILED.search(line)
if m:
user, ip = m.groups()
if is_ignored(ip, ignore):
continue
fails[ip] += 1
users_by_ip[ip].add(user)
user_targets[user] += 1
first_fail_seen.add(ip)
t = TS.match(line)
if t:
by_hour[int(t.group(1))] += 1
hour = hour_of(line)
if hour is not None:
by_hour[hour] += 1
continue
m = ACCEPTED.search(line)
if m:
user, ip = m.groups()
method, user, ip = m.groups()
if is_ignored(ip, ignore):
continue
successes += 1
if method == "password":
password_logins += 1
if user == "root":
root_logins.append(ip)
if ip in first_fail_seen:
success_after_fail[ip] = user
continue
# "Invalid user" lines duplicate Failed lines; only used to count users seen.
m = INVALID.search(line)
if m and m.group(1):
users_by_ip[m.group(2)].add(m.group(1))
if not is_ignored(m.group(2), ignore):
users_by_ip[m.group(2)].add(m.group(1))
continue
m = PREAUTH.search(line)
if m and not is_ignored(m.group(2), ignore):
preauth[m.group(2)] += 1
return {"fails": fails, "users_by_ip": users_by_ip, "user_targets": user_targets,
"by_hour": by_hour, "success_after_fail": success_after_fail,
"successes": successes}
"successes": successes, "root_logins": root_logins, "password_logins": password_logins,
"preauth_drops": preauth}


def suspicious(stats, threshold):
"""IPs with >= threshold failures, most active first."""
return [(ip, n) for ip, n in stats["fails"].most_common() if n >= threshold]


def render(stats, threshold, top=10):
def by_subnet(stats):
"""Failures grouped by /24 (IPv4) or /64 (IPv6), with the number of distinct addresses."""
groups = collections.defaultdict(lambda: [0, set()])
for ip, n in stats["fails"].items():
g = groups[subnet_of(ip)]
g[0] += n
g[1].add(ip)
return sorted(((net, n, len(ips)) for net, (n, ips) in groups.items()), key=lambda r: (-r[1], r[0]))


def to_json(stats, threshold, subnets=False):
data = {
"failed_logins": sum(stats["fails"].values()),
"successful_logins": stats["successes"],
"threshold": threshold,
"ips": [{"ip": ip, "failures": n, "usernames_tried": len(stats["users_by_ip"][ip]),
"over_threshold": n >= threshold} for ip, n in stats["fails"].most_common()],
"usernames": [{"user": u, "failures": n} for u, n in stats["user_targets"].most_common()],
"failures_by_hour": {"%02d" % h: n for h, n in sorted(stats["by_hour"].items())},
"success_after_fail": [{"ip": ip, "user": u} for ip, u in sorted(stats["success_after_fail"].items())],
"root_logins": sorted(set(stats["root_logins"])),
"password_logins": stats["password_logins"],
"preauth_drops": dict(stats["preauth_drops"].most_common()),
}
if subnets:
data["subnets"] = [{"subnet": net, "failures": n, "addresses": k} for net, n, k in by_subnet(stats)]
return data


def render(stats, threshold, top=10, subnets=False):
out = ["Failed logins: %d from %d IP(s); successful logins: %d" % (
sum(stats["fails"].values()), len(stats["fails"]), stats["successes"])]
out.append("\nTop source IPs (failures, distinct usernames tried):")
Expand All @@ -79,6 +157,16 @@ def render(stats, threshold, top=10):
for h in sorted(stats["by_hour"]):
bar = "#" * max(1, round(30 * stats["by_hour"][h] / peak))
out.append(" %02d %-30s %d" % (h, bar, stats["by_hour"][h]))
if subnets:
out.append("\nFailures by subnet (/24 IPv4, /64 IPv6):")
for net, n, k in by_subnet(stats)[:top]:
out.append(" %-40s %5d %4d address(es)" % (net, n, k))
if stats["root_logins"]:
out.append("\nSuccessful root logins (investigate):")
for ip in sorted(set(stats["root_logins"])):
out.append(" from %s" % ip)
if stats["password_logins"]:
out.append("\nSuccessful password (not key) logins: %d" % stats["password_logins"])
if stats["success_after_fail"]:
out.append("\nSuccessful login from an IP that previously failed (investigate):")
for ip, user in sorted(stats["success_after_fail"].items()):
Expand All @@ -93,19 +181,35 @@ def main(argv=None):
help="failures per IP to flag (default 5)")
ap.add_argument("--suggest", action="store_true",
help="print flagged IPs one per line for your blocklist review")
ap.add_argument("--json", action="store_true", help="machine-readable output")
ap.add_argument("--by-subnet", action="store_true",
help="also group failures by /24 (IPv4) or /64 (IPv6) to expose distributed attacks")
ap.add_argument("--ignore", action="append", default=[], metavar="IP_OR_CIDR",
help="leave this address or network out (your own hosts, monitoring); repeatable")
ap.add_argument("--version", action="version", version=__version__)
args = ap.parse_args(argv)
try:
ignore = parse_ignores(args.ignore)
except ValueError as exc:
print("authlog-summary: bad --ignore value: %s" % exc, file=sys.stderr)
return 2
try:
if args.logfile == "-":
stats = analyze(sys.stdin)
stats = analyze(sys.stdin, ignore)
else:
with open(args.logfile, errors="replace") as fh:
stats = analyze(fh)
stats = analyze(fh, ignore)
except OSError as exc:
print("authlog-summary: %s" % exc, file=sys.stderr)
return 2
flagged = suspicious(stats, args.threshold)
print(render(stats, args.threshold))
if args.json:
data = to_json(stats, args.threshold, args.by_subnet)
if args.suggest:
data["suggested_blocklist_candidates"] = [ip for ip, _ in flagged]
print(json.dumps(data, indent=2))
return 1 if flagged else 0
print(render(stats, args.threshold, subnets=args.by_subnet))
if args.suggest and flagged:
print("\nCandidate IPs for blocklist (review before use):")
for ip, _ in flagged:
Expand Down
29 changes: 29 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "authlog-summary"
version = "1.1.0"
description = "Read-only SSH auth log summarizer: failed logins per IP and subnet, root and password logins, JSON output (stdlib-only Python)."
readme = "README.md"
requires-python = ">=3.9"
license = {text = "MIT"}
authors = [{name = "TPOALOS"}]
keywords = ["ssh", "log-analysis", "brute-force", "sysadmin"]
classifiers = [
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Topic :: Security",
]

[project.urls]
Homepage = "https://github.com/TPOALOS/authlog-summary"

[project.scripts]
authlog-summary = "authlog_summary:main"

[tool.setuptools]
py-modules = ["authlog_summary"]
Loading
Loading