Sub topic 11
Least privilege and safer administrative access
This chapter assumes you know how to read a security group rule (protocol, port, source), that rules only allow traffic, and that groups can reference other groups. Here you apply those ideas to keep access narrow, especially for administration.
What least privilege means for a security group
Least privilege means each resource accepts only the traffic it needs, from only the sources that need to send it. A security group has an implicit deny, so every rule you add widens access. Treat each rule as a decision you can justify.
Three questions help you review any rule:
- Is the port the narrowest one that works?
- Is the source the narrowest one that works?
- Does someone still need this rule?
Narrow the source
The source is where most mistakes happen. From widest to narrowest:
| Source | Meaning | Typical use |
|---|---|---|
| `0.0.0.0/0` | Any IPv4 address | Public HTTPS on a load balancer |
| Office or VPN CIDR | A known network range | Admin access from a corporate network |
| `/32` address | One host | A single administrator or bastion |
| Security group reference | Any instance with that group attached | Tier-to-tier traffic |
Open to the world only the ports that must be public, such as 443 on a web tier. Never open administrative ports like SSH (22) or RDP (3389) to 0.0.0.0/0 or ::/0. Scanners find those within minutes.
The example below allows SSH from one administrator address and records a description, so a later reviewer knows why the rule exists.
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"}]'A /32 is safe only while that address stays yours. Home and mobile addresses change, and a stale rule can end up allowing a stranger.
Prefer access paths without open ports
The safest administrative rule is no rule. Two common alternatives remove inbound SSH from the internet entirely:
- AWS Systems Manager Session Manager opens a shell through the SSM agent over outbound HTTPS. The instance needs no inbound port 22, and the instance profile needs the right IAM permissions.
- A bastion host is one hardened instance in a public subnet. Only the bastion's group allows SSH from your office CIDR. Private instances allow SSH only from the bastion's security group.
The diagram below shows the bastion pattern.
The rule on the application group references the bastion group instead of an IP range:
aws ec2 authorize-security-group-ingress \
--group-id sg-0aaaaaaaaaaaaaaaa \
--protocol tcp \
--port 22 \
--source-group sg-0bbbbbbbbbbbbbbbbReplace sg-0bbbbbbbbbbbbbbbb with your bastion's group ID. If you replace or add bastions, the rule keeps working as long as the new host has the same group.
Keep rules reviewable
- Add a description to every rule that is not obvious.
- Use one group per role, such as
bastion-sgorapp-sg, so a rule's purpose follows from the group name. - Remove temporary rules when the work ends. Revoke a rule by repeating the protocol, port and source it was created with.
- Review outbound rules too. The default allows all outbound traffic. For sensitive tiers, limit egress to the destinations they need.
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0
aws ec2 revoke-security-group-ingress \
--group-id sg-0123456789abcdef0 \
--protocol tcp \
--port 22 \
--cidr 203.0.113.25/32Exercise
An application server must accept SSH only for administrators. Name two designs that avoid opening port 22 to the internet, and say what the application group's rule should use as its source in each design that needs one.
Show answer
Use Session Manager, which needs no inbound rule because the SSM agent connects outbound over HTTPS. Or use a bastion host, where the application group allows TCP 22 with the bastion's security group as the source, and the bastion group allows port 22 only from the office CIDR or a /32 address.