You've spent this path building attacker tools. Now flip to defence. In this capstone you'll write a brute-force detector — a script that reads a server's login log and spots the IP addresses hammering it with failed passwords. This is real blue-team work, and it ties together everything you've learned.
The idea
A brute-force attack means many failed logins from one source in a short time. The log records every attempt — so detection is just counting failures per IP and flagging anyone over a threshold.
Reading the log
Linux records SSH logins in /var/log/auth.log. A failed attempt is a line containing Failed password ... from <IP>.
import re
from collections import Counter
fails = Counter()
with open('auth.log') as f:
for line in f:
if 'Failed password' in line:
match = re.search(r'from (\\d+\\.\\d+\\.\\d+\\.\\d+)', line)
if match:
fails[match.group(1)] += 1
Counter tallies failures per IP automatically — no manual bookkeeping.
Flagging the attackers
THRESHOLD = 10
for ip, count in fails.most_common():
if count >= THRESHOLD:
print(f'[!] {ip} — {count} failed logins (possible brute force)')
| Technique | Lesson it came from |
|---|---|
| Reading a file line by line | Files and functions |
| Regex to extract an IP | OSINT scraping |
Counter to tally | Python basics |
| A threshold decision | Conditionals |
Tip: Real tools (like fail2ban) do exactly this, then act — blocking the IP in the firewall automatically. A good next step is to have your script write offending IPs to a file or alert you, rather than just print them. Tune the threshold to your traffic: too low and you flag yourself fat-fingering a password; too high and you miss a slow attack.
You've come full circle
You started by learning why attackers love Python and end by using it to catch them. That's the whole point of ethical hacking: you learn the offence so you can build the defence. Take this detector, improve it, and you've got a genuine portfolio project.