iptables FORWARD 連接埠到內部伺服器

iptables FORWARD 連接埠到內部伺服器

我一直在努力解決一項FORWARD無法按照我希望的方式工作的策略,我無法弄清楚是什麼導致了會話失敗。我希望有人能幫忙。

我的目標是將連接埠 3000 從 eth0 轉送到內部 Windows 機器連接埠 3389 從 eth2,這樣我就可以透過 RDP 進入 Windows 機器。下面的配置腳本僅適用於iptables -P FORWARD ACCEPT,但我不希望這樣。我感覺更好iptables -P FORWARD DROP,但這對我來說不起作用。

這是我一直在使用的腳本的最後一個版本。請注意,這是包含我所有評論內容的完整腳本,因此您可以檢查此處發生的所有內容,但請記住這是FORWARD我需要幫助的部分。


echo "Flush firewall and setting default chain policies..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
#iptables --append FORWARD -p tcp --dport 3000 -d 192.168.1.2 -j ACCEPT

echo "Enabling firewall with new ruleset..."
# allow ssh to eth0
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# allow http-https traffic
iptables -A INPUT -i eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 443 -m state --state ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT

# allow ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -t raw -A OUTPUT -p icmp -j TRACE

# full loopback access
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# minimize http-https DOS attack by limiting burst connections
# may need to adjust this later once web traffic is coming in
iptables -A INPUT -p tcp --dport 80 -m limit --limit 10/minute --limit-burst 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m limit --limit 10/minute --limit-burst 20 -j ACCEPT

# all rdp 3000 to jump box
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 3000 -j LOG --log-prefix "iptables-prerouting: " --log-level 7
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 3000 -j DNAT --to 192.168.1.2:3389
#iptables -A FORWARD -i eth0 -p tcp --dport 3389 -d 192.168.1.2 -j ACCEPT
#iptables -t raw -A PREROUTING -p tcp --dport 3389 -j TRACE
#iptables -t raw -A OUTPUT -p tcp --dport 3389 -j TRACE
iptables -A FORWARD -t filter -o eth0 -m state --state NEW,ESTABLISHED -d 192.168.1.2 -p tcp --dport 3389 -j LOG --log-prefix "iptables-forward: " --log-level 7
iptables -A FORWARD -t filter -o eth0 -m state --state NEW,ESTABLISHED -d 192.168.1.2 -p tcp --dport 3389 -j ACCEPT

# nat internal network to public interface
iptables -A POSTROUTING -o eth0 -t nat -j LOG --log-prefix "iptables-postrouting: " --log-level 7
iptables -A POSTROUTING -o eth0 -t nat -j MASQUERADE

# logging
#iptables -A INPUT -m limit --limit 2/min -j LOG --log-prefix "iptables-input: " --log-level 7
iptables -A FORWARD -j LOG --log-prefix "iptables-forward: " --log-level 7
#iptables -A OUTPUT -j LOG --log-prefix "iptables-output: " --log-level 7

# enable ipv4 forwardning for the system
echo 1 > /proc/sys/net/ipv4/ip_forward

# add route for private network internet access
ip route add 192.168.1.0/24 proto kernel scope link dev eth2

#List iptables ruleset
echo "-----------------------------------------------------------------------------"
echo "Listing iptables..."
iptables -L

echo "-----------------------------------------------------------------------------"
echo "Listing ip routes..."
ip route


我已追蹤連線、啟用日誌記錄規則等,但無法根據提供的資訊確定問題所在。我可以發現預設操作之間痕跡的差異iptables -P FORWARD,但仍然無法大海撈針。

如果我能得到任何指點,我將不勝感激。謝謝!

感謝@HaukeLaging,我的工作正常。這是最終的腳本。



echo "Flush firewall and setting default chain policies..."
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP
iptables --append INPUT -t filter -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables --append FORWARD -t filter -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables --append OUTPUT -t filter -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

echo "Enabling firewall with new ruleset..."
# allow ssh to eth0
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

# allow http-https traffic
iptables -A OUTPUT -o eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
# ----from private network
iptables -A FORWARD -i eth2 -o eth0 -p tcp --dport 80 -j ACCEPT
iptables -A FORWARD -i eth2 -o eth0 -p tcp --dport 443 -j ACCEPT

# allow ping
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-reply -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-request -j ACCEPT
iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
# ----from private network
iptables -A FORWARD -i eth2 -o eth0 -p icmp --icmp-type echo-request -j ACCEPT

# full loopback access
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# minimize http-https DOS attack by limiting burst connections
# may need to adjust this later once web traffic is coming in
iptables -A INPUT -p tcp --dport 80 -m limit --limit 10/minute --limit-burst 20 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m limit --limit 10/minute --limit-burst 20 -j ACCEPT

# rdp 3000 to jump box
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 3000 -j DNAT --to 192.168.1.2:3389
iptables -A FORWARD -i eth0 -p tcp --dport 3389 -d 192.168.1.2 -j ACCEPT

# nat on public interface
iptables -A POSTROUTING -o eth0 -t nat -j MASQUERADE

# logging
#iptables -A INPUT -m limit --limit 2/min -j LOG --log-prefix "iptables-input: " --log-level 7
#iptables -A FORWARD -j LOG --log-prefix "iptables-forward: " --log-level 7
#iptables -A OUTPUT -j LOG --log-prefix "iptables-output: " --log-level 7

# enable ipv4 forwardning for the system
echo 1 > /proc/sys/net/ipv4/ip_forward

# add route for private > public access
ip route add 192.168.1.0/24 proto kernel scope link dev eth2

#List iptables ruleset
echo "-----------------------------------------------------------------------------"
echo "Listing iptables..."
iptables -L

echo "-----------------------------------------------------------------------------"
echo "Listing ip routes..."
ip route

答案1

您必須啟動註解掉的規則

iptables -A FORWARD -i eth0 -p tcp --dport 3389 -d 192.168.1.2 -j ACCEPT

你必須修正這個(當然還有上面的日誌記錄規則):

iptables -A FORWARD -t filter -o eth0 -m state --state NEW,ESTABLISHED -d 192.168.1.2 -p tcp --dport 3389 -j ACCEPT

那是錯的。如果eth0輸出那麼介面192.168.1.2就是來源而不是目的地。但為什麼要允許國家NEW呢?最簡單的是:

iptables -A FORWARD -t filter -m state --state ESTABLISHED,RELATED -j ACCEPT

這是一個共同的規則。如果您想更接近您的連線定義:

iptables -A FORWARD -t filter -o eth0 -m state --state ESTABLISHED -s 192.168.1.2 -p tcp --sport 3389 -j ACCEPT

編輯1:

您可能需要一個允許來自其他系統的所有(?)連接的規則:

iptables -A FORWARD -t filter -o eth0 -m state --state NEW -s 192.168.1.2 -j ACCEPT

順便說一句,引用手冊頁:

“state”模組是“conntrack”的過時版本。

如果您不使用短規則來允許所有已建立的流量,那麼您還需要這樣的東西:

iptables -A FORWARD -t filter -i eth0 -m conntrack --ctstate ESTABLISHED -d 192.168.1.2 -j ACCEPT

相關內容