LESSON 28 · BEGINNER4 min read

Project: Build a Tested Log Triage Script

Turn a messy authentication log into a deterministic JSON and Markdown triage report with tests and documented assumptions.

OWASP Top 10 · MITRE ATT&CK · NIST CSF 2.0Free to readModule 6 of 14
YOUR NEXT SMALL WINS

By the end, you’ll be able to…

  • Parse a realistic authentication log into structured records.
  • Aggregate failures by source and detect a threshold-based signal.
  • Write JSON and Markdown reports with provenance and timestamps.
  • Test normal, empty, malformed, and duplicate input cases.

01Define the Question Before the Code

A useful triage script answers a specific question. For this project: which source addresses produced more than five failed SSH authentication attempts in the supplied log, when did each source first and last appear, and which accounts were targeted? The output should help a human decide what to investigate next, not replace the human.

Write the question in a README before you write code. Then define the input format with three or four real examples. Decide how to handle malformed lines, missing timestamps, IPv6 addresses, and duplicate events. Decide whether the threshold is inclusive or exclusive, and whether it applies per source, per account, or both. Ambiguity in the definition becomes a bug in the implementation.

Keep the first version small. A script that parses one known format and produces a clear report is more valuable than a framework that handles every format badly. Extend it only after the basic output is trustworthy.

02Parse With Provenance

Read the log line by line. Skip blank lines, but count them. For each non-empty line, attempt to extract a timestamp, a source address, an outcome, and a username. If a required field is missing, append a structured error with the line number and continue. Never let one malformed line stop the entire report.

Store each parsed event with its source file and line number. That provenance lets an analyst open the original line and verify your extraction. Normalise timestamps to UTC and keep the original string when parsing fails. Use a compiled regular expression or a structured parser, and add a comment explaining the format you expect.

Do not trust the source address as identity. The report should say 'source address', not 'attacker'. The same address can be a shared proxy, a VPN exit, or a compromised host. Your language shapes how the reader interprets the evidence.

triage.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

03Report for a Human and a Machine

The JSON report is for automation and reproducibility. The Markdown report is for a person who needs to act. Generate the Markdown from the same structured data so the two cannot disagree. Include the threshold, total events, unique sources, flagged sources, and parse errors. Round or format values consistently.

Add a short interpretation section that explains what the data does and does not prove. For example: 'Repeated failures from one address may indicate password guessing, a misconfigured client, or a shared NAT. Confirm with successful-login logs, endpoint telemetry, and asset ownership before blocking an address.' This prevents the report from overstating certainty.

End with recommended next steps. A good triage report might suggest checking whether any flagged source succeeded, whether the targeted accounts exist, whether the address is internal, and whether other hosts saw the same pattern. The report should make the next question obvious.

render_report.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.

04Project Brief: Tests and Evidence

Create a small test suite with four cases: a normal log with repeated failures, an empty log, a malformed line mixed with valid lines, and duplicate events. Assert the total event count, the flagged sources, the error count, and the first and last timestamps. Run the tests before you publish the tool.

Include a README with the question, input format, usage, output schema, assumptions, and limitations. Add a sample synthetic log and the generated reports. Do not include real credentials or production data. If you publish the repository, confirm that the history contains no secrets.

The project is complete when another analyst can run the script on the sample log, get the same reports, read the interpretation, and know what to investigate next. That is the difference between a coding exercise and a security tool.

test_triage.py🐍Python
Ctrl + ↵
Output
Click Run or press Ctrl+Enter
Output will appear here after you run the code.
LET IT CLICK

The ideas worth keeping.

  • Define the investigative question and input format before writing code.
  • Preserve line-level provenance and report parse errors instead of dropping them.
  • Generate human and machine reports from the same structured data.
  • Test empty, normal, malformed, and duplicate input before trusting the tool.
TURN UNDERSTANDING INTO SOMETHING YOU CAN DO

Make it yours.

Type and test your code right here in the interactive workspace. Run your solution, check the output, and compare with the worked guide when ready.

01

Exercise 1: Add an account threshold

Extend the analysis so a source is also flagged when it targets more than two distinct usernames, even if its total failure count is below the event threshold. Describe the data structure change and the new output field.

Explore a worked solution
one_possible_solution.mdCybersecurity
Keep the per-source user set while aggregating. After counting, build a second condition: len(row['users']) > 2. Add 'reason' to each flagged row with values like 'failure_threshold', 'account_spray', or 'both'. The flagged dictionary then includes sources that meet either condition, and the report explains which rule fired. Add tests for a source that hits only the account threshold and one that hits both.

Spraying many accounts is a different pattern from hammering one account. Separate rules make the alert explain itself and reduce analyst guesswork.

02

Exercise 2: Make the script fail safely

The script currently writes triage-report.json in the current directory. Describe three safety improvements for file handling and one for command-line usage.

Explore a worked solution
one_possible_solution.mdCybersecurity
1. Require an explicit --output directory and create it only if it does not exist, resolving and checking the path stays inside the intended workspace. 2. Write to a temporary file and rename it atomically so a crash does not leave a half-written report. 3. Refuse to overwrite an existing report unless --force is supplied, preventing accidental evidence loss. 4. Provide --help with usage, require --input, validate that the input exists and is a regular file, and exit non-zero with a clear message on failure.

Security tools should not destroy evidence or write outside their workspace. Atomic writes and explicit paths reduce the chance of a partial or misplaced report.

A SMALL QUESTION. A USEFUL NEXT STEP.

What stayed with you?

Check the ideas while they’re fresh. Your answers stay in this page and feedback appears as you go.

A LITTLE ACTIVE RECALL0 / 4 answered
01Why should the JSON and Markdown reports be generated from the same data?
02What does a threshold of 'more than five failures' mean?
03Which test case is most important for a log parser?
04Why is 'source address' better wording than 'attacker IP'?

Keep exploring.

The official references behind this lesson, when you want a second angle or the full details.

MODULE 06 · PRACTICAL SELF-CHECK

Pause. Put the pieces together.

You’ve reached the end of Python for Security Automation. Before moving on, use this task to check what you can do independently.

Your log-triage script reads synthetic data and writes a summary with clear limits, and your port scanner returns reproducible JSON and CSV against a lab host.

Try it in your own environment and write down the result. This is a self-check; reading completion does not evaluate your code.

Review this module

One lesson closer.

Reading is a start. Trying it yourself makes the connection.

Completion is saved in this browser. It records reading and hands-on practice progress.