LoginSignup
3
2

More than 3 years have passed since last update.

GCPの公開IPレンジを調べるpythonコード

Last updated at Posted at 2019-09-18

はじめに

GCPリソースのIPレンジを知る必要があったので調べました

概ねここで議論されています
https://gist.github.com/n0531m/f3714f6ad6ef738a3b0a
ただ、シェル芸が読めなかったのでpythonで書き直しました

公式ページでの説明は下記になります
https://cloud.google.com/compute/docs/faq#find_ip_range

説明

_cloud-netblocks.googleusercontent.comのTXTレコードを取得すると、いくつかアドレスが返されます
それらのTXTレコードの中にCIDR表記のIPアドレスが入っています
注意点として、gistの議論にもあるのですが_cloud-netblocks6.googleusercontent.com_cloud-netblocks7.googleusercontent.comは再帰的に_cloud-netblocks1.googleusercontent.comの中に入っているため(2019年9月現在)、再帰を解釈できるよう書く必要があります

コード

import re
import dns.resolver

global ip4, ip6
ip4 = set()
ip6 = set()


def search_dns(target):
    global ip4, ip6
    for dns_info in dns.resolver.query(target, 'TXT'):
        servers = re.findall(r'include:(\S*)', str(dns_info))
        if len(servers) > 0:
            for s in servers:
                print(f'searching domain: {s}')
                search_dns(s)

        ip4 |= set(re.findall(r'ip4:(\S*)', str(dns_info)))
        ip6 |= set(re.findall(r'ip6:(\S*)', str(dns_info)))


spf_gcp = '_cloud-netblocks.googleusercontent.com'
search_dns(spf_gcp)
print('ip4')
print('\n'.join(sorted(ip4)))
print('ip6')
print('\n'.join(sorted(ip6)))
pyproject.toml
[tool.poetry]
name = "gcp_ip_range"
version = "0.1.0"
description = ""
authors = [""]

[tool.poetry.dependencies]
python = "^3.6"
dnspython = "^1.16"

[tool.poetry.dev-dependencies]

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

output IP range

(2019年9月時点)
変わってる可能性もあるので自分で動かしたほうが良さそうです

searching domain: _cloud-netblocks1.googleusercontent.com
searching domain: _cloud-netblocks6.googleusercontent.com
searching domain: _cloud-netblocks7.googleusercontent.com
searching domain: _cloud-netblocks2.googleusercontent.com
searching domain: _cloud-netblocks3.googleusercontent.com
searching domain: _cloud-netblocks4.googleusercontent.com
searching domain: _cloud-netblocks5.googleusercontent.com
ip4
104.154.0.0/15
104.196.0.0/14
107.167.160.0/19
107.178.192.0/18
108.170.192.0/20
108.170.208.0/21
108.170.216.0/22
108.170.220.0/23
108.170.222.0/24
108.59.80.0/20
130.211.128.0/17
130.211.16.0/20
130.211.32.0/19
130.211.4.0/22
130.211.64.0/18
130.211.8.0/21
146.148.16.0/20
146.148.2.0/23
146.148.32.0/19
146.148.4.0/22
146.148.64.0/18
146.148.8.0/21
162.216.148.0/22
162.222.176.0/21
173.255.112.0/20
192.158.28.0/22
199.192.112.0/22
199.223.232.0/22
199.223.236.0/23
208.68.108.0/23
23.236.48.0/20
23.251.128.0/19
34.100.0.0/16
34.102.0.0/15
34.104.0.0/14
34.124.0.0/18
34.64.0.0/11
34.96.0.0/14
35.184.0.0/14
35.188.0.0/15
35.190.0.0/17
35.190.128.0/18
35.190.192.0/19
35.190.224.0/20
35.190.240.0/22
35.192.0.0/14
35.196.0.0/15
35.198.0.0/16
35.199.0.0/17
35.199.128.0/18
35.200.0.0/13
35.208.0.0/13
35.216.0.0/15
35.220.0.0/14
35.224.0.0/13
35.232.0.0/15
35.234.0.0/16
35.235.0.0/17
35.235.192.0/20
35.235.216.0/21
35.235.224.0/20
35.236.0.0/14
35.240.0.0/13
8.34.208.0/20
8.35.192.0/21
8.35.200.0/23
ip6
2600:1900::/35

おまけ

下記によるとG SuiteのIPレンジも同じように取得できそうです
https://support.google.com/a/answer/60764

_spf.google.comから取るといけます(紛らわしいので出力は載せません)

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2