Skip to content

Lesson 15 of 19 · 16m

Logging and Detection That See Attacks

Log the security decision, not the secret — what to capture, what to exclude, shipping logs off-box, and alerting on shape instead of volume.

The breach report lands on a Tuesday and the first question is always the same: when did it start? The honest answer in most organizations is "we don't know — the logs rotated," or worse, "the attacker deleted them." Mandiant's M-Trends reports have tracked global median dwell time for over a decade; it has fallen from hundreds of days in 2011 to roughly ten days in the 2024 report — and "ten days" is still ten days of an attacker walking around inside systems that saw nothing, because nothing was watching.

OWASP calls this A09, Security Logging and Monitoring Failures. The wrong mental model: logging is a debugging tool you add when things break. For security, logging is the only instrument that can tell you the application did exactly what it was designed to do and it was still an attack. A successful login with the right password is indistinguishable from a credential-stuffing hit — unless you log enough context to see the pattern.

What to actually log

The rule: log every event where a security decision was made, with enough context to reconstruct it.

{"ts":"2026-11-16T14:03:11Z","event":"auth.login.fail","ip":"203.0.113.9","user":"a@b.c","reason":"bad_password","request_id":"req_9f2…","ua":"curl/8.5.0","mfa":null}
  • Authentication: success and failure, MFA enrollment/verification/failure, password resets requested and completed, session creation and invalidation.
  • Authorization: every denied request — not just the 403 page, the event, with which rule fired.
  • Privilege changes: role grants, permission edits, admin actions, API key mints — the operations an attacker performs once inside.
  • Boundary failures: input validation rejects at trust boundaries, CSRF token mismatches, CSP violation reports, upload rejections.
  • Money and data movement: transfers, exports, bulk reads, deletions.

What stays out: passwords, session tokens, full card numbers, raw MFA codes, and any PII you don't need — logs get breached as reliably as databases do, and they get read by every engineer and vendor tool with access. A log line containing Authorization: Bearer … is a credential leak filed under "debugging."

Mechanics that matter

  • Structured, not greppable. JSON lines with a schema — ts, event, request_id, actor, ip, outcome — so queries work. Free-text logs are where investigations go to die.
  • Correlate. One request_id minted at the edge, propagated through every log line and downstream call (W3C tracecontext headers if you're fancy), so "what did this session do" is one query, not an afternoon.
  • UTC timestamps, synchronized clocks. An incident reconstructed across five services with skewed clocks is archaeology.
  • Ship logs off-box. If the box holds its own logs, the attacker's first act is rm -rf or a quieter edit. Append-only sink — a logging service, a SIEM, even a separate object store with retention locking — so the system under attack cannot rewrite its own history.

Detection is the other half

Logs without alerting are a write-only database. Alert on shape, not volume: five failed logins on one account is noise; five hundred across five hundred accounts from one /24 in two minutes is credential stuffing. Impossible-travel logins (two countries, one account, one hour). New admin actions outside business hours. A sudden run of 403s on object IDs incrementing by one — that is IDOR enumeration, the pattern every first-lesson bug leaves behind. CSP violation reports are a free XSS tripwire.

Then test the alarms: run a drill where a colleague does something attack-shaped — ten failed logins, a bulk export — and confirm the page fires. An alerting rule that's never triggered in testing is a hope, not a control.

Try this

Pull your app's logs for the last 24 hours and answer three questions without opening a code file: who logged in, what failed, and what did the most recent admin action change. If any answer needs source access or guesswork, your logging is incomplete — and grep for password, token, and secret in the same log file while you're there.

Pitfalls

  • Logging credentials — a session token in a log line is a breach inside a breach.
  • Alert floods — a channel that pages hourly gets muted; tune for events that require a human decision.
  • No retention plan — 7 days of logs can't reconstruct a 30-day intrusion; keep security logs longer than your anticipated detection time, not your disk budget.
  • Clock skew — correlate events across services only if the timestamps mean the same thing everywhere.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.