Skip to content

Instantly share code, notes, and snippets.

@unbelauscht
Last active May 21, 2024 17:15
Show Gist options
  • Save unbelauscht/6d4a4533acbb54618f7e564eb30d16c4 to your computer and use it in GitHub Desktop.
Save unbelauscht/6d4a4533acbb54618f7e564eb30d16c4 to your computer and use it in GitHub Desktop.
checks if a website is using cloudflare
#!/usr/bin/env python3
import ipaddress
import requests
import socket
from sys import argv, exit
def getIPs() -> ipaddress.ip_network:
r4 = requests.get("https://www.cloudflare.com/ips-v4")
r6 = requests.get("https://www.cloudflare.com/ips-v6")
ips = [ ipaddress.ip_network(ip) for ip in r4.content.decode().split("\n") ]
ips = ips + [ ipaddress.ip_network(ip) for ip in r6.content.decode().split("\n") ]
return ips
def resolveHost(host):
addr4 = socket.getaddrinfo(host, None, socket.AF_INET)
addr6 = socket.getaddrinfo(host, None, socket.AF_INET6)
ip4 = [ipaddress.ip_address(str(x[4][0])) for x in addr4]
ip6 = [ipaddress.ip_address(str(x[4][0])) for x in addr6]
ips = set([*ip4, *ip6])
return ips
def main():
cfips = getIPs()
ips = resolveHost(argv[1])
flagTrue = False
for ip in ips:
if any(ip in cfip for cfip in cfips):
print(f"{ip} is in a Cloudflare subnet")
flagTrue = True
else:
print(f"{ip} not in Cloudflare subnet")
if flagTrue:
exit(0)
else:
exit(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment