Today I am going to write a small tutorial on how inter-server communication can be secured via IPsec in transport mode. This tutorial was written for Debian 8 Jessie but it should work similar with other linux distributions aswell.
Here is a small diagram of my test setup:
Each server has only one network interface with an public IP (in this test case, we use private IPs).
As we only want the host to host communication to be encrypted, we can use the transport mode. In the standard tunnel mode, entire packets are encapsulated within a single stream, encrypted, then sent on their way. This is great for LAN-to-LAN connections where you want to allow a subnet (or many subnets) on one side of the tunnel to be able to communicate with a subnet (or many) on the other side of the tunnel.
To contrast, in transport mode, IPsec merely encrypts packet payloads, leaving the IP headers intact. This fact enables easy integration into existing routing infrastructures and makes it perfect for securing host-to-host communication on untrusted networks.
First, install prerequisite packages:
$ sudo apt-get update && sudo apt-get install strongswan
IPsec core functions are provided by the kernel – „strongswan“ provides the user-space tools needed to easily configure connections, in addition to providing the „charon“ daemon, which takes care of the periodic re-keying that is essential to IPsec’s security.
Next, add a line like this to your /etc/ipsec.secrets file (example for cluster01).
10.0.99.31 10.0.99.32 : PSK "strongpresharedkey1" 10.0.99.31 10.0.99.33 : PSK "strongpresharedkey2"
Needless to say, you’ll need to substitute the IDs of your actual servers in addition to providing a strong pre-shared key that will be used to bootstrap the IPsec connection between these two IPs.
So for cluster02 the file should look like:
10.0.99.32 10.0.99.31 : PSK "strongpresharedkey1" 10.0.99.32 10.0.99.33 : PSK "strongpresharedkey3"
Add this line to the bottom of /etc/ipsec.conf:
include /etc/ipsec.d/*.conf
NOTE: this line is optional – it is only required if you’d like to keep each individual IPsec connection configuration in its own file. If you don’t care about this, you can just add the below IPsec config to the /etc/ipsec.conf file and be done with it.
However, if you do want to keep things clean and separate out each connection into its own file (doing this can make automated configuration very simple), then add the above line and then add the following to a new config file. Something like /etc/ipsec.d/cluster01_cluster02.conf for server cluster01:
conn cluster01-cluster02 type=transport authby=secret left=10.0.99.31 right=10.0.99.32 pfs=yes auto=start
Let me digest this for you line by line.
At this point, we’re ready to fire things up. On each server run this command:
$ sudo systemctl restart ipsec $ sudo systemctl status ipsec -l
After doing this, all traffic between cluster01 and cluster02 should be encrypted. To verify this, run a ping between servers and then:
$ sudo tcpdump esp
This will display all ESP packets. You should see two lines (one incoming, one outgoing) for each ping. If you are not seeing ESP traffic, then something is wrong and you’ll need to dig into the joy that is IPsec troubleshooting.
If you get any error messages like „operation not permitted“ make sure to allow IPsec traffic in iptables:
iptables -A INPUT -i eth0 -p udp --sport 500 --dport 500 -j ACCEPT iptables -A INPUT -i eth0 -p udp --sport 4500 --dport 4500 -j ACCEPT iptables -A INPUT -i eth0 -p esp -j ACCEPT iptables -A OUTPUT -o eth0 -p udp --sport 500 --dport 500 -j ACCEPT iptables -A OUTPUT -o eth0 -p udp --sport 4500 --dport 4500 -j ACCEPT iptables -A OUTPUT -o eth0 -p esp -j ACCEPT
/etc/ipsec.d/cluster01_cluster02.conf:
conn cluster01-cluster02 type=transport authby=secret left=10.0.99.31 right=10.0.99.32 pfs=yes auto=start
/etc/ipsec.d/cluster01_cluster03.conf:
conn cluster01-cluster03 type=transport authby=secret left=10.0.99.31 right=10.0.99.33 pfs=yes auto=start
/etc/ipsec.secrets
10.0.99.31 10.0.99.32 : PSK "secret1_2" 10.0.99.31 10.0.99.33 : PSK "secret1_3"
/etc/ipsec.d/cluster02_cluster01.conf:
conn cluster02-cluster01 type=transport authby=secret left=10.0.99.32 right=10.0.99.31 pfs=yes auto=route
/etc/ipsec.d/cluster02_cluster03.conf:
conn cluster02-cluster03 type=transport authby=secret left=10.0.99.32 right=10.0.99.33 pfs=yes auto=start
/etc/ipsec.secrets
10.0.99.32 10.0.99.31 : PSK "secret1_2" 10.0.99.32 10.0.99.33 : PSK "secret2_3"
/etc/ipsec.d/cluster03_cluster01.conf:
conn cluster03-cluster01 type=transport authby=secret left=10.0.99.33 right=10.0.99.31 pfs=yes auto=route
/etc/ipsec.d/cluster03_cluster02.conf:
conn cluster03-cluster02 type=transport authby=secret left=10.0.99.33 right=10.0.99.32 pfs=yes auto=route
/etc/ipsec.secrets
10.0.99.33 10.0.99.31 : PSK "secret1_3" 10.0.99.33 10.0.99.32 : PSK "secret2_3"
This article is based on http://andersonfam.org/2014/04/02/ipsec-transport-mode/.
Du musst angemeldet sein, um einen Kommentar abzugeben.
JoakimSeptember 27, 2016
Thanks, clear and easy guide to get started! got it up and running in under 10 minutes.