Azure Policy Violation Detected


Description

Detects when an Azure resource is found to be non-compliant with assigned Azure Policies. Policy violations indicate that resources do not meet organizational compliance requirements for security, networking, encryption, or other governance controls. Repeated violations may indicate configuration drift or potential security misconfigurations.

Query · python

from panther_azureactivity_helpers import azure_activity_alert_context, azure_parse_json_string

POLICY_AUDIT_OPERATIONS = [
    "MICROSOFT.AUTHORIZATION/POLICIES/AUDIT/ACTION",
    "MICROSOFT.AUTHORIZATION/POLICIES/AUDITIFNOTEXISTS/ACTION",
]
POLICY_CATEGORY = "Policy"
WARNING_LEVEL = "Warning"


def rule(event):
    # Detects Azure Policy violations (audit failures) that indicate resources
    # are not compliant with assigned Azure Policies.
    return all(
        [
            event.get("operationName", "").upper() in POLICY_AUDIT_OPERATIONS,
            event.get("category", "") == POLICY_CATEGORY,
            event.get("level", "") == WARNING_LEVEL,
        ]
    )


def title(event):
    entity = event.deep_get("properties", "entity", default="<UNKNOWN_RESOURCE>")

    # Extract first policy name if available
    policies_json = event.deep_get("properties", "policies", default="[]")
    policies = azure_parse_json_string(policies_json)
    if policies and isinstance(policies, list) and len(policies) > 0:
        policy_name = policies[0].get("policyDefinitionDisplayName", "<UNKNOWN_POLICY>")
    else:
        policy_name = "<UNKNOWN_POLICY>"
    return f"Azure Policy Violation: [{policy_name}] on [{entity}]"


def alert_context(event):
    context = azure_activity_alert_context(event)

    # Add policy-specific context
    context["entity"] = event.deep_get("properties", "entity", default=None)
    context["message"] = event.deep_get("properties", "message", default=None)
    context["is_compliance_check"] = event.deep_get("properties", "isComplianceCheck", default=None)
    context["resource_location"] = event.deep_get("properties", "resourceLocation", default=None)
    return context

Analyst notes

  1. Query Azure Monitor Activity logs for all configuration changes to the resource ID in the 48 hours before the policy violation to identify what triggered the non-compliance
  2. Find all policy violations for the same resource ID in the past 30 days to determine if this is an isolated incident or pattern of configuration drift
  3. Check for other policy violations with the same policy definition name across all resources in the past 7 days to identify broader compliance issues
Raw source Azure Policy Violation Detected · Panther Python
Esc
Published by panther-labs/panther-analysis ↗, licensed under Apache 2.0 ↗. Reproduced here unmodified.
AnalysisType: rule
Filename: azure_policy_violation.py
RuleID: "Azure.MonitorActivity.Policy.Violation"
DisplayName: "Azure Policy Violation Detected"
Enabled: true
LogTypes:
  - Azure.MonitorActivity
Severity: Info
Status: Experimental
Description: >
  Detects when an Azure resource is found to be non-compliant with assigned Azure Policies.
  Policy violations indicate that resources do not meet organizational compliance requirements
  for security, networking, encryption, or other governance controls. Repeated violations
  may indicate configuration drift or potential security misconfigurations.
Runbook: |
  1. Query Azure Monitor Activity logs for all configuration changes to the resource ID in the 48 hours before the policy violation to identify what triggered the non-compliance
  2. Find all policy violations for the same resource ID in the past 30 days to determine if this is an isolated incident or pattern of configuration drift
  3. Check for other policy violations with the same policy definition name across all resources in the past 7 days to identify broader compliance issues
Reference: https://learn.microsoft.com/en-us/azure/governance/policy/overview
SummaryAttributes:
  - properties.entity
  - callerIpAddress
  - correlationId
Tests:
  - Name: Storage Account Policy Violation
    ExpectedResult: true
    Log:
      {
        "time": "2025-12-22T10:30:00.0000000Z",
        "resourceId": "/subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/storage-rg/providers/Microsoft.Storage/storageAccounts/mystorage",
        "operationName": "MICROSOFT.AUTHORIZATION/POLICIES/AUDIT/ACTION",
        "operationVersion": "2021-06-01",
        "category": "Policy",
        "resultType": "Success",
        "callerIpAddress": "1.1.1.1",
        "correlationId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
        "level": "Warning",
        "location": "eastus",
        "properties": {
          "entity": "/subscriptions/12345678-1234-1234-1234-123456789abc/resourceGroups/storage-rg/providers/Microsoft.Storage/storageAccounts/mystorage",
          "eventCategory": "Policy",
          "isComplianceCheck": "False",
          "message": "Microsoft.Authorization/policies/audit/action",
          "policies": "[{\"policyDefinitionId\":\"/providers/Microsoft.Authorization/policyDefinitions/2a1a9cdf-e04d-429a-8416-3bfb72a1b26f\",\"policySetDefinitionId\":\"/providers/Microsoft.Authorization/policySetDefinitions/1f3afdf9-d0c9-4c3d-847f-89da613e70a8\",\"policyDefinitionReferenceId\":\"storageAccountsShouldRestrictNetworkAccessUsingVirtualNetworkRulesMonitoringEffect\",\"policySetDefinitionName\":\"1f3afdf9-d0c9-4c3d-847f-89da613e70a8\",\"policySetDefinitionDisplayName\":\"Microsoft cloud security benchmark\",\"policyDefinitionName\":\"2a1a9cdf-e04d-429a-8416-3bfb72a1b26f\",\"policyDefinitionDisplayName\":\"Storage accounts should restrict network access using virtual network rules\",\"policyDefinitionEffect\":\"Audit\",\"policyAssignmentId\":\"/subscriptions/12345678-1234-1234-1234-123456789abc/providers/Microsoft.Authorization/policyAssignments/SecurityCenterBuiltIn\",\"policyAssignmentName\":\"SecurityCenterBuiltIn\",\"policyAssignmentDisplayName\":\"ASC Default\"}]",
          "resourceLocation": "westus2"
        },
        "tenantId": "87654321-4321-4321-4321-111111111111"
      }
  - Name: Shared Key Access Policy Violation
    ExpectedResult: true
    Log:
      {
        "time": "2025-12-22T11:15:00.0000000Z",
        "resourceId": "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/prod-rg/providers/Microsoft.Storage/storageAccounts/prodstorage",
        "operationName": "microsoft.authorization/policies/audit/action",
        "category": "Policy",
        "resultType": "Success",
        "callerIpAddress": "1.2.3.4",
        "correlationId": "b2c3d4e5-f6a7-8901-bcde-f23456789012",
        "level": "Warning",
        "location": "westeurope",
        "properties": {
          "entity": "/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/prod-rg/providers/Microsoft.Storage/storageAccounts/prodstorage",
          "eventCategory": "Policy",
          "isComplianceCheck": "True",
          "message": "Microsoft.Authorization/policies/audit/action",
          "policies": "[{\"policyDefinitionDisplayName\":\"Storage accounts should prevent shared key access\",\"policySetDefinitionDisplayName\":\"Microsoft cloud security benchmark\",\"policyDefinitionEffect\":\"Audit\",\"policyAssignmentDisplayName\":\"ASC Default\"}]",
          "resourceLocation": "westeurope"
        },
        "tenantId": "22222222-2222-2222-2222-222222222222"
      }
  - Name: AuditIfNotExists Policy Violation
    ExpectedResult: true
    Log:
      {
        "time": "2025-12-22T12:00:00.0000000Z",
        "resourceId": "/subscriptions/33333333-3333-3333-3333-333333333333/resourceGroups/network-rg/providers/Microsoft.Network/virtualNetworks/myvnet",
        "operationName": "MICROSOFT.AUTHORIZATION/POLICIES/AUDITIFNOTEXISTS/ACTION",
        "category": "Policy",
        "resultType": "Success",
        "callerIpAddress": "2.2.2.2",
        "correlationId": "c3d4e5f6-a7b8-9012-cdef-333333333333",
        "level": "Warning",
        "location": "centralus",
        "properties": {
          "entity": "/subscriptions/33333333-3333-3333-3333-333333333333/resourceGroups/network-rg/providers/Microsoft.Network/virtualNetworks/myvnet",
          "eventCategory": "Policy",
          "isComplianceCheck": "False",
          "message": "Microsoft.Authorization/policies/auditifnotexists/action",
          "policies": "[{\"policyDefinitionDisplayName\":\"Network Watcher should be enabled\",\"policyDefinitionEffect\":\"AuditIfNotExists\"}]",
          "resourceLocation": "centralus"
        },
        "tenantId": "33333333-3333-3333-3333-333333333333"
      }
  - Name: Different Category
    ExpectedResult: false
    Log:
      {
        "time": "2025-12-22T14:00:00.0000000Z",
        "resourceId": "/subscriptions/55555555-5555-5555-5555-555555555555/resourceGroups/admin-rg/providers/Microsoft.Storage/storageAccounts/adminstorage",
        "operationName": "MICROSOFT.STORAGE/STORAGEACCOUNTS/WRITE",
        "category": "Administrative",
        "resultType": "Success",
        "callerIpAddress": "1.2.3.4",
        "correlationId": "e5f6a7b8-c9d0-1234-ef01-111111111111",
        "level": "Information",
        "tenantId": "55555555-5555-5555-5555-555555555555"
      }

# ------ paired body: azure_policy_violation.py ------

from panther_azureactivity_helpers import azure_activity_alert_context, azure_parse_json_string

POLICY_AUDIT_OPERATIONS = [
    "MICROSOFT.AUTHORIZATION/POLICIES/AUDIT/ACTION",
    "MICROSOFT.AUTHORIZATION/POLICIES/AUDITIFNOTEXISTS/ACTION",
]
POLICY_CATEGORY = "Policy"
WARNING_LEVEL = "Warning"


def rule(event):
    # Detects Azure Policy violations (audit failures) that indicate resources
    # are not compliant with assigned Azure Policies.
    return all(
        [
            event.get("operationName", "").upper() in POLICY_AUDIT_OPERATIONS,
            event.get("category", "") == POLICY_CATEGORY,
            event.get("level", "") == WARNING_LEVEL,
        ]
    )


def title(event):
    entity = event.deep_get("properties", "entity", default="<UNKNOWN_RESOURCE>")

    # Extract first policy name if available
    policies_json = event.deep_get("properties", "policies", default="[]")
    policies = azure_parse_json_string(policies_json)
    if policies and isinstance(policies, list) and len(policies) > 0:
        policy_name = policies[0].get("policyDefinitionDisplayName", "<UNKNOWN_POLICY>")
    else:
        policy_name = "<UNKNOWN_POLICY>"
    return f"Azure Policy Violation: [{policy_name}] on [{entity}]"


def alert_context(event):
    context = azure_activity_alert_context(event)

    # Add policy-specific context
    context["entity"] = event.deep_get("properties", "entity", default=None)
    context["message"] = event.deep_get("properties", "message", default=None)
    context["is_compliance_check"] = event.deep_get("properties", "isComplianceCheck", default=None)
    context["resource_location"] = event.deep_get("properties", "resourceLocation", default=None)
    return context

Detection rules belong to the projects that publish them and remain under their own licenses. This site indexes and links to them; it claims no rights in them.