概略
IPアドレスを元にしたフィルター設定を行う際に、国別の制御を行いたいことがあります。多くのセキュリティ製品ではそうした国別のアクセス制御(IPレピュテーション)の機能を備えているため、その機能を活用することが早道でしょう。
本稿ではその機能をあえて手動で設定してみることにします。
本稿に記載した内容は、個人的な見解を示したものであり、筆者の所属する企業・団体の公式見解ではありません。
IPアドレスの割り当て
国別のIPレピュテーションを行ううえで、そのIPアドレスがどこの国に割り当てられているのかを確認する必要があります。
IPアドレス全体はICANN(The Internet corporation for Assigned Names and Numbers)が管理し、地域ごとの管理団体である RIR(Regional Internet Registry)にIPアドレスを割り当てて管理を委譲しています。更に RIRはプロバイダーなどの LIR(Local Internet Registry)にIPアドレスを割り当てる仕組みです。
ICANN(IANA)
└── RIR (APNIC, ARINなど)
└── LIR (ISPや企業などに分配)
現在、RIRは 5団体あります。
略称 | 名前 | 管轄地域 |
---|---|---|
AFRINIC | African Network Information Centre | アフリカ全域 |
APNIC | Asia-Pacific Network Information Centre | アジア太平洋 |
ARIN | American Registry for Internet Numbers | 北米(米国・カナダ・カリブ海一部) |
LACNIC | Latin America and Caribbean Network Information Centre | 中南米・カリブ海諸国 |
RIPE NCC | Réseaux IP Européens Network Coordination Centre | 欧州・中東・中央アジアなど |
アジア太平洋を管理している APNICの場合は更に中間の NIR(National Internet Registry)を設けて管理を委譲しています。日本においてはJPNICがその役割を担っています。
ICANN(IANA)
└── RIR (地域インターネットレジストリ: APNIC)
├── NIR (国家インターネットレジストリ、日本・韓国・中国など一部国のみ)
│ └── LIR (ローカルインターネットレジストリ、ISPや事業者)
└── LIR (NIRを経由しない、APNICから直接割当を受けるLIR)
IANAが割り当てたレジストリー
IANAは 8ビットマスク単位で各レジストリーにIPアドレスを割り当てておりIPv4 Address Space Registryに記載されています。
IPv4アドレスの枯渇とNIC間移譲
蛇足ですが、IPv4アドレスの枯渇が叫ばれてからどれほど時間が経ったでしょうか。しかし、今でも IPv4グローバルアドレスは申請すれば確保することもできます。それができるのも、国や地域を超えてレジストリー同士でIPv4アドレスを融通し合っているためです。
そうしたレジストリー間の移譲履歴はJPNICなどで公開されています。
APNICの国別割り当て
さて、アジア太平洋を管理しているAPNICは、国ごとの NIRに管理を移譲していると説明しました。その割り当ては delegated-apnic-latestや拡張機能を使った delegated-apnic-extended-latestというファイルに記載されています。以下にその一部を抜粋しています。
delegate-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|20250625|82104|19830613|20250624|+1000
apnic|*|asn|*|13019|summary
apnic|*|ipv4|*|52920|summary
apnic|*|ipv6|*|16165|summary
apnic|JP|asn|173|1|20020801|allocated
[...]
apnic|JP|ipv4|1.0.16.0|4096|20110412|allocated
[...]
apnic|JP|ipv6|2001:200::|35|19990813|allocated
delegate-apnic-extended-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-EXTENDED.TXT
#
######################################################################
#
2.3|apnic|20250625|178457||20250624|+1000
apnic|*|asn|*|13843|summary
apnic|*|ipv4|*|56237|summary
apnic|*|ipv6|*|108377|summary
apnic|AU|ipv4|1.0.0.0|256|20110811|assigned|A91872ED
[...]
apnic|JP|ipv4|1.0.16.0|4096|20110412|allocated|A92D9378
[...]
apnic|JP|ipv6|2001:200::|35|19990813|allocated|A916B6AA
拡張版では第8カラムが追加されていることがわかります。
カラム番号 | 意味 | 値 | 説明 |
---|---|---|---|
1 | Registry | apnic | 割当元 RIR |
2 | CC | JP | 割当先の国コード(ISO 3166 2文字コード) |
3 | Type | ipv4 | リソースタイプ |
4 | Start | 1.0.16.0 | 割当IPの開始アドレス |
5 | Value | 4096 | 割当IPアドレス数 |
6 | Date | 20110412 | 割当日(YYYYMMDD形式) |
7 | Status | allocated | ステータス(allocated/assignedなど) |
8 | Opaque-ID | A92D9378 | リソース保有者の識別子 |
9 | Extentions | (未使用) | 拡張機能 |
IPアドレスの国別割り当てを確認するためだけでしたら、通常版でも拡張版でもどちらを利用しても問題ありません。
この割り当て一覧では、最初のIPアドレスとそこからの割当数が書かれており、フィルター設定などでよく用いられるCIDR表記にはなっていません。割り当て数からCIDR表記のプレフィックス長を得るには $ n = 32 - \log_2(\text{ホスト数}) $ と言った計算が必要になります。
IPv6アドレスの場合は最初からCIDRプレフィック長が書かれているので、このような計算は不要です。IPv4もそうしてくれると助かるのですが、おそらくこれからも変わることはないでしょう。
IT鎖国スクリプト
delegated-apnic-latestファイルから、日本に割り当てられた IPv4のCIDR表記を得るには、すでに説明した計算式を使って、次のように実行することができます。
curl -s 'https://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' \
| grep '|JP|ipv4|' \
| awk -F\| '{ printf("%s/%d\n", $4, 32-log($5)/log(2)) }'
IPv6の場合は計算は不要で、抽出するだけです。
curl -s https://ftp.apnic.net/stats/apnic/delegated-apnic-latest \
| grep '|JP|ipv6|' \
| awk -F'|' '{ printf("%s/%s\n", $4, $5) }'
このようにすることで日本に割り当てられたIPアドレス一覧を得ることができます。一覧データを、例えば NetFilterなどの定義に加えることで、日本以外からのアクセスを制限することもできます。
最後に
本稿では、国別のIPレピュテーションを手動で設定するために必要な基礎知識と、その実践手順を整理しました。RIRやNIRによるIPアドレス管理の仕組みを理解し、delegated-apnic-latest といった公開データを活用することで、自分の手で国別アドレスリストを抽出できることを確認しました。
こうした手動の試みは、普段はセキュリティ製品に任せてしまう領域の「裏側の仕組み」を理解する良い機会になります。IPアドレスがどのように割り当てられているかを知ることは、単なるフィルタリングにとどまらず、トラブルシューティングやインシデント対応の視点でも役立ちます。
次の一歩としては、ここで作成したスクリプトを自動更新の仕組みに組み込んだり、CIDRリストをファイアウォール設定に即座に反映できるようにするなど、実運用に耐える形へ発展させていくことが考えられます。