-
Notifications
You must be signed in to change notification settings - Fork 11
fix(dns-scanner): extended CNAME/NS results #7442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lcampbell2
wants to merge
7
commits into
master
Choose a base branch
from
fix/split-dns-query-scanner-results
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
26f6811
return dict of rcode answers for each dns query type
lcampbell2 314d207
add function to query and process ns delegations
lcampbell2 ddefa53
use zone apex for probe qname and recheck for ns hostnames
lcampbell2 0155dd6
better func names
lcampbell2 c213301
add registrar checks and move new logic to ns_registrar.py
lcampbell2 61b90a5
revert changes to record gate
lcampbell2 65ee95d
add CNAME rtype to record_exists check
lcampbell2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| import os | ||
|
|
||
| import dns | ||
| import dns.resolver | ||
| import requests | ||
| from dns.exception import Timeout | ||
| from dns.resolver import NXDOMAIN, NoAnswer, NoNameservers | ||
|
|
||
| TIMEOUT = int(os.getenv("SCAN_TIMEOUT", "20")) | ||
|
|
||
|
|
||
| def probe_nameserver( | ||
| where: str, qname: str, qtype: str, recursion_desired: bool, timeout: int | ||
| ): | ||
| query = dns.message.make_query( | ||
| qname, | ||
| dns.rdatatype.from_text(qtype), | ||
| use_edns=True, | ||
| ) | ||
| if not recursion_desired: | ||
| query.flags &= ~dns.flags.RD | ||
| return dns.query.udp(query, where=where, timeout=timeout) | ||
|
|
||
|
|
||
| def get_ns_ip(host: str, resolver): | ||
| ns_ip = None | ||
| try: | ||
| ns_a = resolver.resolve(host, "A") | ||
| if ns_a: | ||
| ns_ip = ns_a[0].to_text() | ||
| except (NoAnswer, NXDOMAIN, NoNameservers, Timeout): | ||
| ns_ip = None | ||
|
|
||
| if ns_ip is None: | ||
| try: | ||
| ns_aaaa = resolver.resolve(host, "AAAA") | ||
| if ns_aaaa: | ||
| ns_ip = ns_aaaa[0].to_text() | ||
| except (NoAnswer, NXDOMAIN, NoNameservers, Timeout): | ||
| ns_ip = None | ||
|
|
||
| return ns_ip | ||
|
|
||
|
|
||
| def check_ns_delegations(domain, zone_apex, ns_records, resolver=None, timeout_sec=10): | ||
| if resolver is None: | ||
| resolver = dns.resolver.get_default_resolver() | ||
|
|
||
| qname = zone_apex | ||
| if not zone_apex: | ||
| qname = domain | ||
|
|
||
| ns_hosts = ns_records.get("hostnames", []) | ||
| if len(ns_hosts) == 0: | ||
| try: | ||
| ns_res = resolver.resolve(domain, dns.rdatatype.NS) | ||
| ns_hosts = [host.to_text() for host in ns_res] | ||
| except (NoAnswer, NXDOMAIN, NoNameservers, Timeout): | ||
| ns_hosts = [] | ||
|
|
||
| output = { | ||
| "ns_hosts": ns_hosts, | ||
| "ns_checks": [], | ||
| "ns_delegation": { | ||
| "total_ns": len(ns_hosts), | ||
| "authoritative_ok": 0, | ||
| "lame_count": 0, | ||
| "lame_type": "none", | ||
| }, | ||
| } | ||
| if len(ns_hosts) == 0: | ||
| output["ns_delegation"]["lame_type"] = "unknown" | ||
| return output | ||
|
|
||
| for host in ns_hosts: | ||
| row = { | ||
| "ns_host": host, | ||
| "qname": qname, | ||
| "qtype": "SOA", | ||
| "rcode": None, | ||
| "answered_authoritatively": False, | ||
| "error": None, | ||
| "timeout": False, | ||
| } | ||
|
|
||
| try: | ||
| ns_ip = get_ns_ip(host, resolver) | ||
| if ns_ip is None: | ||
| row["error"] = "ns_ip_resolution_failed" | ||
| output["ns_delegation"]["lame_count"] += 1 | ||
| output["ns_checks"].append(row) | ||
| continue | ||
|
|
||
| res = probe_nameserver(ns_ip, qname, "SOA", False, timeout_sec) | ||
| row["rcode"] = dns.rcode.to_text(res.rcode()) | ||
| row["answered_authoritatively"] = bool(res.flags & dns.flags.AA) | ||
|
|
||
| if row["answered_authoritatively"] and row["rcode"] in [ | ||
| "NOERROR", | ||
| "NXDOMAIN", | ||
| ]: | ||
| output["ns_delegation"]["authoritative_ok"] += 1 | ||
| else: | ||
| output["ns_delegation"]["lame_count"] += 1 | ||
| except Timeout: | ||
| row["timeout"] = True | ||
| row["error"] = "timeout" | ||
| output["ns_delegation"]["lame_count"] += 1 | ||
| except Exception as e: | ||
| row["error"] = str(e) | ||
| output["ns_delegation"]["lame_count"] += 1 | ||
|
|
||
| output["ns_checks"].append(row) | ||
|
|
||
| ok = output["ns_delegation"]["authoritative_ok"] | ||
| total = output["ns_delegation"]["total_ns"] | ||
|
|
||
| if ok == total: | ||
| output["ns_delegation"]["lame_type"] = "none" | ||
| elif ok == 0: | ||
| output["ns_delegation"]["lame_type"] = "full" | ||
| else: | ||
| output["ns_delegation"]["lame_type"] = "partial" | ||
|
|
||
| return output | ||
|
|
||
|
|
||
| def get_registrar_context(base_domain, ns_hosts=None): | ||
| context = { | ||
| "base_domain": base_domain, | ||
| "lookup_success": False, | ||
| "rdap_url": None, | ||
| "registrar_name": None, | ||
| "registrar_id": None, | ||
| "rdap_nameservers": [], | ||
| "delegation_matches_rdap": None, | ||
| "error": None, | ||
| } | ||
|
|
||
| if not base_domain: | ||
| context["error"] = "missing_base_domain" | ||
| return context | ||
|
|
||
| rdap_url = f"https://rdap.org/domain/{base_domain}" | ||
| context["rdap_url"] = rdap_url | ||
|
|
||
| try: | ||
| response = requests.get(rdap_url, timeout=TIMEOUT) | ||
| response.raise_for_status() | ||
| payload = response.json() | ||
| except Exception as e: | ||
| context["error"] = str(e) | ||
| return context | ||
|
|
||
| context["lookup_success"] = True | ||
|
|
||
| nameservers = payload.get("nameservers", []) | ||
| context["rdap_nameservers"] = [ | ||
| (ns.get("ldhName") or "").rstrip(".").lower() | ||
| for ns in nameservers | ||
| if ns.get("ldhName") | ||
| ] | ||
|
|
||
| entities = payload.get("entities", []) | ||
| for entity in entities: | ||
| roles = [r.lower() for r in entity.get("roles", [])] | ||
| if "registrar" not in roles: | ||
| continue | ||
|
|
||
| context["registrar_id"] = entity.get("handle") | ||
|
|
||
| vcard = entity.get("vcardArray", []) | ||
| if isinstance(vcard, list) and len(vcard) == 2 and isinstance(vcard[1], list): | ||
| for entry in vcard[1]: | ||
| if not isinstance(entry, list) or len(entry) < 4: | ||
| continue | ||
| key = entry[0] | ||
| value = entry[3] | ||
| if key in {"fn", "org"} and value: | ||
| context["registrar_name"] = value | ||
| break | ||
|
|
||
| if context["registrar_name"] is None: | ||
| public_ids = entity.get("publicIds", []) | ||
| if public_ids: | ||
| context["registrar_name"] = public_ids[0].get("identifier") | ||
|
|
||
| break | ||
|
|
||
| if ns_hosts is not None: | ||
| normalized_hosts = {h.rstrip(".").lower() for h in ns_hosts if h} | ||
| rdap_hosts = set(context["rdap_nameservers"]) | ||
| if rdap_hosts: | ||
| context["delegation_matches_rdap"] = normalized_hosts == rdap_hosts | ||
|
|
||
| return context | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm having trouble getting this to flag a real lame nameserver, do you have a working domain I can test it against?