|
| 1 | + |
| 2 | +<# |
| 3 | + .SYNOPSIS |
| 4 | + Validates Threat intelligence is Enabled in Deny Mode on Azure Firewall. |
| 5 | + .DESCRIPTION |
| 6 | + This test validates that Azure Firewall Policies have Threat Intelligence enabled in Deny mode. |
| 7 | + Checks all firewall policies in the subscription and reports their threat intelligence status. |
| 8 | + .NOTES |
| 9 | + Test ID: 25537 |
| 10 | + Category: Azure Network Security |
| 11 | + Required API: Azure Firewall Policies |
| 12 | + #> |
| 13 | + |
| 14 | +function Test-Assessment-25537 { |
| 15 | + [ZtTest( |
| 16 | + Category = 'Azure Network Security', |
| 17 | + ImplementationCost = 'Low', |
| 18 | + MinimumLicense = ('Azure_Firewall_Standard', 'Azure_Firewall_Premium'), |
| 19 | + Pillar = 'Network', |
| 20 | + RiskLevel = 'High', |
| 21 | + SfiPillar = 'Protect networks', |
| 22 | + TenantType = ('Workforce'), |
| 23 | + TestId = 25537, |
| 24 | + Title = 'Threat intelligence is Enabled in Deny Mode on Azure Firewall', |
| 25 | + UserImpact = 'Low' |
| 26 | + )] |
| 27 | + [CmdletBinding()] |
| 28 | + param() |
| 29 | + |
| 30 | + Write-PSFMessage '🟦 Start' -Tag Test -Level VeryVerbose |
| 31 | + |
| 32 | + #region Data Collection |
| 33 | + $activity = 'Azure Firewall Threat Intelligence' |
| 34 | + Write-ZtProgress -Activity $activity -Status 'Enumerating Firewall Policies' |
| 35 | + |
| 36 | + # Query 1: List all subscriptions |
| 37 | + $context = Get-AzContext |
| 38 | + if (($context).Environment.name -ne 'AzureCloud') { |
| 39 | + Write-PSFMessage "This test is only applicable to the Global environment." -Tag Test -Level VeryVerbose |
| 40 | + Add-ZtTestResultDetail -SkippedBecause NotSupported |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + try { |
| 45 | + $accessToken = Get-AzAccessToken -AsSecureString -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |
| 46 | + } |
| 47 | + catch { |
| 48 | + Write-PSFMessage $_.Exception.Message -Tag Test -Level Error |
| 49 | + } |
| 50 | + |
| 51 | + if (-not $accessToken) { |
| 52 | + Write-PSFMessage "Azure authentication token not found." -Tag Test -Level Warning |
| 53 | + Add-ZtTestResultDetail -SkippedBecause 'NotConnectedAzure' |
| 54 | + return |
| 55 | + } |
| 56 | + $resourceManagerUrl = ($context).Environment.ResourceManagerUrl.TrimEnd('/') |
| 57 | + $subscriptionsUri = "$resourceManagerUrl/subscriptions?api-version=2025-03-01" |
| 58 | + |
| 59 | + try { |
| 60 | + $subscriptionsResponse = Invoke-AzRestMethod -Method GET -Uri $subscriptionsUri -ErrorAction Stop |
| 61 | + } |
| 62 | + catch { |
| 63 | + if ($_.Exception.Response.StatusCode -eq 403 -or $_.Exception.Message -like '*403*' -or $_.Exception.Message -like '*Forbidden*') { |
| 64 | + Add-ZtTestResultDetail -SkippedBecause NoAzureAccess |
| 65 | + return |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + $subscriptions = ($subscriptionsResponse.Content | ConvertFrom-Json).value |
| 70 | + |
| 71 | + if (-not $subscriptions) { |
| 72 | + Add-ZtTestResultDetail -SkippedBecause NoAzureAccess |
| 73 | + return |
| 74 | + } |
| 75 | + $results = @() |
| 76 | + foreach ($sub in $subscriptions) { |
| 77 | + |
| 78 | + Set-AzContext -SubscriptionId $sub.subscriptionId | Out-Null |
| 79 | + |
| 80 | + # Query 2 : List Azure Firewall Policies |
| 81 | + try { |
| 82 | + $policiesUri = "$resourceManagerUrl/subscriptions/$($sub.subscriptionId)/providers/Microsoft.Network/firewallPolicies?api-version=2025-03-01" |
| 83 | + Write-ZtProgress -Activity $activity -Status "Enumerating policies in subscription $($sub.displayName)" |
| 84 | + |
| 85 | + $policyResponse = (Invoke-AzRestMethod -Method GET -Uri $policiesUri -ErrorAction Stop ).Content | ConvertFrom-Json |
| 86 | + $policies = $policyResponse.value |
| 87 | + |
| 88 | + } |
| 89 | + catch { |
| 90 | + Write-PSFMessage "Unable to enumerate firewall policies in subscription $($sub.displayName): $($_.Exception.Message)" -Tag Firewall -Level Warning |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + if (-not $policies) { |
| 95 | + continue |
| 96 | + } |
| 97 | + |
| 98 | + # Query 2: Get details for each firewall policy and check threatIntelMode |
| 99 | + foreach ($policyResource in $policies) { |
| 100 | + |
| 101 | + $threatIntelMode = $policyResource.Properties.threatIntelMode |
| 102 | + |
| 103 | + $subContext = Get-AzContext |
| 104 | + |
| 105 | + $results += [PSCustomObject]@{ |
| 106 | + PolicyName = $policyResource.Name |
| 107 | + SubscriptionName = $subContext.Subscription.Name |
| 108 | + SubscriptionId = $subContext.Subscription.Id |
| 109 | + ThreatIntelMode = $threatIntelMode |
| 110 | + PolicyID = $policyResource.Id |
| 111 | + Passed = $threatIntelMode -eq 'Deny' |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + #endregion Data Collection |
| 116 | + |
| 117 | + #region Assessment Logic |
| 118 | + |
| 119 | + |
| 120 | + $passed = ($results | Where-Object { -not $_.Passed }).Count -eq 0 |
| 121 | + $allAlert = ($results | Where-Object { $_.ThreatIntelMode -ne 'Alert' }).Count -eq 0 |
| 122 | + |
| 123 | + $testResultMarkdown = if ($passed) { |
| 124 | + "Threat Intel is enabled in **Alert and Deny** mode.`n`n%TestResult%" |
| 125 | + } |
| 126 | + elseif ($allAlert) { |
| 127 | + "Threat Intel is enabled in **Alert** mode.`n`n%TestResult%" |
| 128 | + } |
| 129 | + else { |
| 130 | + "Threat Intel is not enabled in **Alert and Deny** mode for all Firewall policies.`n`n%TestResult%" |
| 131 | + } |
| 132 | + #endregion Assessment Logic |
| 133 | + |
| 134 | + #region Report Generation |
| 135 | + $mdInfo = "## Firewall policies`n`n" |
| 136 | + $mdInfo += "| Policy name | Subscription name | Threat Intel mode | Result |`n" |
| 137 | + $mdInfo += "| :--- | :--- | :--- | :--- |`n" |
| 138 | + |
| 139 | + foreach ($item in $results | Sort-Object PolicyName) { |
| 140 | + $policyLink = "https://portal.azure.com/#resource$($item.PolicyID)" |
| 141 | + $subLink = "https://portal.azure.com/#resource/subscriptions/$($item.SubscriptionId)" |
| 142 | + $policyMd = "[$(Get-SafeMarkdown -Text $item.PolicyName)]($policyLink)" |
| 143 | + $subMd = "[$(Get-SafeMarkdown -Text $item.SubscriptionName)]($subLink)" |
| 144 | + $icon = if ($item.Passed) { |
| 145 | + '✅' |
| 146 | + } |
| 147 | + else { |
| 148 | + '❌' |
| 149 | + } |
| 150 | + $mdInfo += "| $policyMd | $subMd | $($item.ThreatIntelMode) | $icon |`n" |
| 151 | + } |
| 152 | + |
| 153 | + $testResultMarkdown = $testResultMarkdown -replace '%TestResult%', $mdInfo |
| 154 | + #endregion Report Generation |
| 155 | + $params = @{ |
| 156 | + TestId = '25537' |
| 157 | + Title = 'Threat intelligence is Enabled in Deny Mode on Azure Firewall' |
| 158 | + Status = $passed |
| 159 | + Result = $testResultMarkdown |
| 160 | + } |
| 161 | + |
| 162 | + Add-ZtTestResultDetail @params |
| 163 | +} |
0 commit comments