Back to the practice folder
cd ~/programming-basics-pythoncd ~/programming-basics-pythonSet-Location "$HOME\programming-basics-python"Decisions: if, elif, else
An if runs code only when a condition is True:
failed = 7
if failed > 5:
print("ALERT: possible brute force")
elif failed > 0:
print("A few failed logins, probably typos")
else:
print("No failed logins")
Three things to notice:
- The condition ends with a colon
:. - The lines that belong to the
ifare indented (four spaces is the convention). Python uses indentation, not brackets, to know what belongs together. elifmeans “else if”: checked only when the conditions above were false.
Comparisons you will use constantly: == (equal), != (not equal), >, <, >=, <=. Note == for comparing, and a single = for assigning a value.
For text, in asks “does this appear inside that?”:
line = "Failed password for root from 203.0.113.50 port 50100 ssh2"
"Failed password" in line
"Accepted" in line
The first gives True, the second False. That is the same question grep asks.
Lists
A list holds several values in order, inside square brackets:
ports = [22, 80, 443]
users = ["andi", "dewi", "rina"]
len(users)
users[0]
len() counts items, and users[0] is the first item: counting starts at 0.
Loops: do something for each item
A for loop runs the same code once for each item in a list:
for user in users:
print(f"Checking account: {user}")
Hands-on: flag suspicious log lines
Create flag_lines.py. It loops over a few log lines and flags the failures, just like grep "Failed password" did in Linux Essentials, but now you control what happens to each match:
log_lines = [
"Nov 22 01:12:40 web01 sshd[2202]: Accepted publickey for deploy from 10.0.0.5 port 50210 ssh2",
"Nov 22 02:10:00 web01 sshd[2215]: Failed password for root from 203.0.113.50 port 50100 ssh2",
"Nov 22 02:10:01 web01 sshd[2216]: Failed password for invalid user admin from 203.0.113.50 port 50101 ssh2",
"Nov 22 02:10:02 web01 sshd[2217]: Failed password for root from 203.0.113.50 port 50102 ssh2",
"Nov 22 02:10:42 web01 sshd[2246]: Accepted password for backup from 203.0.113.50 port 50131 ssh2",
]
failed = 0
for line in log_lines:
if "Failed password" in line:
failed = failed + 1
print(f"FLAG: {line}")
print(f"{failed} failed logins found")
if failed >= 3:
print("ALERT: possible brute force")
Run it (python3 flag_lines.py, or py flag_lines.py):
FLAG: Nov 22 02:10:00 web01 sshd[2215]: Failed password for root from 203.0.113.50 port 50100 ssh2
FLAG: Nov 22 02:10:01 web01 sshd[2216]: Failed password for invalid user admin from 203.0.113.50 port 50101 ssh2
FLAG: Nov 22 02:10:02 web01 sshd[2217]: Failed password for root from 203.0.113.50 port 50102 ssh2
3 failed logins found
ALERT: possible brute force
Look at the last log line: an Accepted login from the same address that just failed three times. Your script does not notice it yet. By the end of the track, it will.
What to notice
if/elif/elsemake decisions; indentation decides which lines belong to which block.==compares,=assigns.inchecks whether text appears inside other text.- A list holds items in order, counted from 0; a
forloop handles each item in turn. - Looping over log lines and checking each one is the logic behind every detection rule.