TExcelTAD

IPsec VPNs with IKEv2

The lab company opens a second office and wants its two networks joined across the Internet as if by a private cable. The classic answer is an IPsec site-to-site VPN: the two firewalls authenticate each other, agree on keys, and from then on every packet between the offices travels inside an encrypted, authenticated ESP envelope. IPsec is also widely used for remote-access VPNs (it is built into Windows, macOS, iOS and Android). The protocol layout (AH, ESP, tunnel and transport mode, the packets on the wire) is in IPsec: AH, ESP and IKE. This lesson is about designing, configuring and reviewing an IPsec VPN, using IKEv2 (RFC 7296), the current key-exchange protocol.

What a reviewer looks at

Topic Good Finding
Key exchange IKEv2 IKEv1, especially aggressive mode with pre-shared keys (the PSK hash can be captured and guessed offline)
Authentication certificates (or EAP with MFA for remote users); PSKs only long and random, one per peer short or shared PSKs, the same PSK on every branch
IKE and ESP algorithms AES-GCM (or AES-CBC + SHA-2 HMAC), SHA-2 PRF, DH groups 19/20/21 (ECP) or 31 (Curve25519), group 14+ for MODP DES, 3DES, MD5, SHA-1, DH groups 1, 2, 5
Perfect forward secrecy a fresh DH exchange for each Child SA rekey (PFS on) PFS off
Lifetimes IKE SA hours, Child SA about an hour, rekeyed before expiry days, or no rekeying
Traffic selectors only the subnets that must talk 0.0.0.0/0 on both sides when only two subnets need it
Filtering firewall rules on traffic leaving the tunnel, as on any other boundary "the tunnel is trusted": everything from the branch accepted
Exposure UDP 500 and 4500 (and ESP, IP protocol 50) only from the peer's address IKE open to the Internet on a site-to-site gateway that has fixed peers

Algorithm guidance: RFC 8221 (ESP and AH algorithm requirements) and RFC 8247 (IKEv2 algorithm requirements) list what implementations must, should and must not use; national guidance (NIST, BSI, ANSSI) adds stricter profiles.

A site-to-site configuration

The two lab offices, in strongSwan's swanctl.conf format (strongSwan is the common open-source IKEv2 implementation on Linux). Office A is 10.10.0.0/16 behind 198.51.100.1; office B is 10.20.0.0/16 behind 203.0.113.1.

text
connections {
    office-b {
        version = 2
        local_addrs  = 198.51.100.1
        remote_addrs = 203.0.113.1
        proposals = aes256gcm16-prfsha384-ecp384
        local {
            auth = pubkey
            certs = gw-a.lab.test.crt
            id = gw-a.lab.test
        }
        remote {
            auth = pubkey
            id = gw-b.lab.test
        }
        children {
            office-b-net {
                local_ts  = 10.10.0.0/16
                remote_ts = 10.20.0.0/16
                esp_proposals = aes256gcm16-ecp384
                rekey_time = 1h
                start_action = trap
            }
        }
        rekey_time = 4h
    }
}

Certificates on both sides, one IKE proposal and one ESP proposal (AES-256-GCM, with an ECP-384 DH group in the ESP proposal, which is what turns on PFS for rekeys), traffic selectors that name just the two office networks, and rekeying well inside the lifetimes.

Reviewing proposals in code

Gateways often accept long proposal lists "for compatibility", and the weakest accepted proposal is what an attacker can try to negotiate. The checker splits strongSwan-style proposal strings into algorithms and grades each one against a policy table drawn from the RFCs above (simplified).

python · try it yourself
POLICY = {
    # encryption
    "aes256gcm16": "ok", "aes128gcm16": "ok", "chacha20poly1305": "ok", "aes256": "ok (needs integrity)",
    "aes128": "ok (needs integrity)", "3des": "WEAK", "des": "BROKEN",
    # integrity / PRF
    "sha256": "ok", "sha384": "ok", "sha512": "ok", "prfsha256": "ok", "prfsha384": "ok",
    "sha1": "WEAK", "md5": "BROKEN", "prfsha1": "WEAK", "prfmd5": "BROKEN",
    # DH groups
    "ecp256": "ok", "ecp384": "ok", "curve25519": "ok", "x25519": "ok", "modp3072": "ok", "modp2048": "ok (minimum)",
    "modp1536": "WEAK", "modp1024": "BROKEN", "modp768": "BROKEN",
}
def review(name, proposals):
    worst = []
    for prop in proposals.split(","):
        algs = prop.strip().split("-")
        grades = {a: POLICY.get(a, "unknown") for a in algs}
        bad = {a: g for a, g in grades.items() if g in ("WEAK", "BROKEN", "unknown")}
        has_dh = any(a.startswith(("ecp", "modp", "curve", "x25519")) for a in algs)
        if name.startswith("esp") and not has_dh:
            bad["(no DH group)"] = "no PFS on rekey"
        print(f"  {prop.strip():40} {'ok' if not bad else bad}")
        worst += bad.values()
    return worst

configs = {
    "ike (office-b)": "aes256gcm16-prfsha384-ecp384",
    "esp (office-b)": "aes256gcm16-ecp384",
    "ike (old branch)": "aes256-sha256-modp2048, 3des-sha1-modp1024",
    "esp (old branch)": "aes128-sha1, aes256-sha256",
}
for name, props in configs.items():
    print(name)
    issues = review(name, props)
    print(f"  -> {'FINDING: ' + ', '.join(sorted(set(issues))) if issues else 'meets the policy'}")
ike (office-b)
  aes256gcm16-prfsha384-ecp384             ok
  -> meets the policy
esp (office-b)
  aes256gcm16-ecp384                       ok
  -> meets the policy
ike (old branch)
  aes256-sha256-modp2048                   ok
  3des-sha1-modp1024                       {'3des': 'WEAK', 'sha1': 'WEAK', 'modp1024': 'BROKEN'}
  -> FINDING: BROKEN, WEAK
esp (old branch)
  aes128-sha1                              {'sha1': 'WEAK', '(no DH group)': 'no PFS on rekey'}
  aes256-sha256                            {'(no DH group)': 'no PFS on rekey'}
  -> FINDING: WEAK, no PFS on rekey

The old branch gateway still accepts 3DES with SHA-1 and the 1024-bit MODP group for IKE, and its ESP proposals have no DH group, so rekeys reuse key material derived from the original exchange (no PFS). Removing the old proposal entries is the fix; if an old peer then fails to connect, it is that peer that needs upgrading.

The tunnel at run time

A lab session on gateway A (scope: 198.51.100.1, read-only status commands):

console
root@gw-a:~# swanctl --list-sas
office-b: #3, ESTABLISHED, IKEv2, 7a1c22e0b4d9e211_i* 5f0be9c1a2d33a07_r
  local  'gw-a.lab.test' @ 198.51.100.1[4500]
  remote 'gw-b.lab.test' @ 203.0.113.1[4500]
  AES_GCM_16-256/PRF_HMAC_SHA2_384/ECP_384
  established 2104s ago, rekeying in 11687s
  office-b-net: #9, reqid 1, INSTALLED, TUNNEL-in-UDP, ESP:AES_GCM_16-256/ECP_384
    installed 1210s ago, rekeying in 2150s, expires in 2750s
    in  c4a1f0e2,  48291022 bytes, 61830 packets,     0s ago
    out c9b7d114,  12733019 bytes, 40118 packets,     0s ago
    local  10.10.0.0/16
    remote 10.20.0.0/16

TUNNEL-in-UDP and port 4500 show NAT traversal is active (one side is behind NAT, so ESP is wrapped in UDP). The negotiated algorithms match the configuration, the traffic selectors are the two office networks, and both SAs will rekey before they expire.

How problems and attacks are detected

  • IKE logs: authentication failures (AUTH_FAILED, unknown peer IDs), proposal mismatches (NO_PROPOSAL_CHOSEN) and IKE requests from unknown addresses. On a site-to-site gateway any IKE traffic from a non-peer address is suspicious.
  • SA monitoring: tunnels that flap, rekeys that fail, traffic counters that jump.
  • Traffic after decryption: the tunnel delivers packets into your network, so firewall logs on the tunnel interface, IDS sensors behind the gateway and flow logs apply exactly as for any other zone boundary.

How it is hardened

  • IKEv2 only, certificate authentication (with revocation checking) or EAP with MFA for users.
  • One strong proposal per direction, PFS on, sensible lifetimes.
  • Traffic selectors and firewall rules limited to what must cross; treat the remote office as its own zone.
  • IKE reachable only from the peers (site-to-site), or protected with DoS mitigations (cookies, rate limits) for remote-access gateways.
  • Patch the VPN gateway first: VPN appliances have been among the most exploited Internet-facing systems of recent years, because they are exposed and powerful.

Pitfalls

  • MTU problems after enabling the tunnel: ESP adds overhead, large packets fragment or vanish; clamp the TCP MSS on the tunnel or lower the MTU.
  • 0.0.0.0/0 selectors on a branch: all its Internet traffic now hairpins through the head office (sometimes intended, often not).
  • Leaving IKEv1 enabled "for one old partner".
  • Treating the VPN peer as trusted and skipping filtering on decrypted traffic.

Practice

1. Why is IKEv1 aggressive mode with a PSK a finding?

In aggressive mode the responder sends a hash computed from the PSK before the identities are protected, so an attacker who can start a negotiation can capture it and try to guess the PSK offline. Use IKEv2 with certificates (or at least long random PSKs).

2. Add "esp (new)": "chacha20poly1305-x25519" to configs. What does the checker say?

Both algorithms are in the policy as ok and a DH group is present, so it prints ok and meets the policy.

3. The branch office is compromised by ransomware. What limits the damage to the head office?

Narrow traffic selectors, firewall rules on traffic leaving the tunnel (only the services the branch needs, for example the mail and file servers on their ports), IDS monitoring of tunnel traffic, and the head office's own internal segmentation. A tunnel without filtering makes the two offices one flat network.

Test yourself

Which ports and protocols does an IPsec gateway with NAT traversal use?

  1. TCP 443 only
  2. UDP 500 (IKE), UDP 4500 (NAT-T), and ESP (IP protocol 50) without NAT
  3. TCP 22
  4. UDP 53

What does a DH group in the ESP proposal (e.g. aes256gcm16-ecp384) give you?

  1. Faster encryption
  2. Perfect forward secrecy on Child SA rekeys
  3. Compression
  4. NAT traversal

Which proposal from the old branch is BROKEN under the policy?

  1. aes256-sha256-modp2048
  2. 3des-sha1-modp1024
  3. aes256gcm16-ecp384
  4. aes128gcm16

Why filter traffic coming out of a site-to-site tunnel?

  1. IPsec doesn't encrypt
  2. The remote site is another zone: a compromise there must not reach everything here
  3. Filtering is required by IKE
  4. To reduce MTU