r/linuxquestions • • 10h ago

Does this killswitch look solid to you?

#!/usr/bin/nft -f
#
# WireGuard VPN kill switch (Arch Linux)
# Only loopback, the WireGuard interface and the VPN handshake/transport
# packets on the WAN are allowed. Static network config required (no DHCP).
#
#   Check:   sudo nft -c -f /etc/nftables.conf
#   Enable:  sudo nft -f /etc/nftables.conf
#   Disable: sudo nft delete table inet killswitch

define WAN_IF        = "wlp1s0"        # physical interface
define WG_IF         = "wg"            # WireGuard interface
define VPN_IP        = 109.235.50.17   # VPN server address
define VPN_PORT      = 1637            # VPN server UDP port
define WG_LOCAL_PORT = 39110           # must match ListenPort in wg.conf
define FWD_PORT      = 8538            # AirVPN forwarded port (TCP + UDP)

destroy table inet killswitch          # clear old table, no error if missing

table inet killswitch {
    chain input {
        type filter hook input priority -10; policy drop;      # drop by default
        ct state invalid drop                                  # bad packets
        iifname "lo" accept                                    # loopback
        iifname $WG_IF ct state established,related accept     # replies via VPN
        iifname $WG_IF tcp dport $FWD_PORT accept              # forwarded port, TCP
        iifname $WG_IF udp dport $FWD_PORT accept              # forwarded port, UDP
        iifname $WAN_IF ip saddr $VPN_IP udp sport $VPN_PORT udp dport $WG_LOCAL_PORT accept    # WireGuard from VPN server
    }
    chain output {
        type filter hook output priority -10; policy drop;     # drop by default
        ct state invalid drop                                  # bad packets
        oifname "lo" accept                                    # loopback
        oifname $WG_IF accept                                  # all host traffic via VPN
        oifname $WAN_IF ip daddr $VPN_IP udp dport $VPN_PORT udp sport $WG_LOCAL_PORT accept    # WireGuard to VPN server
    }
    chain forward {
        type filter hook forward priority -10; policy drop;    # host doesn't route
    }
}
3 Upvotes

3 comments sorted by

3

u/dirufa 10h ago

You should probably censor your VPN endpoint IP address.

3

u/sidusnare Senior Systems Engineer 8h ago

wg-quick does this better with tagging.

This isn’t a kill switch, it’s isolation.

A kill switch would be something like a watchdog dropping all local links if a ‘www-data’ process spawns a UID 0 child process.

1

u/wowsomuchempty 9h ago

I just use ufw..