Attack Surface Reduction and Secure Baselines
Learn what makes up a server's attack surface, why cleartext services give way to SSH and SFTP, how CIS Benchmarks and Controls shape a Linux baseline, why some sudo rules amount to root access, and how to record deviations with compensating controls.
Contentsand lesson path
On this page
- Why it matters
- Shrinking the attack surface
- Secure baselines from the CIS Benchmarks
- Deviations and compensating controls
- Check your understanding
- Apply it
- Review prompt
Lessons of Endpoint & Linux Hardening
- Attack Surface Reduction and Secure Baselines Concept · 15 min
- Hardening a Linux Server Against a Benchmark Lab · 30 min
- Compliance Scanning and Drift Control as Code Lab · 25 min
- Host Telemetry with auditd and Sysmon Worked example · 20 min
- Protecting a Mixed Endpoint Fleet Concept · 15 min
- Hardening Check Knowledge check · 8 questions
What you will learn
- Apply a CIS Benchmark-based baseline to a Linux server (accounts, SSH, services, file permissions, kernel parameters, mandatory access control) and justify any deviations.
Prerequisites: Linux CLI
01. Why it matters
Every server you run offers a set of ways in: the network services that listen on its ports, the accounts that can log in, the programs that run with special rights, and the settings that decide what each of those may do. Together these are the server's attack surface. An attacker needs only one weak entry point, while you have to look after all of them. The cheapest defence is to have fewer of them.
A fresh operating system install is built to work for as many people as possible, not to be safe for your workload. It may run services you never use, allow logins you do not need, and ship defaults chosen for convenience. Over months, people add accounts, open ports and relax settings to get work done, and few of those changes are ever undone.
Hardening is the work of removing what you do not need and locking down what is left. A secure baseline is the written list of settings that every server of a given type must have. With a baseline you can harden a hundred servers the same way, check them automatically, and explain to an auditor why each setting is there. This lesson explains how to build one from the CIS Benchmarks and how to handle the cases where a server cannot follow it. The two labs that follow apply a baseline by hand and then as code.
02. Shrinking the attack surface
Think of the attack surface in four parts, and ask the same three questions about each: do we need it, who can reach it, and what can it do?
- Network services. Every listening port runs code that strangers can send data to. A service you do not need should be removed, not just stopped, so that an update or a reboot cannot bring it back.
- Accounts. Each account with a password or key is a credential that can be guessed, stolen or reused. Shared accounts also hide who did what.
- Privileges. Programs that run as root, sudo rules and setuid binaries turn a small foothold into full control if they are too broad.
- Software and configuration. Every installed package can have flaws, and every permissive setting widens what the other three parts can do.
Legacy remote-access services are the clearest example. Telnet and FTP send everything, including the password, as cleartext: anyone who can see the traffic on any network between the client and the server can read it. They also give the client no way to check that it is talking to the real server. SSH encrypts the session and proves the server's identity with a host key that the client remembers. SFTP is a file transfer protocol that runs inside an SSH connection, so it reuses the same encryption, keys and access rules. Note that SFTP is not FTP over TLS; that protocol is called FTPS and keeps FTP's separate data connections.
So the usual move is to remove the Telnet and FTP server packages and give the people who used them SSH or SFTP accounts, ideally with keys instead of passwords. Moving a cleartext service to an unusual port does not help: the traffic is still readable, and port scanners find it in seconds.
03. Secure baselines from the CIS Benchmarks
You do not have to invent a baseline. The Center for Internet Security (CIS) publishes the CIS Benchmarks: consensus-based configuration recommendations for operating systems, cloud services and applications. Each benchmark has profiles. The Level 1 profile is meant to lower the attack surface without hindering normal business use, and can usually be applied fairly quickly. Level 2 is "defence in depth" for environments where security comes first, and some of its settings can break things if you apply them without care. For Debian there is a separate benchmark per release; at the time of writing CIS lists version 2.0.0 for Debian Linux 12.
The CIS Critical Security Controls are a different document. They are a prioritised list of 18 security practices for a whole organisation. Control 4, Secure Configuration of Enterprise Assets and Software, asks you to establish and maintain secure configurations. The benchmarks are one way to meet that control.
A Linux server baseline usually covers these areas:
- Accounts: only root has user ID 0, no account has an empty password, unused accounts are locked, and system accounts cannot log in.
- SSH: no direct root login, keys instead of passwords, few authentication attempts per connection, and only named groups allowed in. A key-only service account with narrow sudo rules can still run automation.
- Services: nothing listens that the server's role does not need, and cleartext services are removed.
- File permissions: files such as
/etc/shadow, which holds password hashes, are readable only by root and its group, and no system file is writable by everyone. - Kernel parameters:
sysctlsettings that, for example, ignore ICMP redirects or turn on address space layout randomisation. - Mandatory access control (MAC): AppArmor or SELinux confine services to the files and actions they need, even when they run as root. Ordinary file permissions are discretionary: the owner decides. MAC rules are set by the administrator and the process cannot change them.
Sudo rules deserve extra care. A rule such as deploy ALL=(root) /usr/bin/vim /etc/nginx/nginx.conf looks narrow: one program, one file. But a full editor, a pager such as less, or an interpreter such as python3 can start other commands from inside itself. If it runs as root, so does whatever it starts, so the rule grants a root shell in practice. The GTFOBins project catalogues which common programs behave this way; defenders use it to review sudo rules.
The fix for editing is sudoedit. It copies the file to a temporary location, runs the user's editor as the user, and only writes the result back as root when the editor exits. The editor never has root rights. The sudoers manual adds one warning: do not grant sudoedit on a file in a directory the user can write to. For tasks other than editing, allow the exact command and arguments, such as /usr/bin/systemctl restart nginx, instead of a general-purpose tool.
04. Deviations and compensating controls
No baseline fits every server. A VPN gateway must forward packets, which the benchmark turns off. A build runner may need to execute programs from /tmp. When a server cannot follow a recommendation, you do not quietly skip it, and you do not drop the whole baseline. You record a deviation: which rule, on which host, why, who owns the risk, and when it will be reviewed again. You also add a compensating control: another measure that reduces the same risk in a different way, such as a strict firewall policy on the gateway or rebuilding the runner from a clean image every day.
A deviation without an owner or an end date tends to live forever. A small check that runs in your pipeline keeps the register honest. This script reads a register and rejects incomplete or expired entries. The date is fixed so that the output stays the same when you run it.
rule,host,reason,owner,compensating_control,expires
ip_forward_off,vpn-gw-01,routes VPN traffic,n.patel,nftables: VPN subnet only,2027-06-30
x11_forwarding_off,build-03,legacy GUI installer,,,2026-09-30
tmp_noexec,ci-07,build tool runs in /tmp,e.moreau,runner rebuilt daily,2027-01-31"""check_deviations.py: reject baseline deviations that are not fully documented."""
import csv
from datetime import date
AS_OF = date(2026, 11, 16) # fixed so the output does not change
REQUIRED = ["reason", "owner", "compensating_control", "expires"]
with open("deviations.csv", newline="") as f:
for row in csv.DictReader(f):
problems = [f"no {field}" for field in REQUIRED if not row[field]]
if row["expires"] and date.fromisoformat(row["expires"]) < AS_OF:
problems.append(f"expired {row['expires']}")
status = "OK " if not problems else "BAD"
line = f"{status} {row['host']:<10} {row['rule']:<19} {'; '.join(problems)}"
print(line.rstrip())python3 check_deviations.pyOK vpn-gw-01 ip_forward_off
BAD build-03 x11_forwarding_off no owner; no compensating_control; expired 2026-09-30
OK ci-07 tmp_noexecThe second entry fails three ways: nobody owns it, nothing compensates for it, and its review date has passed. In a real register, that entry should block the change until someone fills in the gaps or the server is brought back to the baseline. Scanners can read the same register: in lab 3 you will feed an exception list to the trend report so that an accepted deviation is shown as excepted instead of failed, but only until its expiry date.
05. Check your understanding
06. Apply it
The skill project asks you to harden a default Ubuntu Server VM to an agreed score and to justify the deviations. Start the paperwork now:
- List the services, accounts and sudo rules on your VM, and mark each one keep, restrict or remove.
- Pick the CIS profile you will follow and write down why.
- Start a
deviations.csvwith the fields above, and run the checker in your pipeline.
The next lesson, Hardening a Linux Server Against a Benchmark, applies a baseline subset to a container step by step.
07. Review prompt
Without looking back, explain: the four parts of a server's attack surface; why Telnet and FTP are retired and what replaces them; the difference between CIS Level 1 and Level 2 and between the Benchmarks and the Controls; the six areas of a Linux baseline; why a sudo rule for an editor is effectively a root shell and how sudoedit avoids that; and what a complete deviation record contains.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.