Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Scenario test · 60 minutes

Investigate a break-in on a server

After this lesson you can
  • Find the attack in a log, a user list, a key file, a disk report, a process list and the schedules
  • Prove with hashes and modes which parts of a server were changed
  • Contain the incident with a checked sudo rule and write a report that another person can check
Lab files of this lesson (28)

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

You work for a small company. At 02:50 on 21 November the monitoring of the server krbox raised two alerts: a new account, svc-backup, and a disk that is full. Nobody on the team made the account. The person on call did not touch the server. They saved the output of some commands and some files in a folder, and now the folder is yours.

Facts that you know. The staff member Dana works from the address 192.0.2.44. The team's own maintenance key has the comment maint@build. The folder bin holds copies of three programs, and the file SHA256SUMS holds their hashes from last week. The server keeps its clock in UTC, so every time in the log is UTC. Work only on the copies, and run nothing from them.

Save this script as build-case.sh in ~/krlab/linux-admin, and run it once with bash build-case.sh. It builds the folder case-krbox with fixed content, so every reader has the same data. It is not a task.

build-case.sh

#!/bin/bash
# build-case.sh - builds the case folder with fixed content
set -e
mkdir -p ~/krlab/linux-admin && cd ~/krlab/linux-admin
rm -rf case-krbox
mkdir -p case-krbox/bin case-krbox/tmp case-krbox/home/svc-backup/.ssh
cd case-krbox

cat > auth.log <<'LOG'
Nov 21 01:12:09 krbox sshd[2001]: Accepted publickey for dana from 192.0.2.44 port 52100 ssh2
Nov 21 01:55:31 krbox sshd[2044]: Failed password for dana from 192.0.2.44 port 52180 ssh2
Nov 21 01:55:38 krbox sshd[2044]: Accepted password for dana from 192.0.2.44 port 52180 ssh2
Nov 21 02:30:15 krbox sshd[3010]: Failed password for invalid user admin from 203.0.113.9 port 33001 ssh2
Nov 21 02:30:19 krbox sshd[3012]: Failed password for invalid user ubuntu from 203.0.113.9 port 33003 ssh2
Nov 21 02:30:24 krbox sshd[3014]: Failed password for invalid user pi from 203.0.113.9 port 33005 ssh2
LOG
for i in 1 2 3 4 5 6 7 8 9 10 11 12; do
    printf 'Nov 21 02:40:%02d krbox sshd[%d]: Failed password for root from 198.51.100.23 port %d ssh2\n' \
        $((i * 4)) $((3100 + i)) $((41000 + i)) >> auth.log
done
cat >> auth.log <<'LOG'
Nov 21 02:41:07 krbox sshd[3150]: Accepted password for root from 198.51.100.23 port 41050 ssh2
Nov 21 02:44:30 krbox useradd[3160]: new user: name=svc-backup, UID=0, GID=0, home=/home/svc-backup, shell=/bin/bash
Nov 21 02:46:02 krbox sshd[3170]: Accepted publickey for svc-backup from 198.51.100.23 port 41070 ssh2
LOG

cat > passwd <<'TXT'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
dana:x:1000:1000:Dana:/home/dana:/bin/bash
svc-backup:x:0:0::/home/svc-backup:/bin/bash
TXT

cat > home/svc-backup/.ssh/authorized_keys <<'TXT'
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGq7e1f0dummykeyAAAA0001 maint@build
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHx9z2k8dummykeyBBBB0002 root@kali
TXT

cat > ports.txt <<'TXT'
State  Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*
LISTEN 0      511          0.0.0.0:80        0.0.0.0:*
LISTEN 0      5            0.0.0.0:4444      0.0.0.0:*    users:(("cachefix",pid=3187,fd=3))
LISTEN 0      4096       127.0.0.1:5432      0.0.0.0:*
TXT

cat > df.txt <<'TXT'
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda1      ext4   20G  7.1G   12G  38% /
/dev/vdb1      ext4   50G   48G  800M  99% /var
tmpfs          tmpfs 1.9G     0  1.9G   0% /dev/shm
TXT

printf '4.0K\t/var/tmp\n1.2G\t/var/log\n48M\t/var/cache\n44G\t/var/lib\n310M\t/var/backups\n46G\t/var\n' > du.txt

cat > ps.txt <<'TXT'
  PID  PPID USER      NI STAT ARGS
    1     0 root       0 Ss   /sbin/init
  612     1 root       0 Ss   /usr/sbin/sshd -D
  640     1 www-data   0 S    nginx: worker process
 2211   640 www-data   0 Z    [sh] <defunct>
 3187     1 root       0 Ss   /tmp/.cachefix -l 4444
 3190  3187 root       0 S    sleep 3600
 3301     1 backup    10 SN   /usr/local/bin/backup.sh
TXT

cat > crontab.txt <<'TXT'
# m h dom mon dow command
0 3 * * * /usr/local/bin/backup.sh
*/5 * * * * /tmp/.cachefix -l 4444 >/dev/null 2>&1
TXT

cat > cachefix.service <<'TXT'
[Unit]
Description=cache fix helper

[Service]
ExecStart=/tmp/.cachefix -l 4444
Restart=always
User=root

[Install]
WantedBy=multi-user.target
TXT

printf '#!/bin/sh\necho ls v1\n' > bin/ls
printf '#!/bin/sh\necho ps v1\n' > bin/ps
printf '#!/bin/sh\necho ss v1\n' > bin/ss
sha256sum bin/ls bin/ps bin/ss > SHA256SUMS
echo "# patched" >> bin/ps

printf '#!/bin/sh\n' > tmp/.cachefix
echo "temp notes" > tmp/notes.txt
chmod 4755 tmp/.cachefix
chmod 644 tmp/notes.txt
echo "built ~/krlab/linux-admin/case-krbox"

The folder holds: auth.log (the login log), passwd (a copy of the user list), home/svc-backup/.ssh/authorized_keys, ports.txt (a saved list of listening sockets), df.txt (a saved df -hT), du.txt (a saved du -h --max-depth=1 /var), ps.txt (a saved ps -eo pid,ppid,user,ni,stat,args), crontab.txt (the crontab of root), cachefix.service (a unit file that was found), tmp (files from /tmp, with their modes kept), bin and SHA256SUMS.

Your tasks

Work in ~/krlab/linux-admin. There are no commands here. Choose the tools yourself.

  1. The login log, with a script. Write triage.sh. It takes a log file as its only argument and prints two lines: failed: N (the number of failed password logins) and top: ADDRESS (the address with most of them). Without an argument it prints a usage line to stderr and exits with 2. Run it on case-krbox/auth.log, and find the time of the first successful login from that address.
  2. The accounts. Find every account with the user ID 0 besides root. Read the key file of svc-backup, and decide which key does not belong to the team.
  3. The host. Find the file in tmp that has the SUID bit. In ports.txt, find the port that listens on all addresses and is neither SSH nor the web server. Check the programs in bin against SHA256SUMS. Then run your audit.sh of lesson 13 on the folder case-krbox, with the log as AUDIT_LOG, and count the lines of its SUID section.
  4. The disk, the processes and the schedules. Read df.txt, du.txt, ps.txt, crontab.txt and cachefix.service like the output of the commands of lessons 3, 5, 8 and 12.
  5. Contain it on your practice system. Make the group kr_ir (GID 2300) and the account kr_oncall (UID 2300, primary group kr_ir). Write a sudo rule in /etc/sudoers.d/kr_ir that lets the group kr_ir run /usr/bin/systemctl stop cachefix as root without a password, and nothing else. Check it with visudo before you install it.

Answer the results here.

The page cannot read your system, so test the rule as well. The command sudo -u kr_oncall sudo -n /usr/bin/systemctl stop cachefix must not ask for a password. Your practice system has no unit cachefix, so systemctl answers that the unit is not loaded: that answer shows that the rule let the command through.

Hand in

Write a short report in the file ~/krlab/linux-admin/case-krbox/report.md. It is the proof of your work, and you can show it to an employer. Keep it to one page, with these headings:

  1. Summary. Three sentences for a manager: what happened, how sure you are, what you need decided now.
  2. Timeline. A table of time and event, in order, with the same time zone throughout (name it in the heading).
  3. Evidence. For each fact in the timeline: the file, the line or the command, and what it shows. A reader must be able to check it.
  4. Impact. What the attacker could do, and what you do not know.
  5. Actions. What the team does now and later, in order, each with a reason.
  6. Open questions. What you could not tell from this evidence.

Do not claim more than the evidence shows. Mark every guess as a guess.

Review your work

One model solution

Results. 16 failed password logins; the address 198.51.100.23 made 12 of them; its first success was at 02:41:07. The accounts with ID 0 are root and svc-backup. The key file holds 2 keys, and root@kali is not the team's key. The SUID file is tmp/.cachefix, the port is 4444 (process name cachefix), and bin/ps fails its hash check. The disk /var is at 99%, and /var/lib is the biggest folder below it (44G). /tmp/.cachefix runs as PID 3187, started by cron every 5 minutes and by a unit with Restart=always. The zombie belongs to PID 640 (an nginx worker).

How to find them. Each result comes from tools of this course, for example:

cd ~/krlab/linux-admin
./triage.sh case-krbox/auth.log
cd case-krbox
grep Accepted auth.log | grep 198.51.100.23 | head -n 1
awk -F: '$3 == 0 { print $1 }' passwd
wc -l < home/svc-backup/.ssh/authorized_keys
find . -type f -perm -4000
grep -v ' 127.0.0.1:' ports.txt
sha256sum -c SHA256SUMS
cd ..
AUDIT_LOG=case-krbox/auth.log ./audit.sh case-krbox | sed -n '/== SUID/,/== World/p'
cd case-krbox
awk 'NR > 1 && $6 + 0 > 90 { print $7 }' df.txt
sort -rh du.txt | sed -n 2p
grep cachefix ps.txt
awk '$5 ~ /^Z/ { print $2 }' ps.txt
grep -v '^#' crontab.txt
grep '^Restart' cachefix.service

A model triage.sh:

#!/usr/bin/env bash
set -euo pipefail
[[ $# -eq 1 ]] || { echo "usage: $0 LOG" >&2; exit 2; }
echo "failed: $(grep -c 'Failed password' "$1" || true)"
echo "top: $(grep 'Failed password' "$1" | grep -oE 'from [0-9.]+' | cut -d' ' -f2 | sort | uniq -c | sort -rn | head -n 1 | awk '{ print $2 }')"

For task 5, with the rule in a file first: echo '%kr_ir ALL=(root) NOPASSWD: /usr/bin/systemctl stop cachefix' > ir.sudo, then visudo -cf ir.sudo, then sudo install -m 440 -o root -g root ir.sudo /etc/sudoers.d/kr_ir. sudo groupadd -g 2300 kr_ir and sudo useradd -m -u 2300 -g kr_ir kr_oncall make the group and the account. The second command of the last check is refused, because the rule names stop, not start. Remove the lab account, the group and the rule when you finish.

A model report.

Incident report: krbox, 21 November (all times UTC, as in auth.log)

Summary
An attacker most likely guessed the root password (12 failures from
198.51.100.23, then a success), logged in as root, made a second root account, svc-backup, and came back with a key. A program
in /tmp keeps itself alive with cron and a service. We are sure of the login
and the account. We need a decision to take the server offline.

Timeline
02:40:04-02:40:48  12 failed root logins from 198.51.100.23
02:41:07           root login accepted from 198.51.100.23
02:44:30           account svc-backup created with UID 0
02:46:02           svc-backup logs in with a key from 198.51.100.23

Evidence
auth.log: 16 failed logins (triage.sh); 12 of them from 198.51.100.23
  (grep 'Failed password' auth.log | grep -c 198.51.100.23)
auth.log, 02:41:07: "Accepted password for root from 198.51.100.23"
passwd: svc-backup has UID 0, so it has the rights of root
authorized_keys of svc-backup: 2 keys; root@kali is not the team's key
tmp/.cachefix: SUID bit (find, audit.sh); ps.txt: PID 3187; port 4444
crontab.txt: runs it every 5 minutes; cachefix.service: Restart=always
sha256sum -c: bin/ps FAILED, so the program was changed
df.txt: /var is 99% full; du.txt: /var/lib holds 44G (cause not known)

Impact
The attacker had root for about 3 minutes before the account (02:41:07 to
02:44:30), and since then through the key. A changed ps can hide processes. We do not know what data
was read or copied, or whether the full disk comes from the attacker.

Actions
1. Keep the evidence: copy the logs and the disk image before any change.
2. Cut access: block 198.51.100.23, lock svc-backup, remove its keys.
   On-call (kr_oncall) may stop cachefix with the sudo rule; nobody kills
   it by hand before the copy of the evidence is made.
3. Rebuild the server from a clean image. Do not trust a changed system.
4. Set PermitRootLogin no and use keys only. Change every secret.

Open questions
How did the attacker get the root password? What fills /var/lib?
Did the attacker reach other servers with the same key?

Mark your report

You mark this yourself, or you ask a colleague or a mentor to mark it. The page checks the numbers above; it cannot read your report.

CriterionNot yetMeets the standardStrong
FindingsMisses the UID 0 account, the SUID file or bin/ps.Finds the address, the account, the key, the SUID file, port 4444, bin/ps, /var/lib, PID 3187.Links them into one chain of events.
EvidenceFacts with no source.Each fact names a file and a line or command to re-run.Quotes the line with its time.
JudgementFacts and guesses are mixed.Guesses are marked. Names what is unknown, such as the full disk.Ranks findings by risk, with a reason.
ActionsKills or deletes before copying evidence.Evidence, access, rebuild, prevention, each with a reason.Each action has an owner.
Safe containmentThe sudo rule allows ALL, or is unchecked.One exact command, checked with visudo -cf.Tests refused commands, removes lab account.
WritingOver a page, or mixed time zones.One page, one time zone, summary a manager can act on. No blame.A non-specialist can decide from the summary.

Meets the standard in every row: you can show this report as a work sample. Remove names and data that are not yours to share.

This report shows your work on this scenario, on your own machine, without a proctor. It does not show work on a real system or under time pressure.

Get a result link

When all 19 answers above are right, the server can check them again and give you a signed link. You can put the link in a CV or send it to a manager: it opens a page that shows the course, the test, the date and the name you give here. No account, and nothing is stored: keep the link.