mattintosh note

どこかのエンジニアモドキの備忘録

日本のIPアドレスのレンジを調べてiptablesで登録する

とあるサーバで SSH のポートを変えていてもたまに海外から変なパケットが飛んでくることがあった。その都度 ipinfo とかで調べてレンジ指定で DROP していたけど面倒になってきたので日本からの IP だけ ACCEPT に反転することにした。

IP アドレスの一覧は APINIC で公開されているものを使う。

http://ftp.apnic.net/stats/apnic/delegated-apnic-latest

######################################################################
#
#   CONDITIONS OF USE
# ____________________________________________________________________
# 
# 
# 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|20200911|66261|19830613|20200910|+1000
apnic|*|asn|*|10300|summary
apnic|*|ipv4|*|45057|summary
apnic|*|ipv6|*|10904|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|JP|ipv4 になっているレコードを取り出して見てみる。

apnic|JP|ipv4|1.0.16.0|4096|20110412|allocated
apnic|JP|ipv4|1.0.64.0|16384|20110412|allocated
apnic|JP|ipv4|1.1.64.0|16384|20110412|allocated

これを CIDR 表記にしていきたいけどネットワーク部じゃなくて 10 進数のホスト部しか書いてないっぽい。

1.0.16.0 4096

指数とか対数とか忘れたなぁと思いつつ awk でだいたいこんな感じで行けるんではなかろうかと思ってやってみると出来た。二進数にして桁数数えたりするのかと思ったけど log() 関数でいいっぽい。

awk -F '|' '/^apnic\|JP\|ipv4\|/ { printf("%s/%s\n", $4, 32 - log($5) / log(2)) }'

同様の表示をしてくれるサイトと比較してみて問題が無いことを確認。

1.0.16.0/20
1.0.64.0/18
1.1.64.0/18
:

あとはひたすら iptables に登録(もちろん手打ちではやらない)。ルータでそもそも TCP しか通してないので -p tcp は入れてない。

iptables -F INPUT
iptables -A INPUT -i lo                                        -j ACCEPT
iptables -A INPUT -m state --state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -s 10.0.0.0/8                                -j ACCEPT
iptables -A INPUT -s 172.16.0.0/12                             -j ACCEPT
iptables -A INPUT -s 192.168.0.0/16                            -j ACCEPT

iptables -A INPUT -s 1.0.16.0/20                               -j ACCEPT
iptables -A INPUT -s 1.0.64.0/18                               -j ACCEPT
iptables -A INPUT -s 1.1.64.0/18                               -j ACCEPT
:
略
:
iptables -P INPUT DROP