Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 37 ยท Projects and Practice

Project: A Log Analyzer for Scan and Brute-Force Detection

Build a Python log analyzer that parses firewall and authentication logs, flags port scans, brute force and spraying, and writes a prioritised alert list.

Attackers leave tracks, and a small program can find many of them. In this project you build a log analyzer that reads two invented log formats (a firewall log and an SSH authentication log), detects port scanning, brute force and password spraying, and prints alerts in priority order. It is the defender's counterpart to the scanner you built in the previous project.

All data is made up and uses documentation-range addresses.

Requirements

  1. Parse firewall lines into records: time, source, destination port, action.
  2. Detect port scans: one source hitting many distinct ports in a short window.
  3. Parse authentication lines and detect brute force (many failures on one account) and spraying (a few failures on many accounts from one source).
  4. Detect a success after failures, the most important alert.
  5. Print alerts sorted by severity, each with the evidence behind it.

Step 1: the sample logs and parsers

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

Step 2: detect port scans

A scan touches many different ports. Counting distinct ports per source in the log window is a robust first test.

Python Port-scan detector

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 second source made three requests but all to the same port: a normal client, so no alert.

Step 3: authentication logs

Failed and successful logins give the highest-value signals. The detector below handles all three authentication patterns in one pass.

Python Brute force, spraying and 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 critical alert is the one to act on first: a successful login for root right after six failures from one address suggests a guessed password. The response is the one from Logging and Incident Response: contain, reset, investigate.

Step 4: combine and extend

A real analyzer ingests many sources and correlates them. Try these extensions on your own:

  • Windowing: only count failures within 60 seconds of each other (the sliding window).
  • Allow lists: ignore your own vulnerability scanner's address.
  • Enrichment: add the country or reputation of each source (use an offline table in a lab).
  • Output formats: write alerts as JSON for a SIEM, or send a message to a chat webhook.
  • Tuning: log the false positives you meet and adjust thresholds.
Python Count alerts by severity for a dashboard line

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.

What this project shows

Detection logic is simple counting and grouping done carefully: pick the right key (source, account, port), the right window and a threshold you can justify. The hard part, in real life, is tuning so analysts trust the alerts.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why count distinct ports rather than total connections to detect a scan?

    Variety of ports is the signature of scanning.
  2. 2. Which alert deserves the highest priority?

    It suggests a guessed credential, meaning the attacker may already be in.
  3. 3. How is spraying detected?

    Spraying avoids per-account limits, so group by source.
  4. 4. What should you do about repeated false positives?

    Tuning keeps analysts trusting the alert feed.

Summary

  • You parsed two log formats and detected scans, brute force, spraying and success after failures.
  • Group by the right key, use windows and thresholds, and keep the evidence with each alert.
  • Sort alerts by severity so the most important action is first.
  • Tuning and allow lists are as important as the detection logic.

Next: Practice: A Full Assessment Walkthrough