Dear Chen Dgani,
Thank you for your inquiry regarding the Azure Policy to restrict specific IP address ranges in NSG rules. I understand your requirement to block inbound traffic from the IP address 1.2.3.4 and any CIDR ranges that include it (e.g., 1.2.3.0/24, 1.2.0.0/16). If so below I would like offer to u an Azure Policy definition that will deny NSG rules allowing inbound traffic from the specified IP and its associated CIDR ranges
{
"mode": "All",
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.Network/networkSecurityGroups/securityRules"
},
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/access",
"equals": "Allow"
},
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/direction",
"equals": "Inbound"
},
{
"anyOf": [
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefix",
"equals": "1.2.3.4"
},
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefix",
"in": ["1.2.3.4/32", "1.2.3.0/24", "1.2.0.0/16"]
},
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefixes[*]",
"contains": "1.2.3.4"
},
{
"field": "Microsoft.Network/networkSecurityGroups/securityRules/sourceAddressPrefixes[*]",
"in": ["1.2.3.4/32", "1.2.3.0/24", "1.2.0.0/16"]
}
]
}
]
},
"then": {
"effect": "deny"
}
},
"parameters": {}
}
Deploy the Policy and assign this definition at the desired scope (Management Group, Subscription, or Resource Group).
Test it and validate the policy in audit mode before enforcing it.
If additional IP ranges need blocking, expand the in array.
Best regards,
Alex
P.S. If my answer help to you, please Accept my answer