Mapping your AWS attack surface
Tags: sql, cloudsecurity, opensource, aws
Published: 2022-12-27 02:47:00

## What is a cloud attack surface?

Traditional IT security protects the network perimeter with firewalls, vulnerability scanners and patching processes. Discovering the attack surface of your network perimeter was a simple matter of knowing your public IP range and scanning it from an external host.  With the introduction of public cloud technologies, the attack surface expanded to include cloud provider’s APIs.  "Identity is the new perimeter" became the new mantra.

The old network perimeter still exists, of course, and  it has evolved. No longer are all the IP addresses that make up your perimeter assigned to your company. In the cloud, your network perimeter is part of the cloud provider’s IP space. And, cloud-provider-managed resources provide a direct path to your application code. This combination of cloud-provider IP addresses assigned to you, and the URLs of cloud-provider resources that lead to your application and data, comprise your cloud’s network perimeter and define part of the cloud attack surface.

An Organization must monitor and understand the network perimeter of their cloud estate. Resources comprising the externally facing network components of your cloud attack surface can be broadly grouped into IP addresses, hostnames, and URLs. In this blog post, we will provide step-by-step instructions for mapping the network aspects of the cloud attack surface using [Steampipe](https://steampipe.io/).

## Enumerate IP Addresses using Steampipe

For IP Addresses, we'll look mainly at the Elastic Network Interfaces. While other resources like [CloudFront](https://aws.amazon.com/cloudfront/), [S3](https://aws.amazon.com/s3/), and [API Gateway](https://aws.amazon.com/api-gateway/) also leverage IP addresses, the network security protections of those services are the cloud provider's responsibility.

[This query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/public_vpc_ips.sql) will download a list of all public IP addresses tied to the customer’s VPC.

```sql
select
  eni.association_public_ip AS public_ip
from
  aws_ec2_network_interface AS eni
where
  eni.association_public_ip is not Null;
```

## Enumerate Hostnames using Steampipe

To determine the DNS Hostnames used as part of your cloud perimeter, Steampipe can query all of the A records and CNAMEs in your [Route 53](https://aws.amazon.com/route53/) Hosted Zones. A records point directly to IP addresses under your control. CNAMEs are references that can point to hosts or other cloud-provider-managed resources. In either case, you need to understand what exists in your environment.

To query all of the hostnames that point to CNAME and A Records in your Route 53 Hosted Zones, use [this SQL query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/route53.sql):

```sql
select
  r.name as hostname,
  type,
  jsonb_array_elements_text(records) as resource_record
from
  aws_route53_zone as z,
  aws_route53_record as r
where r.zone_id = z.id
  and (type LIKE 'A' OR type LIKE 'CNAME')
  and z.private_zone=false
  and jsonb_pretty(records) not like '%dkim%'
  and jsonb_pretty(records) not like '%acm-validations.aws.%';
```

Review the list of IP addresses returned, and if permitted by the terms of service, scan these hostnames for exposed ports and services using [nmap](https://nmap.org/) or [Nessus](https://www.tenable.com/downloads/nessus?loginAttempted=true).

Note: The above query excludes private DNS for VPCs `z.private_zone=false` and excludes common CNAMEs needed for [ACM](https://aws.amazon.com/certificate-manager/) and [email validation](https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail).

## Enumerate the URLs using Steampipe

A number of AWS services, including [CloudFront](https://aws.amazon.com/cloudfront/), [S3](https://aws.amazon.com/s3/), [API Gateway](https://aws.amazon.com/api-gateway/), and [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html), produce URLs that can be vulnerable. For example, S3 Buckets exist as URLs on the public internet and can be accessed if the bucket is not properly secured. To get a list of all of the URLs for the _public_ buckets in your cloud environment, you can use [this query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/s3_buckets.sql):

```sql
select
  'https://' || name || '.s3.' || region || '.amazonaws.com/' as url
from
  aws_s3_bucket
where
  bucket_policy_is_public is True;
```

To request all of the URLs from CloudFront, [this SQL query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/cloudfront_distributions.sql) will return both the distribution name and any aliases that are part of that distribution:

```sql
select
  'https://' || domain_name as url
from
  aws_cloudfront_distribution
UNION ALL
select
  'https://' || jsonb_array_elements_text(aliases -> 'Items') as url
from
  aws_cloudfront_distribution;
```

API Gateways themselves do not have URLs, but when the customer creates a "[stage](https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-stages.html)" a URL is created. To get a list of all the API Gateway v2 URLs, [this SQL query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/api_gatewayv2.sql) can be used:

```sql
select 'https://' || api_id || '.execute-api.' || region || '.amazonaws.com/' || stage_name as url
from aws_api_gatewayv2_stage;
```

API Gateway URLs typically have the following format:

`https://[api-id].execute-api.[region].amazonaws.com/[stage]/[path]`

where [api-id] is the ID of the API Gateway API, [region] is the AWS region where the API is deployed, [stage] is the stage of the API (such as "prod" or "dev"), and [path] is the path to the specific Lambda function being accessed. For example, the URL "`https://abc123.execute-api.us-east-1.amazonaws.com/prod/hello`" could be used to invoke a Lambda function named "hello" that is associated with the API Gateway API with ID "abc123", and is deployed in the "us-east-1" region.

[Lambda URLs](https://docs.aws.amazon.com/lambda/latest/dg/lambda-urls.html) are a new feature of AWS Lambda, released in April of 2022. AWS allows the creation of Lambda URLs that do not require authentication. Therefore, these Lambda URLs are part of your cloud attack surface. To find the Lambda URLs in your environment, you can use [this Steampipe query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/lambda_url.sql):

```sql
select url_config ->> 'FunctionUrl' as url
from aws_lambda_function
where url_config is not Null;
```

You can pull all these together using SQL UNION queries as demonstrated by [this query](https://github.com/turbot/steampipe-samples/tree/main/all/aws-cloud-perimeter/all_urls.sql). You can run it like:

```
steampipe query all_urls.sql

+-----------------------------------------------------------------------+-------------------------------+
| url                                                                   | type                          |
+-----------------------------------------------------------------------+-------------------------------+
| https://dev-FooliApiStack-780272371.us-east-1.elb.amazonaws.com:443   | application_load_balancer     |
| https://prod-FooliApiStack-1829703886.us-east-1.elb.amazonaws.com:443 | application_load_balancer     |
| https://rwpfqhvj6g.execute-api.us-east-1.amazonaws.com/dev_system     | api_gateway                   |
| https://v5johmngyd.execute-api.us-east-1.amazonaws.com/prod_system    | api_gateway                   |
| https://d7iaso3riah1ur.cloudfront.net                                 | cloudfront_distribution       |
| https://d65z8r284bm5f6.cloudfront.net                                 | cloudfront_distribution       |
| https://da6r321d6ljywr.cloudfront.net                                 | cloudfront_distribution       |
| https://d28s6da8wtxvjm.cloudfront.net                                 | cloudfront_distribution       |
| https://memes.dev1.fooli.media                                        | cloudfront_distribution_alias |
| https://memes.memes.fooli.media                                       | cloudfront_distribution_alias |
| https://oshehk7tpbxfqtkfhco5ojkdyq0fbenx.lambda-url.us-east-1.on.aws/ | lambda_url                    |
| https://f00li-prod-1.s3.us-east-1.amazonaws.com/                      | s3_bucket_url                 |
| https://f00li-dev-1.s3.us-east-1.amazonaws.com/                       | s3_bucket_url                 |
| https://f00li-dev-0.s3.us-east-1.amazonaws.com/                       | s3_bucket_url                 |
| https://f00li-public-bucket-policy.s3.us-east-1.amazonaws.com/        | s3_bucket_url                 |
| https://f00li-prod-0.s3.us-east-1.amazonaws.com/                      | s3_bucket_url                 |
| https://pht-blockchain.s3.eu-central-1.amazonaws.com/                 | s3_bucket_url                 |
+-----------------------------------------------------------------------+-------------------------------+
Time: 115.3s. Rows fetched: 215. Hydrate calls: 317.
```

Empowered with a list of all the exposed URLs in your organization, you can then set up a process to scan these using a number of web-focused [Dynamic Application Security Testing (DAST)](https://www.techopedia.com/definition/30958/dynamic-application-security-testing-dast) tools and scanners such as [Zed Attack Proxy](https://www.zaproxy.org/), [dirsearch (Web path scanner)](https://github.com/maurosoria/dirsearch), [Aquatone](https://github.com/michenriksen/aquatone), and [Nikto2](https://cirt.net/Nikto2). The OWASP® Foundation maintains a [full list of scanning tools](https://owasp.org/www-community/Vulnerability_Scanning_Tools) that could be used.

If you are a larger organization that runs a bug bounty program, scanning your URLs for these low-hanging fruit is a quick and easy way to avoid payouts to researchers who use the same tools.

## Conclusion

Steampipe is a powerful way to find and enumerate your AWS IP addresses, hostnames, and URLs. Follow the steps outlined here to gather the information needed to define and manage the network component of your cloud attack surface,  ensure the security and integrity of your data and applications, and prevent unauthorized access to your network resources.  Of course everyone's situation is unique, and you may find a solution that works better for you. If so, please [let us know](https://steampipe.io/community/join): we love to learn from our community!