Skip to content

Instantly share code, notes, and snippets.

@TobiX
Created February 24, 2017 16:05
Show Gist options
  • Save TobiX/1df4905760026959d8ed991c4dbab5ff to your computer and use it in GitHub Desktop.
Save TobiX/1df4905760026959d8ed991c4dbab5ff to your computer and use it in GitHub Desktop.
Quick-and-dirty script to find sites hosted on CloudFlare
#!/usr/bin/python3
import argparse
# pip install urlextract dnspython tldextract
import dns.name
import dns.resolver
import urlextract
import tldextract
def read_urls(infile):
extractor = urlextract.URLExtract()
urls = set()
with open(infile, "r", encoding="UTF-8") as f:
for line in f:
urls.update(extractor.find_urls(line))
return urls
def lookup_hosts(domains, verbose):
cf = dns.name.from_text('cloudflare.com')
for domain in domains:
try:
answers = dns.resolver.query(domain, 'NS')
is_cf = any(x.target.is_subdomain(cf) for x in answers)
if verbose:
print("%-40s: %s" % (domain, is_cf))
elif is_cf:
print(domain)
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN) as e:
print("%s: %s" % (domain, e))
def get_args():
parser = argparse.ArgumentParser(description='checks for each url from a file if it is hosted on CloudFlare')
parser.add_argument(type=str, default=None, metavar='<file>', dest='file',
help='input text file with URLs to extract. [UTF-8]')
parser.add_argument('--verbose', dest='verbose', action='store_true')
args = parser.parse_args()
return args
if __name__ == '__main__':
args = get_args()
urls = read_urls(args.file)
domains = (tldextract.extract(url).registered_domain for url in urls)
lookup_hosts(domains, args.verbose)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment