networking, updated
IPsec (ESP, AH, IKE)
IPsec encrypts and authenticates IP packets, mostly for site-to-site and remote-access VPNs. IKE negotiates the keys, ESP carries the protected traffic.
- OSI layer
- 3 Network
- Carried in
- IP (ESP, AH); UDP (IKE, NAT-T)
- Ports
- 500/udp (IKE), 4500/udp (NAT-T)
- Identified by
- IP protocol 50 (ESP), 51 (AH)
- Wireshark filter
esp || ah || isakmp- Encrypted
- yes
Packet structure
ESP is what you see on almost every IPsec VPN:
IPsec ESP packet10 bytes + variable
Select a field to see what it does.
AH only authenticates and is rare today, because it cannot pass NAT:
IPsec AH header12 bytes + variable
Select a field to see what it does.
How it works
- IKE phase 1 / IKE_SA_INIT (UDP 500): the peers agree on algorithms, do a Diffie-Hellman exchange and authenticate each other (pre-shared key or certificates). Result: a protected IKE SA.
- IKE phase 2 / CREATE_CHILD_SA: inside that channel they negotiate the IPsec SAs (one per direction, each with an SPI) and the traffic selectors (which subnets go through the tunnel).
- Data: packets are wrapped in ESP. If NAT is detected, IKE moves to UDP 4500 and ESP is wrapped in UDP 4500 too (NAT traversal).
- Rekeying: SAs expire after a time or data limit and are replaced; you see new SPIs.
| Mode | What is protected | Used for |
|---|---|---|
| Tunnel | The whole original IP packet, inside a new IP header | Site-to-site and remote-access VPNs |
| Transport | Only the payload; the original IP header stays | Host-to-host, L2TP/IPsec, GRE over IPsec |
The IPsec VPN guide explains the setup and phases in more depth.
In Wireshark
| Filter | Shows |
|---|---|
isakmp | IKE negotiation (v1 and v2) |
isakmp.exchangetype == 34 | IKEv2 IKE_SA_INIT |
isakmp.exchangetype == 2 || isakmp.exchangetype == 4 | IKEv1 main mode / aggressive mode |
esp | ESP traffic (incl. NAT-T on UDP 4500) |
esp.spi == 0xc0ffee01 | One SA (one direction of one tunnel) |
ah | AH traffic |
udp.port == 4500 && !esp | NAT-T keepalives (1 byte 0xff) and IKE after NAT detection |
Wireshark can decrypt ESP and IKE if you enter the keys (Preferences › Protocols › ESP / ISAKMP), for example exported from strongSwan.
What normal looks like: a short IKE exchange, then ESP with increasing sequence numbers per SPI, rekeys every few hours. Repeated IKE_SA_INIT without completing points to a proposal mismatch or a wrong PSK.
Security notes
- IKEv1 aggressive mode with PSK sends a hash an attacker can crack offline (
ike-scan --aggressive,psk-crack). Use main mode or IKEv2 and long PSKs or certificates. - Weak proposals: DES, 3DES, MD5, SHA-1, DH groups 1, 2 and 5. Prefer AES-GCM and DH group 19/20/21 or 14+.
- Fingerprinting:
ike-scanidentifies VPN vendors from IKE responses and vendor IDs. - Encrypted traffic hides content from IDS: inspection has to happen before encryption or after decryption on the gateway.
- Metadata stays visible: gateway addresses, packet sizes and timing, and in transport mode even the inner addresses.
- Replay: the sequence number and anti-replay window prevent it; disabling anti-replay is a misconfiguration.
Versions and related
- IKEv1 vs IKEv2: IKEv2 (RFC 7296) is simpler, faster and has built-in NAT-T and EAP. Use it for anything new.
- IPv4 / IPv6: ESP and AH are IP protocols 50 and 51, in IPv6 extension headers.
- TLS VPNs (OpenVPN, AnyConnect) and WireGuard (UDP, fixed modern crypto) are the common alternatives.