Goal: Use Mikrotik OVPN client connection as gateway interface to encrypt your internet activity from prying eyes.
What we have:
1. Mikrotik Router (in my case - RB951G-2HnD v6.6)
2. OpenVZ VPS service (in my case - BuyVM with Ubuntu 12.04 LTS Server x64)
3. Working internet connection (via Mikrotik Router)
TUTORIAL:
1. Install Ubuntu 12.04 LTS on your VPS service and start it
2. SSH connect to installed server (with Putty for example)
3. Login as root
4. Use command line:
Code: Select all
apt-get update
apt-get dist-upgrade -y
apt-get install openvpn openssl udev
cp -R /usr/share/doc/openvpn/examples/easy-rsa/ /etc/openvpn
Code: Select all
nano /etc/openvpn/easy-rsa/2.0/whichopensslcnf
Code: Select all
else
cnf="$1/openssl.cnf"
fi
Code: Select all
else
cnf="$1/openssl-1.0.0.cnf"
fi
Code: Select all
nano /etc/openvpn/easy-rsa/2.0/vars
Code: Select all
export EASY_RSA="`pwd`"
Code: Select all
export EASY_RSA="/etc/openvpn/easy-rsa/2.0"
Code: Select all
export KEY_SIZE=1024
Code: Select all
export KEY_SIZE=2048
Initialize the certificate authority and the public key infrastructure (PKI) by issuing the following commands in sequence:
Code: Select all
cd /etc/openvpn/easy-rsa/2.0/
. /etc/openvpn/easy-rsa/2.0/vars
. /etc/openvpn/easy-rsa/2.0/clean-all
. /etc/openvpn/easy-rsa/2.0/build-ca
Code: Select all
. /etc/openvpn/easy-rsa/2.0/build-key-server server
. /etc/openvpn/easy-rsa/2.0/build-key client
. /etc/openvpn/easy-rsa/2.0/build-dh
Code: Select all
cd /etc/openvpn/easy-rsa/2.0/keys
cp ca.crt ca.key dh2048.pem server.crt server.key /etc/openvpn
cd /etc/openvpn/
Code: Select all
nano openvpn.conf
Code: Select all
port 443
proto tcp
dev tun
ca ca.crt
cert server.crt
key server.key
dh dh2048.pem
server 10.10.0.0 255.255.255.0
cipher AES-256-CBC
user nobody
group nogroup
status /var/log/openvpn-status.log
log-append /var/log/openvpn
verb 3
mute 0
max-clients 100
keepalive 10 120
persist-key
persist-tun
push "redirect-gateway def1"
push "dhcp-option DNS 8.8.8.8"
push “dhcp-option DNS 8.8.4.4″
Code: Select all
nano /etc/rc.local
Code: Select all
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -s 10.10.0.0/24 -j ACCEPT
iptables -A FORWARD -j REJECT
iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -j SNAT --to-source YOUR_VPS_IP
save and exit
Code: Select all
nano /etc/sysctl.conf
Code: Select all
net.ipv4.ip_forward=1
Code: Select all
echo 1 > /proc/sys/net/ipv4/ip_forward
Code: Select all
/etc/init.d/openvpn restart
Code: Select all
cd /etc/openvpn/easy-rsa/2.0/keys
openssl rsa -in /etc/openvpn/easy-rsa/2.0/keys/client.key -out /etc/openvpn/easy-rsa/2.0/keys/client.pem
Reboot your installed Ubuntu server.
5. Start WinBox application on local PC
Open "Files"
Drug and drop ca.crt client.crt client.pem from LOCAL PC to Mikrotik File List window
Open "System -> Certificates"
Import ca.crt client.crt and client.pem
CA will be with "T" and Client with "KT" now
Close "Certificates" and open it again or you will get error on the next step.
Rename your CA and Client Certificates as you want to remember it.
Open "PPP"
Click "+" and "OVPN Client" there
Connect to: YOUR_VPS_IP
Port: 443
Mode: ip
User: client
Password:
Profile: default
Certificate: choose your client certificate (not CA)
Auth: sha1
Cipher: aes 256
Click "OK"
Now your OVPN connection should be up and running. If not - you make some mistakes somewhere before.
Now you need to add a Mangle rule which IP addresses you want to give access to VPN through Mikrotik:
Go IP -> “Firewall” -> "Mangle" tab, select "Add new".
In "Chain", select "prerouting".
In "Src. Adress", enter the IP or IP range you want to have routed through the VPN connection.
In "Action", select "mark routing".
In "New Routing Mark", here enter any name for the routing mark, e.g. "OVPN"
Tick Passthrough
Click "OK".
Next, you need to add routes for the new VPN connection:
Go to "IP" and then to "Routes" and "Add New".
Dst. Address: has to be "0.0.0.0/0".
Gateway: Here enter the name of the VPN connection you created, e.g. "OVPN"
Routing Mark: select the routing mark you created before. (OVPN)
Click "OK".
Add Masquerade for this OpenVPN connection:
Now please go to "IP" tab and select "Firewall" and "NAT".
In "Chain", select "srcnat", and check the "Enabled" checkbox.
In "Out. Interface", select the name of the OpenVPN connection you just created and check the box.
In "Action", select "Masquerade".
Click "OK".
If you have firewall - you will need to accept 443 TCP port on out OVPN interface in chain "Output" and all ports on out OVPN interface in chain "Forward"
That's all. If you did all steps, all devices with IPs or IP ranges you enter in the Mangle rule will go to internet via your OVPN tunnel. External IP will be same as YOUR_VPS_IP
Hope you like it. If you have any suggestions, corrections or questions - please write in this topic.

(c) Enot