Azure Network Watcher のセキュリティ グループ ビューを使用した NSG 監査の自動化

Note

セキュリティ グループ ビュー API は管理されなくなったため、間もなく非推奨となる予定です。 同じ機能を提供する有効なセキュリティ規則機能を使用してください。

インフラストラクチャのセキュリティ体制の検証は、多くのお客様が直面する課題です。 この課題は Azure の VM にも同様に存在します。 適用するネットワーク セキュリティ グループ (NSG) 規則に基づいて、同様のセキュリティ プロファイルを確保することが重要です。 セキュリティ グループ ビューを使用すれば、NSG 内の VM に適用された規則の一覧を取得できます。 適切な NSG セキュリティ プロファイルを定義し、一週間ごとにセキュリティ グループ ビューを起動して、その出力を適切なプロファイルと比較してレポートを作成することができます。 この方法で、所定のセキュリティ プロファイルに準拠していないすべての VM を簡単に特定できます。

ネットワーク セキュリティ グループの詳細については、ネットワーク セキュリティ グループの概要に関する記事を参照してください。

前提条件

  • Azure アカウントとアクティブなサブスクリプションをお持ちでない場合は、無料で作成できます
  • 仮想マシン (VM)。

通信の種類

このシナリオでは、仮想マシンについて返されるセキュリティ グループ ビューの結果と既知の適切な基準を比較します。

このシナリオでは次のことを行います。

  • 既知の適切な規則セットを取得する
  • REST API を使って仮想マシンを取得する
  • 仮想マシンのセキュリティ グループ ビューの取得
  • 応答を評価する

規則セットの取得

この例の最初の手順では、既存の基準を処理します。 次の例は、Get-AzNetworkSecurityGroup コマンドレットを使用して既存のネットワーク セキュリティ グループから抽出した 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

Network Watcher の取得

次の手順では、Network Watcher インスタンスを取得します。 $networkWatcher 変数は、AzNetworkWatcherSecurityGroupView コマンドレットに渡されます。

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

仮想マシンの構成の取得

Get-AzNetworkWatcherSecurityGroupView コマンドレットを実行する仮想マシンが必要です。 次の例では VM オブジェクトを取得します。

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

セキュリティ グループ ビューの取得

次に、セキュリティ グループ ビューの結果を取得します。 この結果は、先ほど表示した "基準" の json と比較されます。

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

結果を分析する

応答は、ネットワーク インターフェイスごとにグループ分けされます。 返された各種の規則は有効であり、既定のセキュリティ規則です。 この結果は、適用方法 (サブネットまたは仮想 NIC) によってさらに細かく分けられます。

次の PowerShell スクリプトでは、セキュリティ グループ ビューの結果を既存の NSG の出力と比較します。 次の例は、Compare-Object コマンドレットを使用して結果を比較する方法の単純な例です。

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

次の例は結果です。 最初の規則セットにあった 2 つの規則が比較では見つからなかったことがわかります。

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            : <=

次の手順

ネットワーク セキュリティ グループの作成、変更、削除」を参照して、問題のネットワーク セキュリティ グループとセキュリティ規則を詳しく調べます。