Skip to content

AWS security groups: best practices to reduce public exposure

Ports open to the internet are one of the most common causes of cloud incidents. See how to control and review your rules.

Reading time
9 min
Published

Security groups are instance-level firewalls on AWS: they define what traffic can enter and leave a resource. Because they are easy to create, they end up accumulating rules that are too broad. An administrative port open to 0.0.0.0/0, for example, draws nonstop scanning and brute force, and it is among the most frequent findings in any environment review.

When you build security groups, think in terms of minimal exposure: each rule allows only what is needed. In practice, that means avoiding open ranges on sensitive ports, not forgetting IPv6, and keeping the rules under review. With that care, you can shrink the attack surface without breaking operations.

Inbound, outbound, and the danger of 0.0.0.0/0

Inbound rules control who can reach the resource, and outbound rules control whom the resource can talk to. Most teams take care of inbound and forget outbound. The problem is that restricted outbound traffic is what limits, on a compromised host, data exfiltration and communication with malicious infrastructure.

Opening a port to 0.0.0.0/0 means exposing it to the entire internet. That is acceptable for public service ports, such as 443 behind a load balancer, and dangerous for administrative ports like SSH and RDP, for databases, and for internal dashboards. For administrative access, instead of exposing the port, use a controlled bastion, AWS Systems Manager Session Manager, or a VPN.

IPv6 and the hidden twin rule

IPv6 is often left out of reviews. A rule can be closed on IPv4 and open on ::/0, and that exposure goes unnoticed in reviews that only look at IPv4. Think of each inbound rule as having two faces, the IPv4 range and the IPv6 range. It is common to close one and forget the other when the subnet gets an IPv6 block after the rules were already in place. So in your review, treat ::/0 as strictly as 0.0.0.0/0.

Group references and minimal exposure

Between internal tiers, instead of allowing IP ranges, have security groups reference each other. For example, allow the application group to reach the database group on the database port. That removes the need for fixed IPs, which are fragile, and lets the rule state directly what the architecture intends: the application talks to the database.

  • Open only the ports that each tier really needs
  • Use group references for internal traffic
  • Avoid 0.0.0.0/0 and ::/0 on administrative and database ports
  • Restrict outbound traffic when the workload does not need to talk to the entire internet
  • Standardize names and descriptions so that each rule explains its purpose

Managed prefix lists and security groups at scale

When several rules repeat the same IP range (the blocks of a corporate VPN or of an egress provider, for example), keeping them in sync by hand is asking for mistakes. A managed prefix list solves this: it names the set of CIDRs once and can be referenced in as many security groups as needed. When the range changes, you update the list in one place and every rule follows. This avoids closing access in one group and forgetting the same access in another.

In environments that grow without a standard, security groups multiply and nearly identical groups appear, with small differences between them. That gets in the way of auditing, because the same broad rule shows up in several places and nobody knows for sure which one is in use. Consolidate redundant groups, define a baseline set per application tier, and keep an eye on account limits: there is a cap on rules per security group and on groups associated with each network interface.

  • Centralize reused ranges in managed prefix lists
  • Consolidate redundant and near-duplicate security groups
  • Define a baseline set of groups per tier, reused across workloads
  • Monitor the limits on rules per group and on groups per interface

Continuous review and drift detection

Rules tend to go in during a rush and never come out. Use AWS Config with managed rules to detect sensitive ports open to the internet and to alert you when a security group is changed. CloudTrail completes the picture and shows who made the change and when.

Finding exposure programmatically

A periodic scan catches what manual review lets slip. Cross-check the result with your resource inventory to separate the legitimate exposure of a public service from what was left open by mistake, and repeat the scan in every region, including the ones you deliberately do not use. The starting point for deciding what to close is the AWS CLI, which lists the groups that have an inbound rule open to the internet, as in:

List security groups with an inbound rule open to 0.0.0.0/0
aws ec2 describe-security-groups \  --filters Name=ip-permission.cidr,Values=0.0.0.0/0 \  --query "SecurityGroups[].{ID:GroupId,Name:GroupName}"

Besides the scan, keep a periodic review that questions every broad rule. Is it still needed? Could it be more specific? Temporary rules created for a test tend to become permanent when nobody looks at them again.

Practical checklist

  • Map every rule with 0.0.0.0/0 and ::/0
  • Close direct administrative access from the internet (SSH, RDP)
  • Use Session Manager, a bastion, or a VPN for administration
  • Cover IPv6 in reviews with the same care given to IPv4
  • Have security groups reference each other for internal traffic
  • Restrict outbound rules whenever possible
  • Turn on AWS Config rules for exposed sensitive ports
  • Alert on security group changes recorded in CloudTrail
  • Standardize descriptions that explain the purpose of each rule
  • Review and remove temporary rules from time to time
  • Use managed prefix lists for reused IP ranges
  • Consolidate redundant security groups and monitor the limits per group and per interface
  • Periodically scan for rules open to 0.0.0.0/0 and ::/0 in every region

Best practices

  • Make minimal exposure the default and open only what is necessary
  • Do not expose administrative ports directly to the internet
  • Control outbound traffic to limit exfiltration from a compromised host
  • Use automatic drift detection with AWS Config
  • Document every exception that has to exist
  • Give temporary rules an explicit expiration date
  • Centralize common ranges in prefix lists to avoid rules that drift apart
  • Standardize a baseline set of security groups per application tier

Common mistakes

SSH or RDP open to 0.0.0.0/0
It draws scanning and brute force all the time and opens a direct path to compromise.
Rule closed on IPv4 and open on IPv6
The exposure stays invisible in reviews that only look at IPv4.
Outbound traffic fully open
A compromised host exfiltrates data and talks to malicious infrastructure without any barrier.
Fixed IP ranges between internal tiers
The rules become fragile and hard to audit when the infrastructure changes.
Temporary rules that are never removed
The attack surface grows little by little, without anyone noticing.
Redundant and near-duplicate security groups
They make auditing harder and let the same broad rule repeat without control.

Authorship and review

Cesar Gargiulo

CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.

Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.

Frequently asked questions

What is the difference between a security group and a NACL?
A security group works at the instance level, is stateful (the response to an allowed connection is let through automatically), and only has allow rules. A NACL works at the subnet level, is stateless, and has both allow and deny rules. The two work in layers: the NACL filters at the edge of the subnet, and the security group at the instance.
Is it safe to leave port 443 open to 0.0.0.0/0?
For a public service behind a load balancer, exposing 443 to the internet is expected, because that is how users get in. The care you need is not exposing administrative and database ports the same way. The risk lies in sensitive ports open to the world.
How do I manage instances without exposing SSH?
Use AWS Systems Manager Session Manager, which gives shell access without opening any inbound port, a restricted bastion, or a VPN. With any of these options, SSH and RDP are no longer exposed directly to the internet, and the favorite target of scanning and brute force disappears.
Is it worth restricting outbound rules?
Yes, when the workload does not need to talk to the entire internet. With restricted outbound traffic, a compromised host is limited in data exfiltration and in communication with command and control servers. In sensitive environments the control pays off, but it comes at a price: you need to map the legitimate destinations carefully.
What are managed prefix lists, and when should I use them?
A managed prefix list is a named set of IP ranges that you reference inside security group rules. Instead of repeating the same CIDRs in several groups, you keep the list in one place and update it once. Use one when the same range appears in many rules, such as the blocks of a corporate VPN or of egress providers, because it lowers the risk of closing access in one group and forgetting the equivalent in another.
Is there a limit on rules per security group?
Yes. AWS sets quotas on the number of rules per security group and on how many groups can be associated with each network interface. Some quotas can be raised on request. Even so, if you are bumping into the limits, the most likely cause is overly broad rules or redundant groups, and it is worth consolidating before asking for an increase.
Does a security group protect against denial of service attacks?
That is not its job. A security group filters by source, port, and protocol, but it does not measure volume or tell a legitimate spike from a volumetric attack. Protection against denial of service comes from other layers, such as AWS Shield and application filtering at the edge. The contribution of the security group is indirect: it reduces the exposed surface and, with it, what can become a target.

When to bring in specialist help

In an environment with many security groups and accounts, reviewing exposure means cross-referencing rules, resources, and network context. GUARDIASEC does this mapping in its AWS security service: we separate the real exposures and point out what to close first.