Oracle 무료 계층: Wireguard 및 iptables

Oracle 무료 계층: Wireguard 및 iptables

문제: 트래픽은 "클라이언트"에서 "서버"로 갔다가 다시 돌아올 수 없습니다.

구성:

"섬기는 사람":

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = [redacted]

PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens3 -j MASQUERADE

[Peer]
PublicKey = [redacted]
AllowedIPs = 10.8.0.2/32

"고객":

[Interface]
Address = 10.8.0.2/24
PrivateKey = [redacted]
PostUp = ping -c1 10.8.0.1
DNS = 1.1.1.1

[Peer]
PublicKey = [redacted]
Endpoint = [redacted]:51820
AllowedIPs = 0.0.0.0/0, ::/0

/etc/iptables/rules.v4:

# CLOUD_IMG: This file was created/modified by the Cloud Image build process
# iptables configuration for Oracle Cloud Infrastructure
    
# See the Oracle-Provided Images section in the Oracle Cloud Infrastructure
# documentation for security impact of modifying or removing these rule
    
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [463:49013] 
:InstanceServices - [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p udp --sport 123 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 64738 -j ACCEPT
-A INPUT -p udp -m state --state NEW -m udp --dport 64738 -j ACCEPT
    
# I added the following rule manually. The preceding 5 rules and were either
# added by Oracle (22) or dockers (the other ports). All else is added by Oracle. 
-A INPUT -p udp -m state --state NEW -m udp --dport 51820 -j ACCEPT
#
    
# Commenting out the following two lines makes everything work, but defeats 
# the point of iptables.
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
#
    
-A OUTPUT -d 169.254.0.0/16 -j InstanceServices
-A InstanceServices -d 169.254.0.2/32 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.2.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.4.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.5.0/24 -p tcp -m owner --uid-owner 0 -m tcp --dport 3260 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.2/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 53 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p tcp -m tcp --dport 53 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.3/32 -p tcp -m owner --uid-owner 0 -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.4/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p tcp -m tcp --dport 80 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 67 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp -m udp --dport 69 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.169.254/32 -p udp --dport 123 -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j ACCEPT
-A InstanceServices -d 169.254.0.0/16 -p tcp -m tcp -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j REJECT --reject-with tcp-reset
-A InstanceServices -d 169.254.0.0/16 -p udp -m udp -m comment --comment "See the Oracle-Provided Images section in the Oracle Cloud Infrastructure documentation for security impact of modifying or removing this rule" -j REJECT --reject-with icmp-port-unreachable
COMMIT

논평:

두 피어 사이에 와이어가드 연결과 핑을 설정할 수 있지만 "클라이언트" 피어에서 "서버" 피어로, 그리고 인터넷을 통해 이동할 수 없습니다.

"고객:

$ sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
[#] wg setconf wg0 /dev/fd/63
[#] ip -4 address add 10.8.0.2/24 dev wg0
[#] ip link set mtu 1420 up dev wg0
[#] resolvconf -a wg0 -m 0 -x
[#] wg set wg0 fwmark 51820
[#] ip -6 route add ::/0 dev wg0 table 51820
[#] ip -6 rule add not fwmark 51820 table 51820
[#] ip -6 rule add table main suppress_prefixlength 0
[#] ip6tables-restore -n
[#] ip -4 route add 0.0.0.0/0 dev wg0 table 51820
[#] ip -4 rule add not fwmark 51820 table 51820
[#] ip -4 rule add table main suppress_prefixlength 0
[#] sysctl -q net.ipv4.conf.all.src_valid_mark=1
[#] iptables-restore -n
[#] ping -c1 10.8.0.1
PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=169 ms

--- 10.8.0.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 168.716/168.716/168.716/0.000 ms

$ curl -4 ifconfig.me
curl: (6) Could not resolve host: ifconfig.me

"클라이언트"/etc/resolv.conf:

# Generated by resolvconf
nameserver 1.1.1.1

"서버"/etc/resolv.conf:

nameserver 127.0.0.53
options edns0 trust-ad
search vcn[redacted].oraclevcn.com

위의 설명에서 언급했듯이 /etc/iptables/rules.v4의 INPUT REJECT 규칙을 주석 처리하면 문제가 해결되지만 올바르게 이해했다면 방화벽 보안 관점에서는 권장되지 않습니다.

내 기억이 맞다면 컬 오류는 DNS 문제가 있음을 시사했습니다.

Oracle Cloud 구성에는 다음과 같은 입력 규칙이 있습니다.

참가 규칙

다음과 같은 내보내기 규칙이 있습니다.

수출 규칙

저는 iptables, 네트워킹 및 sysadmin을 처음 접했기 때문에 한동안 이 문제로 어려움을 겪었습니다. 나는 여기서 정확히 무엇을 놓치고 있는지를 밝혀주는 인터넷에서 많은 것을 찾지 못했습니다. 단지 나를 이 지점에 이르게 한 단편적인 부분일 뿐입니다.

문제의 범위를 iptables 구성으로 좁힌 것 같은데 어떻게 해야 할지 모르겠습니다.

정확히 문제가 무엇인지, 그리고 어떤 좋은(그리고 안전한) 해결책이 무엇인지에 대한 조언을 주시면 대단히 감사하겠습니다!

감사해요!

답변1

이 두 개의 iptables 규칙을 삽입하세요.앞으로-A FORWARD -j reject서버 파일의 규칙 /etc/iptables/rules.v4:

-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wg0 -j ACCEPT

실행하면 sudo iptables-save활성 iptables 규칙 목록이 표시됩니다. PostUp서버 wg 구성 파일의 스크립트에 iptables 규칙을 첨부했습니다.뒤쪽에에 설정된 규칙입니다 /etc/iptables/rules.vp4. wg 스크립트 의 플래그 -I대신 iptables 플래그를 사용하여 이를 변경할 수 있습니다 . 이 플래그는 체인의 상단(또는 지정된 인덱스, 기본값은 0)에 규칙을 삽입하는 반면, 이 플래그는 규칙을 추가합니다. 체인 상단 끝까지. 맨 아래.-APostUp-I-A

답변2

Wireguard 구성에 대한 많은(대부분?) 가이드에서는 "서버" wg0.conf 파일에 PostUp/PostDown NAT 규칙 이외의 방화벽 규칙을 추가하는 것에 대해 많이 언급하지 않습니다.

내 경우(Oracle 무료 계층)에는 wg 포트에 대한 INPUT 체인에 하나의 규칙이 필요하고 wg 포트의 패킷이 "서버"를 통해 대상 인터넷으로 이동할 수 있도록 FORWARD 체인에 두 개의 규칙이 필요합니다.

다음은 제가 수동으로 추가한 규칙입니다 /etc/iptables/rules.v4.

-A INPUT -p udp -m state --state NEW -m udp --dport 51820 -j ACCEPT
-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -i wg0 -j ACCEPT

이러한 규칙은 INPUT 및 FORWARD 체인의 REJECT 규칙보다 앞에 있어야 합니다. 그렇지 않으면 무시됩니다.

또한 wg 포트에 대한 Oracle 대시보드에서 수신 규칙을 설정해야 합니다.

이러한 모든 규칙이 적용되면 패킷이 wg 터널을 통해 "클라이언트" 피어에서 "서버" 피어로, 그리고 인터넷으로 이동하는 경로가 열립니다.

관련 정보