Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

For the security and privacy conscious, a trusted VPN is a great tool that provides encrypted communications and assured privacy while accessing unsecured networks (the internet in particular). 

I use several VPNs, I have my own private OpenVPN server which I run on my main (personal) server, and also use a well known VPN service.   I do use both (not at the same time) for various purposes and for different use cases which won't be discussed here.

One of the important reasons for using a VPN is to obfuscate (hide) your ip address.  It's important to note that even the best VPN setups are subject to occasionally occasional drops and disconnects.  When this occurs, your system will likely fallback to using it's standards its standard network interfaces (which it would use without a VPN) and incidentally expose your actual ip address.  In sensitive environments (or countries) this can potentially lead to some very undesirable situations (including being able to find your actual location).

Enter a Kill Switch.  A kill switch is basically some system which ensures that your ip address is not leaked, and ideally blocks all traffic that is not through the VPN.  Hence if your VPN connection drops, your system doesn't fall back to standard network interfaces.

Many VPN services provide built in Kill Switch functionality.  However, I generally prefer to roll my own, with some an implementation that I trust (because I did it).

Guide

Table of Contents
minLevel3

Requirements and setup

This implementation doesn't require much.  Basically you need

...

Make sure UFW is setup and installed (however your distro does it).  For Arch based distros that use systemd, you would simply do

Code Block
languagebash
sudo systemctl enable ufw
sudo ufw enable

...

My killswitch consists of several a single bash scriptsscriptOne for This script supports both starting the killswitch and stopping the other to stopping (and resetting killswitch (which resets the firewall)., plus another option to open an outgoing route to a specific CIDR:

View Git file
pathvpnkillswitch-on
repository-id7
languagebash
branchrefs/remotes/origin/master
linenumberstrue

View Git file
pathvpnkillswitch-off
repository-id7
languagebash
branchrefs/remotes/origin/master
linenumberstrue

You'll need to make each of these files the script executable and ideally put them it in some folder in on your path.  To make them executable, cd into whatever folder contains the scripts and:

Code Block
chmod +x vpnkillswitch

Usage

Basic usage is to simply call the script with a valid option.  The supported options are:

Code Block
-e
	enable killswitch (turn it on
chmod +x vpnkillswitch-off

Usage

...

)

-d
	disable killswitch (turn it off)

-t [CIDR]
	open outgoing (ufw) route to a specific CIDR (ip address or range)	

Note that the -t  option will likely be needed if you also run a web server (for example) on the same server that is running your VPN and want to access web server resources.

Examples

Turn on (enable) the killswitch

Code Block
vpnkillswitch -e

Turn on (enable) the killswitch and allow outgoing route to to some server, e.g. 93.184.216.34:

Code Block
vpnkillswitch -et 93.184.216.34

Turn off (disable) the killswitch

Code Block
vpnkillswitch -d

Integrating killswitch into OpenVPN client ovpn

If you (or your VPN provider) uses OpenVPN you can integrate the killswitch script into your client .ovpn  file so that when you connect the killswitch script is automatically run.  This comes in handy especially if you run your own OpenVPN server (which I do) - and also use that server for other things (such as a web server etc.).  In these types of use cases you can also open an outgoing route to your server (for example to access server web resources while also being connected to said server via OpenVPN).

You can simply add a up  or route-up  directive with the vpnkillswitch  command.  For example, in my client .ovpn  I simply added:

Code Block
script-security 2
route-up "/home/user/.local/bin/vpnkillswitch -et <IP-ADDRESS-OF-OPENVPN-SERVER>"


Info

Replace <IP-ADDRESS-OF-OPENVPN-SERVER> with the IP address of the outgoing route you want to allow.

I use  route-up   here as OpenVPN will execute the script after routes to the VPN server has been added.  This is after any   up  commands have been run (which you might use in your client config to set DNS addresses etc.).

Info

Don't forget that you'll need to disable the kill switch after disconnecting with from OpenVPN.  You can do this by executing:

Code Block
vpnkillswitch -d


References

  1. https://thetinhat.com/tutorials/misc/linux-vpn-drop-protection-firewall.html

...