Having explored the CIA triad and the AAA framework, we now address a hard truth: no single security control is unbreakable. Firewalls are bypassed. Antivirus misses zero-days. Employees click phishing links. Defense in Depth (DiD) accepts this reality and layers controls so that the failure of any one mechanism does not result in total compromise. The principle of Least Privilege complements DiD by minimizing the blast radius of any single compromise. Together, they form the architectural philosophy behind resilient security programs.
Medieval castles relied on a single perimeter: tall walls and a moat. Once breached, the interior was defenseless. Modern cybersecurity adopts a 'hotel' model instead: a guarded perimeter, locked lobby doors, keycard-restricted elevators, and locked room doors. Network segmentation, endpoint detection, application-level authentication, and data-level encryption each represent independent layers. An attacker who compromises a web server in the DMZ must still escalate through additional layers to reach the database tier containing sensitive data.
Defense in Depth is not about buying more tools. It is about ensuring that controls operate at different layers (network, host, application, data) and belong to different control types (preventive, detective, corrective). A second firewall is not depth — it is redundancy within the same layer.
This iptables configuration enforces a three-tier network segmentation: external traffic reaches only the web tier (eth1) on HTTPS; the web tier can reach the database tier (eth2) only on MySQL's port; all other traffic from the web tier to the database tier is explicitly dropped. If the web server is compromised, the attacker inherits these network restrictions — they cannot arbitrarily scan the database tier or pivot to internal services without additional exploits.
Least Privilege dictates that every user, process, and system should operate with the minimum set of permissions required to perform its function — and those permissions should be time-bound and auditable. The 2013 Target breach exemplifies failure: an HVAC vendor had broad network access with no segmentation, allowing attackers to pivot from the HVAC system to point-of-sale terminals. The vendor needed access to monitor temperature sensors — not to reach payment systems.
# Least Privilege: Running a web server as a non-root user
import os
import pwd
def drop_privileges(uid_name: str, gid_name: str):
"""Drop root privileges after binding to privileged port."""
# Get the uid/gid for the unprivileged user
target_uid = pwd.getpwnam(uid_name).pw_uid
target_gid = pwd.getpwnam(gid_name).pw_gid
# Remove supplementary groups first
os.setgroups([])
# Set GID first, then UID (order matters on Linux)
os.setgid(target_gid)
os.setuid(target_uid)
# Verify privilege drop succeeded
if os.getuid() == 0:
raise RuntimeError("CRITICAL: Privilege drop failed — still running as root!")
print(f"[SECURITY] Successfully dropped to uid={target_uid}, gid={target_gid}")
# Call after binding to port 80/443
drop_privileges('www-data', 'www-data')
# [SECURITY] Successfully dropped to uid=33, gid=33This Python pattern demonstrates process-level least privilege. The application starts as root (necessary to bind to privileged ports below 1024), binds the socket, then permanently drops to the unprivileged `www-data` user. If an attacker achieves remote code execution through a web application vulnerability, they inherit `www-data` permissions — not root. This single control can prevent an RCE from becoming a full system takeover.
| Principle | Core Idea | Implementation Examples | Failure Mode When Ignored |
|---|---|---|---|
| Defense in Depth | Layer independent controls | Network segmentation, WAF, endpoint EDR, application auth, DB encryption | Single point of failure; one breach = full compromise |
| Least Privilege | Minimize permissions per entity | Sudo with command restrictions, RBAC, container security contexts, temporary access grants | Privilege escalation; lateral movement; massive data exfiltration |
| Separation of Duties | Split critical functions across roles | Require two admins for production DB access, code review before merge to main | Insider fraud; accidental or malicious destruction with no oversight |
⚠️ Defense in Depth without Least Privilege is incomplete. Layered firewalls won't stop a malicious insider with excessive database permissions. Conversely, Least Privilege without depth leaves you defenseless when the single access control system fails. The principles are symbiotic — implement both or accept gaping holes.
Verify exercises to earn ★ 120 XP and unlock next lab level.