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 · 75 minutes

Scenario test - take over a small server

After this lesson you can
  • Find the weak points of a server that you take over
  • Fix each weak point with the tool of the right lesson
  • Prove every fix with a test and write a short hardening report
Lab files of this lesson (13)

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 colleague who ran the small shop server has left. You get the server, and a note: "It works. I never had time to lock it down." Your team lead wants a hardening report by the end of the day. Nothing may break: the shop page must still load, and SSH must still work for you.

The server is your practice system. A script plants the faults, so that every reader starts from the same state. It also makes the dummy interface krdummy0 with the addresses 198.51.100.10 (the office) and 198.51.100.99 (a stranger), as in lesson 3. Save the script as takeover.sh. It changes the SSH, MariaDB, Apache and firewall settings, rewrites /var/www/html, adds one sysctl file and an account, so run it only on a practice system that you can restore, with sudo bash takeover.sh from your lab folder. It also makes the account kr_ops and a key pair lab_key in the folder where you run it, so that you can prove that SSH works. The SSH server listens on the loopback only, and the file 10-lab-listen.conf keeps it so: leave that file alone.

takeover.sh

#!/bin/bash
# takeover.sh - plants the faults of the scenario test on YOUR practice system.
# Run it once with: sudo bash takeover.sh
# It rewrites the SSH drop-ins, the MariaDB bind setting, the web folder and
# one sysctl file, and makes the account kr_ops with the key ./lab_key.
# Do not run it on a system that is not a lab.
set -u
if [ "$(id -u)" -ne 0 ]; then echo "run it with sudo"; exit 1; fi
export DEBIAN_FRONTEND=noninteractive
apt-get install -y -qq openssh-server apache2 mariadb-server python3 > /dev/null
systemctl enable --now apache2 mariadb > /dev/null 2>&1

# the office network and a stranger, on a dummy interface
ip link show krdummy0 > /dev/null 2>&1 || ip link add krdummy0 type dummy
ip link set krdummy0 up
for a in 198.51.100.10/24 198.51.100.99/24; do
  ip addr show krdummy0 | grep -q "${a%/*}" || ip addr add "$a" dev krdummy0
done

# your way in: the account kr_ops and a key pair in the current folder
[ -f lab_key ] || ssh-keygen -q -t ed25519 -f lab_key -N "" -C "kr_ops@krbox"
chown "${SUDO_USER:-root}": lab_key lab_key.pub
id kr_ops > /dev/null 2>&1 || useradd -m -s /bin/bash kr_ops
install -d -m 700 -o kr_ops -g kr_ops /home/kr_ops/.ssh
install -m 600 -o kr_ops -g kr_ops lab_key.pub /home/kr_ops/.ssh/authorized_keys

# fault 1: root login and passwords over SSH (the server listens on the loopback only)
rm -f /etc/ssh/sshd_config.d/01-krlab.conf
printf 'PermitRootLogin yes\nPasswordAuthentication yes\n' > /etc/ssh/sshd_config.d/00-legacy.conf
printf 'ListenAddress 127.0.0.1\n' > /etc/ssh/sshd_config.d/10-lab-listen.conf
systemctl disable --now ssh.socket > /dev/null 2>&1
systemctl enable --now ssh > /dev/null 2>&1
systemctl restart ssh

# fault 2: the database listens on every address, and has weak accounts
rm -f /etc/mysql/mariadb.conf.d/99-krlab.cnf
printf '[mysqld]\nbind-address = 0.0.0.0\n' > /etc/mysql/mariadb.conf.d/99-legacy.cnf
systemctl restart mariadb
mariadb -e "CREATE USER IF NOT EXISTS ''@'localhost'; CREATE DATABASE IF NOT EXISTS test;"

# fault 3: a status service on port 8088, and no firewall at all
mkdir -p /srv/krstatus
echo "status: ok" > /srv/krstatus/index.html
cat > /etc/systemd/system/krstatus.service <<'EOT'
[Unit]
Description=Old status page

[Service]
ExecStart=/usr/bin/python3 -m http.server 8088 --bind 0.0.0.0 --directory /srv/krstatus

[Install]
WantedBy=multi-user.target
EOT
systemctl daemon-reload
systemctl enable --now krstatus > /dev/null 2>&1
for t in krlab krweb krtask; do nft delete table inet "$t" 2> /dev/null; done
ufw --force disable > /dev/null 2>&1

# fault 4: a web folder that everyone can write, and a folder that lists its files
a2disconf zz-krlab > /dev/null 2>&1
rm -rf /var/www/html/*
mkdir -p /var/www/html/uploads /var/www/html/reports
echo "<h1>shop</h1>" > /var/www/html/index.html
echo "dbpass=Lab-db-pass-1" > /var/www/html/config.php
echo "q1 report" > /var/www/html/reports/q1.txt
chmod 777 /var/www/html/uploads
chmod 666 /var/www/html/config.php
systemctl reload apache2

# fault 5: a kernel setting that accepts route changes from any host
echo "net.ipv4.conf.all.accept_redirects = 1" > /etc/sysctl.d/99-legacy.conf
sysctl -p /etc/sysctl.d/99-legacy.conf > /dev/null

echo "takeover done: the server is yours"

The script prints takeover done: the server is yours at the end. The shop page, http://127.0.0.1/index.html, works. Your way in over SSH is the key: ssh -i lab_key -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new kr_ops@localhost id -un prints kr_ops. A status page that an old tool serves runs on port 8088. You do not know what else is wrong.

The server that you take over
NetworkTaken-over serverYouOffice clientStrangersshdShop pageMariaDBStatus page1234NetworkTaken-over serverYouOffice clientStrangersshdShop pageMariaDBStatuspage1234
  1. You → sshd: key login of kr_ops must keep working
  2. Office client → Shop page: port 80 must keep working
  3. Stranger → Status page: port 8088 is open to everyone
  4. Stranger → MariaDB: it listens on 0.0.0.0

Your tasks

Work in this order. The tasks name the area, not the commands: use the lessons of the course. Test each fix, and keep the proof for your report.

Task 1. Look first. Before you change anything, find the state of the server and write it down. Lesson 8 may have closed the web folder to your own account, so use sudo for every command on /var/www/html. Answer from your own terminal.

Task 2. SSH. Root must not log in over SSH, and a password must not open a login, by any method. Use sshd -T to read the settings that sshd really uses, because a file that is read earlier can win over the file that you write. Check the file before you reload the server. After the fix, the key login of kr_ops (the command above) must still work.

Task 3. The database. MariaDB must listen on the loopback only, and the anonymous account and the test database must be gone. The shop page needs no change: it is a static page, and the database is not on its path. Check that it still loads (200) after the fix.

Task 4. The firewall. The server has no firewall rule. Write a rule set with the policy drop. Allow known connections, SSH and the web server for everyone, and allow the status service on port 8088 only for the network 198.51.100.0/28. Test the status service from 198.51.100.10 and from 198.51.100.99, with a time-out of two seconds.

Task 5. The web folder. No one but the owner may write in the web folder. Apache must still read it, and the shop page must still return 200. A folder without an index page must not list its files, and the Server header must not name the version. The web server must not send a secret to a visitor: ask for http://127.0.0.1/config.php before and after, and look at the text.

Task 6. The kernel. One kernel parameter accepts route changes from any host on the network. Find it in the settings that the script wrote, and fix it for now and for the next boot.

Hand in

Write a short hardening report in the file report.md. Use these headings, in this order:

  1. ## Findings: one line for each fault, with the proof that it was there (a command and what it showed).
  2. ## Fixes: for each fault, what you changed and in which file.
  3. ## Proof: for each fix, the test that shows it works, and the output. Include the test that the shop page and your SSH access (the key login of kr_ops) still work.
  4. ## What is still open: at least two risks that you did not close, and what you would do next.

Keep it to one page. A reader who did not watch you must be able to repeat your tests.

Review your work

A model solution

The names of the faults and the lesson of each:

#FaultLessonFix
1Root login and passwords over SSH4Edit 00-legacy.conf, which sshd reads first.
2MariaDB on 0.0.0.0, anonymous account, test database8Bind to 127.0.0.1, drop the account and the database.
3No firewall, status page on port 80883Default drop, and port 8088 for the office only.
4Web folder writable by all, file listing, version in the header, a password file sent as text8Owner and modes, Options -Indexes, ServerTokens Prod, and the password file moved out of the web folder.
5accept_redirects = 19Set it to 0 in the file and in the running kernel.

SSH. The file 00-legacy.conf is read first, and the first value wins, so a new file named 01-... would not help. Edit the file that sets the value, check it, and reload.

sudo sed -i 's/^PermitRootLogin yes/PermitRootLogin no/; s/^PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config.d/00-legacy.conf
sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication)'

The last line of that output is kbdinteractiveauthentication no: Ubuntu sets it already, and it closes the second way for a password (lesson 4). The database. The same kind of file, 99-legacy.cnf, holds the wrong address.

sudo sed -i 's/0\.0\.0\.0/127.0.0.1/' /etc/mysql/mariadb.conf.d/99-legacy.cnf
sudo mariadb -e "DROP USER IF EXISTS ''@'localhost'; DROP DATABASE IF EXISTS test;"
sudo systemctl restart mariadb
ss -tln | grep 3306

The web folder and Apache. The file config.php has no PHP tag, so Apache sends its text to every visitor (curl -s http://127.0.0.1/config.php prints dbpass=Lab-db-pass-1), and a mode of 640 would not stop it. Move it out of the web folder, and give the new file the owner and mode that a secret needs. Save zz-krlab.conf as in lesson 8, with ServerTokens Prod, ServerSignature Off and Options -Indexes for /var/www/html.

sudo mv /var/www/html/config.php /etc/shop-config.php
sudo chown root:www-data /etc/shop-config.php
sudo chmod 640 /etc/shop-config.php
sudo chown -R root:www-data /var/www/html
sudo find /var/www/html -type d -exec chmod 750 {} +
sudo find /var/www/html -type f -exec chmod 640 {} +
sudo cp zz-krlab.conf /etc/apache2/conf-available/zz-krlab.conf
sudo a2enconf zz-krlab && sudo apache2ctl configtest && sudo systemctl reload apache2

The kernel parameter. The script wrote /etc/sysctl.d/99-legacy.conf.

sudo sed -i 's/= 1/= 0/' /etc/sysctl.d/99-legacy.conf
sudo sysctl -p /etc/sysctl.d/99-legacy.conf

The firewall. Save this file as fix.nft. The rule for the web server has no source, so it works for everyone. Like in lesson 3, there is no iif lo rule, because the test needs the rules to judge addresses of the same machine.

#!/usr/sbin/nft -f
table inet krfix
delete table inet krfix

table inet krfix {
	chain input {
		type filter hook input priority filter; policy drop;
		ct state established,related accept
		tcp dport { 22, 80 } accept
		ip saddr 198.51.100.0/28 tcp dport 8088 accept
	}
}
sudo nft -c -f fix.nft && sudo nft -f fix.nft
curl -s -m 2 -o /dev/null -w "%{http_code}\n" --interface 198.51.100.10 http://198.51.100.10:8088/
curl -s -m 2 -o /dev/null -w "%{http_code}\n" --interface 198.51.100.99 http://198.51.100.10:8088/

Expected proof: 200 for the office, 000 and exit code 28 for the stranger, 403 for /reports/, Server: Apache, 0 writable entries, 127.0.0.1:3306, and 0 for the parameter.

Real open risks, for example: the status service still runs and has no login; the lab database password is still in /etc/shop-config.php and must be changed; no TLS on port 80; no log monitoring; the firewall has no rule for lo or for output; and the packages are not updated.

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
FindingsA fault is missing or guessed.All five faults, each with the command and output from before the fix.Says why a fault hid (00-legacy.conf is read first).
EvidenceA fix shows no test.Each fix has a test, with output, that fails before and passes after.A reader repeats every test from the report alone.
FixesA fix names no file, or was loaded without a check.Each fix names its file; sshd -t, configtest and nft -c ran first.Each fix also says how to undo it.
Nothing brokeNo proof for the shop page or SSH.Shop page 200 and the kr_ops key login shown after all fixes.SSH access also proved before the firewall loaded.
Judgement"Everything is secure", or no open risk.Two real open risks, each with a next step.Open risks ranked with a reason; untested areas named.
WritingLong, not dated, or blames the colleague.One page, dated; a team lead can act on it.Starts with a summary for a non-specialist.

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.

The report shows your work on this scenario, done on your own machine without a proctor. It does not show work on a live server.

Clean up when you have finished. This removes everything that takeover.sh and your fixes added (ufw stays off; enable it again if you want it). The default Apache page is gone: takeover.sh deleted it. If userdel says that the account is in use, wait a few seconds and run it again. The * is in sudo sh -c '...', because after your fix your own account may not list /var/www/html, so root must expand it.

sudo systemctl disable --now krstatus ssh apache2 mariadb
sudo a2disconf zz-krlab
sudo rm -r /etc/systemd/system/krstatus.service /srv/krstatus /etc/sysctl.d/99-legacy.conf
sudo systemctl daemon-reload
sudo rm -f /etc/shop-config.php
sudo rm /etc/ssh/sshd_config.d/00-legacy.conf /etc/ssh/sshd_config.d/10-lab-listen.conf /etc/mysql/mariadb.conf.d/99-legacy.cnf /etc/apache2/conf-available/zz-krlab.conf
sudo sh -c 'rm -r /var/www/html/*'
sudo nft delete table inet krfix
sudo userdel -r kr_ops
sudo ip link del krdummy0
rm lab_key lab_key.pub

Get a result link

When all 14 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.