Module 4 · Programming for Cyber Security · Lesson 10 of 75
Why security people code, and Bash for log triage
Where scripting fits in security jobs, Bash variables, loops and pipes, and the grep, awk, sort and uniq one-liners analysts use every day.
In this lesson · about 35 minutes
- Explain which security tasks are best automated
- Chain grep, awk, sort and uniq to summarise a log
- Read and write a short Bash script with a variable, a loop and a condition
Where code shows up in security work
| Role | Typical scripting |
|---|---|
| SOC analyst | Search and summarise logs; write SIEM queries; enrich alerts with lookups |
| Penetration tester | Automate recon; adjust public exploit code; write small web request scripts |
| Security engineer | Automate patching checks, user reviews, cloud configuration audits |
| Incident responder | Collect evidence from many machines at once; parse artefacts |
| Malware analyst | Decode strings, unpack samples, write detection rules |
The three languages that matter most: Bash (Linux), Python (everything) and PowerShell (Windows). Being able to read JavaScript, PHP and SQL helps a lot in web testing.
Bash building blocks
- Pipes
|send one command's output into the next. Redirects>(overwrite) and>>(append) send it to a file. grep "Failed password" auth.logkeeps matching lines;-iignores case,-vinverts,-ccounts,-Eallows extended patterns.awk '{print $11}'prints the 11th space-separated field.cut -d: -f1 /etc/passwdsplits on colons.sort | uniq -c | sort -nris the classic "count and rank" pattern.head,tail -f(follow a live log),wc -l(count lines).
A tiny script
#!/bin/bash
# alert-if-busy.sh: warn when one address fails to log in too often
LOG=/var/log/auth.log
LIMIT=5
grep "Failed password" "$LOG" | awk '{print $(NF-3)}' | sort | uniq -c |
while read -r count ip; do
if [ "$count" -ge "$LIMIT" ]; then
echo "ALERT: $ip failed $count times"
fi
done
$(NF-3) means "the fourth field from the end", which is where the IP address sits in an sshd line. Make the script runnable with chmod +x alert-if-busy.sh and run it with ./alert-if-busy.sh. Always quote variables ("$LOG") so file names with spaces do not break things.
Try it · watch it happen
A log search, one stage at a time
Watch the log flow through each command in a pipeline. Pause between stages and predict what the next one will keep or change.
08:00:04 sshd: Server listening on 0.0.0.0 port 2208:00:41 CRON: session opened for user root08:00:58 sudo: admin : COMMAND=/usr/bin/apt update08:01:12 sshd: Failed password for root from 203.0.113.4508:01:15 sshd: Failed password for root from 203.0.113.4508:01:19 sshd: Failed password for invalid user admin from 203.0.113.4508:05:00 CRON: session closed for user root08:15:40 sshd: Failed password for learner from 198.51.100.708:16:02 sshd: Accepted publickey for learner from 198.51.100.708:30:11 sshd: Failed password for invalid user test from 203.0.113.4508:31:40 sshd: Failed password for root from 203.0.113.45
08:16:02 sshd: Accepted publickey for learner from 198.51.100.7Hands-on lab (simulated terminal)
Lab: who is attacking SSH?
Your lab web server's /var/log/auth.log has thousands of lines. Find the noisiest attackers in four commands. Output is simulated.
Step 1 of 4. Type each command and press Enter. Nothing runs on your computer.
1. Count how many lines contain 'Failed password'.
Check your understanding0 of 3 correct
Quiz score: 0 of 3
Written by Noviq Academy in its own words. All labs are simulations: no real systems are touched, and every name, address, password and hash is made up. The offensive techniques taught here are for defending systems and for authorised testing only; never scan, access or test a system without the owner's written permission. Tool and product names belong to their owners, and command output may differ slightly from yours. Read-aloud uses your device's built-in voices.