Lab 15: VPN and Tunneling

🎯 Objective

Understand SSH tunnel types, generate SSH key pairs, demonstrate netcat tunneling, compare VPN protocols, and detect tunnel abuse patterns.

πŸ“š Background

Tunneling is the encapsulation of one network protocol within another. This technique is fundamental to VPNs, which create encrypted "tunnels" through untrusted networks (like the internet) to securely connect remote users to corporate networks. SSH (Secure Shell) provides powerful built-in tunneling capabilities that can forward specific ports, create dynamic SOCKS proxies, or reverse-tunnel from behind firewalls.

VPN protocols have evolved significantly, from the broken PPTP (Point-to-Point Tunneling Protocol) of the 1990s through IPsec and OpenVPN to the modern WireGuard protocol. WireGuard, introduced in 2019, uses state-of-the-art cryptography, has only ~4,000 lines of code (vs. 400,000 for OpenVPN), and is faster and simpler to configure while being more secure.

The security community must understand tunneling from both offensive and defensive perspectives. Attackers use tunneling to evade firewalls (DNS tunneling to exfiltrate data, HTTP tunneling for C2 communication). Defenders need to detect anomalous tunneling through traffic analysis, protocol inspection, and behavioral monitoring.

⏱️ Estimated Time

45 minutes

πŸ“‹ Prerequisites

  • Lab 06: Public Key Cryptography

  • Basic networking (TCP, ports)

πŸ› οΈ Tools Used

  • ssh-keygen β€” SSH key pair generation

  • openssl β€” alternative key generation

  • nc (netcat) β€” network connection utility

  • python3 β€” VPN comparison

πŸ”¬ Lab Instructions

Step 1: Generate SSH Key Pairs

πŸ“Έ Verified Output:

πŸ’‘ What this means: Ed25519 provides stronger security than RSA-4096 with much smaller key size and faster operations. Use Ed25519 for new SSH keys. The private key must never leave your machine; the public key goes to servers.

Step 2: SSH Tunnel Types - Local Port Forwarding (-L)

πŸ“Έ Verified Output:

πŸ’‘ What this means: SSH tunnels are legitimately used by developers and admins, but also abused by attackers. Monitor for SSH connections with unusual tunnel usage, especially -R (reverse tunnels) which can expose internal services to internet.

Step 3: Netcat Tunnel Demo

πŸ“Έ Verified Output:

πŸ’‘ What this means: Netcat is the "Swiss Army knife" of networking. It can create servers, clients, file transfers, and basic tunnels. Attackers use it for reverse shells and data exfiltration. Defenders should audit for nc usage with unusual flags.

Step 4: VPN Protocol Comparison

πŸ“Έ Verified Output:

πŸ’‘ What this means: WireGuard replaces OpenVPN's 400,000 lines of code with ~4,000 lines β€” a much smaller attack surface. It's been merged into the Linux kernel (5.6+) and is now the default for many VPN services. PPTP should never be used β€” it can be cracked in real-time.

Step 5: DNS Tunneling - Attack and Detection

πŸ“Έ Verified Output:

πŸ’‘ What this means: DNS tunneling is one of the most effective data exfiltration techniques because DNS is universally allowed. Detecting it requires monitoring DNS query patterns β€” length, entropy, volume β€” rather than blocking DNS entirely.

Step 6: Detecting Tunnel Abuse

πŸ“Έ Verified Output:

πŸ’‘ What this means: Tunneling detection is about pattern recognition. Legitimate traffic has predictable, irregular patterns (human browsing). Malware beacons at regular intervals, encodes data in unusual protocols, and creates long-duration connections with regular small packets.

Step 7: WireGuard Configuration Example

πŸ“Έ Verified Output:

πŸ’‘ What this means: WireGuard's simplicity is its strength. With fewer lines of code, there's less attack surface. The configuration is straightforward: each peer lists the other's public key and allowed IP ranges. Modern Linux systems include WireGuard in the kernel.

Step 8: Cleanup and Summary

πŸ“Έ Verified Output:

πŸ’‘ What this means: SSH tunneling is a powerful tool for secure remote access without opening additional firewall ports. Understand both the legitimate uses (accessing internal services securely) and abuse potential (exfiltration, reverse shells).

βœ… Verification

🚨 Common Mistakes

  • Using PPTP: Absolutely never use PPTP β€” it's cryptographically broken and can be cracked in hours

  • Split tunneling security gap: Split tunneling VPNs (only some traffic through VPN) can leak data if misconfigured

  • Weak PSK for IPsec: Use certificates instead of pre-shared keys for production IPsec

  • Leaving reverse tunnels open: SSH -R tunnels should be explicitly approved and monitored; unauthorized reverse tunnels are backdoors

  • Not monitoring outbound SSH: Attackers love SSH tunnels because port 22 is often allowed outbound

πŸ“ Summary

  • SSH tunnels enable secure access to remote services; -L forwards local ports, -R exposes local services remotely, -D creates a SOCKS proxy

  • VPN protocols range from broken (PPTP) to excellent (WireGuard); always use WireGuard for new deployments

  • WireGuard uses modern cryptography (Curve25519, ChaCha20), ~4,000 lines of code, and is faster than all alternatives

  • DNS and HTTP tunneling are used by attackers to exfiltrate data through allowed ports; detect via anomaly patterns

  • Defending against tunnel abuse: baseline normal traffic, alert on deviations, inspect protocol anomalies, limit outbound protocols

πŸ”— Further Reading

Last updated