Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 38 ยท Projects and Practice

Project: Build a Web Log Attack Detector

Write a defensive log analyzer that parses web access logs, flags probing, brute-force and suspicious paths, groups by source and produces an alert summary.

Defenders learn about attacks from their logs. In this project you build a small analyzer that reads web server access logs, detects common probing patterns, and prints an alert summary. It applies Security Logging and Monitoring Failures and the detection ideas from Identification and Authentication Failures.

The log lines are invented and use reserved documentation addresses. The tool only reads text you give it; use it on logs from systems you operate.

The log format

We use a simplified common log format:

TEXT
203.0.113.5 - - [01/Mar/2025:10:00:01 +0000] "GET /index.html HTTP/1.1" 200 512

Fields: client IP, timestamp, request (method, path, protocol), status and size.

Step 1: parse lines

Python Step 1: parse access log lines

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.

Always handle lines that do not parse: broken lines are themselves interesting, and a crash on bad input would blind your monitoring.

Step 2: detection rules

We implement four simple detectors:

  1. Brute force: many 401 responses on a login path from one IP.
  2. Path probing: many 404 responses from one IP (scanning for hidden files).
  3. Sensitive path requests: requests for files like .env, .git/config or wp-admin that should not exist on this site.
  4. Suspicious payload markers: obvious patterns such as ../ traversal or <script in the URL. These are weak signals, because attackers can encode them, but they are cheap to spot.
Python Step 2: the four detectors

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.

Decoding the URL before matching matters: ..%2f hides the traversal marker from a naive search. Real attackers also double-encode, so production systems use a web application firewall and the application's own validation, not log patterns alone.

Step 3: add time windows

Counting all failures in a whole file would eventually flag every busy user. Real rules count within a window.

Python Step 3: sliding-window brute-force rule

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 the limitation: a "slow and low" client that tries one password every ten minutes slips under this rule. Layer a second, longer window (for example 20 failures per day per account) to catch it.

Step 4: respond

An alert needs an action. Typical responses, in order of increasing disruption:

  1. Create a ticket with the evidence.
  2. Add a temporary rate limit or challenge for the source.
  3. Block the address range at the firewall or CDN for a limited time.
  4. Notify the account owner and force a password reset when the login succeeded after a failure streak.

Blocking IPs is a weak long-term defense, because attackers rotate addresses and shared networks (offices, mobile carriers) share one IP. Prefer controls tied to accounts and behavior.

Extend the project

  • Count a success after many failures from the same IP (a possible compromise).
  • Group alerts by country or network using a lookup table you supply.
  • Emit alerts as JSON for a log platform.
  • Add an allow-list for your own scanners and uptime checks.
  • Write unit tests with a log file for each detector, including a benign case that must not alert.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Why decode the URL before searching for attack markers?

    Encoded forms like ..%2f mean the same as ../, so detection must normalize first (and attackers may double-encode).
  2. 2. What weakness does a 60-second window rule have?

    Add a longer window or per-account limit to catch low-and-slow guessing.
  3. 3. Why is blocking IP addresses a weak long-term defense?

    Controls tied to accounts and behavior hold up better than address blocks.

Summary

  • Parse logs defensively; unparseable lines are data too.
  • Detect brute force, probing, sensitive-path requests and marker strings, normalizing the URL first.
  • Use sliding windows, and a second longer window for slow attacks.
  • Respond proportionally and prefer account- and behavior-based controls.

Next: Project: Build an SSRF Guard