Monday, February 23, 2015

Using Android as a SOCKS server

Yeah as a server. Why? There were plenty of pages floating around the web which said how to connect your Android to a SOCKS server, but very few told you actually how to use it as the server. So this is what I'm going to try and explain.

First let me state why I found this neccessary. My phone carrier disables the use of mobile data through a USB tethering setup except in the event of the user purchasing a "mobile data tethering plan". As a result, when I connect my device to a laptop and enable tethering, the laptop does not receive a network connection. So I set off to try and get around this limitation by tunnelling through a SOCKS server that I would host on my phone, therefore making it appear that the Android was making all of the web requests: no tethering plan needed.

By setting up a SOCKS server, you can use your phone's mobile data to power the network of your other systems, including a laptop which otherwise does not have any other connection to the network.

Essentially the setup is as follows:

Tether your Android by USB to your system.
Get an IP address on your system.
Get an Android SSH server application
Start an SSH server on the Android device
Start a SOCKS server off of the SSH server
Connect from your system to the SOCKS server and bind to a local port
Proxy any connections (browser, mail) through the local bound port.

So here we go.

Tether your Android. Google around if you need to know how to do this, as explaining it will be out of the scope of this post.

Once your Android is tethered, get an IP address for your system, either statically or dynamically. I used a simple dynamic address for this, using the Linux dhcpcd program. On other operating systems (and Linux distributions) the command will vary, so dig around.


NOTE

This post is written only with functionality in mind. It is up to you to figure out the necessary security measures for your specific setup. Some of the steps here may be insecure, or leave you visible to eyes on the Web. I will not be held responsible for your failure to adhere to good security practices.

Now the good stuff.

First you are going to need an SSH Server for Android. In this case, I used Servers Ultimate Pro which was free on the Amazon app store a few months back. You first need to open Servers Ultimate. Keep in mind this was written for version 6.3.10, though it can be applied I'm sure to other versions as long as they support/supported SSH and SOCKS servers. For this, you'll also need the free Server Pack E, which adds support for SSH.

Once inside the application, navigate to the "Servers" tab. Click the Action icon "Plus" to add a new server and select an SSH server. Name the server, give it a (random) port, and make sure that atleast the "Enable Shell" option is selected under the Protocol section in the "Specific" tab. You can also take the opportunity to add your own public key, if you know what that is, although the how to for doing that is out of the scope of this post. Click the save icon and you should be good to go.

Add a second server and make it a SOCKS server. Give it a (different random) port, and that's fine as well. Click "Save" and be on your way.

Click the Play button in the top rght of the "Servers" screen to start your SSH and SOCKS servers. Woohoo!

Now at this point you should have and know the following four things:

An IP Address for you Android Device which is tethered to your system, android-ip
An IP Address for your system needing Internet, system-ip
The port for the SOCKS server, lets call it socks-port
The port for the SSH server in general, lets call that ssh-port

Add one more thing to your list, which is the local port to bind to. Though the default is 1080, for the purposes of explination I'll be using a different port, called bind-port.

Now the part which may unnerve you if you aren't too command line familiar (although why would you be looking for SOCKS server setup instructions). You will ssh from your system to your Android. The command will look something like this:

    ssh -L <bind-port>:<android-ip>:<socks-port> -p <ssh-port> <android-ip>

    ssh -L 1080:192.168.0.111:1080 -p 22 192.168.0.111

alternatively you can use the dynamic port option instead:

    ssh -D <bind-port> -p <ssh-port> <android-ip>

    ssh -D 1080 -p 22 192.168.0.111

This will do the following:

Request an SSH connection to android-ip on port ssh-port. Once connected it will forward the local port bind-port on your system to the socks-port on the Android system. Now you've hacked together a SOCKS proxy for your system from an Android server.

At this point, your proxy is located at localhost:<bind-port>. Some programs have SOCKS proxy support built in, in which case you're good to go. For those that don't you may be able to pass the proxy as an argument to different programs for them to be able to use it, for example:

    google-chrome --proxy-server=socks5://localhost:1080
    curl --socks5-hostname localhost:1080


Finally you can create a configuration file in $HOME/.ssh/config which can contain something like the following:

    Host <proxy-name>
    Hostname <android-ip>
    Port <ssh-port>
    DynamicForward localhost:<bind-port>

and then you can connect to your proxy using

    ssh <proxy-name>


Using this, I was able to pull system updates, git updates (and push), emails from a Thunderbird client, and browse the web on Google Chrome and Firefox, without any WiFi connection or Ethernet connection.

And all without a mobile data tethering plan too.


NOTE

This post is written only with functionality in mind. It is up to you to figure out the necessary security measures for your specific setup. Some of the steps here may be insecure, or leave you visible to eyes on the Web. I will not be held responsible for your failure to adhere to good security practices.

Your mileage may vary.

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: http://pyamsoft-official.blogspot.com/

Check out my Android Applications
Check out my Github Repositories
Check out my AUR packages

Follow my FaceBook Page
Follow my Google+ Page
=========================