VPN: IPsec, SSL VPN, WireGuard and tunnels
A VPN builds an encrypted tunnel over a public network so traffic between a device (or office) and a private network cannot be read on the way. This note covers remote access vs site-to-site, IPsec (IKE + ESP), SSL VPN/OpenVPN, WireGuard, GRE/IPIP, split tunneling and a real WireGuard lab.
115.1 Why is it used?
- On cafe or airport Wi-Fi an attacker on the same network can capture plain traffic and read passwords.
- Exposing an office server directly on the internet is risky; a VPN gives a private, secure path into the network.
- Connects branch offices over the internet without leased lines.
- Encapsulation: the original packet is encrypted and packed inside a new outer packet.
115.2 How a remote access VPN works
- Step 1
1.Laptop on cafe Wi-Fi runs a VPN client
- Step 2
2.Original packet (dst 192.168.50.10) is encrypted
- Step 3
3.Wrapped in an outer packet to VPN gateway 203.0.113.10
- Step 4
4.Attacker on the Wi-Fi sees only random bytes
- Step 5
5.Gateway decrypts and forwards the original packet
- Step 6
6.Office server 192.168.50.10 receives it
115.3 Comparison: remote access vs site-to-site
| Point | Remote access | Site-to-site |
|---|---|---|
| Connects | One device to a network | Network to network (office to office) |
| Where VPN runs | Client app on the laptop | On the routers / gateways |
| Tunnel | Up when the user connects | Permanent, always on |
| Users notice? | Yes, they start the client | No, transparent |
| Example | Laptop to office gateway | Delhi router 203.0.113.1 to Mumbai router 198.51.100.1 |
115.4 IPsec: IKE and ESP
- Step 1
1.Delhi PC 192.168.10.5 sends a normal packet
- Step 2
2.IKE on UDP 500: routers authenticate (PSK or certificate) and build an SA
- Step 3
3.IKE phases = main mode + quick mode (previous video)
- Step 4
4.ESP (IP proto 50), tunnel mode: original packet encrypted, new IP header 203.0.113.1 -> 198.51.100.1
- Step 5
5.NAT on the path? ESP wrapped in UDP 4500 (NAT traversal)
- Step 6
6.Mumbai router decrypts and delivers to server 192.168.20.10
- SA (security association) = the agreed keys and algorithms between two peers.
- ESP header carries an SPI and sequence number; the inner packet is encrypted.
115.5 SSL VPN and OpenVPN
- Hotel/airport firewalls often allow only web traffic, so IKE and ESP get blocked.
- SSL VPN uses TLS, the same encryption as HTTPS.
- OpenVPN: open source, UDP 1194 by default, can run on TCP 443 when needed; looks like normal HTTPS to the firewall.
- A virtual TUN interface (tun0) comes up on the client.
115.6 WireGuard
- Every peer has a private and a public key; peers exchange only public keys.
- Handshake completes in one round trip; then all data travels on one UDP port, 51820.
- AllowedIPs works like a routing table: the public key decides which IP goes to which peer (cryptokey routing).
- Packets from unknown public keys are ignored.
115.7 GRE and IPIP: tunnels without encryption
| Tunnel | IP protocol | Carries | Encrypted? |
|---|---|---|---|
| IPIP | 4 | IPv4 inside IPv4 only | No |
| GRE | 47 | Any protocol, multicast, OSPF updates | No |
| IPsec ESP | 50 | Encrypted original packet | Yes |
| WireGuard | UDP 51820 | Encrypted IP packets | Yes |
| OpenVPN | UDP 1194 / TCP 443 | Encrypted inside TLS | Yes |
- Companies often run GRE over IPsec: GRE carries routing/multicast, IPsec adds encryption.
115.8 Full tunnel vs split tunnel
| Point | Full tunnel | Split tunnel |
|---|---|---|
| What goes in the tunnel | All traffic (0.0.0.0/0) | Only office subnets (e.g. 192.168.50.0/24) |
| Internet traffic (YouTube) | Via office gateway | Directly via the ISP |
| Security policy | Applied to every request | Office firewall does not see internet traffic |
| Cost | More gateway load and latency | Faster, less load |
115.9 Practical: WireGuard between two containers
- Containers office 192.168.135.10 and laptop 192.168.135.20 on 192.168.135.0/24; tunnel IPs 10.8.0.1 and 10.8.0.2.
- The kernel module was not loaded in the Docker VM, so the lab used userspace wireguard-go (same protocol on a TUN device).
- Step 1
1.laptop: ping 10.8.0.1 leaves via wg0
- Step 2
2.WireGuard encrypts it with the office peer's public key
- Step 3
3.Wire: UDP 192.168.135.20:51820 -> 192.168.135.10:51820, length 128
- Step 4
4.office: decrypts, checks AllowedIPs for the source
- Step 5
5.office wg0 sees a clear ICMP echo request from 10.8.0.2
umask 077; wg genkey > /root/privwg pubkey < /root/priv > /root/pubcat /root/pubwireguard-go wg0TGBzuKOQPX2eNU6XmtxbP3M1bnJYBJgLk7cyFi2zdSk=ip address add 10.8.0.2/24 dev wg0wg set wg0 listen-port 51820 \ private-key /root/priv \ peer $OPUB \ allowed-ips 10.8.0.0/24 \ endpoint 192.168.135.10:51820 \ persistent-keepalive 25ip link set wg0 upwg show wg0interface: wg0 listening port: 51820peer: dSLJqokmBKf0AV+uJo3jTqfQRbLScfgBVIpJbUyR/wk= endpoint: 192.168.135.10:51820 allowed ips: 10.8.0.0/24 latest handshake: 8 seconds ago transfer: 92 B received, 180 B sent- latest handshake: 8 seconds ago = tunnel up, both peers connected.
- persistent-keepalive 25 keeps NAT mappings alive.
ping -c 3 10.8.0.1 # on laptoptcpdump -lnn -i eth0 -c 6 udp port 51820 # on office: the wiretcpdump -lnn -i wg0 -c 4 icmp # on office: inside the tunnel64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=6.82 ms64 bytes from 10.8.0.1: icmp_seq=2 ttl=64 time=51.0 ms64 bytes from 10.8.0.1: icmp_seq=3 ttl=64 time=1.49 ms3 packets transmitted, 3 received, 0% packet loss, time 2002ms office$ tcpdump -lnn -i eth0 -c 6 udp port 5182001:59:50.328328 IP 192.168.135.20.51820 > 192.168.135.10.51820: UDP, length 12801:59:50.333264 IP 192.168.135.10.51820 > 192.168.135.20.51820: UDP, length 12801:59:51.351123 IP 192.168.135.20.51820 > 192.168.135.10.51820: UDP, length 12801:59:51.378800 IP 192.168.135.10.51820 > 192.168.135.20.51820: UDP, length 1286 packets captured office$ tcpdump -lnn -i wg0 -c 4 icmp01:59:50.330733 IP 10.8.0.2 > 10.8.0.1: ICMP echo request, id 65051, seq 1, length 6401:59:50.330869 IP 10.8.0.1 > 10.8.0.2: ICMP echo reply, id 65051, seq 1, length 64On eth0 there is only UDP 51820 (no ICMP visible); on wg0, after decryption, the ICMP echo requests and replies are clear.
ip routeip route get 10.8.0.1ip route get 1.1.1.1default via 192.168.135.1 dev eth010.8.0.0/24 dev wg0 proto kernel scope link src 10.8.0.2192.168.135.0/24 dev eth0 proto kernel scope link src 192.168.135.20 10.8.0.1 dev wg0 src 10.8.0.2 uid 01.1.1.1 via 192.168.135.1 dev eth0 src 192.168.135.20 uid 010.8.0.0/24 goes via wg0, 1.1.1.1 via eth0: a split tunnel. For a full tunnel, put 0.0.0.0/0 in AllowedIPs.
115.10 Common mistakes
- Believing a VPN makes you anonymous: it only encrypts the path to the VPN server; the provider sees your requests and sites still know you by logins and cookies.
- Expecting a VPN to stop malware or phishing.
- Assuming GRE/IPIP are secure: they add no encryption.
- Wrong public key on a WireGuard peer: the handshake never completes (lab: ping 100% loss).
115.11 Interview tips
- IPsec = IKE (UDP 500, keys + SA) + ESP (proto 50, encryption); NAT-T uses UDP 4500.
- IPsec vs WireGuard: IPsec is the older standard every vendor supports; WireGuard has a tiny codebase, fixed modern crypto and public keys.
- WireGuard is UDP only; on TCP 443-only networks an SSL VPN (OpenVPN on TCP 443) works better.
- Split tunnel = only office subnets in the tunnel; full tunnel = 0.0.0.0/0.