Back to the practice folder
cd ~/programming-basics-pythoncd ~/programming-basics-pythonSet-Location "$HOME\programming-basics-python"Functions
A function is a named piece of code you can reuse. You define it with def, give it parameters (inputs), and it can return a result:
def ip_from_line(line):
"""Return the IP address after ' from ' in an sshd log line."""
after_from = line.split(" from ")[1]
return after_from.split()[0]
How it works on "... Failed password for root from 203.0.113.50 port 50100 ssh2":
line.split(" from ")cuts the text in two at" from ", giving a list. Item[1]is everything after it:"203.0.113.50 port 50100 ssh2"..split()with nothing inside cuts at spaces. Item[0]is the first word:"203.0.113.50".
The text in triple quotes is a docstring: a short note saying what the function does. Write one for every function; your future self will thank you.
Dictionaries: counting things
A dictionary stores pairs of keys and values, inside curly braces. It is perfect for counting:
failures = {}
failures["203.0.113.50"] = 1
failures["203.0.113.50"] = failures["203.0.113.50"] + 1
failures
That shows {'203.0.113.50': 2}. To count something that may not be in the dictionary yet, use .get(key, 0), which returns 0 when the key is missing:
failures["192.0.2.77"] = failures.get("192.0.2.77", 0) + 1
Reading a file
open() opens a file, and a for loop reads it one line at a time. The with block closes the file automatically when it ends:
with open("auth.log") as log:
for line in log:
print(line)
Hands-on: count failed logins in a real log
Download the sample SSH log. It is the same one from Security Concepts Lesson 3, so if you already have it, reuse it:
2dad2fcfdd2aec7f7f1bd1005a9fc640d1623ebde43b4328af168e09f09bfe0cMove it into your practice folder (it is usually in Downloads):
mv ~/Downloads/auth.log ~/programming-basics-python/
ls -l auth.logmv ~/Downloads/auth.log ~/programming-basics-python/
ls -l auth.logMove-Item "$HOME\Downloads\auth.log" "$HOME\programming-basics-python\"
Get-Item auth.logNow create count_failures.py:
def ip_from_line(line):
"""Return the IP address after ' from ' in an sshd log line."""
after_from = line.split(" from ")[1]
return after_from.split()[0]
failures = {}
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
for ip, count in sorted(failures.items(), key=lambda item: item[1], reverse=True):
print(f"{ip}: {count}")
The last loop needs one more explanation. failures.items() gives each (ip, count) pair. sorted(..., key=lambda item: item[1], reverse=True) sorts the pairs by their count (item[1]), biggest first. lambda is a tiny one-line function; you only need to recognize it for now.
Run it (python3 count_failures.py, or py count_failures.py):
203.0.113.50: 30
192.0.2.77: 8
198.51.100.23: 1
Read the result like an analyst. 203.0.113.50 failed 30 times in under a minute: a fast brute force. 192.0.2.77 failed 8 times, slowly, with common usernames: a patient guesser. 198.51.100.23 failed once: probably a person mistyping a password.
What to notice
- A function names a piece of logic so you can reuse it and test it on its own.
- A dictionary maps keys to values;
.get(key, 0)makes counting easy. with open(...)reads a file line by line and closes it for you.- Thirty lines of Python turned a log into an answer: who is attacking, and how hard.