Skip to main content.

Using stunnel and SSH to bypass IDS

2013-09-04

Live demo in BSD Now Episode 001 | Originally written by TJ for bsdnow.tv | Last updated: 2014/08/20

NOTE: the author/maintainer of the tutorial(s) is no longer with the show, so the information below may be outdated or incorrect.

Here’s a scenario: say you're on an untrusted network, be it corporate, university or at a foreign hotel. You want to tunnel all your traffic through SSH to your trusted server, right? We can't have those pesky script kiddies sniffing your traffic. But what's this? You can't get out on port 22! You can't get out on that other random port you used for SSH either! They're filtering everything but ports 80 and 443. Sometimes just running SSH on port 443 will let you get past this, but other times there’s deep packet inspection in place to prevent that. Any IDS will be able to easily detect SSH on any port you run it on, so we’ll have to find a way to hide it in plain sight. Enter stunnel. It's a simple tool that lets you encapsulate traffic of any protocol in standard SSL/TLS. Your stream of packets will look exactly like a connection to your gmail or anything else. The setup is pretty simple. You'll need stunnel installed on both your client PC and a remote server with sshd already running. Let's install stunnel on the server, make a quick config and setup your key. For FreeBSD, I'll be using ports.

# cd /usr/ports/security/stunnel
# make config-recursive install clean
# vi /usr/local/etc/stunnel/stunnel.conf

In the config, we're going to put the following:

cert=/etc/ssl/stunnel.pem
pid=/var/run/stunnel.pid
setuid = stunnel
setgid = stunnel
[ssh]
accept = your_server_IP:443
connect = 127.0.0.1:22

Now we'll generate the key:

# cd /etc/ssl
# openssl genrsa 1024 > stunnel.key
# openssl req -new -key stunnel.key -x509 -days 1000 -out stunnel.crt
# cat stunnel.crt stunnel.key > stunnel.pem
# chmod 600 stunnel.pem
# service stunnel onestart

Be sure to allow incoming connections to port 443 in your firewall. Now we move over to the client PC. Install stunnel and set up a similar configuration.

# cd /usr/ports/security/stunnel
# make config-recursive install clean
# vi /usr/local/etc/stunnel/stunnel.conf

In the client config, we put:

pid=/var/run/stunnel.pid
client=yes
setuid = stunnel
setgid = stunnel
[ssh]
accept=443
connect=your_server_IP:443

Start the service and test it out:

# service stunnel onestart
$ ssh -vp 443 youruser@localhost

At this point you should be SSHed into your remote server, but all the traffic is tunneled through SSL. Now, say you want to tunnel all your web browsing traffic through this. With stunnel running on both systems, run this on the client:

$ ssh -Cv -ND localhost:9050 localhost

And set your browser proxy settings to tunnel all traffic through a SOCKS host of localhost:9050

To verify everything is working, fire up Wireshark or tcpdump:

Done! It's recommended that you stop the stunnel service when you're not planning on using it, since all SSH connections appear to come from 127.0.0.1, and it can be annoying if someone is trying to bruteforce your login.

Latest News

New announcement

2017-05-25

We understand that Michael Dexter, Brad Davis, and George Rosamond think there should be more real news....

Two Year Anniversary

2015-08-08

We're quickly approaching our two-year anniversary, which will be on episode 105. To celebrate, we've created a unique t-shirt design, available for purchase until the end of August. Shirts will be shipped out around September 1st. Most of the proceeds will support the show, and specifically allow us to buy...

New discussion segment

2015-01-17

We're thinking about adding a new segment to the show where we discuss a topic that the listeners suggest. It's meant to be informative like a tutorial, but more of a "free discussion" format. If you have any subjects you want us to explore, or even just a good name...

How did you get into BSD?

2014-11-26

We've got a fun idea for the holidays this year: just like we ask during the interviews, we want to hear how all the viewers and listeners first got into BSD. Email us your story, either written or a video version, and we'll read and play some of them for...


Episode 259: Long Live Unix

2018-08-15

Direct Download:MP3 AudioVideo This episode was brought to you by Picking the contest winner 1) Vincent 2) Bostjan 3) Andrew 4) Klaus-Hendrik 5) Will 6) Toby 7) Johnny 8) David 9) manfrom 10) Niclas 11) Gary 12) Eddy 13) Bruce 14) Lizz 15) Jim Random number generator Headlines The Strange Birth and Long Life of Unix They say that when one door closes on you, another opens. People generally...

Episode 258: OS Foundations

2018-08-08

Direct Download:MP3 AudioVideo This episode was brought to you by Headlines FreeBSD Foundation Update, July 2018 MESSAGE FROM THE EXECUTIVE DIRECTOR We’re in the middle of summer here, in Boulder, CO. While the days are typically hot, they can also be quite unpredictable. Thanks to the Rocky Mountains, waking up to 50-degree (~10...

Episode 257: Great NetBSD 8

2018-08-01

Direct Download:MP3 AudioVideo This episode was brought to you by Headlines NetBSD v8.0 Released The NetBSD Project is pleased to announce NetBSD 8.0, the sixteenth major release of the NetBSD operating system. This release brings stability improvements, hundreds of bug fixes, and many new features. Some highlights of the...

Episode 2^8: Because Computers

2018-07-25

Direct Download:MP3 AudioVideo This episode was brought to you by Win Celebrate our 256th episode with us. You can win a Mogics Power Bagel (not sponsored). To enter, go find the 4 episodes we did in December of 2017. In the opening, find the 4 letters in the bookshelf behind me. They...