26.7. Responding to a Suspected Compromise

Everything before this section reduces the probability and the blast radius of a compromise. This section rehearses the day neither is enough. It runs as a staged exercise: a synthetic compromised service is planted on sec1 deliberately, with an intruder account and an attacker's SSH key, so the response sequence - capture, contain, preserve, assess, rebuild, verify - can be executed end to end without a real attacker. The procedures are the real thing; only the intruder is synthetic.

The incident response sequence exercised in this chapter

Figure 26.4. Detection leads to volatile-state capture first, then containment; evidence is archived, exposure is assessed, access is locked, and only then is the service rebuilt from trusted content and verified from the independent client. Powering off first would destroy step 2's evidence; wiping first would destroy all of it.

26.7.1. Containing Access While Preserving Evidence

Containment choices trade speed against evidence, and the trade is worth making consciously:

Action What it stops immediately Evidence cost
Stop the service The running compromise Loses the process's memory and sockets - capture them first
Filter the port (nftables) Network reachability None; state on disk intact
Lock the account The intruder's login path None; login records remain
Power off / wipe Everything Destroys volatile state and often the disk trail
Unplug the network Everything remote Preserves state but freezes your own access too

The sequence this chapter executes draws on the first three rows: capture volatile state (next subsection) while the service still runs, then stop the service and filter the port - with the account lock held for Section 26.7.3, after the exposure assessment that justifies it. The management network stays out of every step, so the host answering the administrator's SSH is never the host being contained.

Stage the exercise. The synthetic compromise is a small HTTP service running as a planted intruder account, with content an attacker left behind and a key an attacker added. The unit file, saved as incident-demo.service in the workspace for installation on sec1:

[Unit]
Description=Compromised demo HTTP service

[Service]
User=intruder
WorkingDirectory=/srv/incident-demo
ExecStart=/usr/bin/python3 -m http.server 9090 --bind 192.168.246.10
Restart=on-failure

[Install]
WantedBy=multi-user.target

And the staging script, saved as incident-stage.sh - it creates the account, the planted content, and the attacker's key:

# Stage a synthetic compromise for the response exercise.
id intruder >/dev/null 2>&1 || useradd -m -s /bin/bash intruder
install -d -m 755 /srv/incident-demo
printf 'Compromised demonstration content\n' > /srv/incident-demo/index.html
printf 'Exposed-Demo-Token-123\n' > /srv/incident-demo/token.txt
chown -R intruder:intruder /srv/incident-demo
install -d -o intruder -g intruder -m 700 /home/intruder/.ssh
printf 'ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleAttackerKey incident@example\n' > /home/intruder/.ssh/authorized_keys
chown intruder:intruder /home/intruder/.ssh/authorized_keys
chmod 600 /home/intruder/.ssh/authorized_keys

Run both. On sec1, as john:

$ sudo install -m 644 incident-demo.service /etc/systemd/system/incident-demo.service
$ sudo bash incident-stage.sh
$ sudo systemctl daemon-reload

All three silent. Bring the compromised service up:

$ sudo systemctl enable --now incident-demo.service
Created symlink '/etc/systemd/system/multi-user.target.wants/incident-demo.service' → '/etc/systemd/system/incident-demo.service'.
$ systemctl is-active incident-demo.service
active

The "compromise" is live on port 9090. Confirm it the way an outsider would find it - from secclient, as john:

$ curl -sS http://web.sec1.lab:9090/
Compromised demonstration content

That response is the detection moment: a service no one deployed, serving content no one wrote, on a host you administer. With the volatile captures of the next subsection taken while it runs, stop the service and drop the traffic - the two least destructive containment steps from the table above. On sec1, as john:

$ sudo systemctl stop incident-demo.service

Silent - and its process, memory, and sockets are gone, which is why the captures come first. Disable it so a reboot cannot resurrect it:

$ sudo systemctl disable incident-demo.service
Removed '/etc/systemd/system/multi-user.target.wants/incident-demo.service'.

Then filter the port, keeping the filter in its own table so it can be listed and removed as a unit - the nftables syntax is Chapter 18's subject:

$ sudo nft add table inet incident
$ sudo nft add chain inet incident input '{ type filter hook input priority 0; policy accept; }'
$ sudo nft add rule inet incident input tcp dport 9090 drop
$ sudo nft list table inet incident
table inet incident {
    chain input {
        type filter hook input priority filter; policy accept;
        tcp dport 9090 drop
    }
}

The first three commands are silent; the listing confirms the rule. The table's input chain accepts everything except port 9090, which means SSH and the chapter's other services are untouched. Verify the containment from outside, with a bounded probe - from secclient, as john:

$ curl -sS --max-time 3 http://web.sec1.lab:9090/
curl: (28) Connection timed out after 3002 milliseconds

--max-time 3 bounds the probe, and the timeout - not a refusal, not a reset, but silence - is what a dropped packet looks like from the client side. The service is unreachable, the disk evidence is intact, and the administrator's own access never went through port 9090.

26.7.2. Capturing Volatile State and Establishing a Timeline

Volatile state is everything that disappears when the process ends: the process table, the sockets, the network configuration, the login record as the kernel holds it. The captures below were taken in the exercise before the containment steps - that ordering, capture before containment, is the rule to carry into a real incident. Make an evidence directory and capture the process table first, on sec1, as john, from the workspace:

$ mkdir -p incident
$ sudo ps auxww > incident/processes.txt
$ ls -l incident/processes.txt
-rw-rw-r-- 1 john john 11449 Nov 23 16:43 incident/processes.txt

The redirection writes the file - the terminal shows nothing - and the listing confirms the capture: 11 KB of process state, with auxww including the full command lines that Section 26.3.1 showed can carry secrets. The sockets and network state:

$ sudo ss -tunap > incident/sockets.txt
$ sudo lsof -nP -i > incident/open-sockets.txt
$ ls -l incident/sockets.txt incident/open-sockets.txt
-rw-rw-r-- 1 john john 2180 Nov 23 16:43 incident/open-sockets.txt
-rw-rw-r-- 1 john john 2669 Nov 23 16:43 incident/sockets.txt

Both redirections are silent; ss -tunap lists every TCP, UDP, and raw socket with its owning process, and lsof -nP -i gives the independent view of the same sockets from the file-descriptor side. Two tools for one fact, because a rootkit's first lie is usually to one of them. Addresses, routes, and the login record - these need no privilege to read, so they run as john:

$ ip addr > incident/addresses.txt
$ ip route > incident/routes.txt
$ sudo last -F > incident/logins.txt
$ ls -l incident/addresses.txt incident/logins.txt incident/routes.txt
-rw-rw-r-- 1 john john 1217 Nov 23 16:43 incident/addresses.txt
-rw-rw-r-- 1 john john  232 Nov 23 16:43 incident/logins.txt
-rw-rw-r-- 1 john john  176 Nov 23 16:43 incident/routes.txt

The last -F form writes full dates into the login capture. Finally, hash the compromised content in place - the find form handles arbitrary filenames safely, and -exec ... + batches them into one sha256sum invocation:

$ sudo find /srv/incident-demo -type f -exec sha256sum {} + > incident/file-hashes.txt
$ cat incident/file-hashes.txt
61195fba85b4e54a78b3f6c99edf726ed1fd7be7646ab6e0ffb23140bf8ee997  /srv/incident-demo/token.txt
aa75aff057521303781cc7c9eb64bace9c39c28ee7a755b551411e2c30439d18  /srv/incident-demo/index.html

Two files, two digests - the tokens and the content, frozen as evidence before anything deletes them. The timeline assembles from what these captures hold: the process list places the service in memory, the socket lists place its listeners, the login record places the sessions, and the digests place the content. Correlate them by time - every capture above carries its own timestamps in the filesystem, and the journal's --since filters from Section 8.4.1 bound the window - and the story writes itself: what ran, what it served, who connected, what changed.

Archive the evidence before any rebuild touches the machine. Change into the evidence directory first, as john on sec1:

$ cd ~/debian-handbook/security/incident
$ sudo tar -czf evidence.tgz -C / etc/systemd/system/incident-demo.service srv/incident-demo home/intruder/.ssh/authorized_keys
$ sudo chown john:john evidence.tgz
$ sudo chmod 600 evidence.tgz
$ sha256sum evidence.tgz
e6fc06d4bd3400e626b1a8995d61a658f481dab5b301ff207ef134d4428ca46e  evidence.tgz

The archive packs the unit file, the compromised content, and the attacker's key file - the three artifacts a rebuild is about to destroy - and the digest freezes the archive itself. The ownership handover matters: tar ran as root to read the intruder's files, and the chown + chmod give the evidence to john, mode 600, so the copy that leaves the host carries its permissions with it:

$ stat -c '%a %U:%G %n' evidence.tgz
600 john:john evidence.tgz

From here, scp the archive to a host the compromised machine cannot reach, and record the digest beside it - the same off-host discipline as Section 26.6.3's baselines, applied to the evidence itself.

26.7.3. Assessing Credential and Data Exposure

With the service stopped and the evidence archived, the assessment asks what the intruder could reach. Start with the account itself and its privileges, on sec1, as john:

$ sudo grep -R intruder /etc/passwd /etc/group /etc/sudoers /etc/sudoers.d
/etc/passwd:intruder:x:1002:1002::/home/intruder:/bin/bash
/etc/group:intruder:x:1002:

The grep prints the passwd line and the group line - and, equally important, nothing from sudoers or sudoers.d: the intruder has a login account and no sudo rights. The planted key:

$ sudo cat /home/intruder/.ssh/authorized_keys
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIExampleAttackerKey incident@example

One key, one line - the credential the staging script planted, now read into the record. The assessment continues with the two reviews from Section 26.6.1: the login history in incident/logins.txt and the journal's ssh records, checking for sessions under the intruder's name or under other accounts at odd times. What the service itself could reach is bounded by its unit: it ran as User=intruder with WorkingDirectory=/srv/incident-demo, unconfined - the unit has none of Section 26.2.4's sandboxing, so the honest assessment is that the process could read anything the intruder account could read, and the chapter's own secrets stored with mode 600 under john's account were not among them. A real assessment extends this to the host's actual data: which databases, keys, and credentials the service account could read, and which of those must now be considered exposed and rotated under Section 26.3.2's procedure.

Lock the access: disable the account, then remove the planted key. On sec1, as john:

$ sudo usermod -L -e 1 intruder
$ sudo rm /home/intruder/.ssh/authorized_keys
$ sudo passwd -S intruder
intruder L 2026-11-23 0 99999 7 -1

The passwd -S line is the confirmation in the same vocabulary Chapter 5 uses for account status: the L in the second field says the password is locked, and the 2026-11-23 beside it says the account is expired as of that date - usermod -L locked the password and -e 1 expired the account, so neither a password nor the (now removed) key can log in. The intraday order matters: the key was removed after the account was locked, and both after the evidence archive preserved a copy.

26.7.4. Rebuilding from Trusted Sources and Restoring Verified Data

The last phase replaces the compromised material with content whose provenance you can state. Remove the compromise - the unit and its content, both already archived as evidence:

$ sudo rm -rf /srv/incident-demo
$ sudo rm /etc/systemd/system/incident-demo.service

Both silent. The replacement service is deliberately better than what it replaces: it runs under a systemd dynamic user with the full sandbox directive set from Section 26.2.4, so the same intrusion next week would have had nothing to read. The unit file, saved as handbook-demo.service in the workspace:

[Unit]
Description=Rebuilt demonstration web service

[Service]
DynamicUser=yes
StateDirectory=handbook-web
WorkingDirectory=/srv/handbook-web
ExecStart=/usr/bin/python3 -m http.server 9091 --bind 192.168.246.10
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
NoNewPrivileges=yes
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
RestrictAddressFamilies=AF_INET AF_INET6
UMask=0027

[Install]
WantedBy=multi-user.target

Prepare the trusted content - a new root, a new index, written from the workspace rather than recovered from the compromised tree:

$ sudo install -d -m 755 /srv/handbook-web
$ sudo sh -c 'printf "Rebuilt from trusted content\n" > /srv/handbook-web/index.html'

Both silent. Install the unit, reload, and start:

$ sudo install -m 644 handbook-demo.service /etc/systemd/system/handbook-demo.service
$ sudo systemctl daemon-reload
$ sudo systemctl enable --now handbook-demo.service
Created symlink '/etc/systemd/system/multi-user.target.wants/handbook-demo.service' → '/etc/systemd/system/handbook-demo.service'.
$ systemctl is-active handbook-demo.service
active

And the listener check - the new service is bound to its new port under its dynamic user:

$ sudo ss -tlnp | grep 9091
LISTEN 0      5      192.168.246.10:9091      0.0.0.0:*    users:(("python3",pid=102646,fd=3))                                                  

The rebuilt service listens on 9091 - a different port from the contained 9090, deliberately, so the firewall drop can stay in place until the verification is done. Lift the isolation now, on sec1, as john:

$ sudo nft delete table inet incident

Silent - the whole containment filter goes as one named unit, which is why it was built as a table in the first place. The verification belongs to the independent client, from secclient, as john:

$ curl -sS http://web.sec1.lab:9091/
Rebuilt from trusted content

The rebuilt service answers from outside with exactly the content written from the workspace - not the compromised tree, not a restored copy of it. Two closing checks complete the response. The package the integrity tools watch:

$ sudo apt install --reinstall hello
$ sudo dpkg -V hello

The reinstall prints its package progress; the verification prints nothing, which is the passing result - the system's packaged files still match what Debian shipped. And the account status, one last read:

$ sudo passwd -S intruder
intruder L 2026-11-23 0 99999 7 -1

Still locked, still expired. The response is complete on every axis this chapter can verify: contained, evidenced, assessed, locked, rebuilt, and confirmed from a host the compromised machine does not control.

The exercise's cleanup is bounded to what the staging created: on sec1, sudo systemctl disable --now handbook-demo.service and sudo rm /etc/systemd/system/handbook-demo.service retire the rebuilt demo (both silent), sudo userdel -r intruder removes the synthetic account once the evidence archive is safely elsewhere, and sudo rm -rf /srv/handbook-web /var/lib/handbook-web clears the demo's content and state directory. The workspace keeps its record: incident/ holds the snapshots and the evidence archive, the CA material of Section 26.4 stays for reference, and the practice host returns to the baseline Section 26.1 built - which is the final argument for having built one.