30 Mins

8. Network Layer Security (IPsec & VPNs)

IPsec Protocol Suite: AH vs ESP

IPsec secures IP packets at the network layer. It utilizes two primary protocols: Authentication Header (AH) and Encapsulating Security Payload (ESP).

AH provides connectionless integrity and data origin authentication but fails to provide confidentiality because it does not encrypt the packet payload. ESP provides confidentiality, data origin authentication, and integrity.

The following table compares the capabilities of these protocols:

ProtocolConfidentialityAuthentication
AHNoYes
ESPYesYes
Security Operator Note: Always prioritize ESP over AH in modern VPN deployments to ensure payload confidentiality against traffic sniffing.
Python 3
import hashlib
def calculate_ah_digest(data, secret):
    return hashlib.sha256(secret.encode() + data).hexdigest()
data = b'SensitivePayload'
secret = 'VoidX_Key_2024'
print(f'Authentication Digest: {calculate_ah_digest(data, secret)}')

Tunnel Mode vs Transport Mode

Transport Mode encapsulates only the payload of the IP packet, leaving the original IP header visible. It is primarily used for end-to-end communication between two hosts.

Tunnel Mode encapsulates the entire original IP packet (header + payload) inside a new IP packet with a new header. This is the standard for Gateway-to-Gateway VPN architectures as it hides the internal network topology.

To inspect current network interface modes using terminal tools, you can use the 'ip xfrm' command suite on Linux systems.

Security Operator Note: Use Tunnel Mode when connecting branch offices to a central HQ, as it effectively hides private internal IP addresses behind a public VPN gateway IP.
Text
# List current IPsec Security Policy Database (SPD) entries
sudo ip xfrm policy show
# View current security associations
sudo ip xfrm state show

Internet Key Exchange (IKEv2) Handshake

IKEv2 is a protocol used to set up a Security Association (SA) in the IPsec protocol suite. It is highly efficient, reducing the number of round-trips compared to IKEv1.

The exchange begins with IKE_SA_INIT (negotiating security parameters) followed by IKE_AUTH (authentication and initial child SA creation).

Security Associations (SA) define the algorithms (e.g., AES-GCM) and keys used for the encrypted tunnel session.

Security Operator Note: Monitor IKEv2 exchange logs in /var/log/charon.log on Linux systems to troubleshoot handshake failures during SA negotiation.
Python 3
import struct
def simulate_ike_header(spi, next_payload):
    # Simulate a basic 28-byte IKEv2 header structure
    return struct.pack('>QQBBBL', spi, spi, 32, 0, 1, 0, 0)
header = simulate_ike_header(0x12345678, 33)
print(f'IKEv2 Header Bytes: {header.hex()}')

Auditing VPN Packet Headers

To audit VPN traffic, security operators use TShark or Wireshark to identify ESP packets. ESP packets are identifiable by the IP protocol number 50.

Because ESP encrypts the payload, traditional pattern matching (like IDS signatures) will fail on the inner content, emphasizing the need to analyze the outer packet headers.

We can capture these packets using a specific BPF filter designed for IPsec traffic.

Security Operator Note: When performing traffic analysis, always look for the Security Parameter Index (SPI) in the ESP header to identify specific security associations.
Text
# Capture ESP traffic on interface eth0
tshark -i eth0 -f 'esp' -w vpn_capture.pcap
# Inspect packet summary statistics
tshark -r vpn_capture.pcap -z conv,ip

Knowledge Check

Ready to test your understanding of 8. Network Layer Security (IPsec & VPNs)?

Back