For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/cloudformation-aws-ec2-sensitive-port-is-publicly-exposed.md.
A documentation index is available at /llms.txt.
Security group ingress rules must not expose known sensitive service ports to the entire internet (for example, 0.0.0.0/0 or ::/0) because this makes EC2 instances reachable for unauthorized access, brute-force attacks, and exploitation of network services.
This check inspects AWS::EC2::SecurityGroup resources that are attached to AWS::EC2::Instance (via the instance’s SecurityGroups property). It flags SecurityGroupIngress entries where the CidrIp value ends with /0 and the IpProtocol plus FromPort–ToPort range includes known sensitive ports (for example, SSH 22, RDP 3389, and common database ports). The rule evaluates IpProtocol values of -1/ALL as both TCP and UDP and supports port ranges when mapping to sensitive services.
Remediate by restricting CidrIp to specific trusted CIDR ranges, using source security groups, or placing access behind bastion hosts or VPNs.
Secure configuration example:
MySecurityGroup:Type:AWS::EC2::SecurityGroupProperties:GroupDescription:Allow SSH from admin networkSecurityGroupIngress:- IpProtocol:tcpFromPort:22ToPort:22CidrIp:10.0.0.0/24
Compliant Code Examples
AWSTemplateFormatVersion:2010-09-09T00:00:00ZResources:SafeSecGroup:Type:AWS::EC2::SecurityGroupProperties:SecurityGroupEgress:- IpProtocol:tcpFromPort:22ToPort:22CidrIp:127.0.0.1/32GroupDescription:Allow http and sshVpcId:my-vpcSecurityGroupIngress:- FromPort:80ToPort:80CidrIp:127.0.0.1/32IpProtocol:tcp- ToPort:77CidrIp:127.0.0.1/32IpProtocol:allFromPort:77MyNegativeEC2Instance:Type:AWS::EC2::InstanceProperties:SecurityGroups:- SafeSecGroupKeyName:my-new-rsa-keyImageId:ami-79fd7eeeInstanceType:t3.medium
{"AWSTemplateFormatVersion":"2010-09-09T00:00:00Z","Resources":{"SafeSecGroup":{"Type":"AWS::EC2::SecurityGroup","Properties":{"GroupDescription":"Allow http and ssh","VpcId":"my-vpc","SecurityGroupIngress":[{"FromPort":80,"ToPort":80,"CidrIp":"127.0.0.1/32","IpProtocol":"tcp"},{"ToPort":77,"CidrIp":"127.0.0.1/32","IpProtocol":"all","FromPort":77}],"SecurityGroupEgress":[{"FromPort":22,"ToPort":22,"CidrIp":"127.0.0.1/32","IpProtocol":"tcp"}]}},"MyNegativeEC2Instance":{"Type":"AWS::EC2::Instance","Properties":{"SecurityGroups":["SafeSecGroup"],"KeyName":"my-new-rsa-key","ImageId":"ami-79fd7eee","InstanceType":"t3.medium"}}}}
Non-Compliant Code Examples
AWSTemplateFormatVersion:2010-09-09T00:00:00ZResources:UnsafeSecGroup01:Type:AWS::EC2::SecurityGroupProperties:GroupDescription:Allow http and redisVpcId:my-vpcSecurityGroupIngress:- FromPort:8080ToPort:8080CidrIp:127.0.0.1/32IpProtocol:tcp- IpProtocol:tcpFromPort:6379ToPort:6379CidrIp:10.0.0.1/0SecurityGroupEgress:- FromPort:22ToPort:22CidrIp:0.0.0.0/0IpProtocol:tcpEC2Instance01:Type:AWS::EC2::InstanceProperties:ImageId:ami-79fd7eeeInstanceType:t3.mediumSecurityGroups:- UnsafeSecGroup01KeyName:my-new-rsa-key
AWSTemplateFormatVersion:2010-09-09T00:00:00ZResources:UnsafeSecGroup02:Type:AWS::EC2::SecurityGroupProperties:GroupDescription:Allow http and mysqlVpcId:my-vpcSecurityGroupIngress:- IpProtocol:tcpFromPort:80ToPort:80CidrIp:127.0.0.1/32- ToPort:1434CidrIp:10.0.0.1/0IpProtocol:tcpFromPort:1433- IpProtocol:tcpFromPort:150ToPort:180CidrIp:10.0.0.1/0SecurityGroupEgress:- IpProtocol:tcpFromPort:22ToPort:22CidrIp:0.0.0.0/0EC2Instance02:Type:AWS::EC2::InstanceProperties:InstanceType:t3.mediumSecurityGroups:- UnsafeSecGroup02KeyName:my-new-rsa-keyImageId:ami-79fd7eee
AWSTemplateFormatVersion:2010-09-09T00:00:00ZResources:UnsafeSecGroup03:Type:AWS::EC2::SecurityGroupProperties:SecurityGroupEgress:- IpProtocol:tcpFromPort:22ToPort:22CidrIp:0.0.0.0/0GroupDescription:Allow http and hadoopVpcId:my-vpcSecurityGroupIngress:- ToPort:80CidrIp:0.0.0.0/0IpProtocol:tcpFromPort:80- ToPort:9000CidrIp:10.0.0.1/0IpProtocol:tcpFromPort:9000EC2Instance03:Type:AWS::EC2::InstanceProperties:SecurityGroups:- UnsafeSecGroup03KeyName:my-new-rsa-keyImageId:ami-79fd7eeeInstanceType:t3.medium
1
2
rulesets:- CloudFormation / AWS # Rules to enforce / AWS.
Request a personalized demo
Get Started with Datadog
Ask AI
AI-generated responses may be inaccurate. Verify important info.