RaspberryPi/Old Reverse SSH Stunnel
From charlesreid1
Stunnel on Raspberry Pi
Nominally, stunnel provides SSL encryption and decryption, which provides services not capable of SSL to communicate securely using SSL. (Example: if a mail server listens for unencrypted mail traffic on port 25, and clients send encrypted mail traffic on port 465, stunnel listens on port 465, passes traffic through stunnel to decrypt it, and then passes it to local port 25.
But this can also be used to wrap arbitrary traffic in SSL - and since one type of connection that's allowed through a firewall is an SSL connection, this means we can get our connection past the IDS. stunnel hides that TCP client-server communication stream in an SSL encryption wrapper.
Installing
First, install stunnel
apt-get install -y stunnel4
Edit Configuration File
On the Raspberry Pi, edit the stunnel configuration file:
$ vim /etc/stunnel/stunnel.conf
Add the following information, which will point stunnel to your private key stunnel.pem (we'll cover how to create this next).
More information on this process is here: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-ssl-tunnel-using-stunnel-on-ubuntu
/etc/stunnel/stunnel.conf
client = no [squid] accept = 8888 connect = 127.0.0.1:3128 cert = /etc/stunnel/stunnel.pem
Basically, this accepts incoming connections on port 8888. This then reroutes those incoming connections to 127.0.0.1 (localhost) and port 3128, which is where squid runs by default (localhost:3128). So, we're forwarding everything from port 8888 to port 3128.
Note that it is pointing to a certificate file in /etc/stunnel/stunnel.pem, so our next step will be to create this certificate file.
Generate Private Keys/Certificates for SSL
Now you need to generate private keys, so that stunnel has private keys to use when encrypting using SSL.
First, go to the directory where stunnel keeps all of its files:
$ cd /etc/stunnel/
Generate Private Key
Use the openssl library to generate a 2048-bit private RSA key:
$ openssl genrsa -out key.pem 2048 Generating RSA private key, 2048 bit long modulus .....+++ ..+++ e is 65537 (0x10001)
Generate a Self-Signed Certificate
To do SSL, an stunnel server must have an SSL certificate, which requires a private key and a signature. We already generated a private key, so now we generate a certificate, and use our own key to sign it. Do this by running the following:
$ openssl req -new -x509 -key key.pem -out cert.pem -days 365 You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]:. State or Province Name (full name) [Some-State]:. Locality Name (eg, city) []:. Organization Name (eg, company) [Internet Widgits Pty Ltd]:. Organizational Unit Name (eg, section) []:. Common Name (e.g. server FQDN or YOUR name) []: 10.0.0.243 Email Address []:.
Key and Certificate
Now you have your private key in key.pem and your server's certificate in cert.pem.
$ ls -lh total 16K -rw-r--r-- 1 root root 615 Apr 22 2013 README -rw-r--r-- 1 root root 1.7K Aug 4 18:06 key.pem -rw-r--r-- 1 root root 964 Aug 4 18:08 cert.pem
put those both into the certificate file that we pointed to above, with our Squid proxy:
$ cat key.pem cert.pem >> /etc/stunnel/stunnel.pem
Configure stunnel to Start on Boot
Now configure stunnel by editing the default stunnel file:
$ vim /etc/default/stunnel4
change enable = 0 to enable = 1
Restart stunnel
you can restart the stunnel service now,
$ /etc/init.d/stunnel4 restart
Add Configuration
Now open /etc/stunnel/stunnel.conf and add the following configuration:
sslVersion = all options = NO_SSLv2 cert = /etc/stunnel/server.pem pid = /var/run/stunnel.pid output = /var/log/stunnel [openvpn] client = no accept = 993 connect = 34567
Open Firewall
Now add a firewall a firewall setting on the Raspbery Pi by creating a file firewall.sh:
iptables -A INPUT -p tcp -dport 993 -j ACCEPT
The next step is to restart the stunnel services:
/etc/init.d/stunnel4 restart
Install Squid
Now the final step is to install Squid proxy on the Raspberry Pi:
apt-get install -y squid3