Project: KICS (Provisioning / Security & Compliance)
File: 87482183-a8e7-4e42-a566-7a23ec231c16.md

title: Security Group Ingress With Port Range hide: toc: true navigation: true <style> .highlight .hll { background-color: #ff171742; } .md-content { max-width: 1100px; margin: 0 auto; } </style> Query id: 87482183-a8e7-4e42-a566-7a23ec231c16 Query name: Security Group Ingress With Port Range Platform: CloudFormation Severity: <span style="color:#ff7213">Medium</span> Category: Networking and Firewall URL: AWS Security Group Ingress should have a single port<br> ```yaml title="Positive test num. 1 - yaml file" hl_lines="8 37" Resources: InstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Allow http to client host VpcId: Ref: myVPC SecurityGroupIngress: IpProtocol: tcp Description: TCP FromPort: 80 ToPort: 87 CidrIp: 0.0.0.0/0 SecurityGroupEgress: IpProtocol: tcp Description: TCP FromPort: 80 ToPort: 87 CidrIp: 0.0.0.0/0 OutboundRule: Type: AWS::EC2::SecurityGroupEgress Properties: Description: TCP IpProtocol: tcp FromPort: 0 ToPort: 65535 DestinationSecurityGroupId: Fn::GetAtt: TargetSG GroupId GroupId: Fn::GetAtt: SourceSG GroupId InboundRule: Type: AWS::EC2::SecurityGroupIngress Properties: Description: TCP IpProtocol: tcp FromPort: 0 ToPort: 65535 SourceSecurityGroupId: Fn::GetAtt: SourceSG GroupId GroupId: Fn::GetAtt: TargetSG GroupId ``` ```json title="Positive test num. 2 - json file" hl_lines="9 53" { "Resources": { "InstanceSecurityGroup": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "VpcId": { "Ref": "myVPC" }, "SecurityGroupIngress": [ { "Description": "TCP", "FromPort": 80, "ToPort": 87, "CidrIp": "0.0.0.0/0", "IpProtocol": "tcp" } ], "SecurityGroupEgress": [ { "IpProtocol": "tcp", "Description": "TCP", "FromPort": 80, "ToPort": 87, "CidrIp": "0.0.0.0/0" } ], "GroupDescription": "Allow http to client host" } }, "OutboundRule": { "Type": "AWS::EC2::SecurityGroupEgress", "Properties": { "ToPort": 65535, "DestinationSecurityGroupId": { "Fn::GetAtt": [ "TargetSG", "GroupId" ] }, "GroupId": { "Fn::GetAtt": [ "SourceSG", "GroupId" ] }, "Description": "TCP", "IpProtocol": "tcp", "FromPort": 0 } }, "InboundRule": { "Type": "AWS::EC2::SecurityGroupIngress", "Properties": { "Description": "TCP", "IpProtocol": "tcp", "FromPort": 0, "ToPort": 65535, "SourceSecurityGroupId": { "Fn::GetAtt": [ "SourceSG", "GroupId" ] }, "GroupId": { "Fn::GetAtt": [ "TargetSG", "GroupId" ] } } } } } ``` ```yaml title="Negative test num. 1 - yaml file" Resources: InstanceSecurityGroup: Type: AWS::EC2::SecurityGroup Properties: GroupDescription: Allow http to client host VpcId: Ref: myVPC SecurityGroupIngress: IpProtocol: tcp Description: TCP FromPort: 80 ToPort: 80 CidrIp: 0.0.0.0/0 SecurityGroupEgress: IpProtocol: tcp Description: TCP FromPort: 80 ToPort: 80 CidrIp: 0.0.0.0/0 OutboundRule: Type: AWS::EC2::SecurityGroupEgress Properties: Description: TCP IpProtocol: tcp FromPort: 0 ToPort: 0 DestinationSecurityGroupId: Fn::GetAtt: TargetSG GroupId GroupId: Fn::GetAtt: SourceSG GroupId InboundRule: Type: AWS::EC2::SecurityGroupIngress Properties: Description: TCP IpProtocol: tcp FromPort: 0 ToPort: 0 SourceSecurityGroupId: Fn::GetAtt: SourceSG GroupId GroupId: Fn::GetAtt: TargetSG GroupId ``` ```json title="Negative test num. 2 - json file" { "Resources": { "InboundRule": { "Type": "AWS::EC2::SecurityGroupIngress", "Properties": { "IpProtocol": "tcp", "FromPort": 0, "ToPort": 0, "SourceSecurityGroupId": { "Fn::GetAtt": [ "SourceSG", "GroupId" ] }, "GroupId": { "Fn::GetAtt": [ "TargetSG", "GroupId" ] }, "Description": "TCP" } }, "InstanceSecurityGroup": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "GroupDescription": "Allow http to client host", "VpcId": { "Ref": "myVPC" }, "SecurityGroupIngress": [ { "IpProtocol": "tcp", "Description": "TCP", "FromPort": 80, "ToPort": 80, "CidrIp": "0.0.0.0/0" } ], "SecurityGroupEgress": [ { "IpProtocol": "tcp", "Description": "TCP", "FromPort": 80, "ToPort": 80, "CidrIp": "0.0.0.0/0" } ] } }, "OutboundRule": { "Properties": { "DestinationSecurityGroupId": { "Fn::GetAtt": [ "TargetSG", "GroupId" ] }, "GroupId": { "Fn::GetAtt": [ "SourceSG", "GroupId" ] }, "Description": "TCP", "IpProtocol": "tcp", "FromPort": 0, "ToPort": 0 }, "Type": "AWS::EC2::SecurityGroupEgress" } } } ```