Logging and detection
About this lesson
You cannot detect what you never wrote down. Follow one sign-in service from logging to response: it writes one structured JSON line per attempt, keeps passwords and tokens out of the log, and copies every line to a central store. Then a burst of failed sign-ins arrives, and thirteen lines of real Python, a detection rule with a 60-second window per user, spot it and raise an alert. Finish with triage, tuning the rule, and the response, using guidance from NIST, OWASP and CISA.
What a log is
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.
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
Read the lesson step by step
What a log is
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.
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 outcomeOne line per sign-in
Ana mistypes her password once, then gets in. The sign-in service writes one line per attempt, a small JSON (plain text data) record: time, user, address and event. OWASP names a failure authn_login_fail. Failures matter most.
auth.log (JSON Lines: one event per line) {"t": 20, "user": "ana", "ip": "198.51.100.20", "event": "authn_login_fail"} {"t": 26, "user": "ana", "ip": "198.51.100.20", "event": "authn_login_success"} t seconds since the log began event names from the OWASP Logging VocabularyNever log the secret
A common mistake is logging the whole sign-in request, password included. Then anyone who reads the logs can sign in as Ana. OWASP says never log passwords, session IDs, access tokens or keys. Log the outcome instead.
mistake {"user": "ana", "password": "..."} fix {"user": "ana", "event": "authn_login_fail"} never passwords, session IDs, access tokens, keys, connection strings also encode input so it cannot fake a lineCollect logs in one place
Each line is copied at once to a central log store. An intruder on the server then cannot quietly erase it, and lines from many systems can be compared. Clocks are synced, so their times line up.
ship every line, as it is written store central, read-only, tamper detection clocks synced on every server why logs kept only on the server can be deleted by whoever breaks inA burst of failures
Now someone at 203.0.113.7 tries password after password for ana. Each guess fails, and each failure becomes a line: five in 40 seconds, from an address Ana never uses. The evidence is in the log.
t=100 ana 203.0.113.7 fail t=112 ana 203.0.113.7 fail t=121 ana 203.0.113.7 fail t=133 ana 203.0.113.7 fail t=140 ana 203.0.113.7 fail five failures in 40 s, one addressA detection rule reads lines
A detection rule is code that watches the log. This one reads each line, turns the JSON into a dictionary, and keeps only failures. Each user gets their own list, q, of recent failure times. Ana's typo is there.
for line in sys.stdin one event at a time json.loads(line) text → dictionary if event == authn_login_fail q = fails["ana"] a deque per user q [20] (the typo)A 60-second window
When the failure at t=100 arrives, it joins q. The while loop then drops times 60 seconds old or more, so the typo at t=20 falls out. The rule only counts recent failures, not old mistakes.
q.append(100) q = [20, 100] 100 - 20 = 80 ≥ 60: popleft() q = [100] deque a list that is fast at both endsFive in a minute: alert
More failures arrive at 112, 121, 133, 140. None is a minute old yet, so q grows to five. len(q) equals LIMIT, and the rule prints an alert naming the user and the address.
q = [100, 112, 121, 133, 140] 140 - 100 = 40 < 60: nothing dropped len(q) == LIMIT == 5 print → ALERT ana 203.0.113.7 a sixth failure in the window would not repeat itAn analyst checks the alert
The alert reaches an analyst, who triages it: is it real? They compare it with other logs. Ana signs in from 198.51.100.20, not 203.0.113.7, and nobody there knows the password. It is real: declare an incident.
alert ALERT ana 203.0.113.7 triage real attack, or a false alarm? compare Ana's usual address 198.51.100.20 this burst from 203.0.113.7 verdict real: declare an incidentTuning the rule
Rules need tuning. A limit too low raises false alarms for every typo, and tired analysts start ignoring alerts. Too high, and real attacks slip by. Slow guessing, or one password tried on many accounts, needs other rules.
LIMIT, WINDOW = 5, 60 too low false positives: alert fatigue too high false negatives: missed attacks also need per-address rules, slow-and-low rules review after every incidentRespond, then learn
The analyst responds: the service blocks the address, checks that no guess worked, and turns on MFA (a second proof, like a phone code) for Ana. NIST's incident guide links detect, respond and recover. The response is logged too, and the rule is reviewed.
contain block 203.0.113.7 at the sign-in service check no authn_login_success from that address protect MFA on for ana record the response is logged as well learn review the rule (NIST SP 800-61r3)
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01A developer logs every sign-in request in full so problems are easy to debug. What is the security problem?
OWASP says passwords, session IDs, access tokens and keys should not be recorded in logs. Log the outcome (authn_login_fail) and the who, when and where instead.
02Why are log lines copied at once to a central log store instead of only kept on the server that wrote them?
OWASP Top 10 lists 'Logs are only stored locally' as a failure. Central, protected storage keeps evidence safe from tampering and lets analysts correlate events across systems (CSF DE.AE-03).
03With LIMIT = 5 and WINDOW = 60, ana fails at t = 20 (a typo), then at t = 100, 112, 121, 133 and 140. When does the rule print an alert?
At t = 100 the typo at t = 20 is 80 seconds old, so popleft() drops it. The queue then grows 100, 112, 121, 133, 140; at 140 it holds five times within 40 seconds and len(q) == LIMIT.
04A team sets LIMIT = 1, so every single failed sign-in raises an alert. What is the most likely result?
A threshold that is too low causes alert fatigue; one that is too high misses real attacks. Rules are tuned from normal traffic and reviewed after incidents.
05In NIST CSF 2.0, which outcome describes the analyst deciding the burst of failures is a real incident?
Writing the log lines is PR.PS-04; monitoring and analysing them is DE.CM and DE.AE; declaring the incident is DE.AE-08, which hands over to Respond.
Glossary
- Log and event
- A log is a record of the events happening in an organisation's systems and networks (NIST SP 800-92). An event is one thing that happened, such as a failed sign-in.
- Structured logging (JSON Lines)
- Writing each event as one machine-readable record, here one JSON object per line, with named fields such as time, user, address and event.
- Central log store / SIEM
- A separate system that collects and keeps logs from many sources, protected from tampering. A SIEM (security information and event management) tool also searches and correlates them and runs detection rules.
- Detection rule
- Code or a query that watches events and raises an alert when a pattern appears, such as five failed sign-ins for one user within 60 seconds.
- Threshold and time window
- The count that triggers an alert (LIMIT) and how far back the rule looks (WINDOW). Events older than the window are dropped.
- Alert and triage
- An alert is the rule's signal that something may be wrong. Triage is the analyst's first check: is it real, how serious is it, what next?
- False positive / false negative
- A false positive is an alert with no real attack behind it; a false negative is a real attack with no alert. Tuning balances the two.
- Incident response
- The organised actions taken once an incident is declared: contain it, investigate, recover and learn (NIST SP 800-61 Rev. 3).
Notes and sources
- 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.
- Real teams write rules like this one inside a SIEM (security information and event management) tool or a detection language rather than a loose script, but the logic is the same: group events, count them over a time window, compare with a threshold.
- 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.
- In NIST CSF 2.0 terms: writing the lines is PR.PS-04 (log records are generated and made available for continuous monitoring), the rule is DE.CM and DE.AE (monitor, analyse, correlate), and declaring the incident is DE.AE-08.
- This course is about defence. It explains how attacks work only at the level needed to stop them, and never how to attack a real system.
- NIST SP 800-92: Guide to Computer Security Log Management
- NIST CSRC Glossary: log
- NIST SP 800-61 Rev. 3: Incident Response Recommendations and Considerations for Cybersecurity Risk Management (2025)
- NIST CSWP 29: The NIST Cybersecurity Framework (CSF) 2.0
- OWASP Logging Cheat Sheet
- OWASP Logging Vocabulary Cheat Sheet
- OWASP Top 10:2021 A09 Security Logging and Monitoring Failures
- CISA and partners: Best Practices for Event Logging and Threat Detection (2024)
- Python docs: collections (deque, defaultdict)
- Python docs: json
- RFC 5737: IPv4 Address Blocks Reserved for Documentation
Quiz Unlocks after the last step.