Automatically audit AWS security groups that allow access from public IP addresses
Created by Eugene Shifer (AWS) and Stephen DiCato (AWS)
Summary
As a security best practice, it's crucial to minimize the exposure of AWS resources to only what is absolutely necessary. For example, web servers that serve the general public need to allow inbound access from the internet, but access to other workloads should be restricted to specific networks to reduce unnecessary exposure. Security groups in Amazon Virtual Private Cloud (Amazon VPC) are an effective control to help you limit resource access. However, evaluating security groups can be a cumbersome task, especially in multi-account architectures. AWS Config rules and AWS Security Hub controls can help you identify security groups that permit access from the public internet (0.0.0.0/0) to specific network communication protocols, such as Secure Shell (SSH), HTTP, HTTPS, and Windows remote desktop protocol (RDP). However, these rules and controls are not applicable if services run on non-standard ports or if access is restricted to certain public IP addresses. For instance, this might occur when a web service is associated with TCP port 8443 instead of the standard TCP port 443. This might also occur when developers have access to the server from their home networks, such as for testing purposes.
To address this, you can use the infrastructure as code (IaC) solution provided in this pattern to identify security groups that allow access from any non-private (RFC 1918
Prerequisites and limitations
Prerequisites
An active AWS account
Experience using GitHub
If you're deploying into a single AWS account:
Permissions to create CloudFormation stacks
AWS Config set up in the target account
(Optional) Security Hub set up in the target account
If you're deploying into an AWS organization:
Permissions to create CloudFormation stack sets
Security Hub set up with AWS Organizations integration
AWS Config set up in the accounts where you are deploying this solution
Designate an AWS account to be the delegated administrator for AWS Config and Security Hub
Limitations
If you're deploying to an individual account that doesn't have Security Hub enabled, you can use AWS Config to evaluate the findings.
If you're deploying to an organization that doesn't have a delegated administrator for AWS Config and Security Hub, you must log into the individual member accounts to view the findings.
If you use AWS Control Tower to manage and govern the accounts in your organization, deploy the IaC in this pattern by using Customizations for AWS Control Tower (CfCT). Using the CloudFormation console would create configuration drift from AWS Control Tower guardrails and require that you re-enroll the organizational units (OUs) or managed accounts.
Some AWS services aren’t available in all AWS Regions. For Region availability, see AWS services by Region
. For specific endpoints, see the Service endpoints and quotas page, and choose the link for the service.
Architecture
Deploying into an individual AWS account
The following architecture diagram shows the deployment of the AWS resources within a single AWS account. You provision the resources by using a CloudFormation template directly through the CloudFormation console. If Security Hub is enabled, you can view the results in either AWS Config or Security Hub. If Security Hub is not enabled, you can view the results only in AWS Config.

The diagram shows the following workflow:
You create a CloudFormation stack. This deploys a Lambda function and an AWS Config rule. Both the rule and function are set up with the AWS Identity and Access Management (IAM) permissions that are required to publish resource evaluations in AWS Config and logs.
The AWS Config rule operates in detective evaluation mode and invokes the Lambda function every 24 hours.
The Lambda function assesses the security groups and sends updates to AWS Config.
Security Hub receives all of the AWS Config findings.
You can view the findings in Security Hub or in AWS Config, depending on the services that you have set up in the account.
Deploying into an AWS organization
The following diagram shows deployment of the pattern across multiple accounts that are managed through AWS Organizations and AWS Control Tower. You deploy the CloudFormation template through CfCT. The assessment outcomes are centralized in Security Hub in the delegated administrator account. The AWS CodePipeline workflow section of the diagram shows the background steps that occur during CfCT deployment.

The diagram shows the following workflow:
In the management account, you upload a compressed (ZIP) file of the IaC template to an Amazon Simple Storage Service (Amazon S3) bucket that is deployed by CfCT.
The CfCT pipeline unzips the file, runs cfn-nag
(GitHub) checks, and deploys the template as a CloudFormation stack set. Depending on the configuration you specify in the CfCT manifest file, CloudFormation StackSets deploys stacks into individual accounts or specified OUs. This deploys a Lambda function and an AWS Config rule in the target accounts. Both the rule and function are set up with the IAM permissions that are required to publish resource evaluations in AWS Config and logs.
The AWS Config rule operates in detective evaluation mode and invokes the Lambda function every 24 hours.
The Lambda function assesses the security groups and sends updates to AWS Config.
AWS Config forwards all of the findings to Security Hub.
The Security Hub findings are aggregated in the delegated administrator account.
You can view the aggregated findings in Security Hub in the delegated administrator account.
Tools
AWS services
AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and AWS Regions.
AWS Config provides a detailed view of the resources in your AWS account and how they’re configured. It helps you identify how resources are related to one another and how their configurations have changed over time. An AWS Config rule defines your ideal configuration settings for a resource, and AWS Config can evaluate whether your AWS resources comply with the conditions in the rule.
AWS Control Tower helps you set up and govern an AWS multi-account environment, following prescriptive best practices. Customizations for AWS Control Tower (CfCT) helps you customize your AWS Control Tower landing zone and stay aligned with AWS best practices. Customizations for this solution are implemented through CloudFormation templates and AWS Organizations service control policies (SCPs).
AWS Lambda is a compute service that helps you run code without needing to provision or manage servers. It runs your code only when needed and scales automatically, so you pay only for the compute time that you use.
AWS Organizations is an account management service that helps you consolidate multiple AWS accounts into an organization that you create and centrally manage.
AWS Security Hub provides a comprehensive view of your security state in AWS. It also helps you check your AWS environment against security industry standards and best practices.
Other tools
Python
is a general-purpose computer programming language.
Code repository
The code for this pattern is available in the GitHub Detect vulnerable security groups
Best practices
We recommend that you adhere to the best practices in the following resources:
Best Practices for Organizational Units with AWS Organizations
(AWS Cloud Operations & Migrations Blog) Guidance for Establishing an Initial Foundation using AWS Control Tower on AWS
(AWS Solutions Library) Guidance for creating and modifying AWS Control Tower resources (AWS Control Tower documentation)
CfCT deployment considerations (AWS Control Tower documentation)
Apply least-privilege permissions (IAM documentation)
Epics
Task | Description | Skills required |
---|---|---|
Determine your deployment strategy. | Review the solution and code to determine the deployment strategy for your AWS environment. Determine whether you are deploying into a single account or an AWS organization. | App owner, General AWS |
Clone the repository. | Enter the following command to clone the Detect vulnerable security groups
| App developer, App owner |
Validate the Python version. |
| AWS administrator, App developer |
Task | Description | Skills required |
---|---|---|
Deploy the CloudFormation template. | Deploy the CloudFormation template into your AWS environment. Do one of the following:
| App developer, AWS administrator, General AWS |
Verify the deployment. | In the CloudFormation console | AWS administrator, App owner |
Task | Description | Skills required |
---|---|---|
View the AWS Config rule findings. | In Security Hub, do the following to view a list of individual findings:
In Security Hub, do the following to view a list of total findings grouped by AWS account:
In AWS Config, to view a list of findings, follow the instructions in Viewing Compliance Information and Evaluation Results in the AWS Config documentation. | AWS administrator, AWS systems administrator, Cloud administrator |
Troubleshooting
Issue | Solution |
---|---|
The CloudFormation stack set creation or deletion fails. | When AWS Control Tower is deployed, it enforces necessary guardrails and assumes control over AWS Config aggregators and rules. This includes preventing any direct alterations through CloudFormation. To properly deploy or remove this CloudFormation template, including all associated resources, you must use CfCT. |
CfCT fails to delete the CloudFormation template. | If the CloudFormation template persists even after making necessary changes in the manifest file and removing the template files, confirm that the manifest file contains the |
Related resources
AWS Config Custom Rules (AWS Config documentation)