SSH tunnel to blocked sites while keeping your routes

tl;dr If you can ssh out you can bypass most any network restrictions.

Overzealous IT administrators. Restrictive network policies. Overreaching governmental meddling. What's a humble interweb denizen to do? Short of packing it all in and turning it all off, I say get to know the secure shell, more commonly referred to as ssh. If you've heard of ssh you probably know it can do more than just connect you via an encrypted connection to a remote server. You probably also know that it can forward a local address and port to a remote address and port through your ssh connection using the -L flag.

For example, when I'm on a network that blocks IRC, I use the following command to circumvent it:

ssh -L 6667:irc.freenode.net:6667 user@host.com

Then I connect my IRC client to localhost:6667 which is then forwarded via my ssh connection to irc.freenode.net:6667. Easy peasy. This works because I can control my IRC client and tell it to talk to localhost:6667 instead of irc.freenode.net:6667. What if you have a prebuilt application that needs to dial into a specific website or IP address? You can't control that connection so you intercept it. You can emulate the service IP address locally. Basically you find out the IP address of the service you want to talk to and you tell your computer that it is that IP address by binding it to the local loopback adapter. You know that nifty 127.0.0.1 address? That's your local loopback. The trick is to realize that you aren't limited to that one only. You can add others like so:

sudo ifconfig lo0 add 1.2.3.4

This tells Mac OSX to add address 1.2.3.4 to the local loopback adapter as if your computer actually was that address. This allows your browser and any other application to operate normally, maintaining all network/vpn applied routes and proxies. The only difference is that this one address will now be smuggled out through the gaping hole your ssh connection made in their network. You need to do this before you establish the ssh connection or else it will error out as ssh will not recognize the address as a local address. Once you've established your local loopback, you can set up your ssh session like so:

ssh -L 1.2.3.4:5000:1.2.3.4:5000 -L 1.2.3.4:5001:1.2.3.4:5001 user@host.com

In this case I'm calling on two specific ports to pass through, 5000 and 5001. 

Here's something I pieced together from a few different posts around the web.