-

8 min read

A Guide to DNS Takeovers: The Misunderstood Cousin of Subdomain Takeovers

A Guide to DNS Takeovers: The Misunderstood Cousin of Subdomain Takeovers

Introduction

Let's start with this: A DNS takeover is not the same as a subdomain takeover.

Subdomain takeovers are old news. Hackers who caught onto them early made busloads of bounties by automating their detection and exploitation. They're still out there, but competition is fierce. Crafty hackers built bots that detect and report subdomain takeovers within minutes of them becoming vulnerable.

DNS takeovers are the new Orange. They've become popular among seasoned bug bounty hunters, but are often misunderstood or mistaken for subdomain takeovers.

By the time you've finished reading this article, you'll understand:

  • What a DNS takeover is
  • How it differs from a subdomain takeover
  • How to detect DNS takeovers
  • How to perform DNS takeovers
  • Which providers are vulnerable

Onward! 👇

Domain Name Service (DNS) primer

Put simply: you send a hostname to a DNS server and send back the IP address to which the hostname points.

In practice, DNS is far more complex.

Firstly, the process of resolving hostnames to IP addresses is quite complex and typically requires at least 3 or 4 DNS servers around the internet.

Secondly, DNS does more than just resolve hostnames to IP addresses, DNS also stores other information about domains, verification codes, subdomains, instructions on how/where to send emails, and more.

Resolving a hostname

When we want to resolve a hostname to an IP address, we send that hostname to a DNS server and ask for the corresponding IP address. The DNS server may have the hostname's IP cached, in which case it can just return the IP address to you. If it doesn't have the IP address cached, a whole other process begins.

Let's say we're resolving the hostname "www.google.com" without caching.

We'd first ask one of the Internet's root DNS servers where we can find details for domains ending in .com. These root servers are listed below:

bash

1
a.root-servers.net.
2
b.root-servers.net.
3
c.root-servers.net.
4
d.root-servers.net.
5
e.root-servers.net.
6
f.root-servers.net.
7
g.root-servers.net.
8
h.root-servers.net.
9
i.root-servers.net.
10
j.root-servers.net.
11
k.root-servers.net.
12
l.root-servers.net.
13
m.root-servers.net.

The response will be another list of DNS servers (below). We then ask one of these servers where we can find the details of domains ending in google.com.

bash

1
a.gtld-servers.net.
2
b.gtld-servers.net.
3
c.gtld-servers.net.
4
d.gtld-servers.net.
5
e.gtld-servers.net.
6
f.gtld-servers.net.
7
g.gtld-servers.net.
8
h.gtld-servers.net.
9
i.gtld-servers.net.
10
j.gtld-servers.net.
11
k.gtld-servers.net.
12
l.gtld-servers.net.
13
m.gtld-servers.net.

The response we get is another list of DNS servers (below), we're getting closer! We then query one of these servers, asking what IP address www.google.com is pointing to. You'll see that we're now querying the authoritative DNS servers that Google themselves maintains.

bash

1
ns1.google.com.
2
ns2.google.com.
3
ns3.google.com.
4
ns4.google.com.

Finally, one of these authoritative nameservers will respond with the IP address that www.google.com points to (it will vary):

172.217.167.100

What is a DNS takeover?

A DNS takeover occurs when an attacker can take control of any DNS server in the chain of DNS servers responsible for resolving a hostname. In other words, if we wanted to perform a DNS takeover on www.google.com, we'd need to take control of one of these DNS servers:

bash

1
a.root-servers.net.b.root-servers.net.
2
c.root-servers.net.
3
d.root-servers.net.
4
e.root-servers.net.
5
f.root-servers.net.
6
g.root-servers.net.
7
h.root-servers.net.
8
i.root-servers.net.
9
j.root-servers.net.
10
k.root-servers.net.
11
l.root-servers.net.
12
m.root-servers.net.
13
a.gtld-servers.net.
14
b.gtld-servers.net.
15
c.gtld-servers.net.
16
d.gtld-servers.net.
17
e.gtld-servers.net.
18
f.gtld-servers.net.
19
g.gtld-servers.net.
20
h.gtld-servers.net.
21
i.gtld-servers.net.
22
j.gtld-servers.net.
23
k.gtld-servers.net.
24
l.gtld-servers.net.
25
m.gtld-servers.net.
26
ns1.google.com.
27
ns2.google.com.
28
ns3.google.com.
29
ns4.google.com.

If we controlled any one of these DNS servers, we could change the www.google.com DNS record to point to a server we own instead of Google's website. Not only that, we could also create our own *@www.google.com email addresses, add falsified DNS records for any subdomain of *.www.google.com, and more.

What's the difference between a DNS takeover and a subdomain takeover?

A subdomain takeover occurs when you take over the service that some DNS records are pointing to. A DNS takeover occurs when you take over the DNS server that is assigned to that host.

DNS takeovers are typically more severe because they give the attacker more control. When you achieve a subdomain takeover, the activities you can perform on that host are limited to the capabilities of the service you takeover. For example, if you take over a S3 bucket - you can only host a website/files. When you achieve a DNS takeover you can host any service that takes your fancy, and you can log everything in the process.

How do DNS takeovers work?

So, how exactly can we take control of these DNS servers?

These days, most organizations don't host their own DNS servers - they use cloud providers. All major cloud providers offer a solution for hosting DNS infrastructure.

Most of these cloud providers allow you to create DNS "zones". A zone is just a set of DNS servers that can be managed through the provider's web interface. Here's an example of 4 DNS servers that you might be assigned when you create a Route53 zone in AWS (Note: Route53 is no longer susceptible to DNS takeover attacks):

bash

1
ns-261.awsdns-32.com.
2
ns-1032.awsdns-01.org.
3
ns-1739.awsdns-25.co.uk.
4
ns-1001.awsdns-61.net.

If you registered a new domain name and you wanted to use a cloud DNS provider as your authoritative nameservers, you would:

  • Create a new zone in the DNS provider
  • Set your nameservers to the DNS servers associated with your zone
  • Use the cloud provider console to add/remove/edit the DNS records associated with the domain

Now consider this scenario:

  • The victim buys a domain (example.com)
  • The victim sets up a zone with the DNS provider
  • The victim sets the zone as the authoritative nameservers for their new domain
  • At some point, the victim decided they didn't want to host anything on this domain anymore
  • The victim deletes the zone in the DNS provider but doesn't ever change the authoritative nameservers assigned to the domain.

This creates a situation where a domain's DNS records are controlled by DNS servers that anyone can use, but no one within this service claims the domain. As an attacker, if we can create a zone that uses any of the same nameservers, we could add DNS records for that domain.

Most DNS providers don't allow you to choose which nameservers get assigned. We can overcome this by creating many zones until we get one with a matching DNS server. This would be an extremely tedious process to perform manually - but we can automate the process.

Currently known vulnerable DNS services

A GitHub repository called "Can I Take Over DNS?" documents DNS services currently vulnerable to DNS takeovers and how to perform a takeover on each of them. It is a spinoff of the (in)famous "Can I Takeover XYZ?" repo by EdOverflow (BBAC), which documents the services vulnerable to subdomain takeovers.

At the time of writing, this repo marks the following services as vulnerable:

  • 000Domains
  • Bizland
  • Digital Ocean
  • DNSMadeEasy
  • DNSimple
  • Domain.com
  • Dotster
  • EasyDNS
  • Google Cloud
  • Hurricane Electric
  • Linode
  • MyDomain
  • Name.com
  • NS1
  • TierraNet
  • Reg.ru
  • Yahoo Small Business

I'm sure you can spot some big names on that list!

Detection of DNS takeovers

The exact way to check if a domain is vulnerable varies a bit depending on which service it is pointing to - but generally, there are two criteria:

  • The domain returns a SERVFAIL (or sometimes REFUSED) when you try to resolve it
  • The authoritative nameservers for that domain belong to one of the vulnerable services in the list above

Automating the process

Let's run through a process of detecting DNS takeovers that could be easily automated.

Finding the nameservers for a domain

Firstly, we need to find all nameservers used to resolve the domain we want to check. We can do this using a dig trace, like so:

$ dig tesla.com +trace

While this works well, it is quite a messy output, so I prefer to use my own fork of Olivier Poitrey's "dnstrace". I just added an option to the tool for a simple output of nameservers, without any other information.

bash

1
$ dnstrace -nsonly A google.com
2
3
l.gtld-servers.net.
4
b.gtld-servers.net.
5
c.gtld-servers.net.
6
d.gtld-servers.net.
7
e.gtld-servers.net.
8
f.gtld-servers.net.
9
g.gtld-servers.net.
10
a.gtld-servers.net.
11
h.gtld-servers.net.
12
i.gtld-servers.net.
13
j.gtld-servers.net.
14
k.gtld-servers.net.
15
m.gtld-servers.net.
16
ns2.google.com.
17
ns1.google.com.
18
ns3.google.com.
19
ns4.google.com.

Checking each nameserver

Now, you can check each of these nameservers, to see if any of them belong to a vulnerable service (e.g. ns*.digitalocean.com, ns*.domaindiscover.com, ns****.name.com…). In this case they do not. So in this case, we could see that a DNS takeover is not possible, and move on.

If any of them belonged to a vulnerable service, the next step would be to see if they return a REFUSED or SERVFAIL status when we resolve the domain. For the sake of example, let's check ns*.google.com against ns1.google.com using dig:

bash

1
$ dig @ns1.google.com google.com
2
3
; <<>> DiG 9.10.6 <<>> @ns1.google.com google.com
4
; (1 server found)
5
;; global options: +cmd
6
;; Got answer:
7
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 11314
8
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
9
;; WARNING: recursion requested but not available
10
;; OPT PSEUDOSECTION:
11
; EDNS: version: 0, flags:; udp: 512
12
;; QUESTION SECTION:
13
;google.com. IN A
14
;; ANSWER SECTION:
15
google.com. 300 IN A 142.250.76.110

We would repeat this process for ns2.google.com, ns3.google.com and ns4.google.com.

In this instance, the status returned is "NOERROR", meaning that the domain resolves properly and is therefore not vulnerable. If the status was "REFUSED" or "SERVFAIL" here, we can be fairly certain that the domain is vulnerable. The next step would be to take control of the domain!

Taking control of the domain

The exact method that we use to takeover the domain will depend on the DNS provider, but in general, the process looks something like this:

  1. Register an account with the DNS provider
  2. Create a DNS zone
  3. Check the nameservers that we were assigned to the zone
  4. If the nameservers don't match any of the authoritative nameservers of the domain, delete the DNS zone and return to step 2
  5. Create DNS records for the domain (e.g. an A record pointing to an IP address that we control)
  6. Wait for propagation (usually an hour or 2)

Using Nuclei

There are a few nuclei templates that are designed for DNS-based takeover detection, for example:

yaml

1
id: servfail-refused-hosts
2
3
info:
4
name: DNS Servfail Host Finder
5
author: pdteam
6
severity: info
7
description: A DNS ServFail error occurred. ServFail errors occur when there is an error communicating with a DNS server. This could have a number of causes, including an error on the DNS server itself, or a temporary
8
networking issue.
9
classification:
10
cwe-id: CWE-200
11
tags: dns,takeover
12
13
dns:
14
- name: "{{FQDN}}"
15
type: A
16
17
matchers:
18
- type: word
19
words:
20
- "SERVFAIL"
21
- "REFUSED"

This template will print a line if a DNS lookup on the tested hostname returns a SERVFAIL or REFUSED status. It doesn't go as far as checking if the DNS service is vulnerable, but this is a great start for initial detection.

Mitigation

Despite the risks, many organizations still have not figured out a good workflow for removing assets online without creating situations where they become vulnerable to DNS takeovers. It can be difficult to orchestrate the removal of assets without falling into a vulnerable state because it often requires coordination from multiple service providers.

In general, it helps to think of the removal of assets from the top down, i.e. remove assets in this order:

  1. Remove the domain, or change the authoritative nameservers to something that can not be hijacked
  2. Remove the DNS zone in your DNS provider
  3. Remove the actual asset/service

It also helps to monitor your own cloud environments for situations like this to give you a fighting chance of detecting it before a hacker does!

Conclusion

Hopefully, reading this has given you a good idea of how DNS takeovers work, how they differ from subdomain takeovers, and how to exploit them. Until next time - happy hacking!

- Ben Bidmead (@pry0cc) and Luke Stephens (@hakluke)