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
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
- Outgoing traffic matches the IPsec policy (e.g., access-list).
- IPsec applies encryption/integrity as defined by the SA.
- ESP/AH headers/trailers are added.
- Packet is transmitted over the Internet.
- Receiving device uses SPI to locate SA, decrypts, and verifies integrity before forwarding the original packet.
Configuration Basics (Cisco IOS Example)
- 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
- Configure IKE Phase 1 policy:
crypto isakmp policy 10 encr aes hash sha authentication pre-share group 2 lifetime 86400
- Set pre-shared key:
crypto isakmp key MySecretKey address 203.0.113.1
- Define transform set (IKE Phase 2):
crypto ipsec transform-set MYSET esp-aes esp-sha-hmac
- 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
- Apply crypto map to outside interface:
interface GigabitEthernet0/0 crypto map MYMAP
- (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.
show crypto isakmp sa
– IKE Phase 1 statusshow crypto ipsec sa
– Phase 2/tunnel statusshow crypto map
– Check crypto map applicationdebug crypto isakmp
anddebug crypto ipsec
– Real-time troubleshootingshow 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
- 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
- Configure IKE Phase 1 policy (same settings):
crypto isakmp policy 10 encr aes hash sha authentication pre-share group 2 lifetime 86400
- 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
- Define IPsec transform set:
crypto ipsec transform-set MYSET esp-aes esp-sha-hmac
- 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
- Apply crypto map to outside interface:
interface GigabitEthernet0/0
crypto map MYMAP - (If using NAT) Exempt VPN traffic from NAT.
See earlier example. - Verify tunnel establishment:
show crypto isakmp sa
show crypto ipsec sa
- 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.