firewalld+ipdeny.com+systemdでブラックリストに載ってるIPからの接続を弾く

iptableでもやってたんですが、firewalldに乗り換えたので。

実行するのはこのスクリプトです。何も考えずにファイルにあるものを追加していってますw

ChainNameのところは使っているchain名に変えてください。

#!/bin/bash


ZONE_FILE_URL="http://www.ipdeny.com/ipblocks/data/countries"
for IP in $(curl ${ZONE_FILE_URL}/{cn,tw,kr,ru}.zone | grep -v "^#|^$" )
do
  firewall-cmd --direct --add-rule ipv4 filter ChainName 0 -s ${IP} -j DROP
done

後はこれを定期的に動かせば良いのですが、cronは使わずにsystemdのtimter機能でやります。うちはもうcrontab入れてないんで。

ファイル名はupdate_iptables_filter.serviceとして/etc/systemd/systemに置きます。update_iptables_filter.serviceはこれです。

[Unit]
Description=Update iptables country filter

[Service]
ExecStart=/usr/local/bin/filter_by_country.sh

[Install]
WantedBy=multi-user.target

もうひとつはupdate_iptables_filter.timerです。これも/etc/systemd/systemに置きます。

[Unit]
Description=Update iptables country filter

[Timer]
OnCalendar=weekly
Persistent=true
Unit=update_iptables_filter.service

[Install]
WantedBy=timers.target

後はupdate_iptables_filter.timerをenableにすればOKですね。

CentOS 7実践ガイド (impress top gear)

CentOS 7実践ガイド (impress top gear)