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

RoleTypical scripting
SOC analystSearch and summarise logs; write SIEM queries; enrich alerts with lookups
Penetration testerAutomate recon; adjust public exploit code; write small web request scripts
Security engineerAutomate patching checks, user reviews, cloud configuration audits
Incident responderCollect evidence from many machines at once; parse artefacts
Malware analystDecode 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.log keeps matching lines; -i ignores case, -v inverts, -c counts, -E allows extended patterns.
  • awk '{print $11}' prints the 11th space-separated field. cut -d: -f1 /etc/passwd splits on colons.
  • sort | uniq -c | sort -nr is 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.

auth.log|
grep|
awk|
sort|
uniq -c|
sort -rn
11 lines after: cat auth.log
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
Step 1 of 6Here is the whole diary: 11 lines of everything that happened. Too much to read by eye on a real server, where there could be a million lines.
grep -i "Accepted" auth.log → 1 matching line
08:16:02 sshd: Accepted publickey for learner from 198.51.100.7

Hands-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.

Lab: who is attacking SSH?

1. Count how many lines contain 'Failed password'.

analyst@web1:~$

Check your understanding0 of 3 correct

Question 1

1.Why must sort come before uniq -c?

Question 2

2.What does >> do?

Question 3

3.Which grep option shows lines that do NOT match?

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

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.