Last active
April 18, 2018 19:42
-
-
Save Kentzo/a7673e4d55579b06ef5347bbe7a4bdd6 to your computer and use it in GitHub Desktop.
Resolve IP ranges of GCE, AWS and Cloudflare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Needs the requests and dnspython packages. | |
""" | |
import ipaddress | |
import re | |
import dns.resolver | |
import requests | |
def resolve_cloudflare(): | |
""" | |
@see: https://www.cloudflare.com/ips/ | |
""" | |
v4 = requests.get('https://www.cloudflare.com/ips-v4').text.strip().split('\n') | |
v6 = requests.get('https://www.cloudflare.com/ips-v6').text.strip().split('\n') | |
return v4, v6 | |
def resolve_aws(): | |
""" | |
https://docs.aws.amazon.com/general/latest/gr/aws-ip-ranges.html | |
""" | |
r = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json').json() | |
v4 = [p['ip_prefix'] for p in r['prefixes']] | |
v6 = [p['ipv6_prefix'] for p in r['ipv6_prefixes']] | |
return v4, v6 | |
def resolve_gce(): | |
""" | |
@see: https://gist.github.com/ShakataGaNai/f2a6538f5a6c8081e04b54ed073f0c34 | |
""" | |
def que(netblock): | |
v4 = [] | |
v6 = [] | |
resr = dns.resolver.Resolver() | |
resr.nameservers = ['8.8.8.8'] | |
ans = resr.query(netblock,'TXT') | |
pi = re.compile('include:(.*?)\s',re.IGNORECASE) | |
p4 = re.compile('ip4:(.*?)\s',re.IGNORECASE) | |
p6 = re.compile('ip6:(.*?)\s',re.IGNORECASE) | |
for line in ans: | |
st = line.to_text() | |
for a in pi.findall(st): | |
a4, a6 = que(a) | |
v4.extend(a4) | |
v6.extend(a6) | |
v4.extend(p4.findall(st)) | |
v6.extend(p6.findall(st)) | |
return(v4, v6) | |
return que('_cloud-netblocks.googleusercontent.com') | |
def resolve(): | |
all_v4 = set() | |
all_v6 = set() | |
for resolve in [resolve_cloudflare, resolve_aws, resolve_gce]: | |
v4, v6 = resolve() | |
all_v4.update({ipaddress.ip_network(n) for n in v4}) | |
all_v6.update({ipaddress.ip_network(n) for n in v6}) | |
return all_v4, all_v6 | |
if __name__ == '__main__': | |
print(resolve()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment