![VPN 서버에 연결하는 동안 iptables 규칙 생성](https://rvso.com/image/1072880/VPN%20%EC%84%9C%EB%B2%84%EC%97%90%20%EC%97%B0%EA%B2%B0%ED%95%98%EB%8A%94%20%EB%8F%99%EC%95%88%20iptables%20%EA%B7%9C%EC%B9%99%20%EC%83%9D%EC%84%B1.png)
저는 라우터 역할을 하고 VPN(expressVPN)을 통해 로컬 네트워크의 일부 장치(예: AppleTV)를 터널링하는 가상 Ubuntu 서버(Proxmox의 LXC)를 구축하고 있습니다. 다른 VPN 대상(다른 국가)으로의 연결 및 라우팅이 작동 중입니다.
하지만 일부 서비스에서는 여전히 VPN 서버가 있는 국가에서 전화를 걸지 않는다는 것을 알고 있습니다.DNS 유출???)
그것은 내 클라이언트이기 때문입니다. AppleTV 상자가 예를 들어 1.1.1.1을 DNS로 사용한다고 가정해 보겠습니다. 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
script-security 2
up /usr/local/bin/set-redirect-dns.sh
down /usr/local/bin/unset-redirect-dns.sh
실제 iptables
명령은 이 스크립트에 있습니다. script-security 2
괜찮은지 맨 페이지를 확인하십시오 .
답변2
내 솔루션을 보여주기 위해 내 질문에 답하겠습니다.
나는 해결이 스텁(127.0.0.53)을 통해 DNS를 사용한다는 것을 알게 되었고 따라서 VPN의 DNS를 사용하는 방법을 알고 있습니다.
저는 이 서버를 게이트웨이(및 DNS)로 사용하는 LAN 클라이언트의 DNS 요청을 "그냥" 전달/접속해야 했습니다.
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 게이트웨이를 구축하는 다른 누군가에게 도움이 될 것입니다.