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 →

  1. 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
  1. Set up SSM Session Manager (SSM agent plus an instance role with AmazonSSMManagedInstanceCore), so access needs no open ports or SSH keys.
  2. Remove the port 22 rules, in Terraform where possible.
  3. Check auth logs (/var/log/auth.log or secure) on those boxes for successful logins from unknown IPs.
  4. Prevent: AWS Config rule restricted-ssh with 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".

All Real-World Interview questions Roadmap: Interview readiness

More on Security incidents