NAT/마스커레이딩을 사용한 UFW 규칙

NAT/마스커레이딩을 사용한 UFW 규칙

저는 개인 네트워크의 컴퓨터를 인터넷에 연결할 수 있는 컴퓨터로 제한하여 Ubuntu를 일종의 라우터로 사용하려고 합니다.

Ubuntu 상자에는 두 개의 NIC가 있습니다. 하나는 인터넷 연결(enp0s3)이고 다른 하나는 이 단일 개인 PC(enp0s8)에 연결됩니다.

우선 우분투를 통해 개인 상자에서 DNS를 허용할 수 있는지 확인하고 싶었습니다. 나는 다음과 같은 "경로 허용" 규칙을 추가하기 위해 몇 가지 지침을 따랐습니다.

ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), deny (outgoing), allow (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
8.8.8.8 on enp0s3          ALLOW FWD   Anywhere on enp0s8        
8.8.4.4 on enp0s3          ALLOW FWD   Anywhere on enp0s8        
10.0.1.5 on enp0s8         ALLOW FWD   Anywhere on enp0s3   

10.0.1.5는 내 개인 PC입니다. Wireshark를 사용하면 enp0s3과 enp0s8 모두에서 DNS 요청이 이루어지는 것을 볼 수 있지만 enp0s3의 요청에는 응답이 없습니다.

그래서 조금 더 읽은 후에 NAT 및 매스커레이딩을 설정해야 한다는 것을 알았고, 그래서 rule.before에 다음을 넣었습니다.

# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]

# Forward traffic through eth0 - Change to match you out-interface
-A POSTROUTING -s 10.0.1.0/24 -o enp0s3 -j MASQUERADE

# don't delete the 'COMMIT' line or these nat table rules won't
# be processed
COMMIT

이것은 효과가 있었지만 너무 좋았습니다. 이제 모든 트래픽이 나가고 내 개인 PC로 반환되며 기본 발신 거부 및 기타 UFW 규칙이 모두 우회되는 것 같습니다.

그래서 내 질문은 간단합니다. 개인 PC가 제공된 NAT와 같은 Ubuntu를 통해 연결되기를 원하지만 처음에 ufw 명령줄을 사용하여 구성했다고 생각했던 나가는 제한 사항이 있습니다. NAT가 작업을 수행한 후에 어떻게든 UFW 규칙을 적용할 수 있는 방법이 있나요?

티아.

다음은 전체 before.rules 파일입니다. 유일한 변경 사항은 NAT 테이블 규칙이었습니다.

#
# rules.before
#
# Rules that should be run before the ufw command line added rules. Custom
# rules should be added to one of these chains:
#   ufw-before-input
#   ufw-before-output
#   ufw-before-forward
#

# NAT table rules
*nat
:POSTROUTING ACCEPT [0:0]

# Forward traffic through eth0 - Change to match you out-interface
-A POSTROUTING -s 10.0.1.0/24 -o enp0s3 -j MASQUERADE

# don't delete the 'COMMIT' line or these nat table rules won't
# be processed
COMMIT


# Don't delete these required lines, otherwise there will be errors
*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]
# End required lines


# allow all on loopback
-A ufw-before-input -i lo -j ACCEPT
-A ufw-before-output -o lo -j ACCEPT

# quickly process packets for which we already have a connection
-A ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-output -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-forward -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT

# drop INVALID packets (logs these in loglevel medium and higher)
-A ufw-before-input -m conntrack --ctstate INVALID -j ufw-logging-deny
-A ufw-before-input -m conntrack --ctstate INVALID -j DROP

# ok icmp codes for INPUT
-A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

# ok icmp code for FORWARD
-A ufw-before-forward -p icmp --icmp-type destination-unreachable -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type time-exceeded -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type parameter-problem -j ACCEPT
-A ufw-before-forward -p icmp --icmp-type echo-request -j ACCEPT

# allow dhcp client to work
-A ufw-before-input -p udp --sport 67 --dport 68 -j ACCEPT

#
# ufw-not-local
#
-A ufw-before-input -j ufw-not-local

# if LOCAL, RETURN
-A ufw-not-local -m addrtype --dst-type LOCAL -j RETURN

# if MULTICAST, RETURN
-A ufw-not-local -m addrtype --dst-type MULTICAST -j RETURN

# if BROADCAST, RETURN
-A ufw-not-local -m addrtype --dst-type BROADCAST -j RETURN

# all other non-local packets are dropped
-A ufw-not-local -m limit --limit 3/min --limit-burst 10 -j ufw-logging-deny
-A ufw-not-local -j DROP

# allow MULTICAST mDNS for service discovery (be sure the MULTICAST line above
# is uncommented)
-A ufw-before-input -p udp -d 224.0.0.251 --dport 5353 -j ACCEPT

# allow MULTICAST UPnP for service discovery (be sure the MULTICAST line above
# is uncommented)
-A ufw-before-input -p udp -d 239.255.255.250 --dport 1900 -j ACCEPT

# don't delete the 'COMMIT' line or these rules won't be processed
COMMIT

답변1

라우팅/전달 규칙이 다음과 같은 것 같습니다.완전히 분리되어 있다일반 방화벽 규칙에 따릅니다. NAT 항목을 after.rules로 옮기고 "라우팅된" 트래픽에 대한 기본 규칙을 "거부"로 변경했습니다. POSTROUTING이 발생하는 것 같습니다.~ 후에기본 거부 규칙이 적용됩니다. 따라서 기본 규칙에 따라 항목이 거부되면 NAT로 라우팅되지 않습니다. 물론 "ufw 경로 허용 ..." 종류의 사용자 규칙을 추가할 수 있으며 기본 거부가 적용되기 전에 트래픽을 허용할 수 있으며 그런 다음 예상대로 라우팅됩니다.

관련 정보