Other Articles

EC2 Security Group – Restrict Public SSH and RDP Access

Secure Configuration Checks > AWS

This check ensures that no EC2 security group allows SSH (port 22) or RDP (port 3389) from the entire internet. Open remote access ports are scanned and attacked within minutes of being exposed.

Check Details

  • Resource: EC2 Security Group
  • Check: Restrict public SSH and RDP access (NoUnrestrictedSshRdp)
  • Risk: Attackers can try brute force attacks and exploit remote access flaws

Remediation via AWS Console

  1. Log in to the AWS Management Console and open the Amazon EC2 console.

    Amazon EC2 Console
  2. In the left navigation pane, click Security Groups and select the affected security group.
  3. Open the Inbound rules tab and click Edit inbound rules.

    Security group inbound rules
  4. Find each rule for port 22 or 3389 with the source 0.0.0.0/0 or ::/0. Change the Source to a trusted IP range, or click Delete.

    Edit inbound rules
  5. Click Save rules.
  6. Repeat steps 2 to 5 for every affected security group.

Remediation via AWS CLI

  1. Log in to the AWS Management Console and open CloudShell by clicking the CloudShell icon (>_) in the top navigation bar.

    AWS CloudShell
  2. List security group rules that expose SSH (port 22) or RDP (port 3389) to the internet:

    aws ec2 describe-security-group-rules \
    --query 'SecurityGroupRules[?IsEgress==`false` && (CidrIpv4==`"0.0.0.0/0"` || CidrIpv6==`"::/0"`) && (IpProtocol==`"-1"` || (FromPort<=`22` && ToPort>=`22`) || (FromPort<=`3389` && ToPort>=`3389`))].[GroupId,SecurityGroupRuleId,IpProtocol,FromPort,ToPort,CidrIpv4,CidrIpv6]' \
    --output table
    

    The output includes the security group ID, security group rule ID, protocol, port range, and public CIDR. The query checks the current region. Add --region <region> to check another region.

  3. Allow SSH or RDP access only from a trusted IP address or CIDR range. Add the replacement rule before removing the existing public rule.

    For SSH (port 22):

    aws ec2 authorize-security-group-ingress \
    --group-id <security-group-id> \
    --protocol tcp \
    --port 22 \
    --cidr <trusted-cidr>
    

    For RDP (port 3389):

    aws ec2 authorize-security-group-ingress \
    --group-id <security-group-id> \
    --protocol tcp \
    --port 3389 \
    --cidr <trusted-cidr>
    
  4. Remove the unrestricted inbound rules using their security group rule IDs obtained in step 2:

    aws ec2 revoke-security-group-ingress \
    --group-id <security-group-id> \
    --security-group-rule-ids <security-group-rule-id>
    

    Repeat the command for each exposed rule. This also handles rules that allow all traffic or use wider port ranges, provided you remove the complete offending rule.

    Important: If a broad rule allows additional required traffic, recreate the necessary restricted rules before removing it.

  5. Verify the remaining inbound rules:

    aws ec2 describe-security-groups \
    --group-ids <security-group-id> \
    --query 'SecurityGroups[0].IpPermissions' \
    --output json
    

Note: Replace <security-group-id> with the security group ID, for example sg-0123456789abcdef0. Replace <security-group-rule-id> with the rule ID, for example sgr-0123456789abcdef0. Replace <trusted-cidr> with your trusted IP range, for example 203.0.113.10/32.

If IPv6 access is required, configure an appropriate trusted IPv6 CIDR instead of leaving ::/0 unrestricted. Repeat the checks for every AWS region containing security groups.

Updated on 03 November, 2026