iptables:create_a_vpn_kill_switch
Differences
This shows you the differences between two versions of the page.
iptables:create_a_vpn_kill_switch [2018/06/15 11:19] – created peter | iptables:create_a_vpn_kill_switch [2019/11/29 17:25] (current) – removed peter | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== IPTables - Create a VPN Kill Switch ====== | ||
- | |||
- | |||
- | Use iptables to block all Internet connections in the event your VPN is disconnected. | ||
- | |||
- | It's a mechanism that stops your Internet connection when you're disconnected from the VPN. It protects you from inadvertently leaking sensitive information onto the Internet when the VPN connection drops. | ||
- | |||
- | Some VPN services provide clients with a built-in killswitch, but none are as reliable as using iptables. | ||
- | |||
- | |||
- | ===== Sysctl ===== | ||
- | |||
- | Before you start creating iptables rules, you should make some alterations to the sysctl configuration. | ||
- | |||
- | <code bash> | ||
- | net.ipv4.ip_forward=1 | ||
- | < | ||
- | |||
- | Then, add the following lines to the bottom of the file. Be sure to change the interfaces to match the ones on your machine. | ||
- | |||
- | <file bash> | ||
- | net.ipv6.conf.all.disable_ipv6 = 1 | ||
- | net.ipv6.conf.default.disable_ipv6 = 1 | ||
- | net.ipv6.conf.lo.disable_ipv6 = 1 | ||
- | net.ipv6.conf.eth0.disable_ipv6 = 1 | ||
- | </ | ||
- | |||
- | |||
- | Save and exit. Then run: | ||
- | |||
- | <code bash> | ||
- | # sysctl -p | ||
- | </ | ||
- | |||
- | |||
- | |||
- | |||
- | ===== Set Up The Firewall Document ===== | ||
- | |||
- | Create a file for your firewall rules. | ||
- | |||
- | < | ||
- | *filter | ||
- | |||
- | |||
- | |||
- | COMMIT | ||
- | </ | ||
- | |||
- | |||
- | ===== Base Rules ===== | ||
- | |||
- | Before you configure iptables to allow any traffic you need to switch its default to disallow all traffic. | ||
- | |||
- | <file bash> | ||
- | -P INPUT DROP | ||
- | -P FORWARD DROP | ||
- | -P OUTPUT DROP | ||
- | </ | ||
- | |||
- | |||
- | ===== Input ===== | ||
- | |||
- | It's most secure to only allow inbound traffic from established or related connections. | ||
- | |||
- | <file bash> | ||
- | -A INPUT -m conntrack --ctstate RELATED, | ||
- | </ | ||
- | |||
- | |||
- | ===== Loopback and Ping ===== | ||
- | |||
- | Next, allow the loopback interface and ping. | ||
- | |||
- | <file bash> | ||
- | -A OUTPUT -o lo -j ACCEPT | ||
- | -A OUTPUT -o tun0 -p icmp -j ACCEPT | ||
- | </ | ||
- | |||
- | This assumes that your VPN connection is on **tun0**. | ||
- | |||
- | |||
- | ===== LAN ===== | ||
- | |||
- | It doesn' | ||
- | |||
- | <file bash> | ||
- | -A OUTPUT -d 192.168.1.0/ | ||
- | </ | ||
- | |||
- | |||
- | ===== DNS ===== | ||
- | |||
- | You need to know the IP address of your VPN's DNS server(s). | ||
- | |||
- | <file bash> | ||
- | -A OUTPUT -d 10.45.16.1 -j ACCEPT | ||
- | </ | ||
- | |||
- | |||
- | ===== Allow The VPN ===== | ||
- | |||
- | You need to allow the VPN itself. | ||
- | |||
- | <file bash> | ||
- | -A OUTPUT -p udp -m udp --dport 1194 -j ACCEPT | ||
- | -A OUTPUT -o tun0 -j ACCEPT | ||
- | </ | ||
- | |||
- | Again, check the port and interface that your VPN connection is using. | ||
- | |||
- | You could stop here. This will work just fine for a killswitch. | ||
- | |||
- | From here, you would delete the last line that accepts all traffic on tun0, and replace it with specific allowances for the ports that you want to allow. | ||
- | |||
- | <file bash> | ||
- | -A OUTPUT -o tun0 -p tcp --dport 443 -j ACCEPT | ||
- | -A OUTPUT -o tun0 -p tcp --dport 80 -j ACCEPT | ||
- | |||
- | |||
- | -A OUTPUT -o tun0 -p tcp --dport 993 -j ACCEPT | ||
- | -A OUTPUT -o tun0 -p tcp --dport 465 -j ACCEPT | ||
- | </ | ||
- | |||
- | You get the general idea. It's longer and more tedious, but it gives you more control over what traffic gets through. | ||
- | |||
- | |||
- | ===== IPv6 ===== | ||
- | |||
- | IPv6 is really bad for VPNs right now. Most don't adequately support it, and your information can leak out over that connection. | ||
- | |||
- | Create another file for IPv6 and block everything. | ||
- | |||
- | <file bash> | ||
- | -P INPUT DROP | ||
- | -P FORWARD DROP | ||
- | -P OUTPUT DROP | ||
- | </ | ||
- | |||
- | |||
- | |||
- | ===== Commit ===== | ||
- | |||
- | You need to import your files into iptables in order for them to take effect. | ||
- | |||
- | <code bash> | ||
- | iptables -F && iptables -X | ||
- | </ | ||
- | |||
- | Import the new ones from your files. | ||
- | |||
- | <code bash> | ||
- | iptables-restore < /tmp/ipv4 | ||
- | ip6tables-restore < /tmp/ipv6 | ||
- | </ | ||
- | |||
- | ===== Make It Permanent ===== | ||
- | |||
- | Iptables doesn' | ||
- | |||
- | ===== Debian/ | ||
- | |||
- | Debian-based systems have a program called, **iptables-persistent**. | ||
- | |||
- | When you install it, iptables-persistent will ask you if you want to save your existing configuration. | ||
- | |||
- | <code bash> | ||
- | apt install iptables-persistent | ||
- | </ | ||
- | |||
- | Since Debian systems run services on startup by default, you don't need to do anything else. | ||
- | |||
- | ===== Other Systemd ===== | ||
- | |||
- | Other systems have a couple of different ways to handle this. The first is to edit / | ||
- | |||
- | <file bash / | ||
- | IPTABLES_SAVE_ON_STOP=" | ||
- | </ | ||
- | |||
- | OR | ||
- | |||
- | <file bash / | ||
- | IPTABLES_SAVE_ON_RESTART=" | ||
- | </ | ||
- | |||
- | The other way is to use the save and restore functions of iptables. | ||
- | |||
- | <code bash> | ||
- | mkdir / | ||
- | iptables-save > / | ||
- | ip6tables-save > / | ||
- | </ | ||
- | |||
- | Then, create a script to load those rule when your computer boots up. | ||
- | |||
- | <file bash> | ||
- | #! /bin/bash | ||
- | |||
- | iptables-restore < / | ||
- | ip6tables-restore < / | ||
- | </ | ||
- | |||
- | |||
- | ===== OpenRC ===== | ||
- | |||
- | OpenRC systems like Gentoo have their own way of saving the configurations. | ||
- | |||
- | <code bash> | ||
- | rc-service iptables save | ||
- | rc-service ip6tables save | ||
- | |||
- | rc-service iptables start | ||
- | rc-service ip6tables start | ||
- | |||
- | rc-update add iptables default | ||
- | rc-update add ip6tables default | ||
- | </ | ||
- | |||
- | |||
- | ===== Closing Thoughts ===== | ||
- | |||
- | Using an iptables-based killswitch makes your VPN much more secure. | ||
- | |||
- | Do not trust the so-called killswitches baked into VPN clients. | ||
iptables/create_a_vpn_kill_switch.1529061567.txt.gz · Last modified: 2020/07/15 09:30 (external edit)