Azure policy - IP Addresses range

chen dgani 60 Reputation points
2025-04-01T13:13:19.42+00:00

Hi.

I want to create an Azure Policy Definition that denies any NSG rule allowing inbound traffic from the source IP 1.2.3.4.

Specifically, I want to block any NSG rule that permits traffic to any address range containing 1.2.3.4 using CIDR notation, such as 1.2.3.0/24 or 1.2.0.0/16.

I have seen a similar example for creating an Azure Policy Definition for port ranges, but that case was simpler as it only involved checking integer ranges. (The example in lines 55-56).

Thank you.

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
996 questions
{count} votes

Accepted answer
  1. Alex Burlachenko 4,875 Reputation points
    2025-04-01T14:59:36.39+00:00

    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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.