A scan shows 40 EC2 instances with SSH (port 22) open to 0.0.0.0/0. How do you fix it properly?
BeginnerScenarioSecurity incidents
Answer
Situation read → Removing the rule is quick. The real fix is giving people a better way in, or the rule comes back next week.
What I'd do →
- List the offending security groups:
aws ec2 describe-security-groups \
--filters Name=ip-permission.from-port,Values=22 Name=ip-permission.cidr,Values=0.0.0.0/0 \
--query 'SecurityGroups[].[GroupId,GroupName]' --output table
- Set up SSM Session Manager (SSM agent plus an instance role with
AmazonSSMManagedInstanceCore), so access needs no open ports or SSH keys. - Remove the port 22 rules, in Terraform where possible.
- Check auth logs (
/var/log/auth.logorsecure) on those boxes for successful logins from unknown IPs. - Prevent: AWS Config rule
restricted-sshwith auto-remediation, and a policy check in Terraform CI (Checkov or tfsec).
Why → Session Manager also logs every session to S3 or CloudWatch, which SSH never did.
Interview tip → Lead with "replace SSH with Session Manager", not just "close the port".
More on Security incidents
An AWS access key for a production IAM user was pushed to a public GitHub repo 20 minutes ago. What do you do in the first 30 minutes?AdvancedYou find pods in your cluster running a crypto miner. What do you do?AdvancedA critical CVE is announced in a library you use. You have 200 images across 40 repos. How do you find and patch everything fast?IntermediateGuardDuty reports an EC2 instance talking to a known command-and-control IP. What do you do?AdvancedYou discover an S3 bucket with customer data has been public for three months. What now?IntermediateAn engineer left the company three weeks ago and you find their access key was used yesterday. How do you respond and fix the process?Beginner