Skip to content
Tutorials Center
My learning

Topics

Lesson 57 of 80 ยท Security Operations and Incident Response

Spotting Attacks in Logs

Learn to find brute-force logins, password spraying, impossible travel and odd-hours activity in authentication logs using pandas, with charts.

Collecting logs is only half of security monitoring. The other half is knowing what an attack looks like once it is sitting in thousands of ordinary lines. Most intrusions leave a pattern: a burst of failed logins, a success right after the failures, a login from two continents within an hour, an administrator signing in at 3 a.m.

In this lesson you will turn those patterns into small detection rules and run them on generated authentication logs with pandas. Everything is synthetic and seeded, so you get the same output every time. If you have not seen log formats and SIEM basics yet, start with Logging, Log Analysis and SIEM; the attacks behind these patterns are explained in The Attack Lifecycle and MITRE ATT&CK.

From pattern to rule

A good detection rule is a precise sentence about behavior. "Someone is attacking us" is not a rule. "One source address produced at least 10 failed logins within 5 minutes" is. Each rule you write has three parts:

  1. A feature you can compute from the log (failures per source, distinct usernames, distance between logins).
  2. A threshold or window (10 failures, 5 minutes, 900 km/h).
  3. A response: an alert, a block, or a ticket for an analyst.
flowchart LR
    RAW["Raw auth logs"] --> PARSE["Parse into a table"]
    PARSE --> FEAT["Compute features<br/>per source or user"]
    FEAT --> RULE["Apply thresholds"]
    RULE --> ALERT["Alert with context"]
    ALERT --> TUNE["Review and tune"]
    TUNE -.-> RULE

Detection is a loop: every reviewed alert should improve a threshold or an allow-list.

Our sample data

The next demo builds a small authentication log. It contains ordinary users, one source that hammers a single account (brute force), one source that tries a single common password against many accounts (password spraying), and one lucky success at the end of an attack.

Python Build and summarize a synthetic auth log

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.

Even this plain summary shows two sources with a lot of failures. The difference between them matters, because the defenses differ.

Brute force versus password spraying

  • Brute force (or credential guessing): many guesses against one account. A lockout after a few failures stops it, but lockouts can also be abused to deny service to the real owner.
  • Password spraying: one or two very common passwords tried against many accounts, slowly, so that no single account trips a lockout. Its signature is many distinct usernames from one source, each with a small number of failures.
  • Credential stuffing: username and password pairs leaked from other sites are replayed. Failures are spread across many accounts and often many source addresses.

The rule for brute force is "many failures per source and account in a short window". The rule for spraying is "many distinct accounts failing per source". The next demo implements both with a rolling time window, then checks whether the attacker eventually succeeded, which is what turns an alert into an incident.

Python Detect brute force, spraying and a success after failures

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 last line is the one a human must act on immediately: the attacker guessed the password for carol. Rules that look for failures followed by success have a much lower false-positive rate than failure counts alone, because ordinary users rarely type a password wrong ten times in a row and then get it right.

Real detections must handle shared addresses. A university or office network behind one NAT address can legitimately produce many failures from a single IP. Group by account as well, and keep an allow-list of known networks.

Impossible travel

If an account logs in from Berlin and, forty minutes later, from Sydney, either the person has a teleporter or somebody else holds the credentials. Impossible travel compares consecutive logins of the same user: it converts the distance between the two locations (the great-circle distance) and the time between them into a speed, and flags anything faster than a plane.

Python Impossible travel with the haversine formula

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.

Treat this rule as a hint, not proof. VPNs and mobile carriers make a user appear in a different country, and the location comes from an IP address that can be wrong. Many teams therefore add context before alerting (a new device, a new browser, no MFA prompt) or use the signal to require an extra verification step instead of blocking outright.

Odd hours and unusual behavior

A second behavioral check asks whether the activity fits the person. An accountant who logs in at 9 a.m. every day and suddenly appears at 3:12 a.m. on a Sunday is worth a look. Build a per-user baseline (the usual hours, countries, devices), then score deviations, as in the anomaly examples from Intrusion Detection and Network Monitoring. Keep the rule simple and explainable: analysts must be able to say why an event was flagged.

Charting the evidence

Humans spot bursts in a chart faster than in a table, and a picture is persuasive in an incident report. The demo below plots failed logins per 10-minute bucket and draws the alert threshold.

Python Failed logins per 10 minutes

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.

Notice that the slow password-spraying run (one attempt every four minutes) barely rises above the typo noise in this view, while the brute-force burst towers over it. A single chart or a single rule never covers every attack, which is why you keep several rules and look at the data from more than one angle (per source, per account, per time).

Reducing false positives

  • Context beats counts. Combine signals: failures plus a success, failures plus a new country, failures plus a privileged account.
  • Prioritize by account value. Ten failures against a service account or an administrator deserve a faster response than ten against a test user.
  • Keep an allow-list, with a reason and an owner, for known scanners, load tests and shared NAT addresses.
  • Measure precision. For each rule, track alerts, confirmed incidents and time spent. A rule that never finds anything is either perfect or broken; test it with a simulated attack.
  • Test your detections. Replay known-bad logs, like the ones above, whenever you change a rule.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pattern best matches password spraying?

    Spraying avoids per-account lockouts by trying few passwords per account across many accounts, so the clue is many distinct usernames failing from one source.
  2. 2. Why is 'a success after many failures from the same source' a strong alert?

    Failures alone may be noise. A success following a burst of failures suggests the attacker found a valid credential, which makes it a likely compromise.
  3. 3. How does an impossible-travel rule decide to alert?

    The rule uses the great-circle distance and the elapsed time to compute a speed, flagging values above a plausible limit such as an airliner's speed.
  4. 4. Why should an impossible-travel alert be treated as a hint rather than proof?

    The location is inferred from an IP address, which can be a VPN exit or a carrier gateway, so the signal needs context before it triggers a hard block.

Summary

  • A detection rule is a precise statement: a feature, a threshold or window, and a response.
  • Brute force means many failures against one account; spraying means few attempts across many accounts; stuffing replays leaked pairs. Each needs a different feature.
  • A success after a burst of failures is the strongest sign that guessing worked.
  • Impossible travel turns distance and time into a speed; odd-hours checks compare activity with a per-user baseline. Both are hints that need context.
  • Charts help you see bursts, but use several rules and views, then tune with allow-lists and replayed test logs.

Next: The Incident Response Process, which covers what to do once an alert turns out to be real.