是否有适用于Ubuntu的VPN Monitor / Kill Switch应用程序?

嗨,我正在寻找一个VPN监视器/杀戮开关应用程序,将确保我的VPN连接始终连接。 如果我的安全连接丢失,应用程序将丢弃它正在监视的应用程序以防止数据泄漏。 我知道Windows有这样的应用程序。 但是,我还没有为Linux找到合适的替代方案。

我有相同的设置,“VPN杀死开关”比人们想象的更棘手。

遵循您的规范,其内容为“当VPN崩溃时杀死某些应用程序”,这是一个简单的解决方案。

在Ubuntu上,网络监视器具有网络事件的回调,因此您可以编写脚本来终止所需的应用程序。 示例如下:

编辑/etc/NetworkManager/dispatcher.d/50vpndownkillapps.rb

 #!/usr/bin/env ruby if ARGV == [ 'tun0', 'vpn-down' ] `pkill -f transmission` `pkill -f deluge` end 

使其可执行: chmod 755 /etc/NetworkManager/dispatcher.d/50vpndownkillapps.rb ,并享受:-)

这个脚本是在Ruby中(所以它需要ruby),但它可以简单地转换为shell脚本。

它还假设VPN适配器是tun0 ,这是OpenVPN配置的标准。

我有同样的需求,我开发了自己的解决方案,因为在Linux上似乎没有专门的工具。 没有必要删除/关闭已打开的应用程序! 🙂

您需要设置iptables防火墙,因此您的计算机只能连接到指定的VPN服务器(除了本地之外不允许其他流量,因此不会出现“泄漏”)。 这是一个脚本(在网上找到):

 #!/bin/bash # iptables setup on a local pc # dropping all traffic not going trough vpn # allowes traffic in local area network # special rules for UPNP and Multicast discovery FW="/sbin/iptables" LCL="192.168.1.0/24" VPN="10.0.0.0/12" local_interface="eno1" virtual_interface="tun0" # VPN Servers servers=( 123.123.123.123 124.124.124.124 ) #--------------------------------------------------------------- # Remove old rules and tables #--------------------------------------------------------------- echo "Deleting old iptables rules..." iptables -F iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X echo "Setting up new rules..." #--------------------------------------------------------------- # Default Policy - Drop anything! #--------------------------------------------------------------- $FW -P INPUT DROP $FW -P FORWARD DROP $FW -P OUTPUT DROP #--------------------------------------------------------------- # Allow all local connections via loopback. #--------------------------------------------------------------- $FW -A INPUT -i lo -j ACCEPT $FW -A OUTPUT -o lo -j ACCEPT #--------------------------------------------------------------- # Allow Multicast for local network. #--------------------------------------------------------------- $FW -A INPUT -j ACCEPT -p igmp -s $LCL -d 224.0.0.0/4 -i $local_interface $FW -A OUTPUT -j ACCEPT -p igmp -s $LCL -d 224.0.0.0/4 -o $local_interface #--------------------------------------------------------------- # UPnP uses IGMP multicast to find media servers. # Accept IGMP broadcast packets. # Send SSDP Packets. #--------------------------------------------------------------- $FW -A INPUT -j ACCEPT -p igmp -s $LCL -d 239.0.0.0/8 -i $local_interface $FW -A OUTPUT -j ACCEPT -p udp -s $LCL -d 239.255.255.250 --dport 1900 -o $local_interface #--------------------------------------------------------------- # Allow all bidirectional traffic from your firewall to the # local area network #--------------------------------------------------------------- $FW -A INPUT -j ACCEPT -s $LCL -i $local_interface $FW -A OUTPUT -j ACCEPT -d $LCL -o $local_interface #--------------------------------------------------------------- # Allow all bidirectional traffic from your firewall to the # virtual privat network #--------------------------------------------------------------- $FW -A INPUT -j ACCEPT -i $virtual_interface $FW -A OUTPUT -j ACCEPT -o $virtual_interface #--------------------------------------------------------------- # Connection to VPN servers (UDP 443) #--------------------------------------------------------------- server_count=${#servers[@]} for (( c = 0; c < $server_count; c++ )) do $FW -A INPUT -j ACCEPT -p udp -s ${servers[c]} --sport 1194 -i $local_interface $FW -A OUTPUT -j ACCEPT -p udp -d ${servers[c]} --dport 1194 -o $local_interface $FW -A INPUT -j ACCEPT -p tcp -s ${servers[c]} --sport 443 -i $local_interface $FW -A OUTPUT -j ACCEPT -p tcp -d ${servers[c]} --dport 443 -o $local_interface done #--------------------------------------------------------------- # Log all dropped packages, debug only. # View in /var/log/syslog or /var/log/messages #--------------------------------------------------------------- #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 general: " --log-level 7 #iptables -A logging -j DROP # Disable internet for "no-internet" user #iptables -A OUTPUT -m owner --gid-owner no-internet -j DROP 

您需要设置表servers=() 。 只需指定您喜欢的VPN服务器的IP。

还要检查脚本开头的其他变量是否设置正确,否则会阻止整个连接。

一定要用下面的iptables备份:

 sudo iptables-save > working.iptables.rules 

(用sudo iptables-restore < working.iptables.rules

它支持TCP和UDP连接,如果只需要其中一个,则从for ()循环中删除不需要的两行。 还要检查您的提供商是否使用相同的端口 - 可能不同。

使用fe sudo /home/user/vpn.sh运行此脚本。

如果你想在启动时加载它(iptables通常在重新启动后重置),添加到你的/etc/rc.local文件fe行,如bash /home/user/vpn.sh


下一部分是VPN自动连接器和监视器。 这是我自己的装置:

 #!/bin/bash # CONNECTIONS # Those values can be checked by running `nmcli con show` vpn=( 85e60352-9e93-4be4-8b80-f6aae28d3c94 ) # NUMBER OF CONNECTIONS total=${#vpn[@]} # SLEEP amount=10 # number of seconds to wait after each connection checking cycle countdown=true # enable/disable animated countdown skip=1 # how many seconds to substract between each animated countdown iteration # LOGS dir='/home/user/logs-vpn' # directory for storing logs name='vpn' # prefix/name for a log file seperate=true # create a seperate log file for each init session or log to single file init=false # log init event (with logging setup) start=false # log vpn start event yes=false # log connected events no=false # log disconnected events # STYLE clean='\e[1A\033[K' # clean & move to previous line default='\e[0m' # default blink='\e[5m' # blinking (works only in couple terminals, eg XTerm or tty) dim='\e[2m' # dim/half-bright disconnected='\e[91m' # light red connected='\e[92m' # light green count='\e[94m' # light blue reconnecting='\e[96m' # light cyan initializing='\e[93m' # light yellow connection='\e[1m\e[91m' # bold light red # SETUP time=$(date +"%Y-%m-%d_%H-%M-%S") if $separate; then file="$dir/$time.log" else file="$dir/$name.log" fi # RESET reset # reset screen tput civis -- invisible # disable cursor # RE-TIME time=$(date +"%Y.%m.%d %H:%M:%S") # INITIALIZATION if $init; then printf "$time INIT" >> $file if $yes; then printf " -y" >> $file fi if $no; then printf " -n" >> $file fi printf "\n" >> $file fi # START CONNECTION con=$(nmcli con show --active | grep " vpn") if [[ $con == '' ]]; then if $start; then printf "$time START\n" >> $file fi time=$(date +"%H:%M:%S") echo -e "${dim}[$time]${default} ${initializing}INITIALIZING...${default}" echo "" echo "" random=$(((RANDOM % $total)-1)) try=${vpn[$random]} (sleep 1s && nmcli con up uuid $try) >& /dev/null sleep 10s fi # LOOP while [ "true" ]; do time=$(date +"%H:%M:%S") # CLEAN AFTER COUNTDOWN if $countdown; then echo -en $clean echo -en $clean fi # CHECK CONNECTION con=$(nmcli con show --active | grep " vpn" | cut -f1 -d " ") if [[ $con == '' ]]; then if $no; then printf "$time NO\n" >> $file fi echo -e "${dim}[$time]${default} ${disconnected}DISCONNECTED !!${default}" echo -e "${blink}${reconnecting}re-connecting ...${default}" random=$(((RANDOM % $total)-1)) try=${vpn[$random]} (sleep 1s && nmcli con up uuid $try) >& /dev/null else if $yes; then printf "$time YES\n" >> $file fi arr=(${con//./ }) echo -en $clean echo -e "${dim}[$time]${default} ${connected}CONNECTED${default} (${connection}${arr[0]^^}${default})" fi # SLEEP if $countdown; then echo -e "${count}$amount${default}" for (( c=$amount; c>=1; c=c-$skip )); do echo -en $clean echo -e "${count}$c${default}" sleep $skip done echo -e "${count}0${default}" else sleep $amount fi done 

它将在启动时自动连接并以给定的间隔监视您的连接( amount=10给出10秒间隔)并重新连接连接丢失。 有记录function和一些其他选项。

检查您的连接UUID使用nmcli con show并将您的collections夹(与添加到防火墙的IP匹配)添加到vpn=()表。 每次它将随机选择此表中指定的连接。

您可以将其添加到自动启动(不需要sudo priviledge)。 以下是如何在终端中启动它的示例:

 mate-terminal --command="/home/user/vpn-reconnect.sh" 

......以及它在终端中的运行方式:

在此处输入图像描述

...以下是VPN连接断开后防漏ping的方式:

在此处输入图像描述

请享用 :)

我已经能够用UFW设置一个简单的VPN终止开关。 它适用于我拥有的所有vpn。

这是我的ufw设置:

 sudo ufw default deny outgoing sudo ufw default deny incoming` sudo ufw allow out 443/tcp sudo ufw allow out 1194/udp sudo ufw allow out on tun0 from any to any port 80 sudo ufw allow out on tun0 from any to any port 53 sudo ufw allow out on tun0 from any to any port 67 sudo ufw allow out on tun0 from any to any port 68 

适合我就好:)

我通过设置Ufw来阻止所有传出流量,然后通过引用其各自的IP地址将所有VPN节点列入白名单来解决了这个问题。 这并不像听起来那么繁重:根据我的经验,VPN允许使用DNS查找来获取各种IP地址。

我编写了一个PHP程序来执行此操作,称为ufw-vpn 。 我已经使用了几年,随着时间的推移做了各种小的改进。 你当然需要安装PHP,如果你想克隆它而不是下载它就需要Git。

你也可以用wget抓住它:

 cd /path/to/a/folder wget https://github.com/halfer/ufw-vpn/archive/master.zip unzip master.zip cd ufw-vpn-master 

然后运行命令检查它看起来没问题(没有参数只是呈现语法消息):

 php ufw-vpn.php 

现在,假设您的VPN支持它,您可以使用完全限定的域来获取区域的服务器列表(您需要在提供商的文档中找到它,或者可能从他们的支持部门找到):

 php ufw-vpn.php earth.all.vpn.example.org add 

这应该为您提供一个要添加的防火墙规则的大列表。 要轻松安装它们,您可以这样做:

 php ufw-vpn.php earth.all.vpn.example.org add > add-rules.sh chmod u+x add-rules.sh && sudo add-rules.sh 

VPN提供商会不时更新其IP地址,因此您需要更新您的IP地址才能匹配。 你可以通过差异来做到这一点:

 php ufw-vpn.php earth.all.vpn.example.org diff > diff-rules.sh chmod u+x diff-rules.sh && sudo diff-rules.sh 

对于差异,在执行之前值得检查规则,因为它将删除不属于VPN的任何内容。 因此,如果您有一些自定义规则,则需要在运行之前将其取出。

回购中提供了更多文档,它们都是开源的,因此您可以检查代码是否存在安全问题。 我们非常欢迎提供错误报告和function建议。