Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(arm): NSGRuleUDPAccessRestricted #6616

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
48 changes: 48 additions & 0 deletions checkov/arm/checks/resource/NSGRuleUDPAccessRestricted.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from __future__ import annotations

from checkov.common.models.enums import CheckCategories, CheckResult
from checkov.arm.checks.resource.NSGRulePortAccessRestricted import INTERNET_ADDRESSES
from checkov.arm.base_resource_check import BaseResourceCheck
from typing import List, Dict, Union, Any


class NSGRuleUDPAccessRestricted(BaseResourceCheck):
def __init__(self) -> None:
name = "Ensure that UDP Services are restricted from the Internet "
id = "CKV_AZURE_77"
supported_resources = ['Microsoft.Network/networkSecurityGroups',
'Microsoft.Network/networkSecurityGroups/securityRules']
categories = [CheckCategories.NETWORKING]
super().__init__(name=name, id=id, categories=categories, supported_resources=supported_resources)

def scan_resource_conf(self, conf: Dict[str, Union[str, Dict[str, List[Dict[str, str] | Any]]]]) -> CheckResult:
rule_confs = [conf.get("properties", {})]
evaluated_key_prefix = ''
if isinstance(rule_confs[0], dict) and 'securityRules' in rule_confs[0]:
rule_confs = [rule_confs[0]['securityRules'][0]["properties"]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is failing the dogfood-tests

self.evaluated_keys = ['securityRules']
evaluated_key_prefix = 'securityRules/'
for rule_conf in rule_confs:
if isinstance(rule_conf, dict):
protocol = rule_conf.get('protocol')
direction = rule_conf.get('direction')
access = rule_conf.get('access')
source_address_prefix = rule_conf.get('sourceAddressPrefix')
if isinstance(protocol, str) and protocol.lower() == 'udp' \
and isinstance(direction, str) and direction.lower() == 'inbound' \
and isinstance(access, str) and access.lower() == 'allow' \
and isinstance(source_address_prefix, str) \
and source_address_prefix.lower() in INTERNET_ADDRESSES:
evaluated_key_prefix = f'{evaluated_key_prefix}[{rule_confs.index(rule_conf)}]/' if \
evaluated_key_prefix else ''
self.evaluated_keys = [f'{evaluated_key_prefix}protocol',
f'{evaluated_key_prefix}direction',
f'{evaluated_key_prefix}access',
f'{evaluated_key_prefix}sourceAddressPrefix']
return CheckResult.FAILED
return CheckResult.PASSED
return CheckResult.UNKNOWN
return CheckResult.PASSED


check = NSGRuleUDPAccessRestricted()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "fail",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "fail",
"properties": {
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
},
"tags": {
"environment": "Production"
}
}

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "fail2",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "fail2",
"properties": {
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "any",
"destinationAddressPrefix": "*",
"access": "Allow",
"priority": 100,
"direction": "Inbound"
}
}
]
},
"tags": {
"environment": "Production"
}
}

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "fail3",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Allow",
"direction": "Inbound",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "<nw>/0",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "fail4",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Allow",
"direction": "Inbound",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "/0",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "fail5",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Allow",
"direction": "Inbound",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "Internet",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "pass",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Deny",
"direction": "Inbound",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "pass2",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Allow",
"direction": "Outbound",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2020-11-01",
"name": "pass3",
"location": "[parameters('region')]",
"properties": {
"securityRules": [
{
"name": "test123",
"properties": {
"priority": 100,
"access": "Allow",
"direction": "Inbound",
"protocol": "Tcp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*"
}
}
]
},
"tags": {
"environment": "Production"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"apiVersion": "2020-11-01",
"name": "ruleFail",
"location": "[resourceGroup().location]",
"properties": {
"priority": 100,
"direction": "Inbound",
"access": "Allow",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "*",
"destinationAddressPrefix": "*"
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
]
}
],
"parameters": {
"networkSecurityGroupName": {
"type": "string",
"metadata": {
"description": "The name of the Network Security Group."
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"resources": [
{
"type": "Microsoft.Network/networkSecurityGroups/securityRules",
"apiVersion": "2020-11-01",
"name": "ruleFail2",
"location": "[resourceGroup().location]",
"properties": {
"priority": 100,
"direction": "Inbound",
"access": "Allow",
"protocol": "Udp",
"sourcePortRange": "*",
"destinationPortRange": "*",
"sourceAddressPrefix": "any",
"destinationAddressPrefix": "*"
},
"dependsOn": [
"[resourceId('Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
]
}
],
"parameters": {
"networkSecurityGroupName": {
"type": "string",
"metadata": {
"description": "The name of the Network Security Group."
}
}
}
}
Loading
Loading