Control: 5.3 Ensure no security groups allow ingress from 0.0.0.0/0 to remote server administration ports
Description
Security groups provide stateful filtering of ingress and egress network traffic to AWS resources. It is recommended that no security group allows unrestricted ingress access to remote server administration ports, such as SSH on port 22 and RDP on port 3389, using either the TCP (6), UDP (17), or ALL (-1) protocols.
Public access to remote server administration ports, such as 22 (when used for SSH, not SFTP) and 3389, increases the attack surface of resources and unnecessarily raises the risk of resource compromise.
Remediation
Perform the following to implement the prescribed state:
- Login to the AWS VPC Console at https://console.aws.amazon.com/vpc/home.
 - In the left pane, click 
Security Groups. - For each security group, perform the following:
 
- Select the security group.
 - Click the 
Inbound Rulestab. - Click the 
Edit inbound rulesbutton. - Identify the rules to be edited or removed.
 - Either A) update the Source field to a range other than 0.0.0.0/0, or B) click 
Deleteto remove the offending inbound rule. - Click 
Save rules. 
Usage
Run the control in your terminal:
powerpipe control run aws_compliance.control.cis_v400_5_3Snapshot and share results via Turbot Pipes:
powerpipe loginpowerpipe control run aws_compliance.control.cis_v400_5_3 --shareSQL
This control uses a named query:
with bad_rules as (  select    group_id,    count(*) as num_bad_rules  from    aws_vpc_security_group_rule  where    type = 'ingress'    and (      cidr_ipv4 = '0.0.0.0/0'      or cidr_ipv6 = '::/0'    )    and (        ( ip_protocol = '-1'      -- all traffic        and from_port is null        )        or (            from_port <= 22            and to_port >= 22        )        or (            from_port <= 3389            and to_port >= 3389        )    )  group by    group_id),security_groups as (  select    arn,    tags,    region,    account_id,    group_id,    _ctx  from    aws_vpc_security_group  order by    group_id)select  arn as resource,  case    when bad_rules.group_id is null then 'ok'    else 'alarm'  end as status,  case    when bad_rules.group_id is null then sg.group_id || ' does not allow ingress to port 22 or 3389 from 0.0.0.0/0 or ::/0.'    else  sg.group_id || ' contains ' || bad_rules.num_bad_rules || ' rule(s) that allow ingress to port 22 or 3389 from 0.0.0.0/0 or ::/0.'  end as reason    , sg.region, sg.account_idfrom  security_groups as sg  left join bad_rules on bad_rules.group_id = sg.group_id;