Azure Network Watcher セキュリティ グループ ビューを使用して NSG 監査を自動化する
手記
セキュリティ グループ ビュー 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/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/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 : <=
次の手順
ネットワーク セキュリティ グループ 作成、変更、または削除 を参照して、対象のネットワーク セキュリティ グループとセキュリティ規則を追跡します。