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 | |
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 | |
- 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:
- IKE_SA_INIT: both sides negotiate algorithms and carry out a Diffie-Hellman exchange. This gives an encrypted channel (the IKE SA).
- 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).
- 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 | |
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
- Jim Kurose: IPsec and VPNs (chapter 8 video lectures): https://gaia.cs.umass.edu/kurose_ross/lectures.php
- Computerphile: "VPNs explained", "WireGuard": https://www.youtube.com/@Computerphile
- Jason A. Donenfeld: "WireGuard: Next Generation Secure Network Tunnel" (talk): https://www.wireguard.com/
- Professor Messer: IPsec, VPNs (Security+): https://www.professormesser.com/
- Google: "BeyondCorp: a new approach to enterprise security": https://cloud.google.com/beyondcorp
Articles
- WireGuard whitepaper (J. Donenfeld, NDSS 2017): https://www.wireguard.com/papers/wireguard.pdf
- Cloudflare Learning Center, What is IPsec? and What is Zero Trust?: https://www.cloudflare.com/learning/
- NIST SP 800-77 Rev. 1: Guide to IPsec VPNs: https://csrc.nist.gov/pubs/sp/800/77/r1/final
- NIST SP 800-207: Zero Trust Architecture: https://csrc.nist.gov/pubs/sp/800/207/final
- CISA, Zero Trust Maturity Model v2.0: https://www.cisa.gov/zero-trust-maturity-model
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
- strongSwan (open-source IPsec/IKEv2): https://www.strongswan.org/
- WireGuard: https://www.wireguard.com/
- Wireshark: filters
isakmp(IKE),esp,wg(WireGuard)
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
- 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).
- Always use MFA for VPN access, preferably phishing-resistant MFA (see article 12).
- 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.
- 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).
- Give least privilege after login: VPN users get access only to the systems they need (firewall rules per user group), not the whole network.
- 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.
- Check device health (posture): only managed, patched devices may connect.
- Move towards Zero Trust / ZTNA: access per application, continuous verification, micro-segmentation.
- Have an incident plan for edge-device compromise: integrity checks (vendor tools), factory resets and credential rotation. COATHANGER even survived firmware upgrades.
Extra references
- CISA, Known Exploited Vulnerabilities Catalog: https://www.cisa.gov/known-exploited-vulnerabilities-catalog
- NSA/CISA, Selecting and Hardening Remote Access VPN Solutions (2021): https://media.defense.gov/2021/Sep/28/2002863184/-1/-1/0/CSI_SELECTING-HARDENING-REMOTE-ACCESS-VPNS-20210928.PDF
- MIVD/AIVD, COATHANGER (2024) and NCSC-NL advisory: https://www.ncsc.nl/
- CISA, Emergency Directive 24-01: Ivanti Connect Secure and Ivanti Policy Secure: https://www.cisa.gov/news-events/directives/ed-24-01-mitigate-ivanti-connect-secure-and-ivanti-policy-secure-vulnerabilities
- NCSC-UK, Zero trust architecture design principles: https://www.ncsc.gov.uk/collection/zero-trust-architecture