DevOps 6 min read 1150 words

A forgotten DNS record and an IP that's no longer yours

ES
A forgotten DNS record and an IP that's no longer yours

Woodpeckers spend weeks drilling holes in tree trunks to nest, and when they leave, the hole stays there. It doesn’t take long for another tenant (a starling, a little owl, a bat…) to show up and settle in as if it owned the place. The tree doesn’t notice, and whoever comes looking for the woodpecker at the usual address finds a different creature.

Well, that very thing, with DNS records and AWS IP addresses, is what Intruder has just described on its blog: they took over 28 subdomains from bug bounty programmes, almost half of them belonging to public bodies, including five from the US Department of the Treasury (ftp, irssearch, pcp, rdgw and ww5, all under treasury.gov). According to them, at about 3 dollars per subdomain.

The same old problem

This isn’t new, it even has a name: subdomain takeover through a dangling DNS record. The typical story goes like this: you set up a service on an EC2 instance, give it an Elastic IP (a fixed public IP from AWS) and create an A record pointing to it. One day the service is retired, you delete the instance, release the IP so you stop paying for it… and the DNS record stays there, pointing to an address that goes back into AWS’s common pool and that the next customer may get.

Until now it was assumed this was more theoretical than practical, because Elastic IPs are allocated at random and AWS has an awful lot of them. What Intruder has done is show that, with patience and a script, you can force that randomness.

How they did it

The method, broadly speaking, is of the “keep trying until it works” kind:

  1. Target list: they took the wildcard domains from public bug bounty programmes, enumerated their subdomains and stored which IP each one pointed to.
  2. Request IPs in a loop: they allocated a batch of Elastic IPs, checked whether any were on their list, kept the matches and released the rest. And round again.
  3. More quota: the default limit is 5 Elastic IPs per region, and they raised it to 25 through AWS’s automatic quota approval, in every region.
  4. Rotate regions: if you allocate and release very often in the same region, you tend to get back the IPs you just let go, so they rotated to give each pool time to refresh.

The numbers are what make you think:

ItemValue
IPs cycled in 48 hoursabout 450,000
Rateabout 9,400 IPs per hour
Published EC2 IPv4 addressesabout 80 million (the allocatable ones, probably half)
Covering them all with one accountaround a year
With ten accountsabout five weeks
The eu-west-2 region aloneabout 2.2 million
Subdomains taken over28

In other words, with 0.56% of the space they already got 28. And if instead of taking whatever comes you go after a specific company, knowing which region its servers are in, the haystack is quite a bit smaller.

AWS’s response

After 48 hours, AWS sent them an abuse warning and blocked Elastic IP allocation in every region. Which is fine… except that they moved the IPs they already had to another account in the same organisation and they kept working, and an attacker with a blocked account just has to open another one.

They contacted AWS on 8 October, and on 17 November AWS confirmed it has no plans to change anything: in its view the problem lies in the customer’s DNS records, and allocating and releasing IPs at full speed is already against its usage policies. Intruder suggested a monthly allocation cap and applying blocks per organisation rather than per account, but nothing for now.

I understand AWS’s position, the DNS record is yours and you have to clean it up, but it’s also true that AWS is the one holding the IP pool, and a reasonable limit wouldn’t hurt anyone using the cloud normally.

What you can do with someone else’s subdomain

It may seem like a small thing, an old subdomain nobody uses, but there’s more to it than meets the eye:

  • Convincing phishing: a page on something.treasury.gov passes every “is this the official domain?” check we’ve been taught to do. And since you control the IP, you can get a valid TLS certificate without any trouble.
  • Credentials that arrive on their own: if some old automation still calls that subdomain, now it calls you, with whatever users, tokens or anything else it carries.
  • Parent domain cookies: if an application sets its cookies with Domain= on the main domain, the browser also sends them to every subdomain, including the one that’s no longer yours.

The cookie one is easy to see with an example:

Set-Cookie: session=abc123; Domain=example.com; Secure; HttpOnly

That cookie travels to www.example.com, to app.example.com… and to old.example.com, which is now served by someone else. If you don’t need to share it between subdomains, the simplest thing is to leave Domain out and let it stay on the host that created it.

Checking your own

The good news is that the fix is in your hands. Retiring a service includes deleting its DNS, just as it includes shutting down the instance or releasing the IP, and if possible in that order: DNS first, then the IP. And every now and then it’s worth doing a pass to look for records pointing to IPs you no longer have.

If you use Route 53, a starting point could be something like this (careful, it’s a quick hack, not an audit):

# Elastic IPs you have right now, in every region
for r in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text); do
  aws ec2 describe-addresses --region "$r" --query 'Addresses[].PublicIp' --output text
done | tr '\t' '\n' | grep . | sort -u > my-ips.txt

# IPs the A records in a zone point to
aws route53 list-resource-record-sets --hosted-zone-id YOUR_ZONE \
  --query "ResourceRecordSets[?Type=='A'].ResourceRecords[].Value" --output text \
  | tr '\t' '\n' | grep . | sort -u > dns-ips.txt

# The ones in DNS that aren't yours
comm -13 my-ips.txt dns-ips.txt

Whatever comes out has to be looked at calmly, because you’ll also see IPs that aren’t from AWS (other providers, your office…) or public IPs of instances that aren’t elastic, which carry the same risk once the instance is gone. But any IP within AWS’s ranges that isn’t yours is a woodpecker hole waiting for a tenant. And if your DNS lives somewhere else, the idea is the same: export the records, compare and delete what’s left over.

What I take away from all this is that technical debt isn’t always in the code: sometimes it’s a line in a DNS panel nobody remembers creating. The Treasury ones have already been fixed thanks to the bug bounty, but there are surely many more trunks with empty holes, and now we know that someone with a script and a few dollars can go and check them.