Tunnel all your traffic on “hostile” networks with OpenVPN
Friday, August 20th, 2010I am often on wireless networks that are unsecured ie. either don't use encryption or if they are I may not trust they will not tamper with my data (you never know). To protect my traffic on such networks I decided to tunnel nearly all my traffic through an OpenVPN server while I'm on such networks. I will show you how you can do it yourself on your Linux or Mac laptops. You should be able to do similar in Windows but it may be a bit more work on the client.
OpenVPN server setup
iptables -t nat -A POSTROUTING -s 10.8.0.0/16 -o eth0 -j MASQUERADE
OpenVPN client setup
Tunnel all traffic
Update: Shame on me. Someone has already posted the directions on how to do this at
http://manoftoday.wordpress.com/2006/12/03/openvpn-20-howto/
Thanks to @somic for pointing this out.
script-security 3 system
up /usr/local/bin/set_up_routes.sh
Your set_up_routes.sh would look something like this. Please change the VPN_SERVER_IP variable to the IP of your OpenVPN server.
#!/bin/sh
# Note the wireless network default gateway
DEFAULT_GATEWAY=`netstat -nr | grep ^0.0.0.0 | awk '{ print $2 }'`
# Find out what's the IP on the
VPN_GATEWAY=`netstat -nr | grep tun | grep -v 0.0.0.0 | awk '{ print $2 }' | sort | uniq`
VPN_GATEWAY=`ifconfig | grep 172.16 | cut -f3 -d: | cut -f1 -d" "`
VPN_SERVER_IP="1.2.3.4"
sudo /sbin/route del default
#
sudo /sbin/route add -host $VPN_SERVER_IP gw $DEFAULT_GATEWAY
# Don't tunnel traffic to 2.3.4.5 since it's already SSLized
sudo /sbin/route add -host 2.3.4.5 gw $DEFAULT_GATEWAY
sudo /sbin/route add default gw $VPN_GATEWAY
This script was tested under Ubuntu Linux but should work the same under Mac OS X. On Windows you may need to use PowerShell or use Cygwin.
Tunneling traffic for specific IPs
If you only wish to tunnel traffic for particular set of IPs you only need to add those routes to your ovpn file e.g.
route 72.0.0.0 255.0.0.0 route 75.0.0.0 255.0.0.0