CentOS 7 で特定の IP アドレスを拒否する手順を解説する。
lastb
出力例
最終ログイン: 1970/01/01 (木) 09:00:00 JST日時 pts/4
最後の失敗ログイン: 2019/06/05 (水) 07:19:18 JST 211.252.85.100から開始日時 ssh:notty
最後の正しいログインの後に 59 回の失敗ログインの試行があります
ログインすると失敗ログインの試行があるので lastb
コマンドで確認する。
lastb | less
出力例
root ssh:notty 211.252.85.100 Wed Jun 5 07:19 - 07:19 (00:00)
root ssh:notty 211.252.85.100 Wed Jun 5 07:04 - 07:04 (00:00)
root ssh:notty 211.252.85.100 Wed Jun 5 06:49 - 06:49 (00:00)
同じ IP アドレスから不正アクセスの試行が続いていたので IP アドレスを指定して拒否する。
通常は Fail2ban で自動的に拒否するとよい。
whois
yum --assumeyes install whois
whois 211.252.85.100 | less
whois 211.252.85.100 | grep 'IPv4 Address'
出力例
IPv4 Address : 211.252.84.0 - 211.252.87.255 (/22)
whois
または jwhois
コマンドで IP アドレスの詳細を確認する。
firewall-cmd で IP 拒否
firewall-cmd --zone=drop --permanent --add-source=211.252.84.0/22
firewall-cmd --reload
firewall-cmd
コマンド の --zone
オプションに drop
を、 --add-source
オプションで IP アドレスを CIDR 付きで指定することで設定できる。
--permanent
オプションを指定することで永続的に拒否できるが、 firewall-cmd --reload
で設定をリロードしないと反映されないので注意。
設定確認
firewall-cmd --get-active-zones
出力例
drop
sources: 211.252.84.0/22
public
interfaces: eth0
出力ファイル
cat /etc/firewalld/zones/drop.xml
出力例
<?xml version="1.0" encoding="utf-8"?>
<zone target="DROP">
<short>Drop</short>
<description>Unsolicited incoming network packets are dropped. Incoming packets that are related to outgoing network connections are accepted. Outgoing network connections are allowed.</description>
<source address="211.252.84.0/22"/>
</zone>
設定は /etc/firewalld/zones/drop.xml
に XML 形式で保存される。
拒否設定の解除
firewall-cmd --get-active-zones
firewall-cmd --zone=drop --permanent --remove-source=192.0.2.0/24
firewall-cmd --reload
firewall-cmd --get-active-zones
設定を解除するには --remove-source
オプションで指定する。