What you now know
- Why Python, and your first script. The REPL runs one line; a script runs a file.
python3on macOS and Linux,pyon Windows. - Variables, types, and input. Strings, integers, floats, and booleans behave differently;
input()always returns text and is never to be trusted blindly. - Decisions and loops.
if,elif,else, lists, andforloops: the logic behind every detection rule. - Functions, files, and dictionaries. Reusable logic, reading a log line by line, and counting with a dictionary.
- A first TCP socket. A server binds, listens, and accepts; a client connects. Malware uses the same pattern.
Hands-on: build the triage tool
Remember the last line in Lesson 3: an Accepted login from an address that had just failed many times. That is the pattern of a brute force that worked, and it is what this tool catches. It reads auth.log, counts failures per address, lists every successful login, flags successes from addresses with many failures, and saves a report.
Go to your practice folder, where auth.log from Lesson 4 still is:
cd ~/programming-basics-pythoncd ~/programming-basics-pythonSet-Location "$HOME\programming-basics-python"Create triage.py:
THRESHOLD = 5
def ip_from_line(line):
"""Return the IP address after ' from ' in an sshd log line."""
return line.split(" from ")[1].split()[0]
def user_from_accepted(line):
"""Return the username in an 'Accepted ... for USER from ...' line."""
return line.split(" for ")[1].split()[0]
failures = {}
successes = []
with open("auth.log") as log:
for line in log:
if "Failed password" in line:
ip = ip_from_line(line)
failures[ip] = failures.get(ip, 0) + 1
elif "Accepted" in line:
successes.append((line[:15], user_from_accepted(line), ip_from_line(line)))
report = ["SSH TRIAGE REPORT", ""]
report.append("Failed logins per address:")
for ip, count in sorted(failures.items(), key=lambda item: item[1], reverse=True):
report.append(f" {ip}: {count}")
report.append("")
report.append("Successful logins:")
for time, user, ip in successes:
flag = ""
if failures.get(ip, 0) >= THRESHOLD:
flag = " <-- ALERT: success after many failures"
report.append(f" {time} {user} from {ip}{flag}")
text = "\n".join(report) + "\n"
print(text)
with open("report.txt", "w") as out:
out.write(text)
print("Report saved to report.txt")
What is new:
THRESHOLDis written in capitals by convention, meaning “a setting you might change”.successesis a list of tuples: small fixed groups of values, here(time, user, ip).line[:15]takes the first 15 characters of the line, which is the timestamp."\n".join(report)joins the report lines with line breaks, andopen("report.txt", "w")writes them to a new file.
Run it (python3 triage.py, or py triage.py):
SSH TRIAGE REPORT
Failed logins per address:
203.0.113.50: 30
192.0.2.77: 8
198.51.100.23: 1
Successful logins:
Nov 22 01:12:40 deploy from 10.0.0.5
Nov 22 01:31:15 dewi from 198.51.100.23
Nov 22 02:10:42 backup from 203.0.113.50 <-- ALERT: success after many failures
Nov 22 02:47:19 deploy from 10.0.0.5
Report saved to report.txt
Read it like an analyst. The backup account was broken into from 203.0.113.50, right after 30 failed attempts. In a real incident, that line starts everything: disable or reset the account, look at what backup did next, and block the address. Dewi’s single failure and later success, from the same address, looks like a typo.
You just combined everything from the track into one useful tool: input from a file, decisions, loops, functions, a dictionary, and output to a report.
Where to go from here
You now read and write small scripts. Real tools add things this track skipped on purpose, such as installing packages with pip and isolating them in virtual environments; the second Go further link explains both when you are ready.
Bring your scripts and questions to the community Discord with the Join Discord button at the top of the page.
Next steps:
- SQL Basics asks the same kinds of questions of a database instead of a log file.
- Servers & Services gives you a real server whose real
auth.logyou can run this tool on.