Escaping Restrictive/Untrusted Networks with OpenVPN on EC2

| 13 Comments

Perhaps you are behind a corporate firewall which does not allow you to access certain types of resources on the Internet. Or, perhaps you are accessing the Internet over an open wifi where you do not trust your network traffic to your fellow wifi users or the admins running the local network.

These instructions guide you in setting up an OpenVPN server on an EC2 instance, sending all your network traffic through a secure channel to port 80 on the EC2 instance and from there out to the Internet.

EC2 Instance

Run the latest Ubuntu 9.10 Karmic image. You can find the most current AMI id in a table on http://alestic.com

ec2-run-instances --key <KEYPAIR> ami-1515f67c

Make a note of the instance id (e.g., i-6fceba06). Watch the status using a command like this (replace with your own instance id):

ec2-describe-instances <INSTANCEID>

Repeat the describe instances command until it shows that the instance is “running” and make a note of the external hostname (e.g., ec2-75-101-179-94.compute-1.amazonaws.com).

Connect to the instance using the external hostname you noted.

remotehost=<HOSTNAME>
ssh -i <KEYPAIR>.pem ubuntu@$remotehost

OpenVPN Server

Upgrade the EC2 instance and install the necessary OpenVPN software:

sudo apt-get update &&
sudo apt-get upgrade -y &&
sudo apt-get install -y openvpn
sudo modprobe iptable_nat
echo 1 | sudo tee /proc/sys/net/ipv4/ip_forward
sudo iptables -t nat -A POSTROUTING -s 10.4.0.1/2 -o eth0 -j MASQUERADE

Generate a secret key for secure communication with OpenVPN:

sudo openvpn --genkey --secret ovpn.key

Start the OpenVPN server on the EC2 instance. We are (ab)using port 80 because most closed networks will allow traffic to this port ;-)

sudo openvpn                       --proto tcp-server               --port 80                        --dev tun1                       --secret ovpn.key                --ifconfig 10.4.0.1 10.4.0.2     --daemon

OpenVPN Client

Back on the local (non-EC2) workstation, set up the software:

sudo apt-get install -y openvpn
sudo modprobe tun # if necessary
sudo iptables -I OUTPUT -o tun+ -j ACCEPT
sudo iptables -I INPUT -i tun+ -j ACCEPT

Download the secret key from the EC2 instance:

ssh -i <KEYPAIR>.pem ubuntu@$remotehost 'sudo cat ovpn.key' > ovpn.key
chmod 600 ovpn.key

Start the OpenVPN client:

sudo openvpn                      --proto tcp-client              --remote $remotehost            --port 80                       --dev tun1                      --secret ovpn.key               --redirect-gateway def1         --ifconfig 10.4.0.2 10.4.0.1    --daemon

Edit /etc/resolv.conf and set it so that DNS is resolved by the EC2 name server:

sudo mv /etc/resolv.conf /etc/resolv.conf.save
echo "nameserver 172.16.0.23" | sudo tee /etc/resolv.conf

You should now be able to access any Internet resource securely and without restriction.

Teardown

When you are done with this OpenVPN tunnel, remember to shut down the EC2 instance and restore the DNS configuration:

sudo killall openvpn
ec2-terminate-instances <INSTANCEID>
sudo mv /etc/resolv.conf.save /etc/resolv.conf

If you have ways to improve this approach, please leave a comment.

Disclaimer

These instructions are not intended to assist in illegal activities. If you are breaking the laws or rules of your government or college or company or ISP, then you should understand the security implications of the above steps better than I do and be willing to accept consequences of your actions.

[Update 2009-01-15: Upgrade instructions for Karmic]

13 Comments

"Ubuntu 9.10 Intrepid" should read "Ubuntu 8.10 Intrepid". Interesting article - thanks.

Mark: Fixed, thanks!

Gotta say: love your work Eric. Keep up sharing the goodness and thanks a lot.

Thanks for the howto. I found this really easy to set up.

I'd like to use nonsymmetric keys and use the server with multiple computers. I'm finding the openvpn documentation hard to get into though. Any pointers to tutorials to make those changes?

wibge: Though I have used openvpn with asymmetric keys, it is indeed a lot more work, which is why I didn't choose that approach for a quick tutorial like this. Here are a couple resources which might help in your quest:

- http://openvpn.net/index.php/open-source/documentation/howto.html
- http://dev.riseup.net/grimoire/networking/openvpn/

Nice article. Could you please also provide a Windows client configuration? I have tried a little myself but cannot get it work. It will help me a lot since tons of websites are blocked here.

mitnickcbc: I've heard about Windows, but I don't have any. You'll need to seek help elsewhere for them.

One server and client, before issuing iptables commands:


sudo iptables-save >/etc/iptables.rules

HTH

Following the guide above, I tried to run openvpn on a custom 10gb esb AMI I created from 'Debian 5.0 lenny Server (ami-b1f38ce3)'.

To qualify for the EC2 free tier, I followed this guide
to convert the 15gb Debian 5.0 lenny Server AMI to a 10GB AIM.

Openvpn couldn't find any tun device when it tried to start.
I checked and there wasn't any tun device loaded so I tried to load the tun by placing 'tun' in '/etc/module' and restarting my instance. The tun device didn't load. I read that ec2 doesn't allow kernel modules to be loaded.

I'm not sure where the source of my problem is.
Weather I chosen a kernel without tun compiled when making the 10gb ami or if tun wasn't compiled in 'ami-b1f38ce3' in the 1st place.

What can I do now? Am I still able to enable a tun device for openvpn to work on my current custom AMI?

techntools: The above instructions worked on Ubuntu. I haven't tried Debian.

Combining the above with the info in the link below

http://www.adamsinfo.com/quick-linux-and-windows-openvpn-howto-and-tutorial-including-vpn-routing/

gave me a solution to VPN out on Ubuntu and MS-Windows 7 clients via an OpenVPN server running on Ubuntu on hosted cloud service.

The key element for me was the routing on MS-Windows.

I quote:

On your windows client, you’ll now need to change your default gateway:

Use route print and find out your current default gateway, then, assuming your current local default gateway is: 192.168.1.1 and server’s IP address is XX.XXX.124.95, issue the following commands:

route DELETE 0.0.0.0

route ADD XX.XXX.124.95 MASK 255.255.255.255 192.168.1.1

route ADD 0.0.0.0 MASK 0.0.0.0 10.4.0.1

The first ADD command is used to tell your client how to access the ‘new default gateway’. Without specifying your real default gateway, the client machine would have no idea how to reach your VPN server.
You can specify 10.4.0.1 as your default gateway, as it is now virtually on the same LAN as your 10.4.0.2 adapter, but without the additional route to XX.XXX.124.95, your connection to the server would have to terminate and you’d lose your tun interface.

Thanks for this guide. Doesn't seem to work with Maverick Meerkat though - after following your instructions and trying to connect to the VPN from the client, I'm getting this error:

Note: Cannot ioctl TUNSETIFF tun1: Operation not permitted (errno=1)
Note: Attempting fallback to kernel 2.2 TUN/TAP interface
Cannot open TUN/TAP dev /dev/tun1: No such file or directory (errno=2)
Exiting

Ubuntu 10.10 is installed on both client and server. Looking at the output of ifconfig, there is indeed no tun device.

mwolff:

Thanks for pointing this out. I've added a task to my todo list to update this article for modern Ubuntu :)

Leave a comment

Ubuntu AMIs

Ubuntu AMIs for EC2:


More Entries

Ubuntu Developer Summit, May 2012 (Oakland)
I will be attending the Ubuntu Developer Summit (UDS) next week in Oakland, CA.  This event brings people from around…
Uploading Known ssh Host Key in EC2 user-data Script
The ssh protocol uses two different keys to keep you secure: The user ssh key is the one we normally…
Seeding Torrents with Amazon S3 and s3cmd on Ubuntu
Amazon Web Services is such a huge, complex service with so many products and features that sometimes very simple but…
CloudCamp
There are a number of CloudCamp events coming up in cities around the world. These are free events, organized around…
Use the Same Architecture (64-bit) on All EC2 Instance Types
A few hours ago, Amazon AWS announced that all EC2 instance types can now run 64-bit AMIs. Though t1.micro, m1.small,…
ec2-consistent-snapshot on GitHub and v0.43 Released
The source for ec2-conssitent-snapshot has historically been available here: ec2-consistent-snapshot on Launchpad.net using Bazaar For your convenience, it is now…
You Should Use EBS Boot Instances on Amazon EC2
EBS boot vs. instance-store If you are just getting started with Amazon EC2, then use EBS boot instances and stop…
Retrieve Public ssh Key From EC2
A serverfault poster had a problem that I thought was a cool challenge. I had so much fun coming up…
Running EC2 Instances on a Recurring Schedule with Auto Scaling
Do you want to run short jobs on Amazon EC2 on a recurring schedule, but don’t want to pay for…
AWS Virtual MFA and the Google Authenticator for Android
Amazon just announced that the AWS MFA (multi-factor authentication) now supports virtual or software MFA devices in addition to the…
Updated EBS boot AMIs for Ubuntu 8.04 Hardy on Amazon EC2 (2011-10-06)
Canonical has released updated instance-store AMIs for Ubuntu 8.04 LTS Hardy on Amazon EC2. Read Ben Howard’s announcement on the…
New Release of Alestic Git Server
New AMIs have been released for the Alestic Git Server. Major upgrade points include: Base operating system upgraded to Ubuntu…
Using ServerFault.com for Amazon EC2 Q&A
The Amazon EC2 Forum has been around since the beginning of EC2 and has always been a place where you…
Rebooting vs. Stop/Start of Amazon EC2 Instance
When you reboot a physical computer at your desk it is very similar to shutting down the system, and booting…
Upper Limits on Number of Amazon EC2 Instances by Region
[Update: As predicted, these numbers are already out of date and Amazon has added more public IP address ranges for…
Unavailable Availability Zones on Amazon EC2
I’m taking a class about using Chef with EC2 by Florian Drescher today and Florian mentioned that he noticed one…
Desktop AMI login security with NX
Update 2011-08-04: Amazon Security did more research and investigated the desktop AMIs. They have confirmed that their software incorrectly flagged…
Updated EBS boot AMIs for Ubuntu 8.04 Hardy on Amazon EC2
For folks still using the old, reliable Ubuntu 8.04 LTS Hardy from 2008, Canonical has released updated AMIs for use…
Creating Public AMIs Securely for EC2
Amazon published a tutorial about best practices in creating public AMIs for use on EC2 last week: How To Share…
Canonical Releases Ubuntu 11.04 Natty for Amazon EC2
As steady as clockwork, Ubuntu 11.04 Natty is released on the day scheduled at least eleven months ago; and thanks…