Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 53 ยท Projects

Project: Detect Attacks in Web Server Logs

Build a log hunter in Python: parse access logs, flag injection attempts, brute-force logins and scanners, then rank suspicious IP addresses by evidence.

Web server access logs are one of the cheapest and most honest sources of security evidence. Every request is there: who asked, what they asked for, and how the server answered. Attackers leave marks in that record long before anything obviously breaks, and a short script can find many of them.

In this project you build a small log hunter in Python. It parses access log lines, applies three detections (injection attempts, brute-force logins, and scanning), and ranks the source addresses by how suspicious they look. Everything runs on made-up log lines inside your browser, so nothing touches a real server. It is a stand-in for what a SIEM does, as described in Logging, Monitoring and SIEM Basics; real products add indexing, scale and tuning, but the logic is the same. Only analyze logs from systems you own or are authorized to review.

Three signals in one log

Injection Odd characters and keywords in URLs.
Brute force Many failed logins from one address.
Scanning Lots of 404s on different paths.

Each signal is weak alone and strong together.

1 / 2

Step 1: Parse the log

Most web servers write the combined log format: client address, a timestamp, the request line in quotes, the status code, the response size, the referrer and the user agent. A regular expression with named groups turns each line into a dictionary. Lines that do not match must be counted, never silently dropped, because a weird line can be an attack on the log itself.

Python 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.

The pattern uses \S+ (non-whitespace) and [^"]* (anything but a quote) so that a hostile user agent cannot confuse the field boundaries. Notice the design choice: parsing returns None instead of raising, so one bad line cannot stop the whole run.

Step 2: Write the detections

Each detection answers one question about behaviour. Keep them separate so you can test and tune each on its own.

  • Injection attempts. Look at the decoded request path and query string for markers such as a quote followed by or, union select, <script, ../, or ${jndi:. This is a signature approach: it catches noisy, automated attempts, but a skilled attacker can disguise payloads, and a match does not prove the attack worked. The status code and response size help: a 500 right after a suspicious request deserves a closer look. See SQL Injection for why these payloads matter.
  • Brute force. Count 401 or 403 responses on login paths per source address inside a time window.
  • Scanning. Count how many distinct paths one address requested that returned 404. A normal user hits a few broken links. A scanner tries hundreds of guesses such as /admin, /.env and /backup.zip.

URL-decode before matching, since %27 is a quote and %3Cscript is <script. Decode only once or twice and not forever, so a crafted string cannot make your tool loop.

Python Three detections on sample traffic

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.

Look at the injection output: the first request got a 500, which suggests the application choked on the input and is worth reporting to the developers, while the others got 200 and 400. A detection tells you where to look; the response tells you whether to worry.

Step 3: Score and rank the addresses

A list of alerts is hard to act on. A score per address combines the signals so an analyst starts with the worst offender. Choose weights from experience and revisit them: injection attempts are worth more than a few 404s, and a successful login right after many failures is worth the most because the guessing may have worked.

Python Score suspicious addresses

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.

Capping each count (min(..., 10)) stops one noisy signal from drowning the others. The login_after_failures flag would trigger incident response: lock the account, force a password reset and look for what that session did next.

Limits and improvements

  • Signatures miss things. Encoded, split or novel payloads slip past patterns. Treat the tool as a first filter, backed by a web application firewall and secure code.
  • IP addresses mislead. Attackers rotate addresses, and many users share one behind a NAT or proxy. Behind a CDN, read the real client address from the trusted forwarded header, not from the connection.
  • Context cuts false positives. A security scanner your own team runs will look like an attacker. Keep an allow-list with owners and reasons.
  • Responses matter. A request that returned 200 with a much larger body than usual is worth more attention than one that was rejected.
  • Do not act blindly. Automatically blocking addresses can lock out customers, and attackers can abuse that by spoofing victims' addresses where they can.

Ideas to extend it: add time windows so slow attacks count too, read the user agent for known tool names (easy to fake, but common), and write the results as JSON for a dashboard.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should the log parser count lines it cannot parse instead of ignoring them?

    Silently dropping lines hides evidence. Counting them shows when logs are corrupted, forged or in an unexpected format.
  2. 2. Why do you URL-decode a path before looking for injection patterns?

    Attackers often percent-encode payloads. Matching on the decoded text catches them, with a limit on how many times you decode.
  3. 3. Which behaviour best indicates a scanner rather than a normal visitor?

    Scanners guess many common paths. Counting distinct 404 paths per address separates that from an occasional broken link.
  4. 4. Why is a successful login after many failures from the same address ranked highest?

    Failures followed by a success is the pattern of a brute-force attack that succeeded, so the account needs immediate review.

Summary

  • Parse combined-format access logs with a strict pattern and count the lines that do not match.
  • Detect injection with decoded-path signatures, brute force with failed logins per address, and scanning with distinct 404 paths.
  • Combine signals into a capped, weighted score so analysts see the worst addresses first.
  • Signatures and IP addresses are imperfect: add context, allow-lists and human review before acting.
  • This is a stand-in for SIEM correlation, and it only belongs on logs you are authorized to analyze.

Next: Project: Vulnerability Triage Tracker.