iptables localhostリダイレクトはエコーのみを表示します。

iptables localhostリダイレクトはエコーのみを表示します。

iptablesインターネットからlocalhost、ポート2025のSSHトンネルにトラフィックをリダイレクトしようとしています。何らかの理由でリダイレクトが機能しません。

私はそれを使って接続しましたが、telnet何らかの理由でエコーが発生しましたが、SMTPサーバーにリダイレクトされるバナーは表示されませんでした。システムからポート2025へのリダイレクトをオンにすると、telnetすぐにSMTPサーバーからグリーティングが届きます。

外の世界から接続し、任意のキーを押します。

$ telnet infantile.xyz 25
Trying 94.156.189.160...
Connected to infantile.xyz.
Escape character is '^]'.
cfvghnjdfghdfgh

任意のキーをエコーし​​ます。

リダイレクトのあるシステムに接続すると、インターネット上の外部の人々がどのように作業するかを知ることができます。

# telnet localhost 2025
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
220 infantile.xyz ESMTP Postfix (Debian/GNU)

私のiptables設定は次のとおりです。

Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 252M  124G ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
 270K   16M ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
  308 12340 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
 8988  612K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8 ctstate NEW
2277K  462M UDP        udp  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate NEW
3204K  147M TCP        tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp flags:0x17/0x02 ctstate NEW
2277K  462M REJECT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
2914K  130M REJECT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with tcp-reset
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-proto-unreachable

Chain FORWARD (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 1855 packets, 447K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain TCP (1 references)
 pkts bytes target     prot opt in     out     source               destination         
22104 1144K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
 4976  265K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
 2585  145K ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:443
   95  4836 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:587

Chain UDP (1 references)
 pkts bytes target     prot opt in     out     source               destination

私が作成したリダイレクトルールセットのNATルールは次のとおりです。

Chain PREROUTING (policy ACCEPT 3813 packets, 465K bytes)
 pkts bytes target     prot opt in     out     source               destination         
 2954  174K DNAT       tcp  --  *      *       0.0.0.0/0            94.156.189.0/24      tcp dpt:25 to:127.0.0.1:2025

Chain INPUT (policy ACCEPT 3 packets, 110 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 6798 packets, 408K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 11177 packets, 583K bytes)
 pkts bytes target     prot opt in     out     source               destination    

奇妙なことは、接続を受け入れてエコーですが、とにかく相手(接尾辞)のサービスには応答しないということです。

最初はiptablesこのガイドを使って設定しました。https://wiki.archlinux.org/index.php/simple_stateful_firewall

これがリダイレクトに使用したいものです。

iptables -t nat -I PREROUTING -i eth0 -p tcp -d 94.156.189.160 --dport 25 -j DNAT --to-destination 127.0.0.1:2025

次のsysctlオプションが有効になっています。

net.ipv4.conf.all.forwarding = 1
net.ipv4.conf.all.route_localnet = 1

ベストアンサー1

iptablesこのファイアウォールを介してポートをリダイレクトするには、チェーンにいくつかの規則が必要であり、TCPチェーンポリシーへの変更が必要ですFORWARD

順方向チェーンは受け入れられている必要があります。

# iptables -P FORWARD ACCEPT

リダイレクトされたポートとその宛先を許可する必要があります。

# iptables -A TCP -p tcp --dport 25 -j ACCEPT
# iptables -A TCP -p tcp --dport 2025 -j ACCEPT

このPOSTROUTINGルールは...

# iptables -t nat -A POSTROUTING -j MASQUERADE
# iptables -t nat -I PREROUTING -i eth0 -p tcp -d 94.156.189.160 --dport 25 -j DNAT --to-destination 127.0.0.1:2025

これらのルールを使用すると、iptablesはポート25からの着信パケットをlocalhostポート2025のSSHトンネルにリダイレクトします。マスカレーディングルールはパケットの返却を可能にします。

おすすめ記事