203.0.113.7SIGN-INLOG STORE0 linesq[0]–q[1]–q[2]–q[3]–q[4]–1import json, sys2from collections import defaultdict, deque3LIMIT, WINDOW = 5, 604fails = defaultdict(deque)5for line in sys.stdin:6 e = json.loads(line)7 if e["event"] == "authn_login_fail":8 q = fails[e["user"]]9 q.append(e["t"])10 while e["t"] - q[0] >= WINDOW:11 q.popleft()12 if len(q) == LIMIT:13 print("ALERT", e["user"], e["ip"])
Back: the attacker, Ana at her laptop, the sign-in service and the central log storeFront: the Python detection rule, its memory for Ana (q) and the analystTokens: a sign-in, log lines, a password kept out, guesses, the alert and the responseThe sign-in service writes one JSON line for every attempt, never the password, and copies it at once to a central log store. When five failed sign-ins for ana arrive from 203.0.113.7 within 40 seconds, the Python rule's 60-second window fills, it prints an alert, and an analyst checks it, declares an incident, blocks the address and reviews the rule.

Writing good logs

  1. NIST defines a log as a record of the events in an organisation's systems. Each event should say when, where, who and what. You cannot detect what you never wrote down, so logging comes first.

Detecting an attack

From alert to response

What happens

;; step 1/11 · events
log     a record of events (NIST SP 800-92)
when    the time it happened
where   which app or server
who     which user, from which address
what    the event and its outcome

Real logs carry a full date and time, usually in UTC in ISO 8601 form (2026-11-06T09:00:20Z), and OWASP asks for both the event time and the logging time. The figure uses seconds since the log began so the window maths stays easy to read. Five failures in 60 seconds is an example threshold, not a standard. Each organisation tunes its own numbers from its normal traffic. Services should also slow down or limit repeated failed sign-ins on their own, not only alert.