DEV Community

Cover image for Automated way to restrict all inbound and outbound rules from AWS default security groups

Automated way to restrict all inbound and outbound rules from AWS default security groups

For each VPC created in AWS, a default security group is always automatically created, in this security group there is an inbound rule that allows access to all protocols and ports of the security group itself as source and it also has an outbound rule that allows access to all protocols and ports to the internet as source.

Following good security practices, it is not recommended to use default security groups associated with AWS resources, but rather to create custom security groups with least privileges for these resources.

The objective of this article is to present a script to delete all inbound and outbound rules from the default security groups of all VPCs and regions in an AWS account.

This makes the use of default security groups useless and will reduce AWS compliance security alerts for default security group that does not restrict all traffic.

It is important to remove the association of AWS resources with the default security group if used, to find out if it is being used by an AWS resource you can consult the network interfaces according to this article.

To run the code above you need to install python 3 with dependency boto3 and configure your AWS credentials:

Note 1: Default security groups always have the name "default" and it is not possible to create a security group with that same name, so there is no chance of deleting rules from other security groups.

Note 2: In regions used in your AWS account, it may make sense to manually evaluate the update of the default security group via network interfaces, so in this case it is recommended to exclude the elements (regions) from the regions variable array.

Note 3: This will not prevent more non-restricted default security groups from being created, to accomplish this you can add configuration parameters to your infrastructure as code, as this terraform resource.

Top comments (0)