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
*.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
dig:
BASH
dig staging.example.com CNAME +shortstaging.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
Temporary mitigation: find and remove the obvious ones
BASH
# 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)The architectural fix: monthly DNS zone audits
/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.
