Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 38 ยท Integrity, Logging and SSRF

Security Logging and Monitoring Failures

OWASP A09: what to log, what never to log, how to prevent log injection, and how to turn raw events into alerts so attacks are noticed in minutes, not months.

Prevention fails sometimes. When it does, the difference between a small incident and a catastrophic breach is how fast you notice. Security Logging and Monitoring Failures (A09) means that the application does not record security-relevant events, the logs are never reviewed, or nobody is alerted when something suspicious happens. Studies of breaches repeatedly show attackers staying undetected for months in systems that logged nothing useful.

Logging does not stop attacks by itself. It enables detection, investigation (what happened, to whom, since when) and evidence. It pairs naturally with the authentication checks from Identification and Authentication Failures.

What to log

Log events that matter for security, with enough context to reconstruct a story:

  • Logins, failed logins, logouts, password changes, MFA enrollment and failures.
  • Access control failures (a user requesting something they may not see).
  • Input validation failures that look like probing.
  • High-value actions: payments, role changes, data exports, admin operations.
  • Configuration and permission changes, and deployment events.
  • Server-side errors and unexpected exceptions.

Each entry should include: timestamp (UTC), event type, outcome, user or account ID, source IP, request or correlation ID, and the resource. Use a structured format such as JSON so tools can search it.

What never to log

  • Passwords, even failed ones (they are often one typo from the real one).
  • Session IDs, API keys, tokens and private keys.
  • Full card numbers, national IDs and other regulated data.
  • Anything you would not want in a breach of the log store itself.

Mask or hash sensitive fields, and protect the logs with their own access control: attackers love to read or delete them.

A useful log line

When UTC timestamp.
Who User ID and source IP.
What Event and resource.
Outcome Success or failure.

Add a correlation ID to follow one request.

1 / 2

Structured logging in practice

Python Write structured security events

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The addresses above (203.0.113.x) are reserved documentation addresses. Never put real personal data in training examples.

Log injection

If you write user input straight into a log line, a user can include a newline and forge extra entries:

TEXT
username=bob%0A2025-03-01 10:15:00 INFO login success user=admin

A reader or parser then sees a fake "admin login". Defenses: use structured logs (JSON escapes newlines), encode or strip control characters, and do not trust log fields as truth.

Python Neutralize newline injection in logs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Monitoring turns logs into protection

Logs nobody reads do not help. Build detection that matches your risks:

  • Alert on spikes of failed logins, a login from a new country right after a failure streak, an admin created at night, many 403 or 404 responses from one source, and your own canary (honeypot) records being accessed.
  • Centralize logs off the application server (a SIEM or log platform) so an intruder cannot erase them locally.
  • Synchronize clocks (NTP) so timelines across systems line up.
  • Retain logs long enough to investigate slow attacks, according to law and policy.
  • Test your detection (an authorized exercise should produce an alert) and write an incident response plan with named owners.
  • Alerts must reach a person with authority to act, and the noise must be low enough that people do not ignore them.

A simple threshold alert

Python Alert on a burst of access-denied events

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The sliding window is what real rules do: a handful of denials in a day is normal, a handful per second is not.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which of these must NOT be written to a log?

    Secrets in logs turn a log leak into an account takeover. Log the event and outcome, not the secret.
  2. 2. How does structured (JSON) logging help against log injection?

    Because values are escaped, user input cannot start a new, forged log line.
  3. 3. Why ship logs to a separate central system?

    Off-host, access-controlled logs preserve evidence even if the application server is compromised.
  4. 4. What turns logging into effective monitoring?

    Detection and response are the point: alerts must be tuned, routed to an owner and backed by a playbook.

Summary

  • A09 means attacks go unnoticed because events are not logged, reviewed or alerted on.
  • Log security events with who, what, when and outcome in a structured format; never log secrets.
  • Prevent log injection with structured logs and escaping.
  • Centralize, protect and retain logs, tune alerts and rehearse an incident plan.

Next: Server-Side Request Forgery (SSRF)