OpenVPN Server Gateway with Obfsproxy (SOCKS5)

Last Updated: 2015-11-16

Installing OpenVPN

Add the package repository to your system and install the latest (at least version 2.3.x) package:
$ wget -O - https://swupdate.openvpn.net/repos/repo-public.gpg | sudo apt-key add -
# echo "deb http://swupdate.openvpn.net/apt OSRELEASE main" > /etc/apt/sources.list.d/swupdate.openvpn.net.list
# apt-get update && apt-get -y upgrade && apt-get -y install openvpn openssl
 
Change OSRELEASE to your actual release, e.g. precise, trusty, wheezy, ...

Creating your RSA-Keys and Certificates

You have to download the easy-rsa tools seperately and unzip them into a folder called easy-rsa2:
$ wget https://github.com/OpenVPN/easy-rsa/archive/release/2.x.zip
$ unzip 2.x.zip

In this folder there needs to be an openssl.cnf file to create the certificates, which can be created by linking the included sample openssl.cnf file:
$ ln -s openssl-1.0.0.cnf openssl.cnf

Next step is to change vars to your needs, i.e. $vim vars:
export KEY_SIZE=2048
export CA_EXPIRE=365
export KEY_EXPIRE=365
export KEY_COUNTRY="XX"
export KEY_PROVINCE="Y"
export KEY_CITY="Z"
export KEY_ORG="Internet Ltd."
export KEY_EMAIL=mail@host.domain
export KEY_CN=changeme
export KEY_NAME=changeme
export KEY_OU=changeme
export PKCS11_MODULE_PATH=changeme
export PKCS11_PIN=1234

Having set all the neccessary variables, you have to source the file ($ source vars) and create the keys directory ($ mkdir keys). Now we can start with
$ ./clean-all
to remove all old keys, and then:
$ ./build-ca
$ ./build-key-server server
Here you should input as the common name the actual name of your openvpn server when asked.

To create the keys needed by the client to connect to the server, execute:
$ ./build-key client
$ ./build-dh

This creates a couple of files (most importantly, ca|client|server.crt, ca|client|server.crs, ca|client|server.key), when asked to change something, just hit enter. When asked to confirm, press y. Just be sure not to set any challenge password, because this might allow you to login with it. If you want to secure your client-keys with a passwort, just do:
$ ./build-key-pass client

Last, but not least, you have to create the Diffie-Hellmann file,
$ ./build-dh

and the ta.key file for the TLS-authentification:
$openvpn --genkey --secret ta.key

Finally, create a tar archive for transfering the files need by your client computer to it:
$tar -cf client.tar client.key client.crt ca.crt ta.key
$scp client.tar client:


INFO: Die Dateien mit der Endung .key sind die geheimen Schlüssel, die nur auf dem entsprechenden Rechner, zu dem sie gehören, gespeichert werden sollten. Die .crt-Dateien sind die Zertifikate, die nicht geheim gehalten werden müssen. Die Client-Schlüssel und -Zertifikate müssen nun auf die Clients transferiert werden, server.key bleibt wo sie ist. Die ca.key Datei sollte aus Sicherheitsgründen vom Server entfernt werden (bspw. auf einem USB Stick offline gespeichert), da sie nur für die Erstellung weiterer Schlüssel benötigt wird und im laufenden Server-Betrieb keinen Nutzen hat.

Außerdem muss jeder Client noch die Datei ca.crt erhalten, damit er den Server einwandfrei identifizieren kann. Zusätzlich muss noch darauf geachtet werden, dass die Dateien nie im ASCII-Modus übertragen werden. Dies kann dazu führen, dass die Datei nicht mehr entschlüsselt werden kann und somit ein Verbinden mit dem openVPN-Server nicht möglich ist. Fehlermeldung: "Error: private key password verification failed". Man umgeht das Problem, indem man alle zu übertragenden Dateien in ein Archiv packt. Bei der Übertragung übers Internet sollten die Dateien zusätzlich verschlüsselt werden, z.B. mit GnuPG.

Remove Client access to VPN server

If you want to remove a user’s access to the VPN server, enter the following command:
./etc/openvpn/easy-rsa/2.0/vars
./etc/openvpn/easy-rsa/2.0/revoke-full client

Configuring your OpenVPN-Server

Change your /etc/openvpn/server.conf file to fit your needs, you can use this one as an example (just remember to change the path to your key-files, everything else should be set quite reasonably):
mode server
tls-server
port 1194
proto tcp #for obfsproxy, otherwise udp
dev tun
ca /path/to/keys/ca.crt
cert /path/to/keys/server.crt
key /path/to/keys/server.key
dh /path/to/keys/dh2048.pem
tls-auth /etc/openvpn/ta.key 0
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 208.67.222.222" #you can use any dns servers here, they just have to be accessable from your openvpn-server.
push "dhcp-option DNS 208.67.220.220"
comp-lzo
user openvpn #see next paragraph
group openvpn
status openvpn-status.log
log-append openvpn.log #for debugging quite useful

Noch besser ist es, hier nicht auf die Kennung nobody/nogroup zurückzugreifen, sondern eine eigene spezialisierte openvpn/openvpn-Identität zu schaffen, wobei man die Shell auf "/bin/false" setzen kann.
sudo addgroup --system --no-create-home --disabled-login --group openvpn
sudo adduser --system --no-create-home --disabled-login --ingroup openvpn openvpn

Finding your way: add route

Damit man auch ins Internet gelangen kann, muss man der Server-Firewall (in diesem Fall iptables) noch eine Regel mit geben und das IP-Forwarding aktivieren. Damit folgende Änderungen auch nach dem Reboot bestehen, sollte man sie am besten in die /etc/rc.local eintragen. This is essential if you want to use your OpenVPN server as a gateway to an unrestricted internet and not only for connecting to your local LAN from anywhere in the world.

# initialize natting for openvpn
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -t nat -F POSTROUTING
iptables -t nat -A POSTROUTING -o eth0 -s 10.8.0.0/24 -j MASQUERADE

ODER:

sudo sysctl -w net/ipv4/ip_forward=1
Um diese Änderung permanent zu machen, kann man sie in die Datei /etc/sysctl.conf eintragen:
net.ipv4.ip_forward=1

iptables -A FORWARD -o eth0 -i tun0 -s 10.8.0.0/24 -m conntrack --ctstate NEW -j ACCEPT
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

To save the changes to the iptables use this command:

sudo service iptables save
sudo service iptables restart

OpenVPN Client config

sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/
copy ca.crt, client.crt, client.key, ta.key from the server to, e.g., /etc/openvpn/

edit /etc/openvpn/client.conf and add/change:
client
dev tun
remote openvpn-server-ip openvpn-server-port
proto tcp #for use with obfsproxy (udp for vpn only)
ca /path/to/ca.crt
cert /path/to/client.crt
key /path/to/client.key
remote-cert-tls server
tls-auth /path/to/ta.key 1
comp-lzo
pull

#for obfsproxy only:
remote openvpn-server-ip port(ideally 443, because this most pobably is not blocked)
route openvpn-server-ip 255.255.255.255 net_gateway
socks-proxy-retry
socks-proxy 127.0.0.1 localproxyport

Setting up obfsproxy

pip install obfsproxy

maybe you need to pip install -U setuptools to get the versions needed.

To start obfsproxy on your server:
# obfsproxy --log-file=obfsproxy.log --log-min-severity=info obfs2 \
      --dest=127.0.0.1:1194 --shared-secret=<some-random-key> server 0.0.0.0:443

if you do not need a privileged port like 443, you can (and should!) start it as a normal user.

And to start it on your client:
$ obfsproxy --log-file=obfsproxy.log --log-min-severity=info obfs2 \
      --shared-secret=<some-random-key> socks 127.0.0.1:443

Update: use obfs3 without --shared-secret to get a SOCKS5 proxy, because OpenVPN does NOT work with a SOCKS4 proxy, but throws this error msg:
Starting obfs2 with shared secret:
Received SOCKS handshake data.
or:
Invalid SOCKS command: '3'
followed by:
Connection was lost (Connection was closed cleanly.).
Closing connection.
Tearing down circuit.

You need the latest version (at least [WARNING] Obfsproxy (version: 0.2.13) starting up.) for that. Check for [INFO] OBFSSOCKSv5Factory starting.

Having done that, you can finally start your openvpn server and client via, e.g., sudo service openvpn start and enjoy the internet.

For Windows users

obfsproxy and openvpn

Nach der Installation müssen dann folgende Dateien, die vorhin erstellt wurden, in den Ordner C:\Programme\OpenVPN\config\ kopiert werden:

client.ovpn
client.crt
client.key
ca.crt
ta.key

Wie zu erkennen ist, muss die client.conf-Datei zur Verwendung mit der Windows-GUI die Endung .ovpn haben.

Wer die VPN-Verbindung/en bereits beim Start von Windows aktivieren möchte, kann über "Start -> Ausführen -> services.msc" den Dienst openVPN auf automatisch starten setzen. Damit werden alle konfigurierten Verbindungen aus dem Ordner "C:\Programme\OpenVPN\config\" beim Systemstart automatisch aufgebaut.

Keep in mind to start openvpn-gui.exe as an admin to be able to correctly set the route to redirect all your internet traffic through the OpenVPN-server.

Not yet rated