1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iptablesでポートフォワーディングとフィルタを組み合わせる設定

Posted at

sshdが22番ポートをlistenしているときに、下記の要件を実現するiptables設定をまとめてみました。

  1. 192.0.2.0/24 からsshdに22番ポート経由でアクセス可能
  2. 198.51.100.0/24 からsshdに10022番ポート経由でアクセス可能
  3. 他の組み合わせではアクセス不可

この場合、下記のようにすると一番見通しが良さそうです。REDIRECTされたかどうかの判断にmarkを利用しています。

# iptables -t mangle -A PREROUTING -p tcp --dport 10022 -j MARK --set-mark 10022
# iptables -t nat -A PREROUTING -p tcp --dport 10022 -j REDIRECT --to-port 22
# iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables -A INPUT -s 198.51.100.0/24 -p tcp --dport 22 -m mark --mark 10022 -j ACCEPT
# iptables -A INPUT -m mark --mark 10022 -j DROP
# iptables -A INPUT -s 192.0.2.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
# iptables -A INPUT -j DROP

別解として、REDIRECTの対象となるIPアドレスを絞ってしまう設定も考えられます。ただ、これだとIPアドレスの記述がnatテーブルとfilterテーブルとに分散してしまうのがイマイチかもしれません。

# iptables -t mangle -A PREROUTING -p tcp --dport 10022 -j MARK --set-mark 10022
# iptables -t nat -A PREROUTING -s 198.51.100.0/24 -p tcp --dport 10022 -j REDIRECT --to-port 22
# iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# iptables -A INPUT -p tcp --dport 22 -m mark --mark 10022 -j ACCEPT
# iptables -A INPUT -s 192.0.2.0/24 -p tcp -m tcp --dport 22 -j ACCEPT
# iptables -A INPUT -j DROP

参考URL

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?