使用 UFW 和 NAT 表的更好的 VPN killswitch?
客觀的
給定 Raspberry Pi 上的以下介面:
- eth0 (192.168.0.0/24) - 專用網路(即 NAT)
- wlan0 (192.168.10.0/24) - 具有 Internet 訪問權限的公共網路(即 LAN)
- tun0 (VPN) - VPN 連接
建構實現以下目標的防火牆:
拒絕所有入站流量:
wlan0
,tun0
(阻止傳入連接)路由所有出站流量從
eth0
到tun0
(阻止“橫向”連接;即沒有 LAN 訪問)
- 如果
tun0
關閉,則不允許eth0
使用wlan0
(即 VPN killswitch)主流範例
在這一點上,我已經看到了許多使用 UFW 的“VPN killswitch”範例,它們都有一個共同的配置:
# Defaults ufw default deny outgoing ufw default deny incoming # Allow local over ethernet (without VPN) sudo ufw allow out to 192.168.0.0/24 # Allow out to LAN sudo ufw allow in to 192.168.0.0/24 # Allow in to LAN # Allow outgoing over ethernet to VPN sudo ufw allow out to [VPN] port 1194 proto udp # Allow outgoing over tun0 sudo ufw allow out on tun0 # Allow out over VPN
資料來源:
- https://forums.whirlpool.net.au/archive/2561726
- https://www.reddit.com/r/VPN/comments/4ik46m/vpn_killswitch_setup_using_ufw/
- https://www.youtube.com/watch?v=x9fb2UAWkBE
- https://github.com/andreisid/bash/blob/master/killswitch.sh
我的 NAT 範例(替代)
當然,在我的應用程序中,我有一個中間設備(Raspberry Pi)在執行,它充當路由器、防火牆、DNS 和 DHCP 伺服器以及 VPN 客戶端,所以它的設置略有不同。但是,似乎 NAT 表 (
/etc/ufw/before.rules
) 處理ufw allow out/in to 192.168.0.0/24
語句並且幾乎將所有來自eth0
到tun0
(第二個目標項)的出站流量按原樣路由:# NAT table to "forward" private network to VPN tunnel *nat :POSTROUTING ACCEPT [0:0] -A POSTROUTING -s 192.168.0.0/24 -o tun0 -j MASQUERADE COMMIT
這應該將來自專用網路 (
eth0
) 的任何內容“轉發”到 VPN 隧道 (tun0
) 中,對嗎?還是我也需要FORWARD
這裡的條款?在那之後,我現在不確定是否還有其他需要配置的東西,例如:
# Set defaults (also see /etc/default/ufw) sudo ufw default deny incoming sudo ufw default deny outgoing # Allow incoming requests to DNS/DHCP services (UDP) on eth0 interface only (i.e. Private Network -> Pi:43,67/udp) sudo ufw allow in on eth0 from any to any port 53,67 proto udp # Allow incoming requests to SSH service (TCP) on eth0 interface only (i.e. Private Network -> Pi:22/tcp) sudo ufw allow in on eth0 from any to any port 22 proto tcp # Allow outbound on wlan0 interface for VPN traffic only (i.e. Pi -> LAN:1194) sudo ufw allow out on wlan0 from any to any port 1194 proto udp # Allow all outbound traffic on eth0 (i.e. Pi -> Private Network) sudo ufw allow out on eth0 # Allow all outbound traffic on VPN tunnel only (i.e. Pi -> VPN) sudo ufw allow out on tun0
在我單獨使用 NAT 表完成的測試(
traceroute
、ping
等)中,當我斷開 VPN 時,我可以看到我的出站 Internet 連接在 Raspberry Pi 處停止。但是,我仍在嘗試確認的是這是否涵蓋了所有可能的洩漏情況(即 DNS 等)。注意:我也在使用
dnsmasq
,所以樹莓派也是 DHCP 伺服器發給私網客戶端的 DNS 伺服器。我想知道我需要做的只是配置僅dnsmasq
轉發 DNS 查詢tun0
(如果可能的話),或者轉而選擇轉發到公共 DNS(即 8.8.8.8、8.8.4.4)。此外,截至目前,此設置仍然阻止我的出站連接,但發出sudo ufw allow out on wlan0
恢復該連接(VPN killswitch 仍然有效)。所以我覺得我已經很接近了,也許還有一些規則。非常感謝有人花時間查看這些細節並提供回饋!
所以我想我會留下這個答案,因為我相信我可能剛剛找到了失去的部分(感謝
/var/log/ufw.log
),除非其他人另有看法:# Allow DNS queries # [UFW BLOCK] IN= OUT=wlan0 SRC=192.168.10.x DST=192.168.10.1 LEN=66 TOS=0x00 PREC=0x00 TTL=64 ID=50892 DF PROTO=UDP SPT=22617 DPT=53 LEN=46 sudo ufw allow out on wlan0 from any to any port 53 proto udp
所以我目前的規則集現在看起來像這樣(注意預設傳出):
Status: active Logging: on (low) Default: deny (incoming), deny (outgoing) New profiles: skip To Action From -- ------ ---- 53,67/udp on eth0 ALLOW IN Anywhere 22/tcp on eth0 ALLOW IN Anywhere 1194/udp ALLOW OUT Anywhere on wlan0 Anywhere ALLOW OUT Anywhere on eth0 Anywhere ALLOW OUT Anywhere on tun0 53/udp ALLOW OUT Anywhere on wlan0
命令:
# Allow incoming requests to DNS/DHCP services (UDP) on eth0 interface only (i.e. Private Network -> Pi:43,67/udp) sudo ufw allow in on eth0 from any to any port 53,67 proto udp # Allow incoming requests to SSH service (TCP) on eth0 interface only (i.e. Private Network -> Pi:22/tcp) sudo ufw allow in on eth0 from any to any port 22 proto tcp # Allow outbound on wlan0 interface for DNS and VPN traffic only (i.e. Pi -> LAN:1194) sudo ufw allow out on wlan0 from any to any port 53,1194 proto udp # Allow all outbound traffic on eth0 (i.e. Pi -> Private Network) sudo ufw allow out on eth0 # Allow all outbound traffic on VPN tunnel only (i.e. Pi -> VPN) sudo ufw allow out on tun0 # Set defaults (also see /etc/default/ufw) sudo ufw default deny incoming sudo ufw default deny outgoing
這也與我
/etc/ufw/before.rules
在原始文章中提到的用於處理eth0 -> tun0
“路由”的 NAT 表條目相結合。最後,my
/etc/dnsmasq.conf
包含以下單個server
條目:# Force VPN by selecting public DNS server=8.8.8.8 # Do not read from /etc/resolv.conf and friends for system DNS no-resolv # Do not poll /etc/resolv.conf and friends for system DNS no-poll
確認發送到 8.8.8.8的
traceroute
請求是通過 VPN 並通過配置(隱含 DHCP),客戶端將預設為其 DNS 使用 Pi,而 Pi 又使用此配置。這是一個包裝!