如何使用 firewalld 將特權低於 1024 的連接埠轉送到非特權 1024+ 連接埠?

如何使用 firewalld 將特權低於 1024 的連接埠轉送到非特權 1024+ 連接埠?

問題

如何使用 firewalld 將特權低於 1024 的連接埠轉送到非特權 1024+ 連接埠?

原因

我們為什麼要這樣做?我們希望能夠切換網關上的非特權 1050 連接埠並使用不同的上游郵件伺服器。例如,若要測試不同的垃圾郵件解決方案,請使用連接埠 1051 將郵件傳送至具有不同垃圾郵件過濾解決方案的不同郵件伺服器。

郵件伺服器啟動時會自動連線到網關。自動連線只能發生在 1024 以上的非特權連接埠上。

佈局和設定

佈局

+--------+         +---------------------+         +----------------+
|  WAN   |         |                1050 | <-      |                |
| Client |         |       Gateway       |    \    |   Mail Server  |
|        |  <--->  | 25                  |      -> | 25             |
+--------+         +---------------------+         +----------------+

設定防火牆

清除防火牆,打開端口,設定端口轉發,添加一些服務。

root@gateway:~# firewall-cmd --reload
root@gateway:~# firewall-cmd --zone=public --add-port=25/tcp
root@gateway:~# firewall-cmd --zone=public --add-forward-port=port=25:proto=tcp:toport=1050
root@gateway:~# firewall-cmd --add-service={http,https,smtp}

驗證防火牆

確認防火牆設定...

root@gateway:~# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: etho0
  sources: 
  services: dhcpv6-client http https smtp ssh
  ports: 25/tcp
  protocols: 
  masquerade: no
  forward-ports: port=25:proto=tcp:toport=1050:toaddr=
  source-ports: 
  icmp-blocks: 
  rich rules: 

這就是我們期望在防火牆規則中看到的內容。

結果

這是當我們遠端登入網關上的上游郵件伺服器時得到的結果...

root@gateway:~# telnet localhost 1050
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 debian10email.debian10email ESMTP Postfix (Debian/GNU)

這是我們從遠端客戶端機器得到的...

client@client123:~$ telnet gateway.example.org 25
Trying <IP_of_gateway>...
Connected to gateway.example.org.
Escape character is '^]'.

我們預計也會看到這220 debian10email.debian10email ESMTP Postfix (Debian/GNU)條線,但沒有。

完整性檢查...

考試

只是為了確認連接埠轉送規則是否正確編寫,我們...

  • 在防火牆上開啟連接埠 1025。
  • 連接埠轉送 1025 至 1050
  • 然後檢查我們在遠端客戶端上看到的內容。

調整防火牆

清除防火牆,打開端口,設定端口轉發,以及一些服務。

root@gateway:~# firewall-cmd --reload
root@gateway:~# firewall-cmd --zone=public --add-port=1025/tcp
root@gateway:~# firewall-cmd --zone=public --add-forward-port=port=1025:proto=tcp:toport=1050
root@gateway:~# firewall-cmd --add-service={http,https,smtp}

驗證防火牆

root@gateway:~# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: etho0
  sources: 
  services: dhcpv6-client http https smtp ssh
  ports: 1025/tcp
  protocols: 
  masquerade: no
  forward-ports: port=1025:proto=tcp:toport=1050:toaddr=
  source-ports: 
  icmp-blocks: 
  rich rules: 

結果

client@client123:~$ telnet gateway.example.org 1025
Trying <IP_of_gateway>...
Connected to gateway.example.org.
Escape character is '^]'.
220 debian10email.debian10email ESMTP Postfix (Debian/GNU)

我們有預期的220 debian10email.debian10email ESMTP Postfix (Debian/GNU)線路,因此防火牆會按預期進行連接埠轉送。

結論

特權連接埠和非特權連接埠之間的轉送與非特權連接埠之間的轉送不同。

我們如何在 Debian 10 Buster 上使用防火牆將特權低於 1024 連接埠轉送到非特權 1024+ 連接埠?如果某處有答案,請指出。我們一直沒能找到它。

答案1

您的防火牆配置看起來正確。您測試的機器是否允許與連接埠 25 建立傳出連線?從其他機器嘗試。

相關內容