- Access exclusive content
- Connect with peers
- Share your expertise
- Find support resources
04-16-2019 06:51 PM - last edited on 09-02-2020 10:13 AM by kwadsack
Hi,
Some policies are producing alerts against SGs that are not associated with any resources.
Can you please advise how to customize the query and exclude those SGs?
For example, policy "AWS Security Groups allow internet traffic from internet to MYSQL port (3306)" is defined as follows:
config where cloud.type = 'aws' AND api.name='aws-ec2-describe-security-groups' AND json.rule = (((ipPermissions[?(@.toPort > 3306 && @.fromPort < 3306)].ipRanges[*] contains 0.0.0.0/0) or (ipPermissions[?(@.toPort == 3306 || @.fromPort == 3306)].ipRanges[*] contains 0.0.0.0/0)) or ((ipPermissions[?(@.toPort > 3306 && @.fromPort < 3306)].ipv6Ranges[*].cidrIpv6 contains ::/0) or (ipPermissions[?(@.toPort == 3306 || @.fromPort == 3306)].ipv6Ranges[*].cidrIpv6 contains ::/0)))
Thank you
04-17-2019 06:59 AM - last edited on 06-12-2019 05:49 PM by Protagonist
There isn't a way to exclude SGs attached to any kind of resources, but you can at least put in a few more restrictions. For example, the below query will return all security groups that are open to the public on port 22 that are on a VPC which contains an IGW with an EC2 instance attached.
config where api.name = 'aws-ec2-describe-security-groups' as X; config where api.name = 'aws-ec2-describe-internet-gateways' as Y; config where api.name = 'aws-ec2-describe-instances' as Z; filter '$.Z.securityGroups[*].groupId contains $.X.groupId and $.Y.attachments[*].vpcId contains $.X.vpcId and ($.X.ipPermissions[?(@.toPort==22||@.fromPort==22)].ipv6Ranges[*].cidrIpv6 contains ::/0 or $.X.ipPermissions[?(@.toPort==22||@.fromPort==22)].ipRanges[*] contains 0.0.0.0/0 or $.X.ipPermissions[?(@.toPort>22&&@.fromPort<22)].ipv6Ranges[*].cidrIpv6 contains ::/0 or $.X.ipPermissions[?(@.toPort>22&&@.fromPort<22)].ipRanges[*] contains 0.0.0.0/0)'; show X;
Hope this helps.
04-17-2019 06:59 AM - last edited on 06-12-2019 05:49 PM by Protagonist
There isn't a way to exclude SGs attached to any kind of resources, but you can at least put in a few more restrictions. For example, the below query will return all security groups that are open to the public on port 22 that are on a VPC which contains an IGW with an EC2 instance attached.
config where api.name = 'aws-ec2-describe-security-groups' as X; config where api.name = 'aws-ec2-describe-internet-gateways' as Y; config where api.name = 'aws-ec2-describe-instances' as Z; filter '$.Z.securityGroups[*].groupId contains $.X.groupId and $.Y.attachments[*].vpcId contains $.X.vpcId and ($.X.ipPermissions[?(@.toPort==22||@.fromPort==22)].ipv6Ranges[*].cidrIpv6 contains ::/0 or $.X.ipPermissions[?(@.toPort==22||@.fromPort==22)].ipRanges[*] contains 0.0.0.0/0 or $.X.ipPermissions[?(@.toPort>22&&@.fromPort<22)].ipv6Ranges[*].cidrIpv6 contains ::/0 or $.X.ipPermissions[?(@.toPort>22&&@.fromPort<22)].ipRanges[*] contains 0.0.0.0/0)'; show X;
Hope this helps.
Click Accept as Solution to acknowledge that the answer to your question has been provided.
The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!
These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!
The LIVEcommunity thanks you for your participation!