Security group allows SSH from all addresses

Restrict SSH management access to required sources and strengthen authentication and key management.

Description

Allow TCP 22 on SSH hosts only from approved management addresses or source security groups. Check all attached groups for wider IPv4 or IPv6 grants that also cover the port.

Potential impact

A reachable SSH listener can receive external sign-in attempts. Stolen keys or weak authentication can then lead to unauthorized access.

Remediation

Test administrative access before removing broad rules. Restrict access to a bastion, or prepare the agent, IAM permissions and connectivity for Session Manager. Where SSH remains, review access logs and rotate or revoke keys as needed.

Examples

Supply the actual VpcId and configure the instances and routes separately. After deployment, check that the old OpenSSHIngress rule has been removed.

Before

yaml
Resources:
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: SSH security group
      VpcId: vpc-0123456789abcdef0
  OpenSSHIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: !Ref InstanceSecurityGroup
      IpProtocol: tcp
      FromPort: 20
      ToPort: 22
      CidrIpv6: "::/0"

The first rule admits TCP 20–22, including SSH, from every IPv6 address (::/0).

After

yaml
Resources:
  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: SSH security group
      VpcId: vpc-0123456789abcdef0
  RestrictedSSHIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: !Ref InstanceSecurityGroup
      IpProtocol: tcp
      FromPort: 22
      ToPort: 22
      CidrIp: 203.0.113.10/32

The second admits only TCP 22 from one IPv4 address. Replace the documentation address 203.0.113.10/32 with the real management source. Restrict any required IPv6 management path separately.

References