Objective. Students write Bash scripts that automate common security-administration tasks including user auditing, log parsing, and system health checks.
This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.
VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.
Cybersecurity professionals spend significant time on repetitive tasks: auditing user accounts, scanning logs for threats, and checking system health. Bash scripting lets you encode those tasks once and run them reliably whenever needed. Because Bash is the default shell on most Linux distributions, your scripts run without installing additional software — an important advantage in locked-down enterprise environments. CompTIA CySA+ explicitly tests your ability to use scripting and automation to support security operations, so these skills map directly to your certification goals.
A Bash script is a plain-text file that begins with a shebang line: `#!/bin/bash`. This tells the kernel which interpreter to use. Make the file executable with `chmod +x scriptname.sh`, then run it with `./scriptname.sh`.
Key constructs you will use in every security script:
Poor scripting habits create security vulnerabilities. Follow these rules:
1. Always quote variables. Unquoted variables containing spaces or special characters cause unexpected behavior and can be exploited.
2. Validate input. If your script accepts arguments, check that the expected argument exists and is the correct type before using it: `if [ -z "$1" ]; then echo "Usage: $0 <logfile>"; exit 1; fi`.
3. Use `sudo` only where necessary. Reading `/etc/shadow` requires elevated privilege. Request it explicitly for that command rather than running the entire script as root.
4. Avoid storing credentials in scripts. Never hard-code passwords or API keys.
5. Restrict output file permissions. Reports containing account data are sensitive. Set permissions with `chmod 600 reportfile.txt` immediately after creation.
6. Test in a non-production VM. The lab machines for this course are isolated VMs. Never run untested scripts on production systems.
Three system files power user auditing:
Use `awk -F: '$3 == 0 {print $1}' /etc/passwd` to extract UID-0 accounts. Use `sudo awk -F: '$2 == "" {print $1}' /etc/shadow` to find accounts with no password hash.
`/var/log/auth.log` (Debian/Ubuntu) records authentication events. Failed SSH logins appear as lines containing `Failed password`. The source IP address follows the word `from`.
Pipeline pattern:
```
grep "Failed password" "$LOGFILE" | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn
```
Breaking it down: `grep` filters matching lines; `awk` extracts the IP field (position varies by log format — verify against a real sample); `sort` prepares for deduplication; `uniq -c` counts consecutive identical lines; the final `sort -rn` ranks by count descending.
Accept the log path as `$1` so the script is reusable against any log file.
Extending the log parser to take action is the core of automated threat response. Set a threshold variable at the top of the script: `THRESHOLD=${2:-10}` (uses the second argument if provided, otherwise defaults to 10). After ranking, loop through results and compare each count to the threshold:
```bash
while read -r count ip; do
if [ "$count" -gt "$THRESHOLD" ]; then
sudo ufw deny from "$ip"
echo "$(date --iso-8601=seconds) BLOCKED $ip ($count attempts)" >> audit.log
fi
done
```
`ufw deny from` adds a firewall rule blocking all traffic from that IP. Logging the action with a timestamp creates an audit trail — essential for incident response documentation and compliance.
After writing each script, test it with known data before using it on real systems. Your instructor supplies a sample `auth.log` designed to trigger specific outputs — use it to verify correctness. Check syntax without executing with `bash -n scriptname.sh`. Use `set -x` at the top during debugging to print each command as it runs, then remove it before submission.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.