[最終更新:2007/01/08]

ブルートフォース攻撃を防止するiptables の設定

SSH へ外部から接続する必要があるが、それでも不正進入を試みる輩からサーバーを守りたい。そんな一見矛盾する要求を、24時間以内のログに、1分間に5回以上の進入を試みた輩の IP アドレスがあったらそのアドレスからの接続をブロックしてしまうように設定。

login as: 一般ユーザー名
Authenticating with public key "imported-openssh-key"
Passphrase for key "imported-openssh-key":
Last login: Fri Jan 5 18:33:22 2007
$ su
Password:
# /sbin/iptables -N ssh_blute
# /sbin/iptables -N ssh
# /sbin/iptables -A INPUT -j ssh -p tcp --dport 22
# /sbin/iptables -A ssh_blute -m recent --name ssh_block --set -j LOG --log-level DEBUG --log-prefix "ssh_block:"
# /sbin/iptables -A ssh_blute -j REJECT
# /sbin/iptables -A ssh -p tcp ! --syn -m state --state ESTABLISHED,RELATED -j ACCEPT
# /sbin/iptables -A ssh -p tcp --syn -m recent --name ssh_block --update --seconds 86400 -j REJECT --reject-with icmp-port-unreachable
# /sbin/iptables -A ssh -p tcp --syn -m recent --name ssh_conn --rcheck --seconds 60 --hitcount 5 -j ssh_blute
# /sbin/iptables -A ssh -p tcp --syn -m recent --name ssh_conn --set
# /sbin/iptables -A ssh -p tcp --syn -j ACCEPT
# /sbin/iptables-save >/etc/sysconfig/iptables

もし、直接設定ファイルを変更する場合は、青線で囲った部分の記述を追加する。

# vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p ipv6-crypt -j ACCEPT
-A RH-Firewall-1-INPUT -p ipv6-auth -j ACCEPT
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 25 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

-A ssh -p tcp -m tcp ! --tcp-flags SYN,RST,ACK SYN -m state --state RELATED,ESTABLISHED -j ACCEPT
-A ssh -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m recent --update --seconds 86400 --name ssh_block --rsource -j REJECT --reject-with icmp-port-unreachable
-A ssh -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m recent --rcheck --seconds 60 --hitcount 5 --name ssh_conn --rsource -j ssh_blute
-A ssh -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -m recent --set --name ssh_conn --rsource
-A ssh -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j ACCEPT
-A ssh_blute -m recent --set --name ssh_block --rsource -j LOG --log-prefix "ssh_block:" --log-level 7
-A ssh_blute -j REJECT --reject-with icmp-port-unreachable
COMMIT
# /etc/init.d/iptables restart
ファイアウォールルールを適用中: [ OK ]
チェインポリシーを ACCEPT に設定中filter [ OK ]
iptables モジュールを取り外し中 [ OK ]
iptables ファイアウォールルールを適用中: [ OK ]

設定を確認(青枠内の記述が追加されていることを確認する。)

# /sbin/iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere
ssh tcp -- anywhere anywhere tcp dpt:ssh


Chain FORWARD (policy ACCEPT)
target prot opt source destination
RH-Firewall-1-INPUT all -- anywhere anywhere

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain RH-Firewall-1-INPUT (2 references)
target prot opt source destination
ACCEPT all -- anywhere anywhere
ACCEPT icmp -- anywhere anywhere icmp any
ACCEPT ipv6-crypt-- anywhere anywhere
ACCEPT ipv6-auth-- anywhere anywhere
ACCEPT udp -- anywhere 224.0.0.251 udp dpt:5353
ACCEPT udp -- anywhere anywhere udp dpt:ipp
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:http
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:smtp
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

Chain ssh (1 references)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere tcp flags:!SYN,RST,ACK/SYN state RELATED,ESTABLISHED
REJECT tcp -- anywhere anywhere tcp flags:SYN,RST,ACK/SYN recent: UPDATE seconds: 86400 name: ssh_block side: source reject-with icmp-port-unreachable
ssh_blute tcp -- anywhere anywhere tcp flags:SYN,RST,ACK/SYN recent: CHECK seconds: 60 hit_count: 5 name: ssh_conn side: source
tcp -- anywhere anywhere tcp flags:SYN,RST,ACK/SYN recent: SET name: ssh_conn side: source
ACCEPT tcp -- anywhere anywhere tcp flags:SYN,RST,ACK/SYN

Chain ssh_blute (1 references)
target prot opt source destination
LOG all -- anywhere anywhere recent: SET name: ssh_block side: source LOG level debug prefix `ssh_block:'
REJECT all -- anywhere anywhere reject-with icmp-port-unreachable



SSH の設定
CentOS 4.4 のトップ