Attack surface and defence in depth

Introduction

A bank does not protect its money with one great lock. There is a guard at the door, cameras in the hall, a vault with a timer, and staff who need two keys to open it. Any one of these can fail; it is very unlikely that all of them fail on the same day. Security people call this defence in depth: several independent layers, so that one mistake is not a disaster.

The other half of the idea is to have fewer doors in the first place. Everything an attacker can reach (every open port, every login page, every file upload form, every staff email address) is part of the attack surface. A smaller surface means fewer places to defend.

How it works

Defence in depth uses different kinds of control, because one kind of failure should not break them all:

KindWhat it isExamples
PreventiveStops an attack before it worksFirewall, strong passwords, two-factor login
DetectiveNotices an attack while or after it happensLogs, alerts on strange logins, file checksums
CorrectiveRepairs the damageBackups, patching, resetting stolen passwords

Controls are also grouped by how they work: technical (software and hardware), administrative (rules, training, checklists) and physical (locks, guards, cameras).

Why do layers help so much? If each layer stops attacks independently, an attack gets through only if it gets past every layer, so the chances multiply. The program below follows phishing emails through three layers. The shares each layer stops are made-up numbers for the example, and real layers are never fully independent (a convincing email fools both the filter and the reader), so treat the result as the best case.

// Defence in depth: an attack must get past every layer.
// Each layer stops some share of attacks; we assume the layers fail independently.
const layers = [
  { name: "Email spam filter", stops: 0.9 },
  { name: "Staff who spot phishing", stops: 0.6 },
  { name: "Two-factor login", stops: 0.99 },
];

let getsThrough = 1;
for (const layer of layers) {
  getsThrough = getsThrough * (1 - layer.stops);
  console.log(layer.name.padEnd(24) + "still getting through: " + (getsThrough * 100).toFixed(2) + "%");
}
console.log("Out of 10000 phishing emails, about " + Math.round(10000 * getsThrough) + " lead to a stolen login.");
Open in compiler

Shrinking the attack surface follows a simple rule: anything that is not needed is turned off or removed. Unused ports are closed, old accounts are deleted, test pages are taken down, and services listen only on the networks that need them.

Example

A university's public web server was set up quickly, and a scan shows six open ports. The server only needs to serve web pages and let administrators log in.

// Attack surface: every open port is a door someone can knock on.
const open = [
  { port: 21, service: "FTP" },
  { port: 22, service: "SSH" },
  { port: 23, service: "Telnet" },
  { port: 80, service: "HTTP" },
  { port: 443, service: "HTTPS" },
  { port: 3306, service: "MySQL" },
];
// What a public web server actually needs. (SSH stays, but only for the admins' network.)
const needed = [22, 80, 443];

const toClose = [];
for (const s of open) {
  if (!needed.includes(s.port)) toClose.push(s);
}
for (const s of toClose) console.log("close " + String(s.port).padStart(4) + "  " + s.service);
console.log("Open ports: " + open.length + " -> " + (open.length - toClose.length));
Open in compiler

Line by line

  1. open lists the six services the scan found, each with its port number.
  2. needed lists the three ports this server really needs: 80 and 443 for the website (80 only redirects to the encrypted 443), and 22 for administrators using SSH.
  3. The loop keeps every open service whose port is not in needed; includes checks membership.
  4. FTP (21) and Telnet (23) send passwords as plain text and are not needed. MySQL (3306) is the database, which should never be reachable from the internet.
  5. Closing those three cuts the open ports from 6 to 3, halving the doors an attacker can knock on.

Expected result

close   21  FTP
close   23  Telnet
close 3306  MySQL
Open ports: 6 -> 3

Port 22 stays open, but defence in depth still applies to it: the next steps would be allowing SSH only from the administrators' network and turning off password logins in favour of keys.

In the real world

  • Mobile wallets combine a PIN, a one-time code sent to the phone, device checks and fraud alerts: four layers, so a stolen PIN alone is not enough.
  • The principle of least functionality in many security standards is attack-surface reduction under another name: install and enable only what the job needs.
  • After many large breaches, the investigation finds that the attacker passed through several weak layers, not one: an unpatched server, then a shared admin password, then no alerting.
  • Network scanners such as nmap are used by defenders to list their own open ports, exactly like the example. Scan only systems you own or have written permission to test.

Common mistakes

  • Stacking copies of the same control. Two firewalls with the same rules fail together; layers should be of different kinds.
  • Forgetting detection. Preventive controls eventually fail, and without logs nobody notices.
  • Leaving default services running. A fresh server often has services switched on that nobody asked for.
  • Trusting the inside of the network. An attacker who gets one machine inside should still meet more layers.

Best practices

  • List the attack surface: every port, account, form and integration that someone outside can reach.
  • Remove what is not needed before adding protection to what is.
  • Mix preventive, detective and corrective controls for every important asset.
  • Test the layers one at a time: what happens if this one fails?

Summary

Defence in depth puts several independent layers between an attacker and an asset, so that the chance of getting through is the product of the chances of passing each one. Mixing preventive, detective and corrective controls, and technical, administrative and physical ones, keeps the layers from failing together. Reducing the attack surface means there are fewer places to defend at all: close unused ports, remove unused accounts and switch off what the job does not need.

Quick check

1. Three independent layers each stop half of all attacks. What share of attacks gets through all three?
2. Which of these is a detective control?
3. A public web server has MySQL's port 3306 open to the internet. What is the best first step?
4. Why is putting two identical firewalls in a row weak defence in depth?

Key takeaways

  • With independent layers, the chance of getting through all of them is the product of the chances of passing each.
  • Mix preventive, detective and corrective controls so the layers do not fail together.
  • Remove what is not needed before protecting what is: every closed port is one door fewer.
  • Real layers are never fully independent, so the multiplied chance is a best case.

Practice problems for this lesson

Was this lesson clear?

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.