Press enter or click to view image in full size
Let’s look into the subdomain takeover vulnerability, what makes a web app vulnerable to it, and how to avoid this from happening.
The best way to understand Subdomain take over is to understand what is happening in the DNS layer, so let’s take a look at our example using the command dig.
digWhen we run dig CNAME domain we are generally asking, "Does this domain have a CNAME record?" CNAME Record is an alias record for another domain, useful when multiple names should refer to the same host or service. However, this sometimes can also make an application vulnerable if the CNAME is living outside your control.
Let’s take a closer look to understand why this domain is not vulnerable to domain takeover.
dig CNAME myapp.api.acme.comWhen we run `dig CNAME domain` we are generally asking, “Does this domain have a CNAME record?”
CNAME Record is an alias record for another domain, useful when multiple names should refer to the same host or service. However, this sometimes can also make an application vulnerable if the CNAME is living outside your control.
Let’s take a closer look to understand why this domain is not vulnerable to domain takeover.
[..redacted..]
;; QUERY: 1, ANSWER: 0
;; AUTHORITY SECTION:
acme.com. 60 IN SOA ns-cloud-b1.googledomains.com. cloud-dns-hostmaster.google.com.
[..redacted..]From the output, we can see ANSWER: 0 , so there is not CNAME record. When we asked if the above domain had a valid CNAME record the answer was no. However, it does not mean it does not point anywhere, it only means it doesn’t use a CNAME record to do so.
There still could be a A, AAAA, ALIAS for the record, which is the case on our example.
myapp.api.acme.com.acme.com, managed on Google Cloud DNS. So the subdomain myapp.api.acme.com doesn’t point anywhere — but it’s still defined in the DNS zone.acme.com) still owns it.Below is a diagam showing the normal state flow of an application that is vulnerable to subdomain take over that is working correctly, then when it goes into limbo state which is when the application is removed but the CNAME record still exist within the ACME DNS zone.
At last what happens when an attacker successfully takes over the subdomain.
Press enter or click to view image in full size
dig CNAME test2.cybersamurai.co.uk[...redacted...]
;;QUERY: 1, ANSWER: 1
;; AUTHORITY SECTION:
test2.cybersamurai.co.uk. 300 IN CNAME v0.app.
[...redacted...]
The CNAME for this record is been re-directed to v0.app which is a third-party making this domain vulnerable to subdomain takeover. If the app is deleted anyone could create a new App within v0.app with that same name and instantly serve content under test2.cybersamurai.co.uk
myapp.api.acme.com would become vulnerable if Acme added a record like:
myapp.api.acme.com. CNAME stage-app.herokuapp.com.In that case:
no such app.stage-app and take control of the subdomain.However:
acme.com subdomain or a GCP-managed service), that’s safe — because no one else can claim that target.So the rule is:
The best way to learn is to write it, and if possible why not write your own program to help solidify your understanding of a subject, and this is what I did.
There are a few good tools already to check for Domain take over I am familiar with.
subsy run --targets domains.txt --timeout 30
subjack -w domains.txt --timeout 30 -t 100 -v
nuclei -tl | grep takeover
However, I find the best way to learn is to try writing something myself, so I can see the code in action. As well as an old friend told me, "That best way to learn is repetition repetition repetition"
Here is a tool I created called subtake
Subtake is available on github and will perform the checks we have talked about on this article.
The main program is written in bash script build using dig and regex, simple yet powerful tools.
It will perform a lookup on the domain specified or file list provided, If the result from the dig CNAME domain is available, and there is a CNAME for the domain within our vulnerable list, and the IP does not exist, this will tag the domain as "Vulnerable". If the CNAME is within our vulnerable list and the IP exist the domain is tagged as "At Risk" if the CNAME does not exist or is not within our vulnerable list the domain is marked as "Safe" Any other results its marked as "Unknown".
If you enjoy the article or have any feedback on anything mentioned comment below.