Coder Social home page Coder Social logo

vpn-killswitch's Introduction

VPN Killswitch

This is inspired from CK-s-Firewall-killswitch

A VPN (Virtual Private Network) is often used to avoid censorship, surveillance, or geolocation. This is done by routing the internet traffic from your device to the remote VPN server through an encrypted tunnel. Sometimes, the VPN connection may drop, which will result in transmitting your traffic through the public internet rather than an encrypted VPN tunnel.

There are some reasons that cause a disconnection

  • VPN Protocol
  • Not good enough signal strength
  • Firewall/router configuration

The chance of a drop in the connection to the remote server is less, but there may be an unexpected drop which shouldn't be risked on as it will reveal the user's real IP address.

For this reason, A Kill switch technique is implemented, which would prevent the unencrypted/unprotected access to the internet.

VPN Killswitch with IPTABLES

Most of the VPNs do come with a killswitch, but are not as reliable as using iptables (as it is not dependent on the VPN service and is a kernel feature).

Requirements

  • A Linux machine with root privileges
  • A VPN provider
  • iptables should be installed in the machine

Rules

  • Set the base rules to disallow all the traffic
iptables -P INPUT DROP
iptables -P FORWARD DROP
ipables -P OUTPUT DROP
  • Allow Loopback and Ping. Assuming that the VPN connection is on tun0, check with ip a.
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -o lo -j ACCEPT
iptables -A OUTPUT -o tun0 -j ACCEPT
  • Allow to communicate to the DHCP server
iptables -A OUTPUT -d 255.255.255.255 -j ACCEPT
iptables -A INPUT -s 255.255.255.255 -j ACCEPT
  • Allow to communicate within the LAN
iptables -A INPUT -s 192.168.1.0/24 -d 196.168.1.0/24 -j ACCEPT
iptables -A OUTPUT -s 192.168.1.0/24 -d 196.168.1.0/24 -j ACCEPT
  • Allow established sessions to receive traffic
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  • Allow DNS. To do this, get the VPN's DNS server. To do so, see the resolv.conf if the VPN has access to it
iptables -A OUTPUT -d 193.138.218.74 -j ACCEPT
  • Allow the VPN connection.

In this context Mullvad VPN is used. Specifically Singapore

iptables -A OUTPUT -o eth*  -p udp -m multiport --dports 53,1300:1302,1194:1197 -d 185.128.24.0/24,37.120.208.0/24 -j ACCEPT

Assuming the network interface as eth*.

Or

In this context OpenVPN is used used.

iptables -A OUTPUT -p udp -m udp --dport 1194 -j ACCEPT
  • IPv6

It is preferred to drop the IPv6 connections

ip6tables -P INPUT DROP
ip6tables -P FORWARD DROP
ip6tables -P OUTPUT DROP
  • Log all the dropped packages (debug only)
iptables -N logging
iptables -A INPUT -j logging
iptables -A OUTPUT -j logging
iptables -A logging -m limit --limit 2/min -j LOG --log-prefix "IPTables: " --log-level 7
iptables -A logging -j DROP
  • To save the rules
iptables-save > /etc/iptables/iptables.rules
  • To restore the rules
iptables-restore < /etc/iptables/iptables.rules
  • For Persistent rules, use iptables-persistent.

vpn-killswitch's People

Contributors

theupbeat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.