-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
cloudflare_utils.py
64 lines (49 loc) · 1.73 KB
/
cloudflare_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import dns
import ipaddress
import sys
import requests
def get_cloudflare_ip_ranges():
cloudflare_ip_ranges_url = 'https://www.cloudflare.com/ips-v4'
ip_ranges = []
ip_ranges_fallback = [
"103.21.244.0/22",
"103.22.200.0/22",
"103.31.4.0/22",
"104.16.0.0/12",
"108.162.192.0/18",
"131.0.72.0/22",
"141.101.64.0/18",
"162.158.0.0/15",
"172.64.0.0/13",
"173.245.48.0/20",
"188.114.96.0/20",
"190.93.240.0/20",
"197.234.240.0/22",
"198.41.128.0/17"
]
try:
print('[*] Retrieving Cloudflare IP ranges from {}'.format(cloudflare_ip_ranges_url))
page_content = requests.get(cloudflare_ip_ranges_url, timeout=10)
ip_ranges_text = page_content.text
ip_ranges = [ip for ip in ip_ranges_text.split("\n") if ip]
except requests.exceptions.RequestException:
sys.stderr.write('[-] Failed to retrieve Cloudflare IP ranges - using a default (possibly outdated) list\n')
ip_ranges = ip_ranges_fallback
finally:
return ip_ranges
cloudflare_ip_ranges = get_cloudflare_ip_ranges()
if sys.version_info[0] == 2:
cloudflare_subnets = [ipaddress.ip_network(ip_range.decode('utf-8')) for ip_range in cloudflare_ip_ranges]
else:
cloudflare_subnets = [ipaddress.ip_network(ip_range) for ip_range in cloudflare_ip_ranges]
def is_cloudflare_ip(ip):
for cloudflare_subnet in cloudflare_subnets:
if cloudflare_subnet.overlaps(ipaddress.ip_network(ip)):
return True
return False
def uses_cloudflare(domain):
answers = dns.resolver.query(domain, 'A')
for answer in answers:
if is_cloudflare_ip(answer):
return True
return False