Why Python, for a defender
Python is a programming language designed to be easy to read. It is the most common language for security tools, automation scripts, and data analysis. Defenders use it to:
- search and summarize logs faster than by hand,
- glue tools together (take the output of one, feed it to another),
- read and understand scripts found during an incident.
This track will not make you a software developer. It will make you comfortable reading and writing short scripts, which is what most security jobs ask for.
Before you start: Python 3
Check whether Python 3 is already installed:
python3 --versionpython3 --versionpy --versionIf it prints Python 3. followed by a version number, you are set. If not:
- macOS: running
python3the first time may offer to install the “command line developer tools”. Accept, wait for it to finish, and try again. Or install from python.org/downloads. - Linux: Python 3 is almost always installed. If not, ask in the Discord which package your distribution uses.
- Windows: download the installer from python.org/downloads. On the first screen, tick Add python.exe to PATH, then install. Close and reopen PowerShell, and run
py --versionagain.
Your practice folder
Every file in this track goes into one folder:
mkdir -p ~/programming-basics-python && cd ~/programming-basics-pythonmkdir -p ~/programming-basics-python && cd ~/programming-basics-pythonNew-Item -ItemType Directory -Path "$HOME\programming-basics-python" -Force
Set-Location "$HOME\programming-basics-python"Every later lesson starts by going back into this folder.
The interactive prompt
Python has an interactive mode, often called the REPL: you type a line, Python runs it right away and shows the result. Start it with python3 (Windows: py). The prompt changes to >>>. Try these, one per line:
2 + 3
10 * 60 * 60
print("Hello from Python")
len("blueteam")
10 * 60 * 60 is the number of seconds in ten hours. len counts the characters in a piece of text. Leave the REPL with exit().
Your first script
A script is Python saved in a file, so you can run it again. Create hello.py in your practice folder with any plain-text editor: nano hello.py on macOS and Linux (Linux Essentials Lesson 4), or notepad hello.py on Windows. Type:
print("Hello from my first script")
name = "defender"
print("Ready to automate the boring stuff,", name)
Save it, then run it:
python3 hello.pypython3 hello.pypy hello.pyHello from my first script
Ready to automate the boring stuff, defender
What to notice
- Python runs one line at a time in the REPL, or a whole file as a script.
- On Windows, the command is
py; on macOS and Linux,python3. - A script is just a text file. Read any script before you run it: it can do anything you can do.