Someone on your team spins up a staging environment, points staging.example.com via a CNAME to an S3 bucket or a cloud load balancer, finishes the project, tears down the cloud resource, and moves on. The CNAME stays. Weeks later, an attacker claims that same S3 bucket name or cloud hostname, and now staging.example.com serves whatever they want under your domain. This is a subdomain takeover, and it happens more often than most teams expect.
The pattern repeats across support queues: a security scanner flags a subdomain serving unexpected content, the team scrambles to figure out who created the record, and the fix is deleting one DNS entry. The real fix is never needing that scramble again.
Initial impact: a subdomain serving someone else's content
The typical discovery goes like this. A security tool, a penetration tester, or sometimes a confused visitor reports that a subdomain on your domain returns content you did not publish. Maybe it is a default cloud provider page. Maybe it is something worse — phishing content, SEO spam, or a credential harvesting form, all served under your legitimate domain name.
Because the subdomain is yours, browsers trust it. Cookies scoped to *.example.com may be readable. If you have a wildcard SSL certificate, the attacker's content gets served over HTTPS with a valid certificate bearing your domain. From a visitor's perspective, it looks official.
Investigation: tracing the CNAME to nowhere
The investigation usually starts with dig:
dig staging.example.com CNAME +short
This returns something like staging.example.com.s3.amazonaws.com. or old-project-abc123.us-east-1.elb.amazonaws.com. — a target hostname that no longer resolves to a resource you control.
The critical question: does that target still exist in your cloud account? If the answer is no, and the cloud provider allows other accounts to claim the same hostname (which is the case for services like S3 bucket names, Azure App Service custom domains, GitHub Pages, Heroku apps, and others), then anyone can register that name and receive traffic from your subdomain.
This is the core of a dangling CNAME. The DNS record points somewhere. That somewhere is not yours anymore. Whoever claims it next inherits your subdomain's traffic.
Why this keeps happening
Three patterns cause most dangling CNAMEs:
Provisioning and teardown are handled by different people. The developer who created the staging environment is not the same person who manages DNS. When the cloud resource gets deleted, nobody tells the DNS admin. Or the DNS admin is the developer, who simply forgot.
Infrastructure-as-code covers compute but not DNS. Teams use Terraform or CloudFormation for servers and load balancers but manage DNS through a web panel. The teardown playbook destroys the stack but leaves the zone file untouched.
Long TTLs hide the problem. If the CNAME has a TTL of several hours or days, the breakage is not immediately visible after deleting the target resource. By the time the cached record expires and resolvers try to follow the CNAME to a dead end, the team has moved on. The record sits in the zone, forgotten, until someone with bad intentions finds it.
For context on how DNS caching and TTL behavior can mask changes, there is a related discussion in Lowered DNS TTL Before Migration, but Visitors Still Hit the Old Server.
Temporary mitigation: find and remove the obvious ones
The immediate fix for a known dangling CNAME is straightforward: delete the DNS record. But the harder question is finding all of them across your zones before an attacker does.
A basic approach: export every CNAME from your zone, then check whether each target resolves. Here is a minimal script that works with a standard zone file or any tool that can list your CNAMEs:
# List all CNAMEs in a zone, then check if each target resolves
# Adjust the zone export method for your DNS provider or control panel
while IFS= read -r line; do
name=(echo "line" | awk '{print $1}')
target=(echo "line" | awk '{print NF}' | sed 's/\\.//')
result=(host "target" 2>&1)
if echo "$result" | grep -qi 'NXDOMAIN\\|not found\\|no address'; then
echo "DANGLING: name ->target"
fi
done < <(grep -i 'CNAME' /path/to/your/zonefile)
This is deliberately simple. It checks whether the CNAME target returns NXDOMAIN (the domain does not exist) or has no address records. A result of NXDOMAIN is a strong signal that the target resource was deleted and the CNAME is dangling.
However, NXDOMAIN alone does not catch every case. Some cloud services return a valid IP even after you delete your resource — the hostname still resolves, but it points to a default "resource not found" page that someone else could later claim. For those, you need to go further: check whether the target resource actually exists in your cloud account using your provider's CLI or API.
The architectural fix: monthly DNS zone audits
Deleting one dangling record fixes one subdomain. Preventing the pattern requires a recurring audit. Monthly is a reasonable cadence for most teams — frequent enough to catch records before attackers do, infrequent enough to not be ignored.
A useful monthly audit covers three checks:
1. Export all CNAME records from every zone you manage. How you do this depends on your DNS provider. Some offer API access or CLI tools. If you manage DNS through cPanel/WHM, you can parse the zone files in /var/named/ on the server. If you use a cloud DNS service, use its API or CLI to list records.
2. For each CNAME target, verify it resolves and that you control the destination. Resolution alone is not enough. You need to confirm the target resource exists in your account. For cloud resources, this means querying your provider's API. For example, if a CNAME points to an S3 bucket hostname, check whether that bucket exists in your account. If it points to a load balancer, check whether that load balancer is still provisioned.
3. Flag and remove records whose targets are gone. Any CNAME whose target returns NXDOMAIN is an immediate candidate for deletion. Any CNAME whose target resolves but does not correspond to a resource in your account needs investigation — it might be a third-party service you still use, or it might be dangling.
The output of this audit should be a short list: subdomain, CNAME target, status (resolves/NXDOMAIN), and whether the target resource exists in your infrastructure. Review it, delete what is dead, and document what remains.
What to watch beyond CNAMEs
CNAME records get the most attention for subdomain takeover, but they are not the only vector. A records pointing to IP addresses you no longer own (released elastic IPs, deprovisioned VPS instances) create a similar risk. The new owner of that IP receives your subdomain's traffic.
NS delegations are another blind spot. If you delegated a subdomain's DNS to a third-party service and later stopped using that service, the NS records may still point there. If the attacker can create an account on that service and claim authority over your subdomain's zone, they control all records under it.
MX records pointing to decommissioned mail servers can also cause problems — not subdomain takeover in the traditional sense, but potential email interception if someone else claims the target hostname.
For related DNS record conflicts worth understanding, see Why a CNAME on Your Root Domain Breaks MX and TXT Records.
Making the audit stick
The audit only works if it actually runs. A few practical suggestions:
Tie it to an existing process. If your team already has a monthly infrastructure review, security standup, or change advisory board meeting, add the DNS audit output as a standing agenda item. A five-minute review of a short list is easy to maintain. A separate "DNS audit meeting" will get skipped by month three.
Automate the export and check, review the results manually. The script that lists dangling CNAMEs can run on a cron job or a CI pipeline and post results to a Slack channel, email, or ticket. Automated deletion is risky — you might remove a record that looks dangling but is actually pointing to a partner's infrastructure. Human review of a short list is fast and safe.
Track DNS records alongside infrastructure provisioning. If your team uses infrastructure-as-code, include DNS records in the same stack or module as the resource they point to. When the resource is destroyed, the DNS record goes with it. This does not eliminate the need for audits (manual changes still happen), but it reduces the volume of drift.
Document ownership. For every CNAME that points to an external service, note who owns the relationship and when it was last verified. A CNAME to a SaaS provider's hostname is fine as long as your account with that provider is active. If the account lapses, the CNAME becomes dangling.
Dangling DNS records are one of those problems that feel minor until they are not. The fix is not complicated — export, check, delete, repeat. The hard part is doing it consistently. A monthly cadence, a simple script, and five minutes of human review keep your subdomains from becoming someone else's attack surface.