OPSEC / Anonymity Dual VPN based on OpenVPN

TaskMaster

New member
Lucifer
Joined
Jul 23, 2022
Messages
6
Hellcoins
♆344

Let's set up the second server first:​

Step 1Install OpenVPN​

QUOTE:
sudo apt update
sudo apt install openvpn easy-rsa

Step 2: Create a CA Directory​

OpenVPN is a virtual private network using TLS/SSL. This means that OpenVPN uses certificates to encrypt traffic between the server and clients. To issue trusted certificates, we will need to create our own CA.

Create a user named e.g. openvpn-caand change to its home directory:
QUOTE:
sudo adduser openvpn-ca
sudo usermod -aG sudo openvpn-ca
sudo su -openvpn-ca
First, let's copy the template directory easy-rsato our home directory with the commandmake-cadir:
QUOTE:
make-cadir ~ /openvpn-ca
cd ~ /openvpn-ca

Step 3: Set up CA variables​

To set up our CA variables, we need to edit the vars. Open this file in your text editor:
QUOTE:
Inside the file, you will find variables that you can edit and that set the parameters of the certificates when they are created. We only need to change a few variables.
QUOTE:
~/openvpn-ca/vars
QUOTE:
. . .

export KEY_COUNTRY="US"
export KEY_PROVINCE="NY"
export KEY_CITY="New York City"
export KEY_ORG="CodeBy"
export KEY_EMAIL="[email protected]"
export KEY_OU="Community"

. . .
While we are in this file, we will edit the value KEY_NAMEjust below, which fills in the subject field of the certificates. For simplicity, let's give it a name vpnsrv2:
QUOTE:
~/openvpn-ca/vars
QUOTE:
export KEY_NAME="vpnsrv2"
Save and close the file.

Step 4: Create a Certificate Authority​

Now we can use the variables and easy-rsa utilities we've set up to create a certificate authority.

Make sure you are in the CA directory and use the command sourceon the vars. In my case, I also needed to add a symlink to the file openssl-1.0.0.cnf:
QUOTE:
cd ~/openvpn-ca
ln -s ~/openvpn-ca/openssl-1.0.0.cnf openssl.cnf
source vars
You should see the following output:
QUOTE:
Conclusion
NOTE: If you run ./clean-all, I will be doing a rm -rf on /home/sammy/openvpn-ca/keys
Let's make sure we're working in a "clean environment" by running the following command:
QUOTE:
./clean-all
Now we can create our root CA with the command:
QUOTE:
./build-ca
This command will start the process of generating the root CA key and certificate. Since we have set all the variables in the vars file, all the necessary values will be entered automatically. Press ENTER to confirm your selection.

We now have a certificate authority that we can use to create all the other files we need.

Step 5: Create a certificate, key, and encryption files for the server​

Next, let's create a certificate, a key pair, and some additional files used to implement encryption for our server.

Let's start by creating an OpenVPN certificate and keys for the server. This can be done with the following command:

QUOTE:
Note: If you previously chose a name other than server, you will need to slightly change some of the instructions. For example, when copying the created files to the /etc/openvpn directory, you will have to replace the names with the ones you specify. You will also need to modify the /etc/openvpn/server.conf file to point to the correct .crt and .key files.
QUOTE:
./build-key-server vpnsrv2
The output will again contain the default values passed to this command ( server) as well as the values from the vars.

Accept all defaults by pressing ENTER . Don't set a challenge password . At the end of the process, type y twice to sign and validate the creation of the certificate:
QUOTE:
Conclusion
. . .

Certificate is to be certified until May 1 17:51:16 2026 GMT (3650 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
Let's create the rest of the files. We can generate strong Diffie-Hellman keys used in key exchange with the command:
QUOTE:
./build-dh
It may take several minutes for this command to complete.

Next, we can generate an HMAC signature to enhance the server's ability to verify TSL integrity :

QUOTE:
sudo openvpn --genkey --secret keys/ta.key
sudo chown openvpn-ca:eek:penvpn-ca keys/ta.key

Step 6: Create a certificate and key pair for the client​

Next, we can generate a certificate and a key pair for the client. In general, this can be done on the client machine and then signed by the server's certificate authority, but in this article, for simplicity, we will generate the signed key on the server.

In this article, we will create a key and certificate for only one client. If you have multiple clients, you can repeat this process as many times as you like. Just pass a unique value to the script each time.

Since we can come back to this step later, we will repeat the command sourcefor the vars. We will use the option clientsrv2to generate the first certificate and key.

To create files without a password to facilitate automatic connections, use the command build-key:

QUOTE:
cd ~/openvpn-ca
source vars
./build-key clientsrv2
During the file creation process, all default values will be entered, you can press ENTER . Do not specify a challenge password and enter y when prompted to sign and validate the creation of a certificate.

Step 7Configure OpenVPN Service​

Next, we will configure the OpenVPN service using the files we created earlier.

Copying files to the OpenVPN directory We need to copy the files we need to the directory/etc/openvpn.

First, let's copy the files we created. They are in the directory ~/openvpn-ca/keysin which they were created. We need to copy the certificate and key of the certificate authority, the certificate and key of the server, the HMAC signature and the Diffie-Hellman file :
QUOTE:
cd ~/openvpn-ca/keys
sudo mkdir /etc/openvpn/keys
sudo cp ca.crt vpnsrv2.crt vpnsrv2.key dh2048.pem ta.key /etc/openvpn/keys/
Next, we need to copy and unpack the sample OpenVPN configuration file to the configuration directory, we will use this file as a base for our settings:
QUOTE:
gunzip -c /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz | sudo tee /etc/openvpn/server.conf
Setting up the OpenVPN configuration Now that our files are in place, let's set up the server's configuration file:
QUOTE:
sudo you /etc/openvpn/server.conf
Basic setup

The address of the VPN server


QUOTE:
/etc/openvpn/server.conf
QUOTE:
# ethernet bridging. See the man page for more info.
server 10.8.1.0 255.255.255.0
Find the HMAC section by searching for the tls-auth. Remove ";" to uncomment the line with tls-auth. Next, add a parameter key-directionand set its value to "0" :
QUOTE:
/etc/openvpn/server.conf
QUOTE:
tls-auth keys/ta.key 0 # This file is secret
key-direction 0
Next, let's find the encryption section, we are interested in the commented lines cipher. Remove ";" to uncomment the line AES-256-CBC:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
cipher AES-256-CBC
Below this line, add a line authand select the HMAC algorithm . A good choice would be SHA512:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
auth SHA512
Finally, find settings userand groupremove ";" to uncomment these lines:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
user nobody
group nogroup
You need to comment out the following directives. Find the section redirect-gatewayand add ";" to the beginning of the line:


QUOTE:
/etc/openvpn/server.conf
QUOTE:
;push "redirect-gateway def1 bypass-dhcp"
Below is the section dhcp-option.
QUOTE:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
;push "dhcp-option DNS 208.67.222.222"
;push "dhcp-option DNS 208.67.220.220"
(Optional) Port and Protocol Configuration By default , OpenVPN uses port 1194 and UDP protocol to connect with clients. If you need to change the port due to some restrictions on your clients, you can do so by changing the port.

QUOTE:
/etc/openvpn/server.conf
QUOTE:
port 1194

# TCP or UDP server?
proto tcp4
(Optional) Using a custom certificate name and key If you specified a parameter other than , while using the command ./build-key-serverjust above vpnsrv2, change the settings of certand keyto point to the correct files .crtand .key. If you used vpnsrv2, these settings should look like this:
QUOTE:
/etc/openvpn/server.conf
QUOTE:
ca keys/ca.crt
cert keys/vpnsrv2.crt
key keys/vpnsrv2.key

dh keys/dh2048.pem
Save and close the file.

Step 8. Setting up the network configuration of the server​

Next, we need to set up the server's network configuration so that OpenVPN can forward traffic correctly.

Configuring IP Redirection First let's allow the server to redirect traffic. This is the core functionality of our VPN server.

Set it up in a file /etc/sysctl.conf:
QUOTE:
sudo you /etc/sysctl.conf
Find the setting line net.ipv4.ip_forward. Remove the "#" from the beginning of the line to uncomment it:


QUOTE:
/etc/sysctl.conf
QUOTE:
net.ipv4.ip_forward=1
Save and close the file.

To apply the settings to the current session, type the command:

QUOTE:
sudo sysctl -p
Setting up UFW rules to hide client connections You need to install a UFW firewall . We will need a firewall to manipulate traffic entering the server. We have to change the settings file to hide connections (masquerading).
QUOTE:
sudo apt update
sudo apt install ufw
Open the file /etc/ufw/before.rulesand add the appropriate settings there:
QUOTE:
sudo you /etc/ufw/before.rules
This file contains the UFW settings that are applied before the UFW rules are applied . Add the lines highlighted in red to the beginning of the file. This will set up the default rules for the chain POSTROUTINGin the table natand will hide all traffic from the VPN :
QUOTE:
/etc/ufw/before.rules
QUOTE:
#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
# ufw-before-input
# ufw-before-output
# ufw-before-forward
#

# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to eth0
-A POSTROUTING -s 10.8.1.0/24 -o eth0 -j MASQUERADE
COMMIT
# END OPENVPN RULES

# Don't delete these required lines, otherwise there will be errors
. . .
Save and close the file.

Now we need to tell UFW that it needs to allow forwarded packets by default. To do this, open the file /etc/default/ufw:
QUOTE:
sudo vi /etc/default/ufw
Find the directive in the file DEFAULT_FORWARD_POLICY. We will change the value from DROPto ACCEPT:
QUOTE:
/etc/default/ufw
QUOTE:
DEFAULT_FORWARD_POLICY="ACCEPT"
Save and close the file.

Opening the OpenVPN Port and Applying the Changes Next, we'll configure the firewall itself to allow traffic to OpenVPN .

If you did not change the port and protocol in the file , you will need to allow UDP/etc/openvpn/server.conf traffic on port 1194 . If you have changed these settings, enter the values you specified. In my case it is TCP port 1194

Also add your SSH port

QUOTE:
sudo ufw allow 22
sudo ufw allow 1194/tcp
Now deactivate and activate UFW to apply the changes:
QUOTE:
sudo ufw disable
sudo ufw enable
Our server is now configured to handle OpenVPN traffic.

Step 9Enable the OpenVPN service​

We are ready to enable the OpenVPN service on our server. We can do this with systemd .

We need to start the OpenVPN server by specifying the name of our configuration file as a variable after the systemd filename . The configuration file for our server is named /etc/openvpn/server.conf, so we'll add @serverto the end of the filename when we call it:
QUOTE:
sudo systemctl start openvpn@server
Verify that the service has been successfully started with the command:
QUOTE:
sudo systemctl status openvpn@server
If everything is in order, configure the service to automatically turn on when the server boots:
QUOTE:
sudo systemctl enable openvpn@server

Step 10: Create the Client Configuration Infrastructure​

Next, let's set up the system to easily create configuration files for clients.

Creating the client configuration directory structure In your home directory, create a directory structure for storing files:
QUOTE:
sudo su -openvpn-ca
mkdir -p ~/client-configs/files
Since our configuration files will contain client keys, we need to set the permissions for the created directories:
QUOTE:
chmod 700 ~/client-configs/files
Creating a base configuration Next, copy the example configuration into our directory to use as our base configuration:
QUOTE:
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ~/client-configs/base.conf
Open this file in your text editor:
QUOTE:
vi ~/client-configs/base.conf
Let's make a few changes to this file.

First find the directive remote. This directive tells the client the address of our OpenVPN server . This should be the public IP address of your OpenVPN server . If you have changed the port that the OpenVPN server is listening on , change the default port to 1194your value:

QUOTE:
~/client-configs/base.conf
QUOTE:
. . .
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote server_IP_address 1194
. . .
Make sure the protocol matches the server settings:


QUOTE:
~/client-configs/base.conf
QUOTE:
proto tcp
Next, uncomment the directives userand groupremove the ";" :
QUOTE:
~/client-configs/base.conf
QUOTE:
# Downgrade privileges after initialization (non-Windows only)
user nobody
group nogroup
Find the directives ca, certand key. Comment out these directives as we will be adding certificates and keys in the file itself:
QUOTE:
~/client-configs/base.conf
QUOTE:
# SSL/TLS parms.
# See the server config file for more
# description. It's best to use
# a separate .crt/.key file pair
# for each client. A single ca
# file can be used for all clients.
#ca ca.crt
#cert client.crt
#key client.key
Add settings cipherand authaccording to those specified in the file /etc/openvpn/server.conf:

QUOTE:
~/client-configs/base.conf
QUOTE:
cipher AES-256-CBC
auth SHA512
Next, add the directive key-directionanywhere in the file. It must have the value "1" for the server to work correctly:
QUOTE:
~/client-configs/base.conf
QUOTE:
key-direction 1
Creating a script to generate configuration files Now let's create a simple script to generate configuration files with relevant certificates, keys, and encryption files. It will place the generated configuration files in the ~/client-configs/files.

Create and open a file make_config.shinside the directory ~/client-configs:
QUOTE:
vi ~/client-configs/make_config.sh
Paste the following text into this file:

QUOTE:
~/client-configs/make_config.sh
QUOTE:
#!/bin/bash

# First argument: Client identifier

KEY_DIR=~/openvpn-ca/keys
OUTPUT_DIR=~/client-configs/files
BASE_CONFIG=~/client-configs/base.conf

cat ${BASE_CONFIG} \
<(echo -e '<ca>') \
${KEY_DIR}/ca.crt \
<(echo -e '</ca>\n<cert>') \
${KEY_DIR}/${1}.crt \
<(echo -e '</cert>\n<key>') \
${KEY_DIR}/${1}.key \
<(echo -e '</key>\n<tls-auth>') \
${KEY_DIR}/ta.key \
<( echo -e'</tls-auth>') \
> ${OUTPUT_DIR}/${1}.ovpn
Save and close the file.

Make it executable with the command:
QUOTE:
chmod 700 ~/client-configs/make_config.sh

Step 11: Generating Client Configurations​

Now we can easily generate client configuration files.

If you followed all the steps in this article, you have generated clientsrv2.crtthe client certificate and key with the clientsrv2.keycommand ./build-key clientsrv2in step 6. You can generate the configuration for these files by navigating to the directory ~/client-configsand using the script we just created:

QUOTE:
cd ~/client-configs
./make_config.sh clientsrv2
If everything went well, we should get the file clientsrv2.ovpnin the directory ~/client-configs/files:
QUOTE:
ls ~/client-configs/files
QUOTE:
Conclusion
clientsrv2.ovpn
Delivering configurations to the first server Now we need to move the configuration file to the first server SRV1 .

On the first server, we do everything identically, except for the following points:​

QUOTE:
sudo you /etc/ufw/before.rules
Change interface eth0to tun1and address 10.8.1.0/24to10.8.0.0/24
QUOTE:
/etc/ufw/before.rules
QUOTE:
#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
# ufw-before-input
# ufw-before-output
# ufw-before-forward
#

# START OPENVPN RULES
# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]
# Allow traffic from OpenVPN client to tun1
-A POSTROUTING -s 10.8.0.0/24 -o tun1 -j MASQUERADE
COMMIT
# END OPENVPN RULES

# Don't delete these required lines, otherwise there will be errors
. . .
Save and close the file.

Redirect all traffic through the VPN server

The address of the VPN server
QUOTE:
sudo you /etc/openvpn/server.conf
QUOTE:
/etc/openvpn/server.conf
QUOTE:
# ethernet bridging. See the man page for more info.
server 10.8.0.0 255.255.255.0
Set the protocol to udp4
QUOTE:
/etc/openvpn/server.conf
QUOTE:
# TCP or UDP server?
proto udp4
Next, uncomment the following lines. Section redirect-gateway:

QUOTE:
/etc/openvpn/server.conf
QUOTE:
push "redirect-gateway def1 bypass-dhcp"
and sectiondhcp-option

QUOTE:
/etc/openvpn/server.conf
QUOTE:
push "dhcp-option DNS 208.67.222.222"
push "dhcp-option DNS 208.67.220.220"
also this directive
QUOTE:
explicit-exit-notify 1
We write the routing rules on the first server:

Let's create a script /etc/openvpn/upstream-route.shcontaining the following commands:
QUOTE:
#! /bin/sh

ip rule add from 10.8.0.0/24 table 120
ip route add default dev tun1 table 120

exit 0
QUOTE:
chmod +x /etc/openvpn/upstream-route.sh
Next, add them to the configuration file of the client that connects to the second server.
QUOTE:
we clientsrv2.ovpn
QUOTE:
clientsrv2.ovpn
QUOTE:
script-security 2
up upstream-route.sh
Also in this file it is necessary to specify that the OpenVPN client always occupies the interface tun1:

QUOTE:
clientsrv2.ovpn
QUOTE:
Copy clientsrv2.ovpn to OpenVPN root folder and rename it to client.conf
QUOTE:
sudo cp clientsrv2.ovpn /etc/openvpn/client.conf
Setting up autostart
QUOTE:
# server
sudo systemctl start openvpn@server
sudo systemctl enable openvpn@server

# client to srv2
sudo systemctl start openvpn@client
sudo systemctl start openvpn@client
In the same way, we create the infrastructure for setting up clients. Step 10 And transfer the generated file to the client machine.

Connection on Linux
QUOTE:
sudo apt update
sudo apt install openvpn
QUOTE:
sudo openvpn --config clientsrv1.ovpn
As a result, you will connect to the server.
 
Top