Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Set-up · 15 minutes

Set up your security basics lab

After this lesson you can
  • Create the lab folder for the course
  • Check the tools that the lessons use
  • Read a check report and fix a missing tool
Lab files of this lesson (1)

The lab folder as it stands after this lesson. Unzip it in ~/krlab.

More

The zip holds every file that the course has printed up to here. Type the code yourself first: you learn more.

What you need

All work happens on your own computer, in one folder. You need five things. You should also be able to read a short Python program and use cd and ls. Linux Part 1 and lessons 1 to 5 of the Python course teach this, and Part 1 lesson 7 shows how to edit files in the terminal.

NeedWhy
A terminalYou type every command in it. Use a Linux terminal, the Terminal app on macOS, or WSL on Windows.
Python 3.11 or laterThe programs of the lessons use only its standard library.
openssl (version 3 or later)Lesson 5 encrypts and signs with it.
sha256sumLessons 2, 8 and 9 use it to fingerprint files.
xxdLesson 5 uses it to show the bytes of a file.

You need no account, no network after the install, and no administrator rights for the lessons. Everything you do is on files that you make yourself. Never point a tool of this course at a computer that is not yours.

Set it up

1. Open a terminal and make the lab folder

mkdir (make directory) creates a directory. The option -p (parents) creates the folders above it too, and gives no error if the folder exists. cd (change directory) moves you into it. pwd (print working directory) shows where you are.

mkdir -p ~/krlab/security-basics
cd ~/krlab/security-basics
pwd
/home/you/krlab/security-basics

The sign ~ is the short name of your home directory. Every lesson of this course uses this folder.

2. Learn how to save a file

Many lessons say "Write name.py". That means: put the text of the block into a file of that name, in the lab folder. There are two ways. The first is the editor nano: type nano name.py, paste the text, press Ctrl+O and Enter to save, then Ctrl+X to leave. The second is a here-document: type the line cat > name.py <<'EOF', paste the text, and type EOF on a line of its own. The shell writes everything between the two lines into the file. The quotes around EOF matter: they stop the shell from changing the text.

cat > name.py <<'EOF'
(the lines of the file)
EOF

Check a saved file with cat name.py, which prints it.

3. Write the check script

The script labcheck.py asks five questions about your machine. Save it in the lab folder, as step 2 showed. shutil.which looks for a program on your PATH (the list of folders where the shell finds programs). hasattr asks whether a module has a given name. subprocess.run runs a program and, with capture_output=True, text=True, keeps the text that it prints. sys.exit ends the program with a status: 0 means success, 1 means failure.

labcheck.py

import hashlib
import shutil
import subprocess
import sys

results = []


def report(ok, text):
    results.append(ok)
    print(("ok    " if ok else "FAIL  ") + text)


version = ".".join(str(n) for n in sys.version_info[:3])
report(sys.version_info >= (3, 11), "python " + version + " (3.11 or later)")
openssl = ""
if shutil.which("openssl"):
    openssl = subprocess.run(["openssl", "version"], capture_output=True, text=True).stdout
report(openssl.startswith("OpenSSL") and int(openssl.split()[1].split(".")[0]) >= 3, "openssl 3 or later found")
report(shutil.which("sha256sum") is not None or shutil.which("shasum") is not None, "sha256sum or shasum found")
report(shutil.which("xxd") is not None, "xxd found")
report(hasattr(hashlib, "scrypt"), "hashlib.scrypt available")

print(f"lab ready: {sum(results)} of {len(results)}")
sys.exit(0 if all(results) else 1)

4. Run it

python3 starts the Python interpreter and runs the file that you name.

python3 labcheck.py
ok    python 3.12.3 (3.11 or later)
ok    openssl 3 or later found
ok    sha256sum or shasum found
ok    xxd found
ok    hashlib.scrypt available
lab ready: 5 of 5

Your Python version is different, and that is fine. The last line must say 5 of 5.

5. Know what differs on macOS

macOS has no sha256sum. It has shasum -a 256, which prints the same hash in the same layout. Wherever a lesson says sha256sum, type shasum -a 256. Three more differences: stat -c '%a %U %n' file becomes stat -f '%Lp %Su %N' file, sed -i becomes sed -i '', and the openssl that comes with macOS (LibreSSL) cannot sign with Ed25519. Install OpenSSL 3 with your package manager (brew install openssl) and use that program for lesson 5.

Check your lab

Run the script again and type its last line below. Type it exactly, with the numbers.

If you see 4 of 5 or less, a line that starts with FAIL names the missing part. Fix it with the table below, and run the script again.

If it fails

The line saysWhat to do
FAIL python ... (3.11 or later)Your Python is too old. Install a current Python 3 from your system's package manager, then run python3 --version.
FAIL openssl 3 or later foundInstall it: sudo apt install openssl on Debian, Ubuntu and Kali, or brew install openssl on macOS. The openssl of macOS (LibreSSL) fails this line: put the new program first on your PATH, as brew tells you.
FAIL sha256sum or shasum foundOn Linux the package coreutils holds sha256sum. On macOS shasum is part of the system, so check your PATH.
FAIL xxd foundInstall it: sudo apt install xxd on Debian, Ubuntu and Kali. On macOS it is part of the system.
FAIL hashlib.scrypt availableYour Python was built without a current OpenSSL. Install Python from your package manager, not from an old build.
python3: command not foundOn Windows, open WSL first. On some systems the program is called python: try python labcheck.py.

After a fix, run python3 labcheck.py again. Do not go on to lesson 1 until you see lab ready: 5 of 5.