SkillPilotAI

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

Remote access VPN from a cafe
  1. 1.Laptop on cafe Wi-Fi runs a VPN client

  2. 2.Original packet (dst 192.168.50.10) is encrypted

  3. 3.Wrapped in an outer packet to VPN gateway 203.0.113.10

  4. 4.Attacker on the Wi-Fi sees only random bytes

  5. 5.Gateway decrypts and forwards the original packet

  6. 6.Office server 192.168.50.10 receives it

115.3 Comparison: remote access vs site-to-site

PointRemote accessSite-to-site
ConnectsOne device to a networkNetwork to network (office to office)
Where VPN runsClient app on the laptopOn the routers / gateways
TunnelUp when the user connectsPermanent, always on
Users notice?Yes, they start the clientNo, transparent
ExampleLaptop to office gatewayDelhi router 203.0.113.1 to Mumbai router 198.51.100.1

115.4 IPsec: IKE and ESP

IPsec site-to-site tunnel
  1. 1.Delhi PC 192.168.10.5 sends a normal packet

  2. 2.IKE on UDP 500: routers authenticate (PSK or certificate) and build an SA

  3. 3.IKE phases = main mode + quick mode (previous video)

  4. 4.ESP (IP proto 50), tunnel mode: original packet encrypted, new IP header 203.0.113.1 -> 198.51.100.1

  5. 5.NAT on the path? ESP wrapped in UDP 4500 (NAT traversal)

  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

TunnelIP protocolCarriesEncrypted?
IPIP4IPv4 inside IPv4 onlyNo
GRE47Any protocol, multicast, OSPF updatesNo
IPsec ESP50Encrypted original packetYes
WireGuardUDP 51820Encrypted IP packetsYes
OpenVPNUDP 1194 / TCP 443Encrypted inside TLSYes
  • Companies often run GRE over IPsec: GRE carries routing/multicast, IPsec adds encryption.

115.8 Full tunnel vs split tunnel

PointFull tunnelSplit tunnel
What goes in the tunnelAll traffic (0.0.0.0/0)Only office subnets (e.g. 192.168.50.0/24)
Internet traffic (YouTube)Via office gatewayDirectly via the ISP
Security policyApplied to every requestOffice firewall does not see internet traffic
CostMore gateway load and latencyFaster, 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).
Lab packet path
  1. 1.laptop: ping 10.8.0.1 leaves via wg0

  2. 2.WireGuard encrypts it with the office peer's public key

  3. 3.Wire: UDP 192.168.135.20:51820 -> 192.168.135.10:51820, length 128

  4. 4.office: decrypts, checks AllowedIPs for the source

  5. 5.office wg0 sees a clear ICMP echo request from 10.8.0.2

Terminal
umask 077; wg genkey > /root/privwg pubkey < /root/priv > /root/pubcat /root/pubwireguard-go wg0
Output
TGBzuKOQPX2eNU6XmtxbP3M1bnJYBJgLk7cyFi2zdSk=
Terminal
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 wg0
Output
interface: 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.
Terminal
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 tunnel
Output
64 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 64

On eth0 there is only UDP 51820 (no ICMP visible); on wg0, after decryption, the ICMP echo requests and replies are clear.

Terminal
ip routeip route get 10.8.0.1ip route get 1.1.1.1
Output
default 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 0

10.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.