Linux

Ubuntu / iptables:使用伺服器作為中間人與防火牆伺服器交談

  • May 29, 2015

我有一種情況,我需要與只允許來自一個特定 IP 的連接的 API 進行通信。

因此 api.example.com 接受來自 whitelisted-ip 的埠 443 上的連接

我希望能夠從任何地方連接到白名單 IP 埠 443,並讓它將數據包轉發到埠 443 上的 api.example.com,然後將響應發送回連接的機器。

我認為有一些類似於透明魷魚代理的方法,但我無法弄清楚。

我嘗試使用以下語句遵循此處的範例(http://www.tldp.org/HOWTO/TransparentProxy-6.html ),但沒有成功。

iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 443 -j DNAT --to api.example.com:443
iptables -t nat -A POSTROUTING -o eth1 -d api.example.com -j SNAT --to whitelisted-ip

任何幫助,將不勝感激。

更新:

我也嘗試了以下方法,但無濟於事:

iptables -t nat -A PREROUTING  -p tcp -d whitelisted-ip  --dport 443 -j DNAT --to api.example.com:443
iptables -t nat -A POSTROUTING -p tcp -s api.example.com --sport 443 -j SNAT --to whitelisted-ip:443

謝謝

據我了解,您希望使用一個 ip(列入白名單的 ip)從任何地方訪問伺服器,這不是您正在使用的客戶端 ip。您不能這樣做,因為您可以使用白名單 IP 訪問伺服器,但伺服器將無法響應,因為它不是您在網路中的 IP 地址

在 whitelisted-ip shell 上,您可以嘗試使用netcat執行此類任務。

通過在 whitelisted-ip shell 上發出以下命令,您將綁定埠 4443 並將連接轉發到 api.example.com:443。

nc -l -p 4443 -c "nc api.example.com 443"

因此,您可以從任何其他端點連接到 whitelisted-ip:4443。

提醒:我不建議在沒有任何保護的情況下將其綁定在任何可公開訪問的介面上,您可以將埠綁定到環回介面添加-s 127.0.0.1參數。

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