Skip to content

Commit

Permalink
async dns resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
gray-host committed Mar 12, 2024
1 parent 4fef9ba commit d3363cf
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions manual-scans/gcp-cname.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import datetime

import aiohttp
import dns.resolver
import dns.asyncresolver
import google.cloud.dns
import asyncio
from utils_vulnerable import get_vulnerable_list
Expand All @@ -14,34 +12,36 @@
vulnerable_domains = []
vulnerability_list = get_vulnerable_list()

def vulnerable_cname(domain_name):

async def vulnerable_cname(domain_name):
# Handle wildcard A records by passing in a random 5 character string
if domain_name[0] == "*":
random_string = "".join(choice(ascii_letters + digits) for _ in range(5))
domain_name = random_string + domain_name[1:]

global aRecords

try:
aRecords = dns.resolver.resolve(domain_name, "A")
return False
result = await dns.asyncresolver.resolve(domain_name, "A")
return ""

except dns.resolver.NXDOMAIN:
try:
dns.resolver.resolve(domain_name, "CNAME")
return True
result = await dns.asyncresolver.resolve(domain_name, "CNAME")
return domain_name

except dns.resolver.NoNameservers:
return False
return ""

except (dns.resolver.NoAnswer, dns.resolver.NoNameservers):
return False
return ""


def gcp(project):
async def run_queries(queries):
return await asyncio.gather(*queries)

i = 0

def gcp(project):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
print(f"Searching for Google Cloud DNS hosted zones in {project} project")
dns_client = google.cloud.dns.client.Client(project)
try:
Expand All @@ -57,23 +57,17 @@ def gcp(project):
r
for r in records
if "CNAME" in r.record_type
and r.rrdatas
and any(vulnerability in r.rrdatas[0] for vulnerability in vulnerability_list.keys())
and r.rrdatas
and any(vulnerability in r.rrdatas[0] for vulnerability in vulnerability_list)
]

for resource_record_set in resource_record_sets:
cname_record = resource_record_set.name
cname_value = resource_record_set.rrdatas[0]
print(f"Testing {resource_record_set.name} for vulnerability")
result = vulnerable_cname(cname_record)
i = i + 1
if result:
vulnerable_domains.append(cname_record)
my_print(f"{str(i)}.{cname_record} CNAME {cname_value}", "ERROR")

else:
my_print(f"{str(i)}.{cname_record} CNAME {cname_value}", "SECURE")

queries = [loop.create_task(vulnerable_cname(rrset.name))
for rrset
in resource_record_sets]
tasks = asyncio.run(run_queries(queries))
for result in tasks:
if result != "":
vulnerable_domains.append(result)
return 1

except google.api_core.exceptions.Forbidden:
Expand Down

0 comments on commit d3363cf

Please sign in to comment.