Bylaw

Gallery › Monitoring › Azure Community Policy

Deploy Windows Diagnostic Agent to Collect Security Related Events

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

This Policy will Deploy the Windows Diagnostic Agent and collect the following Security events: Audit success, Audit failure; and the following System events: Critical, Error, Warning. Additionaly, to account for VMs provisioned from custom images where the image SKU is blank this Policy is keyed to look for the storageProfile.osDisk.osType property of a VM. This property does not exist at provisioning time, but is populated by the VM agent after provisioining, and so will not trigger an automatic remediati

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONazurepolicy.json · 187 lines
{
  "name": "c0684160-102c-4d85-a658-d54f16e05ef7",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy Windows Diagnostic Agent to Collect Security Related Events",
    "description": "This Policy will Deploy the Windows Diagnostic Agent and collect the following Security events: Audit success, Audit failure; and the following System events: Critical, Error, Warning. Additionaly, to account for VMs provisioned from custom images where the image SKU is blank this Policy is keyed to look for the storageProfile.osDisk.osType property of a VM. This property does not exist at provisioning time, but is populated by the VM agent after provisioining, and so will not trigger an automatic remediati",
    "metadata": {
      "version": "1.0.0",
      "category": "Monitoring"
    },
    "mode": "Indexed",
    "parameters": {
      "storageAccountId": {
        "type": "String",
        "metadata": {
          "strongType": "Microsoft.Storage/storageAccounts",
          "assignPermissions": true,
          "displayName": "Storage Account",
          "description": "This is the Storage Account that all log data will be sent to."
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          },
          {
            "field": "Microsoft.Compute/virtualMachines/storageProfile.osDisk.osType",
            "equals": "Windows"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Compute/virtualMachines/extensions",
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/92aaf0da-9dab-42b6-94a3-d43ce8d16293"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/type",
                "equals": "IaaSDiagnostics"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/publisher",
                "equals": "Microsoft.Azure.Diagnostics"
              },
              {
                "field": "Microsoft.Compute/virtualMachines/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "vmName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "storageAccountId": {
                    "type": "string"
                  },
                  "vmId": {
                    "type": "string"
                  }
                },
                "variables": {
                  "vmExtensionName": "Microsoft.Insights.VMDiagnosticsSettings",
                  "vmExtensionPublisher": "Microsoft.Azure.Diagnostics",
                  "vmExtensionType": "IaaSDiagnostics",
                  "vmExtensionTypeHandlerVersion": "1.5",
                  "storageAccountId": "/subscriptions/ba1f7dcc-89de-4858-9f8b-b2ad61c895b5/resourceGroups/PLS/providers/Microsoft.Storage/storageAccounts/plsguestdiag",
                  "existingdiagnosticsStorageAccountName": "[last(split(parameters('storageAccountId'), '/'))]"
                },
                "resources": [
                  {
                    "name": "[concat(parameters('vmName'), '/', variables('vmExtensionName'))]",
                    "type": "Microsoft.Compute/virtualMachines/extensions",
                    "location": "[parameters('location')]",
                    "apiVersion": "2018-06-01",
                    "properties": {
                      "publisher": "[variables('vmExtensionPublisher')]",
                      "type": "[variables('vmExtensionType')]",
                      "typeHandlerVersion": "[variables('vmExtensionTypeHandlerVersion')]",
                      "autoUpgradeMinorVersion": true,
                      "settings": {
                        "StorageAccount": "[variables('existingdiagnosticsStorageAccountName')]",
                        "WadCfg": {
                          "DiagnosticMonitorConfiguration": {
                            "overallQuotaInMB": 5120,
                            "Metrics": {
                              "resourceId": "/subscriptions/ba1f7dcc-89de-4858-9f8b-b2ad61c895b5/resourceGroups/PLS/providers/Microsoft.Compute/virtualMachines/PLS",
                              "MetricAggregation": [
                                {
                                  "scheduledTransferPeriod": "PT1H"
                                },
                                {
                                  "scheduledTransferPeriod": "PT1M"
                                }
                              ]
                            },
                            "DiagnosticInfrastructureLogs": {
                              "scheduledTransferLogLevelFilter": "Error",
                              "scheduledTransferPeriod": "PT1M"
                            },
                            "PerformanceCounters": {
                              "scheduledTransferPeriod": "PT1M",
                              "PerformanceCounterConfiguration": []
                            },
                            "WindowsEventLog": {
                              "scheduledTransferPeriod": "PT1M",
                              "DataSource": [
                                {
                                  "name": "System!*[System[(Level=1 or Level=2 or Level=3)]]"
                                },
                                {
                                  "name": "Security!*[System[(band(Keywords,13510798882111488))]]"
                                }
                              ]
                            },
                            "Directories": {
                              "scheduledTransferPeriod": "PT1M"
                            }
                          }
                        }
                      },
                      "protectedSettings": {
                        "storageAccountName": "[variables('existingdiagnosticsStorageAccountName')]",
                        "storageAccountKey": "[listkeys(parameters('storageAccountId'), '2015-05-01-preview').key1]",
                        "storageAccountEndPoint": "https://core.windows.net"
                      }
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('Enabled windows diagnostic extension for VM', ': ', parameters('vmName'))]"
                  }
                }
              },
              "parameters": {
                "vmName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "storageAccountId": {
                  "value": "[parameters('storageAccountId')]"
                },
                "vmId": {
                  "value": "[field('id')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawc0684160-102c-4d85-a658-d54f16e05ef7.bylaw.ps1 · 45 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'c0684160-102c-4d85-a658-d54f16e05ef7' {

    Properties @{
        DisplayName = 'Deploy Windows Diagnostic Agent to Collect Security Related Events'
        Description = 'This Policy will Deploy the Windows Diagnostic Agent and collect the following Security events: Audit success, Audit failure; and the following System events: Critical, Error, Warning. Additionaly, to account for VMs provisioned from custom images where the image SKU is blank this Policy is keyed to look for the storageProfile.osDisk.osType property of a VM. This property does not exist at provisioning time, but is populated by the VM agent after provisioining, and so will not trigger an automatic remediati'
        Version     = '1.0.0'
        Category    = 'Monitoring'
        Mode        = 'Indexed'
    }

    Parameter StorageAccountId @{
        Type        = 'String'
        DisplayName = 'Storage Account'
        Description = 'This is the Storage Account that all log data will be sent to.'
        Metadata    = [ordered]@{ strongType = 'Microsoft.Storage/storageAccounts'; assignPermissions = $true }
    }
    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Compute/virtualMachines' {

        Require 'storageProfile.osDisk.osType is not Windows' {
            Field storageProfile.osDisk.osType | Must -Not -Be 'Windows' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.Compute/virtualMachines/extensions' -Role 'Log Analytics Contributor' -Effect (Param Effect) {
        Require 'type is IaaSDiagnostics, publisher is Microsoft.Azure.Diagnostics and provisioningState is Succeeded' {
            Field 'Microsoft.Compute/virtualMachines/extensions/type' | Must -Be 'IaaSDiagnostics'
            Field publisher | Must -Be 'Microsoft.Azure.Diagnostics'
            Field provisioningState | Must -Be 'Succeeded'
        }
        Deployment -TemplateFile './templates/c0684160-102c-4d85-a658-d54f16e05ef7.json' -Parameters ([ordered]@{
                vmName           = (Arm '[field(''name'')]')
                location         = (Arm '[field(''location'')]')
                storageAccountId = (Param StorageAccountId)
                vmId             = (Arm '[field(''id'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/Monitoring/deploy-windows-diagnostic-agent-to-collect-security-related-events/azurepolicy.json in Azure/Community-Policy, under the MIT licence.