你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure 网络观察程序安全组视图自动进行 NSG 审核

注意

安全组视图 API 不再维护,很快就会被弃用。 请使用提供相同功能的有效安全规则功能

客户经常会遇到验证基础结构安全局势的难题。 对于 Azure 中的 VM,也存在类似的难题。 必须应用一个基于网络安全组 (NSG) 规则的类似安全配置文件。 现在,使用安全组视图可以获取应用到 NSG 中 VM 的规则列表。 可以定义一个黄金 NSG 安全配置文件,然后每周启动安全组视图,将输出与该黄金配置文件进行比较并创建报告。 这样,便可以轻松识别不符合指定的安全配置文件的所有 VM。

若要深入了解网络安全组,请参阅网络安全组概述

先决条件

  • 如果没有具备有效订阅的 Azure 帐户,请免费创建一个
  • 虚拟机 (VM)。

方案

本方案将已知合理的基准与针对虚拟机返回的安全组视图结果进行比较。

在此方案中,将:

  • 检索已知合理的规则集
  • 使用 REST API 检索虚拟机
  • 获取虚拟机的安全组视图
  • 评估响应

检索规则集

本示例的第一个步骤是处理现有的基准。 以下示例是使用 Get-AzNetworkSecurityGroup cmdlet 从现有网络安全组中提取的、用作本示例基准的一些 JSON 代码。

[
    {
        "Description":  null,
        "Protocol":  "TCP",
        "SourcePortRange":  "*",
        "DestinationPortRange":  "3389",
        "SourceAddressPrefix":  "*",
        "DestinationAddressPrefix":  "*",
        "Access":  "Allow",
        "Priority":  1000,
        "Direction":  "Inbound",
        "ProvisioningState":  "Succeeded",
        "Name":  "default-allow-rdp",
        "Etag":  "W/\"d8859256-1c4c-4b93-ba7d-73d9bf67c4f1\"",
        "Id":  "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testrg/providers/Microsoft.Network/networkSecurityGroups/testvm1-nsg/securityRules/default-allow-rdp"
    },
    {
        "Description":  null,
        "Protocol":  "*",
        "SourcePortRange":  "*",
        "DestinationPortRange":  "111",
        "SourceAddressPrefix":  "*",
        "DestinationAddressPrefix":  "*",
        "Access":  "Allow",
        "Priority":  1010,
        "Direction":  "Inbound",
        "ProvisioningState":  "Succeeded",
        "Name":  "MyRuleDoNotDelete",
        "Etag":  "W/\"d8859256-1c4c-4b93-ba7d-73d9bf67c4f1\"",
        "Id":  "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testrg/providers/Microsoft.Network/networkSecurityGroups/testvm1-nsg/securityRules/MyRuleDoNotDelete"
    },
    {
        "Description":  null,
        "Protocol":  "*",
        "SourcePortRange":  "*",
        "DestinationPortRange":  "112",
        "SourceAddressPrefix":  "*",
        "DestinationAddressPrefix":  "*",
        "Access":  "Allow",
        "Priority":  1020,
        "Direction":  "Inbound",
        "ProvisioningState":  "Succeeded",
        "Name":  "My2ndRuleDoNotDelete",
        "Etag":  "W/\"d8859256-1c4c-4b93-ba7d-73d9bf67c4f1\"",
        "Id":  "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testrg/providers/Microsoft.Network/networkSecurityGroups/testvm1-nsg/securityRules/My2ndRuleDoNotDelete"
    },
    {
        "Description":  null,
        "Protocol":  "TCP",
        "SourcePortRange":  "*",
        "DestinationPortRange":  "5672",
        "SourceAddressPrefix":  "*",
        "DestinationAddressPrefix":  "*",
        "Access":  "Deny",
        "Priority":  1030,
        "Direction":  "Inbound",
        "ProvisioningState":  "Succeeded",
        "Name":  "ThisRuleNeedsToStay",
        "Etag":  "W/\"d8859256-1c4c-4b93-ba7d-73d9bf67c4f1\"",
        "Id":  "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/testrg/providers/Microsoft.Network/networkSecurityGroups/testvm1-nsg/securityRules/ThisRuleNeedsToStay"
    }
]

将规则集转换为 PowerShell 对象

在此步骤中,我们将要读取一个 JSON 文件,该文件是前面使用应该对本示例中的网络安全组应用的规则创建的。

$nsgbaserules = Get-Content -Path C:\temp\testvm1-nsg.json | ConvertFrom-Json

检索网络观察程序

下一步是检索网络观察程序实例。 将 $networkWatcher 变量传递给 AzNetworkWatcherSecurityGroupView cmdlet。

$networkWatcher = Get-AzResource | Where {$_.ResourceType -eq "Microsoft.Network/networkWatchers" -and $_.Location -eq "WestCentralUS" } 

检索虚拟机配置

需要针对虚拟机运行 Get-AzNetworkWatcherSecurityGroupView cmdlet。 以下示例获取 VM 对象。

$VM = Get-AzVM -ResourceGroupName "testrg" -Name "testvm1"

检索安全组视图

下一步是检索安全组视图结果。 将此结果与前面所示的“基准”JSON 进行比较。

$secgroup = Get-AzNetworkWatcherSecurityGroupView -NetworkWatcher $networkWatcher -TargetVirtualMachineId $VM.Id

分析结果

响应已按网络接口分组。 返回的不同类型的规则是有效的默认安全规则。 结果已按其应用方式(针对子网或虚拟 NIC 应用)进一步细分。

以下 PowerShell 脚本将安全组视图的结果与 NSG 的现有输出进行比较。 以下简单示例演示如何使用 Compare-Object cmdlet 来比较结果。

Compare-Object -ReferenceObject $nsgbaserules `
-DifferenceObject $secgroup.NetworkInterfaces[0].NetworkInterfaceSecurityRules `
-Property Name,Description,Protocol,SourcePortRange,DestinationPortRange,SourceAddressPrefix,DestinationAddressPrefix,Access,Priority,Direction

以下示例为结果。 可以看到,第一个规则集中的两条规则未出现在比较结果中。

Name                     : My2ndRuleDoNotDelete
Description              : 
Protocol                 : *
SourcePortRange          : *
DestinationPortRange     : 112
SourceAddressPrefix      : *
DestinationAddressPrefix : *
Access                   : Allow
Priority                 : 1020
Direction                : Inbound
SideIndicator            : <=

Name                     : ThisRuleNeedsToStay
Description              : 
Protocol                 : TCP
SourcePortRange          : *
DestinationPortRange     : 5672
SourceAddressPrefix      : *
DestinationAddressPrefix : *
Access                   : Deny
Priority                 : 1030
Direction                : Inbound
SideIndicator            : <=

后续步骤

请参阅创建、更改或删除网络安全组来跟踪有问题的网络安全组和安全规则。