iptables 규칙 - nat 테이블을 사용하여 서비스 전달/가장

iptables 규칙 - nat 테이블을 사용하여 서비스 전달/가장

iptables다음과 같이 구성된 네트워크(Docker 컨테이너)에서 연습을 완료해야 합니다 .

  1. router2개의 네트워크 인터페이스( eth0 <- public 10.9.0.0/24; eth1 <- lan 192.168.60.0/24; 두 네트워크 모두에 router있음 )가 있는 호스트 x.x.x.254와 기본 포트 22의 SSH 서버
  2. IP가 있고 기본 포트 22와 23에 SSH와 TELNET이 jumpbox있는 호스트 ;lan192.168.60.10
  3. IP가 admin있는 호스트 .public10.9.0.2

우리에게 보낸 요청 중 하나는 SSH(포트 2222) 및 TELNET 서비스를 호스트로 전달/가장하는 것 jumbox입니다 . 문제는 이러한 포트를 리디렉션할 수 없고 연결이 영구적으로 보류된다는 것입니다(반환 패킷이 올바르게 구성되지 않았기 때문인 것 같습니다). 현재 내 구성은 다음과 같습니다(참고자료 참조 ).routeradmin

ADMIN'S RULES

#!/bin/bash

# Reset IPv4 rules
iptables -t filter -F
iptables -t filter -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X

# Set default policies for chains
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# Allow loopback traffic
iptables -A INPUT -i lo -j ACCEPT

# Allow traffic for ongoing connections
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT


# ====================== #
#  NAT INTERNAL NETWORK  #
# ====================== #

# Masquerade internal network hosts
iptables -t nat -A POSTROUTING -s 192.168.60.0/24 -o eth0 -j MASQUERADE

# Forward internal network requests to external network
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT


# =============== #
#  ADMIN'S RULES  #
# =============== #

# Allow 'admin' connection to 'jumpbox' SSH (on 2222 port)
iptables -t nat -A POSTROUTING -s 192.168.60.10 -p tcp --sport 22   -j SNAT --to 10.9.0.254:2222
iptables -t nat -A PREROUTING  -s 10.9.0.2      -p tcp --dport 2222 -j DNAT --to 192.168.60.10:22
iptables        -A FORWARD     -s 10.9.0.2      -p tcp --dport 22   -j ACCEPT

# Allow 'admin' connection to 'jumpbox' TELNET
iptables -t nat -A POSTROUTING -s 192.168.60.10 -p tcp --sport 23 -j SNAT --to 10.9.0.254
iptables -t nat -A PREROUTING  -s 10.9.0.2      -p tcp --dport 23 -j DNAT --to 192.168.60.10
iptables        -A FORWARD     -s 10.9.0.2      -p tcp --dport 23 -j ACCEPT

누군가 나에게 해결책을 알려주실 수 있나요?
미리 감사드립니다.


편집하다:

여러 번 시도한 끝에 다음 명령을 사용하여 의도에 성공했습니다.

# Allow 'admin' connection to 'jumpbox' SSH (on 2222 port)
iptables -t nat -A PREROUTING  -s 10.9.0.2 -d 10.9.0.254    -p tcp --dport 2222 -j DNAT   --to 192.168.60.10:22
iptables        -A FORWARD     -s 10.9.0.2 -d 192.168.60.10 -p tcp --dport 22   -j ACCEPT
iptables -t nat -A POSTROUTING             -d 192.168.60.10 -p tcp --dport 22   -j SNAT   --to 192.168.60.254

# Allow 'admin' connection to 'jumpbox' TELNET
iptables -t nat -A PREROUTING  -s 10.9.0.2 -d 10.9.0.254    -p tcp --dport 23 -j DNAT   --to 192.168.60.10
iptables        -A FORWARD     -s 10.9.0.2 -d 192.168.60.10 -p tcp --dport 23 -j ACCEPT
iptables -t nat -A POSTROUTING             -d 192.168.60.10 -p tcp --dport 23 -j SNAT   --to 192.168.60.254

이제 내부 IP를 사용하여 연결을 시도하면 문제가 발생합니다.

$ telnet 192.168.60.10 23  # or
$ ssh -p2222 192.168.60.10

나는 이것을 할 수 있지만,이 행동을 차단해야 해요.

답변1

나는 다음 규칙을 통해 목표를 달성할 수 있었습니다.

# ...

# Allow 'admin' connection to 'jumpbox' SSH (on 2222 port)
iptables -t nat -A PREROUTING  -s 10.9.0.2 -d 10.9.0.254                                -p tcp --dport 2222 -j DNAT   --to 192.168.60.10:22
iptables        -A FORWARD     -s 10.9.0.2 -d 192.168.60.10 -m conntrack --ctstate DNAT -p tcp --dport 22   -j ACCEPT
iptables -t nat -A POSTROUTING             -d 192.168.60.10                             -p tcp --dport 22   -j SNAT   --to 192.168.60.254

# Allow 'admin' connection to 'jumpbox' TELNET
iptables -t nat -A PREROUTING  -s 10.9.0.2 -d 10.9.0.254                                -p tcp --dport 23 -j DNAT   --to 192.168.60.10
iptables        -A FORWARD     -s 10.9.0.2 -d 192.168.60.10 -m conntrack --ctstate DNAT -p tcp --dport 23 -j ACCEPT
iptables -t nat -A POSTROUTING             -d 192.168.60.10                             -p tcp --dport 23 -j SNAT   --to 192.168.60.254

관련 정보