{"id":64,"date":"2009-08-27T21:31:51","date_gmt":"2009-08-28T02:31:51","guid":{"rendered":"http:\/\/blog.alanporter.com\/2009-08-27\/iphone-vpn"},"modified":"2009-08-27T21:31:51","modified_gmt":"2009-08-28T02:31:51","slug":"iphone-vpn","status":"publish","type":"post","link":"https:\/\/alanporter.com\/blog\/2009\/08\/27\/iphone-vpn\/","title":{"rendered":"iPhone VPN"},"content":{"rendered":"<p>One of the coolest features of the iPhone is the way it uses the best data network that it can find.  If you&#8217;re at home or at work, or even at a coffee shop, it will use the local wifi network.  But if you&#8217;re out of range of any suitable wifi networks, it will use AT&#038;T&#8217;s &#8220;3G&#8221; (UMTS) network.  And if it can&#8217;t find a UMTS network, it&#8217;ll fall back to EDGE.  Phone companies call this hybrid approach &#8220;ABC&#8221;, or &#8220;always best connection&#8221;.<\/p>\n<p>Now that I have an internet device in my pocket, I find myself using public (or otherwise open) wifi connections quite a bit.  And this carries with it some unintended consequences.  That is&#8230; everything I type and everything I read is transmitted in the clear, unencrypted.<\/p>\n<p>I try to make a habit of encrypting my data traffic whenever possible.  My mail server is set up to only allow SSL connections.  So no matter where I check my mail from, I am forced to use an encrypted connection.  Similarly, banks and commerce web sites usually force you to switch to HTTPS before you start entering information.  But there are a lot of applications on the iPhone that do not use encryption at all.<\/p>\n<p>You might ask yourself why bother to encrypt your Twitter connection, since what you type is going to be blasted out to the world anyway.  But the point is&#8230;<\/p>\n<blockquote><p>If you encrypt <em>everything<\/em>, then nothing is left to chance.<\/p><\/blockquote>\n<p>So I decided to explore a VPN option on the iPhone.  It supports three flavors of VPN: <strong>L2TP<\/strong>, <strong>PPTP<\/strong> and <strong>IPSec<\/strong>.  I was disappointed (but not surprised) that &#8220;<strong>openvpn<\/strong>&#8221; was not an option, since I already use this excellent open source SSL-based VPN package.<\/p>\n<p>So I decided to give PPTP a try.<\/p>\n<p><strong>Setting up the PPTP server<\/strong><\/p>\n<p>On my Ubuntu 8.04 LTS server, I installed a PPTP server called, appropriately enough, &#8220;<strong>pptpd<\/strong>&#8220;.  Configuration was very easy.  Most of the setup was done for me after I did the standard <code>apt-get install pptpd<\/code>.  I simply needed to pick a private subnet that would be used for my VPN clients, and an IP address in that subnet to use for the server.  I chose the <code>172.16.4.0\/16<\/code> subnet and <code>172.16.4.1<\/code> for the server (these addresses are part of a <em>private network address space<\/em>, defined by <a href=\"http:\/\/www.faqs.org\/rfcs\/rfc1918.html\">RFC 1918<\/a>, just like 192.168.x.x and 10.x.x.x addresses).<\/p>\n<p>My <code>\/etc\/pptp.conf<\/code> configuration file for the pptp daemon looks like this:<\/p>\n<pre>\noption \/etc\/ppp\/pptpd-options\nlogwtmp\nlocalip 172.16.4.1\nremoteip 172.16.4.2-250<\/pre>\n<p>I also needed to tell the daemon to give out some DNS addresses when a client connects, so in the <code>\/etc\/ppp\/pptpd-options<\/code> file, I added the two &#8220;ms-dns&#8221; lines below:<\/p>\n<pre>\nname pptpd\nrefuse-pap\nrefuse-chap\nrefuse-mschap\nrequire-mschap-v2\nrequire-mppe-128\nms-dns 208.67.222.222  # resolver1.opendns.com\nms-dns 208.67.220.220  # resolver2.opendns.com\nproxyarp\nnodefaultroute\nlock\nnobsdcomp<\/pre>\n<p>Finally, I needed to add an entry into the <code>\/etc\/ppp\/chap-secrets<\/code> file that would contain my password.  Mine looks like this:<\/p>\n<pre>\nalan pptpd MyHardToGuessPassword *<\/pre>\n<p>At this point, the PPTP server was completely configured, so I restarted it with <code>service pptpd restart<\/code>.<\/p>\n<p><strong>Setting up the iPhone<\/strong><\/p>\n<p>On the iPhone, I needed to set up a VPN client.  This is very easy.  On the settings screen, go to general \/ network \/ VPN and &#8220;Add VPN Configuration&#8230;&#8221;.   Then just fill in the blanks.<\/p>\n<ul>\n<li>choose &#8220;PPTP&#8221;<\/li>\n<li> enter a description<\/li>\n<li> your server&#8217;s IP address<\/li>\n<li> the username (from above)<\/li>\n<li> RSA SecurID=OFF<\/li>\n<li> the password (from above)<\/li>\n<li> encryption level = Auto<\/li>\n<li> &#8220;Send All Traffic&#8221; = ON<\/li>\n<li> Proxy = OFF<\/li>\n<\/ul>\n<p>Click on &#8220;Save&#8221; and you will see a switch in the network tab and also in the main settings tab to turn the VPN on and off.<\/p>\n<p>For now, I am leaving it off unless I am on a public network.  I am not sure, but I think that keeping the VPN alive might use a lot of battery.  So I do not use it unless I need it.<\/p>\n<p><strong>Networking<\/strong><\/p>\n<p>For me to get this VPN on the internet, I had to do two more things: punch a hole in my firewall for the PPTP traffic, and forward traffic from my VPN out to the rest of the world.<\/p>\n<p>For my server, both of these tasks were handed by the same tool: <strong>shorewall<\/strong>.<\/p>\n<p>I added a &#8220;masquerade&#8221; rule to <code>\/etc\/shorewall\/masq<\/code> to NAT all of the traffic from 172.16.4.x out through my main network interface.<\/p>\n<pre>\neth0         172.16.4.0\/24    # OpenVPN and PPTP<\/pre>\n<p>And then I added two rules to <code>\/etc\/shorewall\/rules<\/code> to allow the PPTP traffic in.<\/p>\n<pre>\nACCEPT  net  fw   tcp  1723  # PPTP\nACCEPT  net  fw   gre        # PPTP<\/pre>\n<p>When shorewall starts, it will generate the <strong>iptables<\/strong> rules that are used by the kernel to filter packets.  If you&#8217;re using hand-written iptables rules, then you will need some rules that look something like this:<\/p>\n<pre>\n# accept \"gre\" protocol traffic (PPTP tunnel traffic)\niptables -A INPUT -p gre -j ACCEPT\niptables -A OUTPUT -p gre -j ACCEPT\n# accept PPTP control traffic to TCP port 1723\n# (my server IP is 11.22.33.44)\niptables -A INPUT -p tcp --sport 1723 -s 11.22.33.44 -j ACCEPT\niptables -A OUTPUT -p tcp --dport 1723 -d 11.22.33.44 -j ACCEPT\n# masquerade\/NAT internet traffic out of interface eth0\niptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\n# turn on packet forwarding\necho \"1\" > \/proc\/sys\/net\/ipv4\/ip_forward<\/pre>\n<p><strong>Conclusion<\/strong><\/p>\n<p>Now, when I am in a coffee shop, I can turn on the VPN easily by flipping the switch in the iPhone&#8217;s main settings screen.  It will make a TCP connection to my server, negotiate a few things, and then send all further network traffic through an encrypted tunnel directly to my server, which relays it out to the internet.<\/p>\n<p>You can test this by going to a web site like <a href=\"http:\/\/www.whatismyip.com\/\">http:\/\/www.whatismyip.com\/<\/a>.  If the VPN is working, it will show your server&#8217;s IP address. If not, it&#8217;ll show the coffee shop&#8217;s IP address.<\/p>\n<p>Once I am using the VPN, anyone in the coffee shop who happens to be sniffing traffic on the wireless network will only see a single connection from my iPhone to my server, but the contents of this connection will be scrambled.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the coolest features of the iPhone is the way it uses the best data network that it can find. If you&#8217;re at home or at work, or even at a coffee shop, it will use the local wifi network. But if you&#8217;re out of range of any suitable wifi networks, it will use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6,12,15],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-software","category-iphone","category-security","category-tips-tricks","count-0","even alt","author-alan","last"],"_links":{"self":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/comments?post=64"}],"version-history":[{"count":0,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/posts\/64\/revisions"}],"wp:attachment":[{"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/media?parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/categories?post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alanporter.com\/blog\/wp-json\/wp\/v2\/tags?post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}