Stage 6 of 7 · about 10–16 hours · beginner · checked 2026-11-26

Scripting, Git and regex

Goal. You can use enough Python, PowerShell or Bash, Git and regular expressions to automate a small task and keep your work under version control.

Why it matters#

Defenders repeat things: pulling the same fields from logs, checking a list of addresses, testing a rule. Doing it by hand is slow and error-prone. Scripts make it repeatable, Git records what changed and why, and regular expressions find patterns in messy text. You do not need to be a software developer.

What you will learn#

The command line#

Learn to move between folders, list and read files, search inside them, and connect commands with a pipe (|), which feeds one command's output into the next.

Task Bash (Linux, macOS) PowerShell
List files ls -la Get-ChildItem
Read a file cat file.log Get-Content file.log
Search text grep "failed" file.log Select-String "failed" file.log
Count lines wc -l file.log (Get-Content file.log).Count
Help man grep Get-Help Select-String

Pick one language first#

  • PowerShell is built into Windows and passes objects rather than text. Choose it if you work mostly with Windows.
  • Bash is the usual Linux shell, good for gluing tools together.
  • Python runs everywhere and suits anything with logic: parsing JSON, calling APIs, producing reports.

Whichever you pick, you need variables, conditions, loops, functions, reading and writing files, and error handling. Here is a small Python script that counts failed sign-ins by address in a file of JSON lines:

Python
import json
from collections import Counter

failures = Counter()
with open("signins.jsonl", encoding="utf-8") as handle:
    for line in handle:
        event = json.loads(line)
        if event.get("result") == "failure":
            failures[event.get("source_ip", "unknown")] += 1

for address, count in failures.most_common(10):
    print(f"{address}\t{count}")

Read it line by line: open the file, turn each line into a record, count the failures, print the top ten. The same in PowerShell:

PowerShell
Get-Content .\signins.jsonl |
  ConvertFrom-Json |
  Where-Object result -eq 'failure' |
  Group-Object source_ip |
  Sort-Object Count -Descending |
  Select-Object -First 10 Count, Name

Script safely#

  • Read a script before you run it, especially one you found online.
  • Never put passwords or keys in a script or a repository. Use environment variables or a secrets store.
  • Test on copies. Add a "show what would happen" mode before anything that deletes or changes.
  • Treat input from logs as untrusted: attackers control much of what ends up in them.

Regular expressions#

A regular expression (regex) is a pattern that describes text.

Pattern Matches
. Any one character
\d, \w, \s A digit, a word character, white space
+, *, ? One or more, zero or more, optional
{1,3} Between one and three times
^, $ Start and end of a line
[a-f0-9] One of the listed characters
(?:cmd|powershell) Either word, without capturing
\. A real full stop

For example, this finds things shaped like an IPv4 address:

Text
\b(?:\d{1,3}\.){3}\d{1,3}\b

It also matches 999.999.999.999, which is not an address. That is the lesson: a pattern describes shape, not meaning. Test it against text that should match and text that should not. Regex dialects differ between tools (Python, .NET, RE2, PCRE), and a badly written pattern can be very slow on large data.

Git#

Git records versions of files. The ideas:

  • A repository holds files and their history.
  • A commit is a saved snapshot with a message saying why.
  • A branch is a line of work you can change without disturbing the main one.
  • A remote is a copy elsewhere, for example on GitHub. push sends, pull fetches.
  • A pull request asks others to review a change before it is merged.

A first session:

Bash
git init
git status
git add notes.md
git commit -m "Add notes on failed sign-ins"
git switch -c improve-script
git diff
git log --oneline

Add a .gitignore file to keep out data, secrets and generated files. Once a secret is committed, deleting the file is not enough: it stays in history, so change the secret.

Data formats and APIs#

You will meet CSV (rows and columns), JSON (nested keys and values) and YAML (used by Sigma rules and many configurations, where indentation matters). An API lets a program ask a service for data, usually over HTTPS with a token. Respect rate limits and the service's terms.

The same invented record in YAML, where indentation shows what belongs to what and - starts a list item:

YAML
user: asha
source_ip: 192.0.2.10
result: failure
tags:
  - vpn
  - after-hours

Using AI assistants#

They are handy for explaining a script or drafting a regex. They also invent options and write subtly wrong code. Run nothing you cannot explain, and never paste real logs, secrets or personal data into a service you are not allowed to use for them.

Do this#

  1. Command line drill (1–2 hours). Using any log file from your own machine, count lines, search for a word and save the matches to a new file.
  2. Write the counter (2–3 hours). Make a small invented signins.jsonl with documentation addresses, run one of the scripts above, then change it to count by user and to show only counts above five.
  3. Regex practice (2 hours). Write patterns for an email address, a Windows file path and a timestamp. For each, list three strings that must match and three that must not.
  4. Version it (1–2 hours). Put your scripts in a Git repository with a .gitignore and a short README. Make a branch, change something, review the diff and merge it.

Resources#

Free first

Paid or needs an account

  • GitHub (free account) - hosts repositories and pull requests.

Checkpoint#

You are ready to move on when you can:

Go deeper (optional)#

  • Add tests to your script so a change that breaks it is noticed.
  • Detection Engineering stage 7 puts all of this to work.

0 / 5 checkpoints ticked

View this page on GitHub

9 lines · 59 stages · 77 pages 0 of 59 stages done