Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 31 ยท Practice, Interviews and Cheat Sheet

Practice Exercises with Solutions

Six hands-on defensive security exercises with solutions: severity scoring, log parsing, scan-model reading, cracking-time math, report writing and web logs.

Reading about security builds understanding, but only practice builds speed. A defender who has already written the "count failed logins per user" loop once will write it again in two minutes during an incident. This page gives you six short exercises that mirror everyday defensive work: ranking findings, parsing logs, reading scan results, estimating guessing time, writing report text and spotting probing in web logs.

Every exercise has the same four parts: a task description, a starter you can edit and run (it prints FAIL lines until you finish), a hint, and a complete solution. Try each one before you open the solution. All data is invented, all addresses are from the reserved documentation ranges, and nothing here scans or contacts anything. The runnable parts are Python stand-ins for the tools you would use on a real Kali machine, which cannot run in a browser.

Six exercises

Scoring Band and sort findings.
Log parsing Count failures per user.
Scan model Find unexpected open ports.
Guessing math How long would it take?

Then report writing and web log analysis.

1 / 2

Exercise 1: band and rank findings

A scan produced five findings with scores between 0 and 10. Write band(score) that returns Critical (9.0 and above), High (7.0 to 8.9), Medium (4.0 to 6.9), Low (0.1 to 3.9) or None (exactly 0). Then write sort_findings(findings) that returns the findings with the highest score first. Review Vulnerability Assessment Workflow if the bands are unfamiliar.

Python Exercise 1 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: test the largest boundary first (score >= 9), then fall through to smaller ones. For sorting, sorted(..., key=...) with reverse=True does the job.

Python Exercise 1 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2: failed logins per user

Given SSH log lines, return a Counter of failed logins per user name. Lines for invalid users read Failed password for invalid user admin from ..., lines for valid users read Failed password for root from .... Successful logins must not be counted. This is the same skill as in Project: Security Log Analyzer.

Python Exercise 2 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: the optional words invalid user can be matched with (?:invalid user )?, and the user name with (?P<user>\S+).

Python Exercise 2 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 3: read a scan model

A simulated scan gives each host a dictionary of port states. A policy lists the ports each host is allowed to expose. Write unexpected_open(scan, policy), which returns {host: [ports]} for open ports that the policy does not allow (leave out hosts with none), and hidden_by_firewall(scan), which returns (host, port) pairs that are filtered. Remember what filtered means from How Port Scanning Works: no answer, so the scanner cannot see behind it.

Python Exercise 3 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: build a set of the open ports for a host and subtract the allowed set; sort the result so the output is stable.

Python Exercise 3 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 4: how long would guessing take?

Attackers measure password strength in guesses. Write keyspace(pool, length) (the number of possible passwords), time_to_exhaust(pool, length, guesses_per_second) (seconds to try them all) and human(seconds) that formats seconds as seconds, minutes, hours, days or years with one decimal place. Use 365.25 days per year. Background: Password Strength, Entropy and Cracking Resistance.

Python Exercise 4 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: human can test from the largest unit down (>= 31557600 seconds is a year, >= 86400 a day, and so on).

Python Exercise 4 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice what the last three lines show: going from 8 lowercase letters to 12 mixed characters moves the estimate from seconds to millions of years, but only against a fast hash with a random password. Against a slow hash such as bcrypt the guess rate is many orders of magnitude lower, which is why storage matters as much as the password itself.

Exercise 5: write report text

A finding is a dictionary with id, title, severity, asset and fix. Write render_finding(f) returning three lines: [SEVERITY] ID: Title, Asset: ... and Fix: ..., with the severity in uppercase. Then write executive_summary(findings) that returns text like 3 findings: 1 Critical, 2 High., counting severities in the order Critical, High, Medium, Low and skipping zero counts. Compare your phrasing with Writing a Professional Security Report.

Python Exercise 5 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: watch the singular and plural, and build the severity list with a loop over the fixed order instead of the order in the data.

Python Exercise 5 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 6: spot probing in a web log

Using the common log format, write status_classes(lines) that returns a Counter of 2xx, 3xx, 4xx, 5xx groups, and probers(lines, min_misses=3) that returns the sorted list of client addresses with at least min_misses responses of status 404. Skip lines that do not match. See Reading Web Server Logs for Attacks.

Python Exercise 6 starter

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Hint: one regular expression with groups for the address and the status can serve both functions, so write a small helper that yields (ip, status) pairs.

Python Exercise 6 solution

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where to go from here

Change the data and predict the output before you run the code. Add a failing line to Exercise 2, an extra host to Exercise 3, or a 16-character password to Exercise 4. Then combine the pieces: parse a log, score the findings, and produce a one-paragraph report. That is a miniature version of the work in Project: Vulnerability Risk Register.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In Exercise 3, why does the solution sort the list of unexpected ports?

    A set has no defined order. Sorting turns it into a predictable list that a test or a report reader can rely on.
  2. 2. Why is the "time to guess" estimate in Exercise 4 not a real-world promise?

    Human-chosen passwords are guessed far faster than the keyspace suggests, and slow hashes lower the guess rate dramatically. The figure is only an upper-bound model.
  3. 3. What is the main benefit of writing small check calls like those in the exercises?

    Small checks are the seed of unit tests. They turn a vague "it seems to work" into a precise pass or fail for each behavior.
  4. 4. In Exercise 6, why does probers count only status 404 for the same address?

    A single client requesting many missing paths is a typical probing pattern. It still needs human review, since broken links also cause 404 answers.

Summary

  • Practice with small, testable functions: banding, parsing, set comparison, arithmetic and formatting cover much of daily defensive work.
  • Collect and sort results so output is stable, and write a check for every behavior.
  • Scan results are views from one vantage point; filtered means unknown, not safe.
  • Guess-time estimates are models: they depend on randomness, the guess rate and the hash.
  • Reports need exact, consistent wording, including singular and plural.

Next: Security Interview Questions.