Notes
Defence in depth
The idea is defence in depth. Reduce the ways in: open ports and accounts. Keep software patched. Use strong authentication. Run every service with the least privilege it needs. Add mandatory access control, so even a hijacked service is boxed in. And log enough to detect and investigate. Each layer assumes the one before it has failed, which is the right assumption.
- fewer ways in: ports, users
- patched software
- strong authentication
- least privilege for services
- mandatory access control
- logging and audit to detect and investigate
- each layer assumes the one before it failed
Patch
Most successful attacks use bugs that were fixed long ago, on servers nobody updated. So patching comes first. Unattended upgrades installs security updates automatically every day on Ubuntu. Kernel fixes need a reboot, flagged by the reboot required file, or a live patching service. In container and cloud setups the habit changes: rebuild the image with updates and redeploy, rather than patching servers by hand.
$ sudo apt update
$ apt list --upgradable
$ sudo apt install
unattended-upgrades
security updates, daily
/var/run/reboot-required
kernel updates need a reboot
(or live patching)
in images: rebuild, redeploy Fewer ports
Every listening port is a door. ss minus t l n p shows them all. On our machine ShopRung listened on zero zero zero zero, every network address, so it would be reachable from anywhere a packet can arrive. Other services listened only on one two seven zero zero one, the local machine, which is safe. Bind internal services to localhost or a private address, and use a host firewall, ufw or nftables, plus cloud security groups, to allow only the ports you mean, often just SSH and HTTPS.
LISTEN 0.0.0.0:8080 node ← ShopRung on ALL addresses LISTEN 127.0.0.1:46879 ... ← local only: not reachable bind internal services to 127.0.0.1 or a private IP firewall: allow 22, 443 only (ufw, nftables, cloud SGs)
Least privilege
Least privilege means nothing runs with more power than it needs. Setuid programs run as root whoever starts them, so they deserve a list. Our machine had thirteen, all expected: sudo, su, passwd, mount, an SSH helper and a few others. A new setuid file that you did not install, especially a shell, is a classic backdoor. Services run as their own user, with no sudo rights and no login shell.
$ find /usr -perm -4000 -type f 13 found, e.g. /usr/bin/sudo /usr/bin/su /usr/bin/passwd /usr/bin/mount /usr/lib/openssh/ssh-keysign audit new ones: a setuid shell is a classic backdoor services: own user, no sudo, no login shell
Authentication
For logins: SSH with keys only and no root login, as the last episode set up. fail2ban watches the auth log and bans addresses after repeated failures, which cuts the noise of guessing bots. Humans should use multi-factor authentication through a bastion or single sign-on. Remove accounts nobody uses, and give service accounts no login shell, so a stolen password for them opens nothing.
- SSH: keys only, no root login (last episode)
- fail2ban: ban IPs after repeated failures
- MFA for humans (bastion, SSO)
- remove unused accounts
- lock service accounts: usermod -s /usr/sbin/nologin
SELinux and AppArmor
Normal permissions are discretionary: a file's owner decides. Mandatory access control adds a policy that every process must obey, even root's. So if attackers hijack nginx, the policy still stops it reading files outside its web folders. SELinux, the default on Red Hat systems, puts a label on every file and process and allows only labelled pairs. AppArmor, the default on Ubuntu, gives each program a profile listing the paths it may use. Our sandbox ran neither, so try them in your lab VM.
- normal permissions: the owner decides (discretionary)
- MAC: a policy decides; even a hacked root process is boxed in SELinux: labels on every file and process (RHEL)
- AppArmor: per-program path profiles (Ubuntu, Debian)
Debug, do not disable
The common reaction to a denial is to switch SELinux off. Do not. A denial is information. On SELinux, getenforce shows the mode, ausearch shows the recent denials, and ls minus capital Z shows labels; very often files copied into a new folder simply have the wrong label, and restorecon fixes them. On AppArmor, aa status lists profiles and the kernel log shows each denied path. Fix the label or the profile, and keep the protection.
SELinux: $ getenforce Enforcing $ ausearch -m avc -ts recent $ ls -Z /srv/app labels $ restorecon -Rv /srv/app AppArmor: $ sudo aa-status $ journalctl -k | grep DENIED never: setenforce 0 for good
Log and audit
Finally, keep evidence. Authentication logs show who logged in. auditd can record access to sensitive files and commands. Ship logs to another system, because attackers delete local logs first, and keep clocks in sync so events line up across machines. And remember the systemd sandboxing options from episode nine, protect system and no new privileges: a few lines that remove whole classes of damage.
- auth logs: /var/log/auth.log or journalctl -u ssh
- auditd: watch sensitive files and commands
- ship logs off the server (an attacker deletes local ones)
- time in sync (episode 19)
- systemd sandboxing: ProtectSystem, NoNewPrivileges
Common mistakes
Mistakes. Switching off SELinux or AppArmor to make a problem go away. Services bound to every address by default. No automatic security updates. One wide-open security group shared by every server. Logs kept only on the machine an attacker controls. And hardening once at setup but never checking again; tools like Lynis or CIS benchmarks let you re-scan regularly.
- setenforce 0 / disabling AppArmor to fix a denial
- services bound to 0.0.0.0 by default
- no automatic security updates
- one open security group for every server
- logs only on the server
- hardening once, never checking again
Say it like this
In the interview: I patch automatically and rebuild images regularly, close unused ports and bind internal services to localhost, and firewall the rest. SSH with keys only and no root login, multi-factor for humans. Each service runs as its own unprivileged user, with systemd sandboxing. SELinux or AppArmor stays enforcing, and I debug denials instead of disabling it. I audit setuid files and ship logs to a central system.
- "Patch automatically, close
- unused ports and bind internal
- services locally, firewall the rest, SSH keys only, no root
- login, least privilege for services, keep SELinux or AppArmor enforcing, audit setuid, and ship logs off the box."