![VPNサーバーに接続中にiptablesルールを生成する](https://rvso.com/image/1072880/VPN%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%81%AB%E6%8E%A5%E7%B6%9A%E4%B8%AD%E3%81%ABiptables%E3%83%AB%E3%83%BC%E3%83%AB%E3%82%92%E7%94%9F%E6%88%90%E3%81%99%E3%82%8B.png)
私は、ルーターとして機能し、VPN (expressVPN) を介してローカル ネットワークの一部のデバイス (AppleTV など) をトンネルする仮想 Ubuntu サーバー (Proxmox 上の LXC) を構築しています。異なる VPN の宛先 (異なる国) への接続とルーティングは機能しています。
しかし、一部のサービスでは、VPNサーバーが設置されている国から電話をかけていないことを認識しています。(DNSリーク???)
これは、私のクライアント、たとえば AppleTV ボックスが DNS として 1.1.1.1 を使用しているためです。VPN サーバーがゲートウェイに公開した DNS サーバーを認識しません。
それを防ぐために、接続時に受け取る VPN の DNS サービスを使用したいと思います。AppleTV を DNS としてゲートウェイに指定し、VPN サーバーが提供する DNS サーバーへのリクエストを DNAT するための iptables ルールをいくつか記述しました。(意味が通じるといいのですが)
# Route DNS Traffic
iptables -t nat -A PREROUTING -p tcp --dst x.x.x.x --dport 53 -j DNAT --to-destination 10.54.0.1:53
iptables -t nat -A PREROUTING -p udp --dst x.x.x.x --dport 53 -j DNAT --to-destination 10.54.0.1:53
それはうまくいきます。
質問
OpenVPN 接続を確立するときにこれらのルールを自動的に生成するにはどうすればよいですか? 別の VPN ステーション (別の国など) に接続すると、特定の DNS アドレスが変更されます。または、より良い解決策はありますか?
に入れることを考えていましたupdate-systemd-resolved
が、それはあまりにも複雑すぎるようです。簡単な方法があると思います。
どのようなヒントでもいただければ幸いです。
答え1
ようこそ。openvpn *.conf ファイルで コマンドを使用できます。up
これdown
により、カスタム スクリプトを実行できます。
script-security 2
この Road Example を実行する場合は必須です。
script-security 2
up /usr/local/bin/set-redirect-dns.sh
down /usr/local/bin/unset-redirect-dns.sh
実際のコマンドはこれらのスクリプト内にあります。問題がないiptables
場合は、man ページで確認してください。script-security 2
答え2
解決策を示すために、私自身の質問に答えます。
解決済みはスタブ (127.0.0.53) 経由で DNS を使用するため、VPN の DNS の使用方法がわかることがわかりました。
このサーバーをゲートウェイ (および DNS) として使用している LAN クライアントからの DNS 要求を転送/NAT するだけで済みました。
openvpn クライアント構成に次の行を追加しました:
mci@vpngateway:/etc/openvpn$ head destinations/belgium_udp.ovpn -n4
script-security 2
up /etc/openvpn/linkup.sh
down /etc/openvpn/linkdown.sh
down-pre
私のスクリプトはアップされています...
mci@vpngateway:/etc/openvpn$ cat linkup.sh
#!/bin/bash
# First let systemd-resolved do its magic
/etc/openvpn/update-systemd-resolved $@
# Set iptable rules
/etc/openvpn/iptables_baserules.sh
# Set DNS
/etc/openvpn/iptables_setdns.sh
そして下
mci@vpngateway:/etc/openvpn$ cat linkdown.sh
#!/bin/bash
# First delete DNS iptables rules
/etc/openvpn/iptables_removedns.sh
# After let systemd-resolved do its magic
/etc/openvpn/update-systemd-resolved $@
主に他のものを落とすための基本的なルール
mci@vpngateway:/etc/openvpn$ cat iptables_baserules.sh
#!/bin/bash
# Flush
iptables -t nat -F
iptables -t mangle -F
iptables -F
iptables -X
# Block All
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
# allow Localhost
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
# Make sure you can communicate with any DHCP server
iptables -A OUTPUT -d 255.255.255.255 -j ACCEPT
iptables -A INPUT -s 255.255.255.255 -j ACCEPT
# Make sure that you can communicate within your own network
iptables -A INPUT -s x.x.x.0/24 -d x.x.x.0/24 -j ACCEPT
iptables -A OUTPUT -s x.x.x.0/24 -d x.x.x.0/24 -j ACCEPT
# Allow established sessions to receive traffic:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow TUN
iptables -A INPUT -i tun0 -j ACCEPT
iptables -A FORWARD -i tun0 -j ACCEPT
iptables -A FORWARD -o tun0 -j ACCEPT
iptables -t nat -A POSTROUTING -o tun0 -j MASQUERADE
iptables -A OUTPUT -o tun0 -j ACCEPT
# allow VPN connection
iptables -I OUTPUT 1 -p udp --destination-port 1195 -m comment --comment "Allow VPN connection" -j ACCEPT
iptables -I OUTPUT 1 -p tcp --destination-port 1195 -m comment --comment "Allow VPN connection" -j ACCEPT
# Block All
iptables -A OUTPUT -j DROP
iptables -A INPUT -j DROP
iptables -A FORWARD -j DROP
これらのルールに注意してください。VPN 接続が閉じられた後、ノード自体が名前を検索できるようにするために、これらのルールを追加する必要がありました (おそらく、VPN 構成を別のサーバーに切り替えたいためです)。
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A OUTPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
最後にDNS NATを動的に追加します
mci@vpngateway:/etc/openvpn$ cat iptables_setdns.sh
#!/bin/bash
# Route DNS Traffic
# Get DNS from VPN
dns=$(resolvectl dns -i tun0)
dns=${dns##* }
echo "Found DNS on tun0: ${dns}"
iptables -t nat -A PREROUTING -p tcp --dst x.x.x.x --dport 53 -j DNAT --to-destination ${dns}:53
iptables -t nat -A PREROUTING -p udp --dst x.x.x.x --dport 53 -j DNAT --to-destination ${dns}:53
そして後で削除する
mci@vpngateway:/etc/openvpn$ cat iptables_removedns.sh
#!/bin/bash
# Route DNS Traffic
# Get DNS from VPN
dns=$(resolvectl dns -i tun0)
dns=${dns##* }
echo "Found DNS on tun0: ${dns}"
iptables -t nat -D PREROUTING -p tcp --dst x.x.x.x --dport 53 -j DNAT --to-destination ${dns}:53
iptables -t nat -D PREROUTING -p udp --dst x.x.x.x --dport 53 -j DNAT --to-destination ${dns}:53
おそらくこれは、VPN ゲートウェイを構築している他の誰かの役に立つでしょう。