Sub topic 11
Least privilege and safer administrative access
This chapter assumes you know how security group rules are built (direction, protocol, port range, source), that rules only allow traffic, and that groups can reference each other. It focuses on one question: how do you keep rules as narrow as the workload needs, and how do you reach instances for administration without exposing management ports to the internet?
What least privilege means for a security group
Least privilege means each resource gets only the network access it needs to do its job. For a security group, a rule is justified when you can answer three questions:
- Which client needs this access?
- Which protocol and port does it use?
- Which direction does the traffic start in?
If you cannot answer all three, the rule is probably wider than it should be. Every extra rule increases the number of ways an attacker can reach an instance after finding one weak service, a leaked credential, or a misconfigured application.
Security groups give you three levers to narrow a rule:
- Source or destination. Prefer a specific security group, a prefix list, or a
/32address over a broad CIDR block. - Port range. Open one port, not a range. A range such as 0-65535 turns a firewall rule into a statement that nothing is filtered.
- Direction. Add inbound rules only for services the instance hosts. Treat outbound rules as a separate decision (see below).
Rules that should trigger a review
Some patterns appear in almost every account audit. The table lists them with a safer replacement.
| Risky rule | Why it is a problem | Safer approach |
|---|---|---|
| SSH (22) or RDP (3389) from `0.0.0.0/0` | Automated scanners find open management ports within minutes and try passwords and stolen keys | Use Session Manager, or restrict to a known admin address or a bastion group |
| All traffic (`-1`) from `0.0.0.0/0` | The group no longer filters anything | List the ports the service uses |
| A database port open to a CIDR that covers a whole subnet or VPC | Any instance in that range, including unrelated or compromised ones, can connect | Reference the application tier's security group |
| A wide port range such as 1024-65535 | Opens ports for services you did not plan to expose | Open each required port individually |
| Rules without descriptions | Nobody remembers why they exist, so nobody dares remove them | Add a description to every rule |
Opening HTTPS to 0.0.0.0/0 on a public web tier is normal and is not on this list. A public service has to accept connections from the public. The danger is applying that same openness to ports that were never meant to be public.
Prefer group references over address ranges
Chapter-level design in a tiered architecture relies on references, and least privilege is the reason. When the application tier allows port 8080 from the web tier's security group, access follows the attachment, not the address. A new web instance is allowed as soon as it has the group. An instance outside the group is refused even if it sits in the same subnet.
The reference is also precise about who can connect. A CIDR rule says "anything that happens to hold an address in this range". A group reference says "only resources that were deliberately given this group".
This command allows PostgreSQL into a database group only from the application tier's group:
aws ec2 authorize-security-group-ingress \
--group-id sg-0bbbbbbbbbbbbbbbb \
--ip-permissions 'IpProtocol=tcp,FromPort=5432,ToPort=5432,UserIdGroupPairs=[{GroupId=sg-0aaaaaaaaaaaaaaaa,Description="App tier to PostgreSQL"}]'Here sg-0bbbbbbbbbbbbbbbb is the database group and sg-0aaaaaaaaaaaaaaaa is the application group. The database accepts nothing else on port 5432.
Tightening outbound rules
A new security group created with the console or CLI allows all outbound traffic by default. That default is convenient, but it means a compromised instance can call out to any address on any port. This is how many attacks exfiltrate data or fetch further tools.
Whether to restrict outbound traffic is a trade-off:
- Keep the default for ordinary web and application tiers if you rely on other controls, such as monitoring and VPC endpoints, and need the simplicity.
- Restrict outbound for sensitive tiers such as databases. A database usually has no reason to start connections to the internet.
If you restrict outbound rules, remember that security groups are stateful. Replies to allowed inbound connections are returned automatically, so you do not need an outbound rule for them. You do need outbound rules for connections the instance starts itself, such as OS package updates, DNS, and calls to AWS APIs. Removing the default rule without adding those back is a common cause of instances that run but cannot patch themselves or reach AWS services.
Safer administrative access
Administration is where least privilege is most often broken, because opening SSH to the world is the quickest way to get a connection working. There are four common approaches. They are ordered here from the one that needs the fewest open ports to the one that needs the most.
Option 1: AWS Systems Manager Session Manager
Session Manager gives you a shell on an instance without any inbound rule. The SSM Agent on the instance opens an outbound HTTPS connection to the Systems Manager service, and your session travels over that connection. Your access is controlled by IAM permissions and can be logged to CloudTrail, Amazon S3, or CloudWatch Logs.
Requirements:
- The SSM Agent must be running on the instance (it is preinstalled on many Amazon-provided AMIs).
- The instance profile must grant Systems Manager permissions, for example the
AmazonSSMManagedInstanceCoremanaged policy. - The instance needs outbound HTTPS to the Systems Manager endpoints, through a NAT gateway, an internet gateway, or VPC interface endpoints.
With this approach you can delete the SSH rule entirely. The instance's security group then has no inbound management port for anyone to scan.
Option 2: A single trusted address
If you need SSH or RDP, allow only the address that will connect, as a /32 for IPv4. Always add a description:
aws ec2 authorize-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=203.0.113.25/32,Description="Admin workstation"}]'The weakness is that home and office addresses change. When the address changes, someone edits the rule, and in a hurry that edit often becomes 0.0.0.0/0. If you can, use a fixed egress address such as a corporate VPN or NAT address, and allow that.
Option 3: A bastion host with its own group
A bastion host is a hardened instance that is the only entry point for administration. You allow SSH from your trusted address to the bastion's group, and you allow SSH on the private instances only from the bastion's group:
aws ec2 authorize-security-group-ingress \
--group-id sg-0cccccccccccccccc \
--protocol tcp \
--port 22 \
--source-group sg-0ddddddddddddddddIn this command sg-0cccccccccccccccc is attached to the private instances and sg-0dddddddddddddddd is attached to the bastion. The private instances now have no path from the internet. The cost is that you must patch, monitor, and secure the bastion, and it is still a reachable SSH port.
Option 4: EC2 Instance Connect Endpoint
An EC2 Instance Connect Endpoint lets you connect to instances in private subnets over SSH or RDP without a public IP address or a bastion. The endpoint is a resource in your subnet, and traffic from it to your instance appears to come from the endpoint's network interface. You can give the endpoint its own security group and allow SSH on the instance only from that group, with IAM controlling who can use the endpoint.
Comparing the options
| Option | Inbound port open on instance | Access control | Extra infrastructure |
|---|---|---|---|
| Session Manager | None | IAM | SSM Agent, instance profile, outbound path to the service |
| Single trusted address | 22 or 3389 from one `/32` | Source IP and key or password | None |
| Bastion host | 22 from the bastion group | Source IP, then key or password | One hardened instance to maintain |
| Instance Connect Endpoint | 22 or 3389 from the endpoint's group | IAM | One endpoint per VPC subnet setup |
Keeping rules maintainable and auditable
Narrow rules only stay narrow if people can read and review them. Some habits that help:
- Describe every rule. A description such as "Admin workstation" or "App tier to PostgreSQL" explains intent. Descriptions are supported on rules created from the CLI, as in the examples above.
- One group per role. Create groups named for what they do, such as
web-sg,app-sg,db-sgandbastion-sg, and attach the right ones to each instance. Avoid a single shared group that grows a rule for every new need. - Use tags and consistent names so you can find who owns a group.
- Define rules in code. With Terraform or another infrastructure-as-code tool, a change to a rule goes through review and leaves a history. Manual console edits tend to be temporary fixes that stay forever.
- Remove temporary rules promptly. If you open a port to debug a problem, revoke it in the same session. Set a reminder if you cannot do it immediately.
- Review unused groups and rules. Use
aws ec2 describe-security-groupsto list rules, and look for sources of0.0.0.0/0or::/0on ports other than the ones you intend to be public.
You can find the risky rules in an account with a query. This command lists group IDs that have an inbound rule on port 22 from any IPv4 address:
aws ec2 describe-security-groups \
--filters Name=ip-permission.from-port,Values=22 \
Name=ip-permission.to-port,Values=22 \
Name=ip-permission.cidr,Values=0.0.0.0/0 \
--query 'SecurityGroups[].[GroupId,GroupName]' \
--output tableRemember to check IPv6 as well. A rule that allows ::/0 is the IPv6 equivalent of 0.0.0.0/0, and you can find it with the filter ip-permission.ipv6-cidr.
Common mistakes
- Fixing a connection problem by opening everything. If a connection fails, find the missing rule instead of widening a good one. The troubleshooting chapter covers how.
- Using a CIDR when a group would do. Instances inside your own VPC should usually reference groups. A CIDR is correct for outside clients that have no group.
- Forgetting the second address family. A fix applied to IPv4 only leaves IPv6 open, or the reverse.
- Leaving the default security group in use. Its rules allow traffic from other members of the same group, which can grant more access than intended. Create purpose-built groups instead.
- Treating the bastion as safe by default. It is an internet-facing server. It needs patches, logging and a tight source rule like any other.
Exercise
A database instance in a private subnet runs PostgreSQL on port 5432. Application servers in the group app-sg must connect to it. An administrator also needs a shell on the database instance, and the team does not want any inbound management port open. Describe the inbound rules on the database's security group and how the administrator gets a shell.
Show answer
The database group needs one inbound rule: TCP 5432 with the source set to app-sg (a security group reference, not a CIDR). It needs no inbound rule for SSH. The administrator uses Session Manager, which works over an outbound HTTPS connection from the SSM Agent, so the instance needs the SSM permissions in its instance profile and an outbound path to Systems Manager (NAT gateway or VPC endpoints). Access is controlled by IAM instead of a network rule.
Exercise
A security audit finds a group with inbound TCP 22 from 0.0.0.0/0 on an instance that is reached by admins from a corporate VPN with the egress address 198.51.100.10. Write the CLI commands to fix it.
Show answer
First add the narrow rule, then revoke the broad one, so access is not interrupted: aws ec2 authorize-security-group-ingress --group-id sg-0123456789abcdef0 --ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=198.51.100.10/32,Description="Corporate VPN egress"}]' aws ec2 revoke-security-group-ingress --group-id sg-0123456789abcdef0 --protocol tcp --port 22 --cidr 0.0.0.0/0