Thursday, January 22, 2015

Intercept all HTTP + SSL Android traffic and bypass SSL Pinning

How to intercept all Android HTTP / HTTPS network traffic on Windows and bypass SSL Pinning


  1.  Install ADB / Android SDK or use the AppUse VM
  2. Root the android device
  3. Install Android Cydia Substrate
  4. adb install com.saurik.substrate.apk
  5. Install Android SSL Trust Killer
  6. adb install Android-SSL-TrustKiller.apk
  7. Export Burp Root CA Certificate
  8. Push Burp Cert to the sdcard
  9. adb push PortSwiggerCA.cer /sdcard
  10. Install Burp Cert in the Android Trust Store
  11. Settings > Security > Install from device storage
  12. On Windows, create a Wireless hotspot sharing your Internet / external connection
    1. Create the hotspot:
    2. ​netsh wlan set hostednetwork mode=allow ssid=MyHotspot key=MyPassword keyUsage=persistent
    3. To start the hotspot
    4. netsh wlan start hostednetwork
    5. Or to stop the hotspot:
    6. netsh wlan stop hostednetwork
  13. Enabled Internet Connection Sharing with your external connection:
  14. Right click your connection's NIC, "Sharing" tab, check the box, select "MyHotspot". The external NIC icon should say "shared".
  15. Connect the Android to the wifi Hotspot using the key configured previously.
  16. Optional: If you prefer to use a static network configuration as opposed to DHCP, go to the Wifi connection advanced settings and look for the IP address attributed. Switch to a static IP and set this IP manually. The gateway should be the IP address of the Windows "MyHotspot" interface. The DNS Server should be your ISP's / corporate network one. Reconnect the Wifi with the new network configuration.
  17. Try to ping the Android device's IP address from the Windows.
  18. Run Burp and make it listen on the Hotspot's interface IP address.
  19. You can configure this proxy in the Wireless connection advanced settings, but that would take effect only for proxy-aware apps such as the web browser. Preferably, and since your device is rooted, use an app such as ProxyDroid to make all the apps go through the proxy transparently. You should have Play Store working as well.
  20. Configure your Burp's upstream proxies rules if needed.
  21. You should be able to intercept all HTTP/HTTPS traffic. If not, well, go back to step 1.

Thursday, January 8, 2015

Setup a Bittorrent download/seed box on Android

I used to run XMBC (now named Kodi) on a Raspberry Pi, which worked great, but it lacked a bit a power. Also, the Linux versions of Kodi are not usually updated as much as the Windows or Android versions.

Definitely being an Android person, I decided to get myself a nice Android TV box for christmas. After having rooted it, installed all the media-center stuff and connected a nice big external USB hard drive for media storage, came the part of looking for a way to install a Bittorrent client.

As it turns out there are quite a lot of choices from the Google Play Store. But nothing matched what I needed:
  • A bittorrent client that runs as a daemon all the time ;
  • Must support scheduling to download only at night ;
  • Must have a directory watching feature ;
  • Must be manageable remotely via Web UI.

I tried different ones with no luck. After some research I came across an acceptable solution: Installing a bootstrapped version of Ubuntu Precise on the top of the Android OS. There is an elegant solution with video tutorials for that here.

The installation is pretty straightforward, you should be done in less than 15 minutes. Just watch the video tutorials first.

I chose a 2GB disk image with a Precise installation. When it finishes installing the base packages, type the following commands from your Android prompt (You can use ConnectBot or Terminal Emulator for this).

- Jump into the Ubuntu chrooted environment:
deb

- Update the packages list and upgrade them to the latest version
apt-get update -y && apt-get upgrade -y

- Install Andromize (A package designed to furthermore adapt your installation to the Android environment)
apt-get install andromize

- Clean the APT cache
apt-get clean

- Create a new user
adduser jeremy

- Create a password
passwd jeremy

- Add the user to the sudo group
adduser jeremy sudo

- Install SSH Server
apt-get install openssh-server

You may optionally want to change the SSH listen port in /etc/ssh/sshd_config.

At this point your base system is set up. You can logout your Ubuntu shell to be dropped back on the Android shell and see the options to the "deb" command with "deb h":

root@NEO-X8H-PLUS:/sdcard # deb h
Script to integrate Debian binaries on an Android phone

Without options: start a root bash shell
bootdeb ?: help, display this help text
bootdeb u: umount, remove symlinks and unmount Debian disk
bootdeb k: kill, kill (-KILL) programs, then unmount Debian disk
bootdeb r: reboot, mount Debian readonly and reboot (last resort)
bootdeb c: clean, clean symlinks to Android root from Debian disk
bootdeb s: sshd, start the openssh server
bootdeb S: sshd-stop, stop the openssh server
bootdeb x: xrdp, start the xrdp server
bootdeb X: xrdp-stop, stop the xrdp server

To umount the Ubuntu image, use "deb u". For now, you can just run the SSH server using "deb s" and then connect to it, either locally with ConnectBot or remotely with any SSH client.

Now for the Bittorrent installation part. I chose the Deluge client as it as activiely maintained and developed, and it matches all the criteria I needed.

- Install the following package to get "add-apt-repository" on your Ubuntu installation
sudo apt-get install python-software-properties

Note: In some situations, you may need to install the package "software-properties-common" instead.

- Add the Deluge PPA to your installation, and install the Deluge daemon and WebUI Server (refer to this page for more details):
sudo add-apt-repository ppa:deluge-team/ppa
sudo apt-get update
sudo apt-get install deluged deluge-web

Run the daemon and the WebUI Server:
deluged
deluge-web &

All done! you should now be able to point your browser to http://android.ip.address:8112/. The default password to the WebUI is "deluge". Then take some time to adjust the configuration such as the download paths, bandwidth limits and schedules.

For a more seamless experience, you can make Kodi periodically scan your download folder to have movies added to the library automatically!

Note: When you run certain commands from within your Linux installation, you may see errors such as "passwd: u:r:init:s0 is not authorized to change the password of [...]". In this case, you need to temporarily disable SELinux back on the Android shell by using the command below:
setenforce 0

Friday, August 1, 2014

XOR a string or file with a key in Python

Here is an elegant example about how to XOR a string using a key with Python's itertools cycle:

>>> from itertools import cycle
>>> 
>>> def do_xor(key, str):
...     str = str.replace(' ', '').decode('hex')
...     key = ''.join(key.split()[::-1]).decode('hex')
...     
...     return ''.join([chr(ord(a) ^ ord(b)) for a,b in zip(str, cycle(key))])
... 
>>> message = "8E D8 51 66 8B D9 03 67 8D FE 0A 3E E1 97 15 13"
>>> key = "13 37 ba be"
>>> 
>>> print do_xor(key, message)
0bfu5c4t3D=-_-"
>>>

If your string isn't in a hexadecimal representation, just remove the "decode('hex')" bits.

Following up on the example above, here is a script that allows quick and easy file encryption. Just chose a key that you will remember and this script will help easily protecting sensitive information on disk:

import os, sys
from itertools import cycle

if len(sys.argv) != 3:
 print "usage: %s <filename> <key>" % sys.argv[0]
 sys.exit()

if os.path.exists(sys.argv[1]):
 data = open(sys.argv[1]).read()
 xored = [chr(ord(a) ^ ord(b)) for a,b in zip(data, cycle(sys.argv[2]))]
 
 open(sys.argv[1], 'w').write(''.join(xored))

 print "file xored"
 
else:
 print "%s: file not found" % sys.argv[1]

Example usage:

user@host:~$ cat /tmp/a
this is a simple text file.
user@host:~$ python xorfile.py /tmp/a Sup3rS3cr3t
file xored
user@host:~$ cat /tmp/a
<... some unprintable characters ...>
user@host:~$ python xorfile.py /tmp/a Sup3rS3cr3t
file xored
user@host:~$ cat /tmp/a
this is a simple text file.
user@host:~$ 

Monday, July 21, 2014

NTP behind a HTTP proxy?

As NTP uses UDP port 123, of course setting the date via NTP (ntpdate) behind a proxy isn't possible without doing some tunneling/encapsulation wizardry. There is, however, a very nice solution using the time api. As it works over a Web server, the proxy problem is immediately resolved.

For example, to set your date/time according to your timezone:

# export http_proxy=http://proxy.mycompany.com:8080/
# date -s "$(wget -q -O - http://www.timeapi.org/gmt)"

Easy, right!

Tuesday, July 8, 2014

Poor man's SSL-VPN with socat

Another cool feature of socat is the ability to create TUN/TAP network interfaces. It can allow to quickly merge two remote networks. Of course, you can use any address type that socat supports, but here we're going to setup an OpenSSL mutually-trusted VPN.

Imagine the following setup: You want to make your home network remotely accessible from your work's computer, securely over a trusted [as long as you keep your private keys safe] SSL channel.

First we'll generate client and server private keys and certificates:

#!/bin/bash
function generate_cert()
{
 openssl genrsa -out $1.key 1024
 openssl req -new -key $1.key -x509 -days 365 -out $1.crt
 cat $1.key $1.crt > $1.pem
 chmod 600 $1.key $1.pem
}

# Generate server certificate
generate_cert socat_server

# Generate client certificate
generate_cert socat_client

  • Answer a few questions to create the certificates.
  • Copy the socat_server.pem and socat_client.crt files to the server-side machine at home.
  • Copy the socat_client.pem and socat_server.crt files to the client workstation.

1. Server-side configuration


On the server-side (home), run socat as an OpenSSL Server and make it create the TUN/TAP interface.

Note: If the TUN interface address that you choose to isn't part of your home network's subnet, you may want to enable IP forwarding on the server machine to be able to access other resources on your home network. For quick and dirty VPN, you can just choose to use an arbitrary IP address within your home subnet and you will be able to access resources on that subnet.

socat -d -d OPENSSL-LISTEN:9111,bind=192.168.0.254,cert=socat_server.pem,cafile=socat_client.crt,reuseaddr,fork TUN:10.1.1.1/24,up

You might want to create a port forwarding rule on your home router to map an external port to the socat server's listening port.

2. Client-side configuration


On the client-side (work) bring up the socat TUN interface by connecting to the server: (5.6.7.8 is the home ISP's public IP address).

If you can connect directly:
socat -d -d OPENSSL:5.6.7.8:9111,cert=socat_client.pem,cafile=socat_server.crt TUN:10.1.1.10/24,iff-up=1

if you need to go through a proxy, an additional socat will be required:

a. Create the TCP forwarder
socat TCP4-LISTEN:65432,bind=127.0.0.1,reuseaddr,fork PROXY:proxy.mycompany.com:5.6.7.8:9111,proxyport=3128 &

b. Create the OpenSSL tunnel over the proxy tunnel:
socat -d -d OPENSSL:127.0.0.1:65432,cert=socat_client.pem,cafile=socat_server.crt TUN:10.1.1.10/24,iff-up=1


You should now be able to access your home computers from your client workstation. If required, you can add routes on your workstation to access any remote network through the socat VPN gateway (10.1.1.1).


Wednesday, July 2, 2014

Using iptables + socat to tunnel outbound connections through proxy

EDIT: Here is the tool I wrote to facilitate creation of transparent tunnels over http proxies - on my GitHub account.

I've known about socat for a while, but never really got into using it until recently. Its possibilities are endless and it's always reliable. I'm thinking about writing a wrapper around it.

For now, imagine you've got the following setup: A thick/thin app, script, program, whatever with no native proxy support, connecting to an external host. You may want to tunnel the connections through your company's proxy, or maybe perform man-in-the-middle on the outbound streams. (side note: Contextis' CANAPE is excellent for that matter, but more complex to setup, and Windows only).

Obviously, you could edit your hosts file, or even possibly modify the source code to make it connect to wherever you want. But there are some situations where you can't / don't want / are too lazy to do that.

You can use an iptables rule to redirect the outbound connections to that host to your loopback interface, then have socat listening on a local port and tunnel sessions through the proxy. It can also be used to setup a local man-in-the-middle scenario, where you want to be able to put an intercepting proxy in the middle of the communication.

The network flow diagram is as follows:




You can add the "reuseaddr" command-line parameter to the socat listening local socket parameter to allow it to rebind to a previously open port. Also add the "fork" parameter if needed to prevent it from exiting after the first tunneled TCP session is finished. You don't need to enable IP Forwarding on your box.

For example, if I want to tunnel all direct connections made to google.com:443. First, you need to create the Iptables rule accordingly. As root:

iptables -t nat -A OUTPUT -p tcp -d google.com --dport 443 -j DNAT --to-destination 127.0.0.1:443

Then, you must create the socat tunnel (reuseaddr and fork are optional). As root:

socat TCP4-LISTEN:443,bind=127.0.0.1,reuseaddr,fork PROXY:proxy.mycompany.com:google.com:443,proxyport=3128

When your work is finished, kill the socat instance with CTRL+C and delete the now useless iptables rule (just use -D instead of -A):

iptables -t nat -D OUTPUT -p tcp -d google.com --dport 443 -j DNAT --to-destination 127.0.0.1:443

And because I like shell scripts, here goes:

#!/bin/bash
if [ -z $2 ]; then
 echo "usage: $0 <dest_host> <dest_port>"
 exit
fi

if [ $EUID -ne 0 ]; then
 echo "error: must be run as root"
 exit
fi


PROXYHOST=192.168.100.1
PROXYPORT=3128

# Forward all outgoing traffic directed to ext_host:ext_port to the loopback interface
echo Creating rule...
iptables -t nat -A OUTPUT -p tcp -d $1 --dport $2 -j DNAT --to-destination 127.0.0.1:$2

# use socat to tunnel connections to the local port through the proxy
echo "127.0.0.1:$2 <--> $PROXYHOST:$PROXYPORT <--> $1:$2 (stop with ctrl+c)"
socat TCP4-LISTEN:$2,bind=127.0.0.1,reuseaddr,fork PROXY:$PROXYHOST:$1:$2,proxyport=$PROXYPORT

# remove created iptables rule
echo Removing rule...
iptables -t nat -D OUTPUT -p tcp -d $1 --dport $2 -j DNAT --to-destination 127.0.0.1:$2

Enjoy ;)

Thursday, June 26, 2014

How to use Socat to connect to an SSL service over a HTTP proxy

SOCAT is in my opinion one of the best networking / relaying tools out there. Within my corporate network, I need to go through a HTTP proxy to reach the outside. Today I needed to connect to a Freenode IRC channel, using of course the IRC client of choice, IRSSI. Unfortunately, whilst IRSSI supports HTTP proxies, it fails at establishing an SSL connection when using one.

At this point, there are two possible solutions:

  • Connect using a clear-text IRC session over the proxy (which was out of consideration in my case)
  • Relay the server's SSL port to localhost over the proxy (yay)

Socat turned out to be the most easy to setup and worked flawlessly. Here is the setup for this particular example:


All connections to the local port 6666 would then be tunneled through the proxy, and forwarded to the destination server. So if the endpoint service is SSL-enabled, connect to your local port over SSL and the session gets encrypted end to end.

Here is a small Bash script for that purpose.
#!/bin/bash
if [ -z $3 ]; then
 echo "usage: $0 <listenport> <desthost> <destport>"
 exit
fi

LOCALPORT=$1
PROXYHOST=10.0.0.1
PROXYPORT=3128
DESTHOST=$2
DESTPORT=$3

socat TCP4-LISTEN:$LOCALPORT,bind=127.0.0.1 PROXY:$PROXYHOST:$DESTHOST:$DESTPORT,proxyport=$PROXYPORT &

echo SOCAT listening on 127.0.0.1:$LOCALPORT, forwarding to $DESTHOST:$DESTPORT

Note: Should your proxy require authentication, the socat command must be changed to:

socat TCP4-LISTEN:$LOCALPORT,bind=127.0.0.1 PROXY:$PROXYHOST:$DESTHOST:$DESTPORT,proxyport=$PROXYPORT,proxyauth=$PROXYUSER:$PROXYPASS