IPsec Fundamentals

Detailed Explanation for Network Security & VPNs

What is IPsec?

IPsec (Internet Protocol Security) is a suite of protocols designed to secure IP communications by authenticating and encrypting each IP packet in a communication session. It is a core technology for building secure VPNs (Virtual Private Networks) and is widely used for both site-to-site and remote-access VPN scenarios.

  • Confidentiality: Data is encrypted to prevent eavesdropping.
  • Integrity: Ensures the data is not tampered with in transit.
  • Authentication: Verifies the identities of the communication parties.
IPsec is commonly used to secure data between offices (site-to-site VPNs), remote users, or even between servers on a LAN.

IPsec Components

1. Authentication Header (AH)

  • Provides data integrity, sender authentication, and anti-replay protection.
  • No encryption (does not provide confidentiality).
  • Inserts AH header between the IP header and payload.

2. Encapsulating Security Payload (ESP)

  • Provides data confidentiality (encryption), integrity, authentication, and anti-replay protection.
  • ESP header appears after the IP header (transport mode) or after a new IP header (tunnel mode); payload is encrypted.
  • ESP is the most widely used IPsec protocol.

3. Security Associations (SA) and Security Parameter Index (SPI)

  • Security Association (SA): Defines how data is secured (algorithms, keys, lifetime). Each direction requires its own SA.
  • Security Parameter Index (SPI): Unique identifier for each SA, used in ESP/AH headers.

IPsec Modes

1. Transport Mode

  • Encrypts/authenticates only the payload (data); original IP header is left intact.
  • Use Case: Host-to-host communication on trusted networks (e.g., server-to-server encryption).
  • Example: Two internal servers exchanging sensitive data.

2. Tunnel Mode

  • Encrypts/authenticates the entire original IP packet (header + data), encapsulating it within a new IP packet.
  • Use Case: Site-to-site VPNs and secure gateway-to-gateway communications over the Internet.
  • Example: Two branch routers establish a VPN tunnel over the public Internet.

IPsec Protocols and Headers

Feature AH ESP
Provides Encryption No Yes
Provides Integrity Yes Yes
Provides Authentication Yes Yes (optional)
Packet Structure AH header after IP header ESP header after IP header, payload is encrypted
Use in Practice Rare, legacy Widely used (VPNs)
ESP Tunnel Mode Packet Structure:
New IP Header | ESP Header | Original IP Header | Payload (Encrypted) | ESP Trailer | ESP Auth

Key Management

1. Manual Keying

  • Pre-shared keys configured manually on both ends.
  • Not scalable and rarely used except for simple lab/test setups.

2. Automated Keying (IKE)

  • Internet Key Exchange (IKE) automates negotiation and management of cryptographic keys.
  • IKEv1: Original standard, uses Main/Aggressive mode for Phase 1.
  • IKEv2: Modern, more efficient, supports NAT traversal and improved security.

IKE Phases

Phase 1 (ISAKMP SA Establishment)

  • Establishes a secure, authenticated channel to protect further negotiation.
  • Negotiates encryption, hash, authentication, DH group, and SA lifetime.
  • Authenticates peers using pre-shared keys or digital certificates.

Phase 2 (IPsec SA Negotiation)

  • Negotiates IPsec SAs (protocols, algorithms, traffic selectors).
  • Defines what traffic is protected and which algorithms to use.
  • Supports Perfect Forward Secrecy (PFS) for improved security.

Encryption and Authentication Algorithms

Algorithm Type Options Notes
Encryption AES (preferred), 3DES (legacy) AES is faster and more secure; use where possible.
Integrity/Hashing SHA-1, SHA-2, MD5 (legacy) SHA-2 is strongest. Avoid MD5 (weak).
Authentication Pre-shared keys, Digital certificates Certificates are more secure and scalable.

Perfect Forward Secrecy (PFS)

  • Ensures compromise of one key does not affect past/future session keys.
  • Forces a new Diffie-Hellman key exchange for each session.
  • Recommended for high-security environments.

IPsec Policy and Transform Sets

  • Policy: Defines which traffic should be protected (using access-lists/selectors).
  • Transform Set: Specifies the protocols/algorithms for securing traffic (e.g., ESP with AES & SHA).
  • Both VPN peers must have compatible policies and transform sets.
Example:
crypto ipsec transform-set MYSET esp-aes esp-sha-hmac

NAT Traversal (NAT-T)

  • Problem: NAT modifies IP headers, breaking IPsec integrity checks.
  • Solution: NAT-T encapsulates IPsec packets inside UDP (port 4500), allowing VPN traffic to traverse NAT devices.

IPsec Packet Flow

  1. Outgoing traffic matches the IPsec policy (e.g., access-list).
  2. IPsec applies encryption/integrity as defined by the SA.
  3. ESP/AH headers/trailers are added.
  4. Packet is transmitted over the Internet.
  5. Receiving device uses SPI to locate SA, decrypts, and verifies integrity before forwarding the original packet.

Configuration Basics (Cisco IOS Example)

  1. Define interesting traffic (ACL):
    access-list 100 permit ip 192.168.1.0 0.0.0.255 192.168.2.0 0.0.0.255
  2. Configure IKE Phase 1 policy:
    crypto isakmp policy 10
     encr aes
     hash sha
     authentication pre-share
     group 2
     lifetime 86400
            
  3. Set pre-shared key:
    crypto isakmp key MySecretKey address 203.0.113.1
  4. Define transform set (IKE Phase 2):
    crypto ipsec transform-set MYSET esp-aes esp-sha-hmac
  5. Create crypto map and bind elements:
    crypto map MYMAP 10 ipsec-isakmp
     set peer 203.0.113.1
     set transform-set MYSET
     match address 100
            
  6. Apply crypto map to outside interface:
    interface GigabitEthernet0/0
     crypto map MYMAP
            
  7. (Optional) Exempt VPN traffic from NAT:
    ip nat inside source list 101 interface GigabitEthernet0/0 overload
    access-list 101 deny ip 192.168.1.0 0.0.0.255 192.168.2.0 0.0.0.255
    access-list 101 permit ip 192.168.1.0 0.0.0.255 any
            

Troubleshooting IPsec

  • Check Phase 1/2 negotiation (parameters, keys, algorithms).
  • Verify connectivity (no firewalls blocking UDP 500/4500 or ESP protocol 50).
  • Ensure ACLs and crypto maps match on both peers.
Useful commands (Cisco IOS):
  • show crypto isakmp sa – IKE Phase 1 status
  • show crypto ipsec sa – Phase 2/tunnel status
  • show crypto map – Check crypto map application
  • debug crypto isakmp and debug crypto ipsec – Real-time troubleshooting
  • show crypto engine connections active

IPsec Use Cases

  • Site-to-Site VPN: Securely connect office networks over the Internet.
  • Remote-Access VPN: Securely connect individual users to company resources.
  • Host-to-Host: Protect data between servers (e.g., database replication).
  • Cloud Connectivity: Secure connections to IaaS or hybrid cloud environments.

Key Points & Exam Tips

  • IPsec provides encryption, integrity, and authentication at the IP layer.
  • ESP is most common; AH is rarely used in practice.
  • Use tunnel mode for site-to-site VPNs; transport mode for host-to-host.
  • IKEv2 is preferred for new deployments.
  • Both ends must match on policies, keys, and transform sets.
  • For NAT, use NAT exemption and NAT-T as required.
  • PFS is a security best practice.
  • AES and SHA are recommended for encryption/integrity.
  • Troubleshoot with show crypto isakmp sa, show crypto ipsec sa, and debug commands.

Step-by-Step: Site-to-Site IPsec VPN Example

Scenario:
  • Head Office Router (A): Public IP 198.51.100.1, LAN 192.168.1.0/24
  • Branch Office Router (B): Public IP 203.0.113.1, LAN 192.168.2.0/24
Goal: Encrypt all traffic between both LANs.
  1. Define ACL for interesting traffic (on each router):
    access-list 100 permit ip 192.168.1.0 0.0.0.255 192.168.2.0 0.0.0.255
  2. Configure IKE Phase 1 policy (same settings):
    crypto isakmp policy 10
     encr aes
     hash sha
     authentication pre-share
     group 2
     lifetime 86400
            
  3. Set pre-shared key:
    Router A: crypto isakmp key MySecretKey address 203.0.113.1
    Router B: crypto isakmp key MySecretKey address 198.51.100.1
  4. Define IPsec transform set:
    crypto ipsec transform-set MYSET esp-aes esp-sha-hmac
  5. Create crypto map and reference ACL/peer/transform set:
    crypto map MYMAP 10 ipsec-isakmp
     set peer [Other Router’s Public IP]
     set transform-set MYSET
     match address 100
            
  6. Apply crypto map to outside interface:
    interface GigabitEthernet0/0
    crypto map MYMAP
  7. (If using NAT) Exempt VPN traffic from NAT.
    See earlier example.
  8. Verify tunnel establishment:
    show crypto isakmp sa
    show crypto ipsec sa
  9. Test end-to-end connectivity:
    ping 192.168.2.10 (from a PC in the HQ LAN to a PC in the Branch LAN)

When to Use

  • For secure, always-on connectivity between two networks (e.g., offices, B2B partners).
  • Not suitable for individual users—use remote access VPN for mobile clients.

Exam Tips & Key Reminders

  • Both ends must match on IKE policies, transform sets, pre-shared key, and interesting traffic.
  • Use tunnel mode for site-to-site (default).
  • Configure NAT exemption for VPN traffic if NAT is in use.
  • Check for mismatched parameters, ACL errors, or missing crypto map assignments if tunnel fails.
  • Debug commands are powerful, but use carefully in production.

IPsec Fundamentals Quiz

1. What is the primary purpose of IPsec?

Correct answer is A. IPsec authenticates and encrypts IP packets to ensure confidentiality, integrity, and authentication over untrusted networks.

2. Which IPsec component provides encryption, integrity, authentication, and anti-replay protection?

Correct answer is C. ESP provides encryption along with integrity, authentication, and anti-replay features.

3. What is the difference between Transport Mode and Tunnel Mode in IPsec?

Correct answer is B. Transport mode encrypts the payload only; Tunnel mode encrypts the entire original IP packet and adds a new header.

4. What is the role of the Security Association (SA) in IPsec?

Correct answer is D. An SA is a logical connection defining security parameters like encryption and keys for IPsec communication.

5. Which protocol is used to automate key negotiation and management in IPsec?

Correct answer is A. IKE automates the negotiation and management of cryptographic keys in IPsec.

6. What is Perfect Forward Secrecy (PFS) in IPsec?

Correct answer is B. PFS generates new keys for each session, preventing key compromise from affecting other sessions.

7. Why is NAT Traversal (NAT-T) important in IPsec?

Correct answer is D. NAT-T encapsulates IPsec packets in UDP to overcome NAT device issues breaking IPsec integrity checks.

8. Which encryption algorithm is preferred in modern IPsec deployments?

Correct answer is C. AES is widely used today due to its strong security and performance.

9. What command is used to view the status of ISAKMP Phase 1 security associations on Cisco devices?

Correct answer is B. This command displays the status of IKE Phase 1 security associations.

10. What is a common troubleshooting cause for IPsec tunnel failures?

Correct answer is A. Parameter mismatches such as pre-shared keys or encryption algorithms cause negotiation failures.

← Back to Home