Part 1 · Basics · Lesson 11 · 40 minutes
Build a script that triages a login log
- Turn a pipe of text tools into a script that takes a file name
- Print a report that shows on the screen and is saved in a file
- Make the script runnable only by you and your group, and pack it with a fingerprint
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
Your team lead has a habit. Each morning she asks the same three questions about the login log of a server: how many logins failed, from which addresses, and did anyone get in? So far you typed the same pipes each time. Now she says: "Put it in one script that I can run on any log, and give me a file that I can attach to a ticket." You have the tools. This lesson joins them.
Recall
What does "tee" do in "COMMAND | tee report.txt"?
It writes the output of COMMAND into report.txt and also prints it on the screen. tee -a adds to the end of the file instead of replacing it.
A script has the mode 750. Who may run it?
The owner (7: read, write, run) and the group (5: read and run). Others have no access (0).
The idea
Triage means a first quick look that decides what needs attention now. For a login log, three questions give the first picture: how many logins failed, which addresses made them, and which logins were accepted. The third question matters most. A run of failures and then an accepted login from the same address is what password guessing looks like when it works.
Each question is a tool of this part. The script runs them in order, and $1 (lesson 3) is the name of the log that you give.
- Log file → Count failures: grep -c counts the failed lines
- Count failures → Rank addresses: grep, awk, sort, uniq -c, sort -rn
- Rank addresses → List successes: grep Accepted, awk picks the fields
- List successes → Report: it prints; tee saves it later
Worked example
1. Make a login log to work on
The lines are fixed, so that you get the same numbers as this page. Run the block once. The folder triage is the lab of this lesson.
mkdir -p ~/krlab/linux-basics/triage && cd ~/krlab/linux-basics/triage
cat > auth.log <<'EOF'
Nov 22 02:14:05 krbox sshd[3101]: Failed password for root from 198.51.100.23 port 52110 ssh2
Nov 22 02:14:09 krbox sshd[3101]: Failed password for root from 198.51.100.23 port 52112 ssh2
Nov 22 02:14:14 krbox sshd[3104]: Failed password for invalid user test from 198.51.100.23 port 52118 ssh2
Nov 22 02:31:40 krbox sshd[3120]: Failed password for admin from 203.0.113.9 port 40001 ssh2
Nov 22 02:31:44 krbox sshd[3120]: Accepted password for admin from 203.0.113.9 port 40003 ssh2
Nov 22 07:55:02 krbox sshd[3188]: Accepted publickey for dana from 192.0.2.44 port 51234 ssh2
Nov 22 08:03:17 krbox sshd[3190]: Failed password for dana from 192.0.2.44 port 51300 ssh2
Nov 22 08:03:25 krbox sshd[3190]: Failed password for dana from 192.0.2.44 port 51301 ssh2
Nov 22 08:03:41 krbox sshd[3190]: Accepted publickey for dana from 192.0.2.44 port 51302 ssh2
Nov 22 09:12:30 krbox CRON[3300]: pam_unix(cron:session): session opened for user root
EOF
wc -l auth.log10 auth.log2. Ask the three questions by hand
Test each question as a command before it goes into a script. grep -c counts the lines that match (lesson 4). In a failed line, the address is the fourth field from the end, $(NF-3) (lesson 5). In an accepted line, the user is field 9 and the address is field 11. sort and uniq -c count each address, and sort -rn puts the biggest count first (lesson 4).
grep -c "Failed password" auth.log
grep "Failed password" auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn
grep "Accepted" auth.log | awk '{print $9, $11}'6
3 198.51.100.23
2 192.0.2.44
1 203.0.113.9
admin 203.0.113.9
dana 192.0.2.44
dana 192.0.2.44Read the result as a triage note. One address made half of the failures. Another address failed once and then got in as admin. The third address is a user who mistyped twice and then used a key.
3. Put the questions into a script
A script is a text file of commands (lesson 3). Add one echo line before each question, so that the reader of the report knows what each block is. Write "$1" with double quotes (lesson 3), so that a file name with a space stays one word. Save the text below as triage.sh in the folder triage, with nano or vim (lesson 7), and run it with bash.
triage.sh
#!/bin/bash
# triage.sh - counts and ranks the failed logins in an auth log
# usage: bash triage.sh LOGFILE
echo "Log file: $1"
echo "Failed logins:"
grep -c "Failed password" "$1"
echo "Failures by address (count, address):"
grep "Failed password" "$1" | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn
echo "Accepted logins (user, address):"
grep "Accepted" "$1" | awk '{print $9, $11}'bash triage.sh auth.logLog file: auth.log
Failed logins:
6
Failures by address (count, address):
3 198.51.100.23
2 192.0.2.44
1 203.0.113.9
Accepted logins (user, address):
admin 203.0.113.9
dana 192.0.2.44
dana 192.0.2.444. Save the report and close the script
tee (lesson 6) shows the report and saves it. chmod 750 (lesson 10) lets you and your group run the script, lets only you change it, and shuts everyone else out. ./triage.sh runs a script by its own name (lesson 3).
chmod 750 triage.sh
./triage.sh auth.log | tee report.txt
ls -l triage.shLog file: auth.log
Failed logins:
6
Failures by address (count, address):
3 198.51.100.23
2 192.0.2.44
1 203.0.113.9
Accepted logins (user, address):
admin 203.0.113.9
dana 192.0.2.44
dana 192.0.2.44
-rwxr-x--- 1 you you 391 Nov 22 09:15 triage.shThe script has one weakness. Give it a name that does not exist, and send the errors to a file (lesson 6):
./triage.sh nofile.log 2> errors.txt
cat errors.txtLog file: nofile.log
Failed logins:
Failures by address (count, address):
Accepted logins (user, address):
grep: nofile.log: No such file or directory
grep: nofile.log: No such file or directory
grep: nofile.log: No such file or directoryThe report on the screen looks like a log with nothing in it, which is wrong: the log was never read. The three error lines are the proof. A script that you hand to others should stop at the first error. Part 2 teaches that.
5. Pack the kit and fingerprint it
tar -czf packs files (lesson 9), tar -tf lists the archive before anyone extracts it, and sha256sum prints the fingerprint that the reader of your ticket can compare.
tar -czf triage-kit.tar.gz triage.sh report.txt
tar -tf triage-kit.tar.gz
sha256sum triage-kit.tar.gz > triage-kit.tar.gz.sha256
sha256sum -c triage-kit.tar.gz.sha256triage.sh
report.txt
triage-kit.tar.gz: OKYour fingerprint differs from the fingerprint on another machine, because the archive holds times and owners.
Your turn
A second server, gate, has a log too. Run this block once. It builds the data.
cd ~/krlab/linux-basics/triage
cat > gate.log <<'EOF'
Nov 22 01:02:11 gate sshd[811]: Failed password for root from 192.0.2.150 port 33010 ssh2
Nov 22 01:02:15 gate sshd[811]: Failed password for root from 192.0.2.150 port 33012 ssh2
Nov 22 01:02:19 gate sshd[811]: Failed password for invalid user oracle from 192.0.2.150 port 33014 ssh2
Nov 22 01:02:24 gate sshd[811]: Failed password for invalid user git from 192.0.2.150 port 33016 ssh2
Nov 22 01:02:30 gate sshd[811]: Failed password for root from 192.0.2.150 port 33018 ssh2
Nov 22 01:40:02 gate sshd[840]: Failed password for ravi from 198.51.100.8 port 41000 ssh2
Nov 22 01:40:09 gate sshd[840]: Accepted password for ravi from 198.51.100.8 port 41002 ssh2
Nov 22 02:00:00 gate sshd[850]: Failed password for root from 203.0.113.61 port 50400 ssh2
Nov 22 02:00:05 gate sshd[850]: Failed password for root from 203.0.113.61 port 50402 ssh2
Nov 22 03:15:44 gate sshd[870]: Accepted publickey for lena from 192.0.2.30 port 60100 ssh2
Nov 22 03:15:50 gate sshd[870]: pam_unix(sshd:session): session opened for user lena
Nov 22 03:30:12 gate sshd[890]: Accepted password for root from 192.0.2.150 port 33100 ssh2
EOFThere are no steps here. Do this, with your own triage.sh:
- Run the script on
gate.logand save the report ingate-report.txt, so that it also shows on the screen. - Change the script so that it also prints the line
Accepted logins in total:and then the number of accepted logins. Run it ongate.logagain. - Read the report as a triage note. Which address looks like password guessing, and what happened to the account that it attacked?
Check yourself
Why does the script use "$1" and not a fixed file name?
$1 is the first word after the name of the script, so the same script works on every log. A fixed name would make you edit the script for each file.
The script prints "Failed logins:" and then nothing, but no error shows in the report. What went wrong?
The error did not go to the file. grep writes errors to stderr, which stays on the screen, and tee saves only stdout. Run the script with 2>&1 to put both in the report, or check that the log exists before you start.
Five failures from one address is not proof of an attack. What else do you read in the report?
The accepted logins. Failures followed by an accepted login for the same account, from the same address, is the pattern that matters. Failures alone may be a user who forgot a password, or a scan that failed.
Field notes
- On Ubuntu and Debian,
sshdwrites this log in/var/log/auth.log. Other systems keep it in the journal. Old logs are rotated into files such asauth.log.1, so run the script on each file of the period that you examine. - An address is not a person. Many users can share one address behind a company network or a VPN, and a guessing attack can use many addresses, each with a few tries. Count by address and by user name.
- MITRE ATT&CK names this behaviour "Brute Force: Password Guessing" (T1110.001). Failures and then a success is the signal that a detection rule looks for.
- Keep the original log and the fingerprint of the kit. A report that you cannot reproduce from the original is worth less as evidence.
- The script is a first look, not a detection system. Part 2 shows how to write a script that checks its input and stops on an error.