firewalldでzoneを作ってsshのブルートフォースアタック対策を入れる位までのめも

そろそろfirewalldを使ってみようかな〜ということで、yaourt -S firewalldしてインストールして設定してみました。 やったことはzoneを新規に作って、source(192.168.11.0/24)をそこに割り当てて、サービス(sshとか)を許可したりということ。

まずはデフォルトゾーンの確認とhome zoneのssh設定を確認。デフォルトは自分で設定して、enp101s0をI/Fに設定済み。

[root@saga ~]# firewall-cmd --get-default-zone
home
[root@saga ~]# iptables -L | grep ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate NEW

homenet zoneの作成

[root@saga ~]# firewall-cmd --permanent --new-zone=homenet
success

設定変更したのでリロード

[root@saga ~]# firewall-cmd --reload
success

作成したzoneの確認

[root@saga ~]# firewall-cmd --zone=homenet --list-all
homenet
  interfaces:
  sources:
  services:
  ports:
  masquerade: no
  forward-ports:
  icmp-blocks:
  rich rules:

homenetにsourceを設定

[root@saga ~]# firewall-cmd --permanent --add-source=192.168.11.0/24 --zone=homenet
success

homenetでsshを許可

[root@saga ~]# firewall-cmd --permanent --add-service=ssh --zone=homenet
success

デフォルトのzoneをhomenetに変更

[root@saga ~]# firewall-cmd --set-default-zone=homenet
success

home zoneに設定していたenp101s0をhomenetに変更。

[root@saga ~]# firewall-cmd --zone=homenet --change-interface=enp101s0
success

firewall-cmd --reloadしてからiptable -Lで設定された内容を確認。

Chain IN_homenet_allow (1 references)
target     prot opt source               destination
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh ctstate NEW

ssh brute force attack対策。60秒間に5回の試行が行われたら60秒間DROPする。IN_homenetが対象のchainで、後ろの[0-2]はruleの優先度。0はログを取っているだけなので、DROPするのは1のルール。0と1に引っかからなければ2のルールでアクセスできる。

[root@saga ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter IN_homenet 0 -p tcp -m tcp --dport 22 -m recent --update --seconds 60 --hitcount 5 --rttl --name SSH --rsource -m comment --comment "SSH Brute-force protection" -j LOG --log-prefix "SSH_brute_force "
success
[root@saga ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter IN_homenet 1 -p tcp -m tcp --dport 22 -m recent --update --seconds 60 --hitcount 5 --rttl --name SSH --rsource -m comment --comment "SSH Brute-force protection" -j DROP
success
[root@saga ~]# firewall-cmd --permanent --direct --add-rule ipv4 filter IN_homenet 2 -p tcp -m tcp --dport 22 -m state --state NEW -m recent --set --name SSH --rsource -m comment --comment "SSH Brute-force protection" -j ACCEPT
success

いつもどおり、firewall-cmd --reloadしてiptablesで確認。

Chain IN_homenet (2 references)
target     prot opt source               destination
LOG        tcp  --  anywhere             anywhere             tcp dpt:ssh recent: UPDATE seconds: 60 hit_count: 5 TTL-Match name: SSH side: source mask: 255.255.255.255 /* SSH Brute-force protection */ LOG level warning prefix "SSH_brute_force "
DROP       tcp  --  anywhere             anywhere             tcp dpt:ssh recent: UPDATE seconds: 60 hit_count: 5 TTL-Match name: SSH side: source mask: 255.255.255.255 /* SSH Brute-force protection */
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:ssh state NEW recent: SET name: SSH side: source mask: 255.255.255.255 /* SSH Brute-force protection */

logはjournalctlで確認できます。

ちなみに、ssh brute force attack対策する理由は単に↓です。

ここまで設定してみた感想としては、iptablesで直接設定していくよりは良いかもな〜というところ。 まだ、設定方法をググって何をやってるのかを理解して設定していってるだけだけど。

今回参考にしたWebサイトはこちら

https://fedoraproject.org/wiki/FirewallD/jp

FirewallD: Adding Services and Direct Rules ~ IT Notes and Scribblings

RHEL7: How to mitigate HTTP attacks. - CertDepot

CentOS7(RHEL7) Firewalldの基礎の基礎 | LINUXの勉強ブログ

15時間でわかるCentOS集中講座

15時間でわかるCentOS集中講座