Skip to content
BoKSA

14: VPNs and IPsec

14: VPNs and IPsec

Course: Networking - Security Advanced · Updated: October 2026 Literature: K&R chapter 8: network-layer security: IPsec and Virtual Private Networks Prerequisites: Article 11: Cryptography, Article 13: TLS

Learning objectives

After studying this article you can:

  • explain why organisations use Virtual Private Networks (VPNs)
  • name the services of IPsec: integrity, origin authentication, replay protection, confidentiality
  • explain the difference between AH and ESP, and between transport mode and tunnel mode
  • explain Security Associations (SA), the SPI, the SAD/SPD and IKE
  • compare IPsec with TLS VPNs and WireGuard
  • explain the concept of Zero Trust and why it is replacing the classic VPN

1. Why VPNs?

Organisations want private networks between their locations and for their remote workers:

  • A truly private network (their own lines, routers and DNS) is expensive.
  • A VPN sends the traffic over the public Internet, but encrypts it first. Logically it behaves like a private network.
1
2
3
4
5
6
 Head office                  public Internet                  Branch office
 +---------+                                                   +---------+
 | LAN     |--[router + IPsec]=====encrypted tunnel=====[router + IPsec]--| LAN |
 +---------+                         ║                         +---------+
                                     ║
                         [laptop with VPN client]   ← remote worker

Types of VPN:

Type Example
Site-to-site Two offices connected by an IPsec tunnel between their routers/firewalls
Remote access An employee connects from home with a VPN client (IPsec/IKEv2, SSL-VPN, WireGuard)
Consumer VPN Commercial services (NordVPN, Proton VPN, Mullvad) that hide your IP address from websites and protect you on public Wi-Fi. This is a matter of trusting the VPN provider instead of the ISP.

2. IPsec

IPsec (RFC 4301) protects traffic at the network layer: all traffic between two endpoints, whatever the application (TCP, UDP, ICMP, ...).

2.1 IPsec services

  • Data integrity
  • Origin authentication
  • Replay attack prevention
  • Confidentiality (encryption)

2.2 Two protocols

Protocol Integrity + authentication Confidentiality Used in practice?
AH (Authentication Header, IP protocol 51) Yes (also covers the IP header) No Rarely (it also doesn't work through NAT)
ESP (Encapsulating Security Payload, IP protocol 50) Yes Yes Yes, this is the standard

2.3 Two modes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Original packet:          | IP header | TCP/UDP | data |

Transport mode (host ↔ host):
                          | IP header | ESP hdr | TCP/UDP | data | ESP trailer | ESP auth |
                                                 └──── encrypted ─────────┘
                                       └──────────── authenticated ───────┘

Tunnel mode (router ↔ router, the VPN case):
   | NEW IP header | ESP hdr | ORIGINAL IP header | TCP/UDP | data | ESP trl | ESP auth |
                              └──────────────── encrypted ─────────────────┘
  • Transport mode: protects only the payload. It is used directly between two hosts.
  • Tunnel mode: protects the whole original packet, including the internal IP addresses, and puts it inside a new IP packet between the two VPN gateways. This is the most common mode for VPNs. An observer only sees the two gateways talking.

NAT Traversal (NAT-T): ESP doesn't work well through NAT, so it is usually wrapped in UDP port 4500.

2.4 Security Associations (SA)

Before data is sent, a Security Association is set up from the sender to the receiver:

  • SAs are simplex (one direction), so a two-way connection needs two SAs.
  • Both ends store state information about the SA:
  • a 32-bit SA identifier: the Security Parameter Index (SPI), which is in every ESP header
  • origin and destination interfaces (IP addresses)
  • the type of encryption (e.g. AES-256-GCM) and the encryption key
  • the type of integrity check (e.g. HMAC-SHA-256) and the authentication key
  • The Security Association Database (SAD) stores the SAs.
  • The Security Policy Database (SPD) decides which traffic must be protected (e.g. "everything from 10.1.0.0/16 to 10.2.0.0/16 → through the tunnel").

Replay protection: every ESP packet has a sequence number. The receiver keeps a sliding window and drops duplicates and very old packets.

2.5 IKE: Internet Key Exchange

Configuring SAs by hand doesn't scale. IKE (current version IKEv2, RFC 7296) does it automatically:

  1. IKE_SA_INIT: both sides negotiate algorithms and carry out a Diffie-Hellman exchange. This gives an encrypted channel (the IKE SA).
  2. IKE_AUTH: both sides authenticate each other with certificates or a pre-shared key (PSK), or EAP for users, and create the first IPsec SA (child SA).
  3. Keys are renewed regularly (rekeying).

IKE runs over UDP 500 (and 4500 with NAT-T). IKEv2 is post-quantum ready: RFC 9370 allows extra key exchanges (e.g. ML-KEM) in addition to classic DH.

3. Other VPN technologies

Technology Layer Description Status (2026)
IPsec/IKEv2 3 The standard for site-to-site, built into Windows, macOS, iOS and Android Widely used
TLS/SSL VPN (OpenVPN, Cisco AnyConnect/Secure Client, Fortinet, Palo Alto GlobalProtect, Ivanti) Application (TLS over TCP/UDP) Works through almost any firewall (port 443). Often has a web portal. Common for remote access, but these appliances are a frequent target for attackers (see below)
WireGuard (2020, in the Linux kernel) 3 (over UDP) Minimal (~4,000 lines of code compared with ~100,000+ for IPsec/OpenVPN). Fixed modern crypto (Curve25519, ChaCha20-Poly1305, BLAKE2s). No algorithm negotiation, so less can go wrong. Fast. Very popular. Basis of Tailscale and consumer VPNs.
SSH tunnels Application Port forwarding (ssh -L) for ad hoc tunnels For administrators
MPLS VPN 2.5 VPN service from a provider, not encrypted, separated by labels Being replaced by SD-WAN

Example: WireGuard configuration

1
2
3
4
5
6
7
8
[Interface]
PrivateKey = <client private key>
Address    = 10.10.0.2/32

[Peer]
PublicKey  = <server public key>
Endpoint   = vpn.example.com:51820
AllowedIPs = 10.10.0.0/16          # which traffic goes through the tunnel

4. From VPN to Zero Trust

The classic VPN follows the castle-and-moat model. Once you are "inside" the network (through the VPN), you are trusted and can often reach everything. This model fails when:

  • an attacker steals VPN credentials, or exploits a VPN appliance, and then has broad access
  • applications move to the cloud (SaaS), so there's no single "inside" any more
  • employees work from anywhere, on many devices

Zero Trust (NIST SP 800-207) says: "Never trust, always verify."

  • There is no implicit trust based on network location.
  • Every request is authenticated and authorised, based on identity, device health, context and risk.
  • Least privilege: access to one specific application, not to the whole network.
  • Micro-segmentation and continuous monitoring.
  • Implementations: ZTNA (Zero Trust Network Access, e.g. Cloudflare Access, Zscaler Private Access, Microsoft Entra Private Access, Tailscale/Netbird), identity-aware proxies (Google BeyondCorp), and SASE (Secure Access Service Edge: ZTNA + SD-WAN + cloud firewall).

5. Summary

  • VPNs create private networks over the public Internet by encrypting traffic.
  • IPsec works at the network layer. ESP in tunnel mode is the usual VPN setup. SAs (with an SPI) hold the keys and algorithms, and IKEv2 sets them up automatically.
  • WireGuard is the modern, simple alternative. TLS VPNs are common for remote access.
  • Zero Trust / ZTNA replaces "trust once you're inside" with continuous verification per application.

References

Videos

Articles

Standards

  • RFC 4301: Security Architecture for IP · RFC 4303: ESP · RFC 4302: AH
  • RFC 7296: IKEv2 · RFC 3948: UDP encapsulation of ESP (NAT-T)
  • RFC 9370: Multiple key exchanges in IKEv2 (post-quantum)

Tools


Cyber Security

Why it matters

VPN gateways sit at the edge of the network, are reachable from the whole Internet, and give access to the internal network. That makes them among the most attacked systems of recent years:

Year Incident
2019–2021 Pulse Secure (CVE-2019-11510) and Fortinet (CVE-2018-13379) VPN bugs were exploited on a large scale. Lists of stolen credentials for tens of thousands of VPNs were published.
2020 Citrix ADC/NetScaler (CVE-2019-19781): a large impact in the Netherlands as well. NCSC-NL advised switching the systems off.
2023 Citrix Bleed (CVE-2023-4966): session tokens leaked and were used by ransomware groups (LockBit)
2024 Ivanti Connect Secure zero-days were exploited by Chinese state actors. CISA ordered US federal agencies to disconnect the devices.
2024 COATHANGER: the Dutch MIVD/AIVD revealed that Chinese state hackers had planted malware on FortiGate devices in a Dutch Defence network. Later, more than 20,000 FortiGate systems worldwide turned out to be affected.
2023–2025 Many ransomware attacks start with VPN accounts without MFA (e.g. Akira and Conti groups using Cisco VPN accounts)

Other risks:

  • Weak pre-shared keys for IPsec can be cracked offline (IKEv1 aggressive mode).
  • Split tunnelling lets a device be on the VPN and the Internet at the same time. It is a possible bridge for attackers.
  • Trust in consumer VPN providers: some log everything, sell data or are insecure.
  • VPN = full network access: after logging in, an attacker can often move laterally anywhere.

How to make it safe

  1. Patch edge devices immediately. VPN and firewall vulnerabilities are often exploited within days, sometimes hours. Subscribe to vendor advisories and the CISA KEV catalogue (Known Exploited Vulnerabilities).
  2. Always use MFA for VPN access, preferably phishing-resistant MFA (see article 12).
  3. Use strong crypto: IKEv2 only (no IKEv1), AES-GCM, SHA-2, DH groups ≥ 19 (ECP-256) or 31 (Curve25519). Use certificates instead of PSKs, or long random PSKs.
  4. Minimise the attack surface: disable unused web portals and features, restrict management interfaces (never on the Internet), and consider pre-authentication (e.g. a ZTNA broker in front of the VPN).
  5. Give least privilege after login: VPN users get access only to the systems they need (firewall rules per user group), not the whole network.
  6. Log and monitor VPN logins (time, location, device, failed attempts) and send them to the SIEM (see article 17). Watch for logins from unusual countries or hosting providers.
  7. Check device health (posture): only managed, patched devices may connect.
  8. Move towards Zero Trust / ZTNA: access per application, continuous verification, micro-segmentation.
  9. Have an incident plan for edge-device compromise: integrity checks (vendor tools), factory resets and credential rotation. COATHANGER even survived firmware upgrades.

Extra references