そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

firewalldで日本国内のIPアドレスのみ許可する設定

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・firewalldで日本国内のIPアドレスのみ許可する設定をする手順を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

firewalldで日本国内のIPアドレスのみ許可する設定

手順① 日本国内のIPアドレスの一覧ファイルを作成

APNICのサイトから、IPアドレスの一覧ファイルをダウンロードして、
国コードがJPのものを抽出して、日本国内のIPアドレス一覧を作成します。

www.apnic.net

[root@SV-ALTAIR ~]# mkdir /root/iptables
[root@SV-ALTAIR ~]# vi /root/iptables/jpip_setup.sh
/root/iptables/jpip_setup.sh
#!/bin/bash
COUNTRYLIST='JP'
curl -s -o /root/iptables/delegated-apnic-latest http://ftp.apnic.net/stats/apnic/delegated-apnic-latest
:> /root/iptables/jpip
for country in $COUNTRYLIST
do
        for ip in `cat /root/iptables/delegated-apnic-latest | grep "apnic|$country|ipv4|"`
        do
                COUNTRY=`echo $ip | awk -F"|" '{ print $2 }'`
                IPADDR=`echo $ip | awk -F"|" '{ print $4 }'`
                TMPCIDR=`echo $ip | awk -F"|" '{ print $5 }'`

                FLTCIDR=32
                while [ $TMPCIDR -ne 1 ];
                do
                        TMPCIDR=$((TMPCIDR/2))
                        FLTCIDR=$((FLTCIDR-1))
                done
                echo "$IPADDR/$FLTCIDR" >> /root/iptables/jpip
        done
done

APNICからダウンロードするdelegated-apnic-latest というファイルは以下のような形式です。

######################################################################
#
# 
# ____________________________________________________________________
# 
# 
# The files are freely available for download and use on the condition 
# that APNIC will not be held responsible for any loss or damage 
# arising from the use of the information contained in these reports.
# 
# APNIC endeavours to the best of its ability to ensure the accuracy 
# of these reports; however, APNIC makes no guarantee in this regard.
# 
# In particular, it should be noted that these reports seek to 
# indicate where resources were first allocated or assigned. It is not
# intended that these reports be considered as an authoritative 
# statement of the location in which any specific resource may 
# currently be in use.
# 
# For more information see: 
#
#       http://www.apnic.net/db/rir-stats-format.html
# or
#    ftp://ftp.apnic.net/pub/apnic/stats/apnic/README.TXT
#
######################################################################
#
2|apnic|20230624|76950|19830613|20230623|+1000
apnic|*|asn|*|12040|summary
apnic|*|ipv4|*|50741|summary
apnic|*|ipv6|*|14169|summary
apnic|JP|asn|173|1|20020801|allocated
apnic|NZ|asn|681|1|20020801|allocated
apnic|AU|asn|1221|1|20000131|allocated
apnic|JP|asn|1233|1|20020801|allocated
apnic|KR|asn|1237|1|20020801|allocated
apnic|SG|asn|1250|1|20020801|allocated
apnic|TW|asn|1659|1|20020801|allocated
apnic|KR|asn|1704|1|20020801|allocated
apnic|TW|asn|1768|2|20020801|allocated
apnic|KR|asn|1781|1|20020801|allocated

上記のjpip_setup.sh を実行すると、
以下のような形式で日本国内のIPアドレス一覧(CIDR表記) jpipが出力されます。

1.0.16.0/20
1.0.64.0/18
1.1.64.0/18
1.5.0.0/16
1.21.0.0/18
1.21.64.0/19
1.21.96.0/20
1.21.112.0/20
1.21.128.0/20
1.21.144.0/20
1.21.160.0/19
1.21.192.0/18
1.33.0.0/16
1.66.0.0/15
1.72.0.0/13
1.112.0.0/14
14.0.8.0/22
14.1.4.0/22
14.1.8.0/21
14.3.0.0/16
14.8.0.0/13
14.101.0.0/16
14.102.132.0/22
14.102.164.0/22
14.102.192.0/19
14.128.0.0/22
14.128.16.0/20

手順② firewalldに日本国内ゾーン[domestic]を作成

firewall-cmd --new-zone=domestic --permanent
firewall-cmd --new-ipset=domestic --type=hash:net --permanent
firewall-cmd --ipset=domestic --add-entries-from-file=/root/iptables/jpip --permanent
firewall-cmd --info-ipset=domestic --permanent
firewall-cmd --zone=domestic --add-source=ipset:domestic --permanent
[root@SV-ALTAIR ~]# firewall-cmd --new-zone=domestic --permanent
[root@SV-ALTAIR ~]# firewall-cmd --new-ipset=domestic --type=hash:net --permanent
[root@SV-ALTAIR ~]# firewall-cmd --ipset=domestic --add-entries-from-file=/root/iptables/jpip --permanent
[root@SV-ALTAIR ~]# firewall-cmd --info-ipset=domestic --permanent
[root@SV-ALTAIR ~]# firewall-cmd --zone=domestic --add-source=ipset:domestic --permanent

手順③ cronで定期的に更新

/etc/cron.monthly/fiewall_ipset.sh

#!/bin/bash

sh /root/iptables/jpip_setup.sh
firewall-cmd --reload
echo "firewall OK" | mail -s "firewall ipset upadte `hostname`" root

手順④ firewalldの再起動時にも実行されるように設定

/usr/lib/systemd/system/firewalld.service

[Unit]
Description=firewalld - dynamic firewall daemon
Before=network-pre.target
Wants=network-pre.target
After=dbus.service
After=polkit.service
Conflicts=iptables.service ip6tables.service ebtables.service ipset.service nftables.service
Documentation=man:firewalld(1)

[Service]
EnvironmentFile=-/etc/sysconfig/firewalld
ExecStartPre=/root/iptables/jpip_setup.sh    ←←←★ここに追記
ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
ExecReload=/bin/kill -HUP $MAINPID
# supress to log debug and error output also to /var/log/messages
StandardOutput=null
StandardError=null
Type=dbus
BusName=org.fedoraproject.FirewallD1
KillMode=mixed

[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service

参考にしたページ

systemexpress.co.jp