Iptables

埠轉發到 OpenVPN 連接一次然後停止連接

  • September 21, 2015

我正在嘗試通過 OpenVPN 連接 (10.8.0.100) 將 IMAP 和 SMTP 埠從外部 IP 地址 (xxxx) 轉發到專用網路上的電腦。我目前有一個 OpenVPN 伺服器 (10.8.0.1),它與外部 IP (xxxx) 存在於同一台機器上。

我使用 iptables 從 xxxx 轉發埠 993、587 和 143 到 10.8.0.100,我可以通過 telnet 從外部連接到任何這些埠並從內部 IMAP 和 SMTP 伺服器獲得響應,但是一旦我關閉連接,我無法重新連接一段時間(大約 30 秒)。如果我嘗試立即連接,它只會掛在那裡說“正在嘗試 xxxx ..”並且不會連接。如果我停止嘗試一段時間,然後再試一次,它會再次正常響應。但是如果我關閉連接,它會再次發生。

如果我在帶有 OpenVPN 伺服器本身的機器上,也會發生這種情況。如果我在埠 143 上 telnet 到 10.8.0.100,假設它會連接,然後如果我關閉連接並重試,它就在那裡。

root@x.x.x.x:~# telnet 10.8.0.100 143
Trying 10.8.0.100...
Connected to 10.8.0.100.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE STARTTLS AUTH=PLAIN] Dovecot ready.
d
d BAD Error in IMAP command received by server.
d
d BAD Error in IMAP command received by server.
d
* BYE Too many invalid IMAP commands.
Connection closed by foreign host.
root@x.x.x.x:~# telnet 10.8.0.100 143
Trying 10.8.0.100... 

現在,如果我進入 IMAP/SMTP 伺服器所在的專用網路中的電腦,我可以毫無問題地遠端登錄這些埠,關閉它們並儘快重新打開。

我一直在尋找幾天來嘗試解決此問題的方法,但我似乎無法在網路上找到與此問題相關的任何內容。任何幫助,將不勝感激。

更新 添加 iptables-save 輸出:

~# iptables-save
# Generated by iptables-save v1.4.21 on Sat Sep 19 00:13:24 2015
*nat
:PREROUTING ACCEPT [7476:440381]
:INPUT ACCEPT [6361:381728]
:OUTPUT ACCEPT [5842:414754]
:POSTROUTING ACCEPT [5829:413974]
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport 993 -j DNAT --to-destination 10.8.0.100:993
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport 587 -j DNAT --to-destination 10.8.0.100:587
-A PREROUTING -d x.x.x.x/32 -p tcp -m tcp --dport 143 -j DNAT --to-destination 10.8.0.100:143
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -d 10.8.0.100/32 -p tcp -m tcp --dport 993 -j SNAT --to-source 10.8.0.1:993
-A POSTROUTING -d 10.8.0.100/32 -p tcp -m tcp --dport 587 -j SNAT --to-source 10.8.0.1:587
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
-A POSTROUTING -d 10.8.0.100/32 -p tcp -m tcp --dport 143 -j SNAT --to-source 10.8.0.1:143
-A POSTROUTING -s 10.0.0.0/8 -o eth0 -j MASQUERADE
COMMIT
# Completed on Sat Sep 19 00:13:24 2015
# Generated by iptables-save v1.4.21 on Sat Sep 19 00:13:24 2015
*filter
:INPUT DROP [42:2113]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:ufw-after-forward - [0:0]
:ufw-after-input - [0:0]
:ufw-after-logging-forward - [0:0]
:ufw-after-logging-input - [0:0]
:ufw-after-logging-output - [0:0]
:ufw-after-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-before-input - [0:0]
:ufw-before-logging-forward - [0:0]
:ufw-before-logging-input - [0:0]
:ufw-before-logging-output - [0:0]
:ufw-before-output - [0:0]
:ufw-logging-allow - [0:0]
:ufw-logging-deny - [0:0]
:ufw-not-local - [0:0]
:ufw-reject-forward - [0:0]
:ufw-reject-input - [0:0]
:ufw-reject-output - [0:0]
:ufw-skip-to-policy-forward - [0:0]
:ufw-skip-to-policy-input - [0:0]
:ufw-skip-to-policy-output - [0:0]
:ufw-track-forward - [0:0]
:ufw-track-input - [0:0]
:ufw-track-output - [0:0]
:ufw-user-forward - [0:0]
:ufw-user-input - [0:0]
:ufw-user-limit - [0:0]
:ufw-user-limit-accept - [0:0]
:ufw-user-logging-forward - [0:0]
:ufw-user-logging-input - [0:0]
:ufw-user-logging-output - [0:0]
:ufw-user-output - [0:0]
-A INPUT -j ufw-before-logging-input
-A INPUT -j ufw-before-input
-A INPUT -j ufw-after-input
-A INPUT -j ufw-after-logging-input
-A INPUT -j ufw-reject-input
-A INPUT -j ufw-track-input
-A INPUT -i eth0 -p tcp -m tcp --dport 993 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 25 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 587 -m state --state NEW,ESTABLISHED -j ACCEPT
-A INPUT -i eth0 -p tcp -m tcp --dport 143 -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -s 10.8.0.0/24 -p tcp -m tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
-A FORWARD -j ufw-before-logging-forward
-A FORWARD -j ufw-before-forward
-A FORWARD -j ufw-after-forward
-A FORWARD -j ufw-after-logging-forward
-A FORWARD -j ufw-reject-forward
-A FORWARD -j ufw-track-forward
-A OUTPUT -j ufw-before-logging-output
-A OUTPUT -j ufw-before-output
-A OUTPUT -j ufw-after-output
-A OUTPUT -j ufw-after-logging-output
-A OUTPUT -j ufw-reject-output
-A OUTPUT -j ufw-track-output
-A OUTPUT -o eth0 -p tcp -m tcp --sport 993 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --sport 25 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --sport 587 -m state --state ESTABLISHED -j ACCEPT
-A OUTPUT -o eth0 -p tcp -m tcp --sport 143 -m state --state ESTABLISHED -j ACCEPT
-A ufw-after-input -p udp -m udp --dport 137 -j ufw-skip-to-policy-input
-A ufw-after-input -p udp -m udp --dport 138 -j ufw-skip-to-policy-input
-A ufw-after-input -p tcp -m tcp --dport 139 -j ufw-skip-to-policy-input
-A ufw-after-input -p tcp -m tcp --dport 445 -j ufw-skip-to-policy-input
-A ufw-after-input -p udp -m udp --dport 67 -j ufw-skip-to-policy-input
-A ufw-after-input -p udp -m udp --dport 68 -j ufw-skip-to-policy-input
-A ufw-after-input -m addrtype --dst-type BROADCAST -j ufw-skip-to-policy-input
-A ufw-after-logging-input -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW BLOCK] "
-A ufw-before-forward -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-forward -p icmp -m icmp --icmp-type 3 -j ACCEPT
-A ufw-before-forward -p icmp -m icmp --icmp-type 4 -j ACCEPT
-A ufw-before-forward -p icmp -m icmp --icmp-type 11 -j ACCEPT
-A ufw-before-forward -p icmp -m icmp --icmp-type 12 -j ACCEPT
-A ufw-before-forward -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A ufw-before-forward -j ufw-user-forward
-A ufw-before-input -i lo -j ACCEPT
-A ufw-before-input -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-input -m conntrack --ctstate INVALID -j ufw-logging-deny
-A ufw-before-input -m conntrack --ctstate INVALID -j DROP
-A ufw-before-input -p icmp -m icmp --icmp-type 3 -j ACCEPT
-A ufw-before-input -p icmp -m icmp --icmp-type 4 -j ACCEPT
-A ufw-before-input -p icmp -m icmp --icmp-type 11 -j ACCEPT
-A ufw-before-input -p icmp -m icmp --icmp-type 12 -j ACCEPT
-A ufw-before-input -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A ufw-before-input -p udp -m udp --sport 67 --dport 68 -j ACCEPT
-A ufw-before-input -j ufw-not-local
-A ufw-before-input -d y.y.y.y/32 -p udp -m udp --dport 5353 -j ACCEPT
-A ufw-before-input -d z.z.z.z/32 -p udp -m udp --dport 1900 -j ACCEPT
-A ufw-before-input -j ufw-user-input
-A ufw-before-output -o lo -j ACCEPT
-A ufw-before-output -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A ufw-before-output -j ufw-user-output
-A ufw-logging-allow -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW ALLOW] "
-A ufw-logging-deny -m conntrack --ctstate INVALID -m limit --limit 3/min --limit-burst 10 -j RETURN
-A ufw-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW BLOCK] "
-A ufw-not-local -m addrtype --dst-type LOCAL -j RETURN
-A ufw-not-local -m addrtype --dst-type MULTICAST -j RETURN
-A ufw-not-local -m addrtype --dst-type BROADCAST -j RETURN
-A ufw-not-local -m limit --limit 30/min --limit-burst 10 -j ufw-logging-deny
-A ufw-not-local -j DROP
-A ufw-skip-to-policy-forward -j ACCEPT
-A ufw-skip-to-policy-input -j DROP
-A ufw-skip-to-policy-output -j ACCEPT
-A ufw-track-forward -p tcp -m conntrack --ctstate NEW -j ACCEPT
-A ufw-track-forward -p udp -m conntrack --ctstate NEW -j ACCEPT
-A ufw-track-output -p tcp -m conntrack --ctstate NEW -j ACCEPT
-A ufw-track-output -p udp -m conntrack --ctstate NEW -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 22 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 22 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 1194 -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 25 -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 587 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 587 -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 465 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 465 -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 993 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 993 -j ACCEPT
-A ufw-user-input -p tcp -m tcp --dport 143 -j ACCEPT
-A ufw-user-input -p udp -m udp --dport 143 -j ACCEPT
-A ufw-user-limit -m limit --limit 3/min -j LOG --log-prefix "[UFW LIMIT BLOCK] "
-A ufw-user-limit -j REJECT --reject-with icmp-port-unreachable
-A ufw-user-limit-accept -j ACCEPT
COMMIT

**修復:**我決定從一組乾淨的 iptables 規則開始,執行以下操作:

iptables -F
iptables --table nat --flush
iptables --delete
iptables --table nat --delete-chain
iptables -F (maybe not necessary but did it at the end anyhow)

我決定禁用 ufw 並使用 iptables 在埠 1194(OpenVPN 埠)上允許 udp。

iptables -I INPUT -p udp -m udp --dport 1194 -j ACCEPT

然後使用https://unix.stackexchange.com/questions/76300/iptables-port-to-another-ip-port-from-the-inside作為我的指導,我做了以下事情:

iptables -t nat -A PREROUTING -p tcp --dport 143 -d x.x.x.x -j DNAT --to-destination 10.8.0.100
iptables -t nat -A POSTROUTING -p tcp --dport 143 -d 10.8.0.100 -j SNAT --to-source 10.8.0.1
iptables -A FORWARD -p tcp --dport 143 -d 10.8.0.100 -j ACCEPT

現在,我可以遠端登錄到埠 143,關閉連接並立即重新打開連接,而無需等待。我對所有 IMAP(S) 和 SMTP(S) 埠都這樣做了。

引用自:https://serverfault.com/questions/723440