PowerShell を使用して Azure Stack HCI でネットワーク セキュリティ グループを構成する

適用対象: Azure Stack HCI、バージョン 23H2 および 22H2。Windows Server 2022、Windows Server 2019、Windows Server 2016

この記事では、Windows PowerShellを使用して Azure Stack HCI の Datacenter Firewall for Software Defined Networking (SDN) を使用してデータ トラフィック フローを管理するようにネットワーク セキュリティ グループ (NSG) を構成する手順について説明します。 データセンター ファイアウォールを有効にして構成するには、サブネットまたはネットワーク インターフェイスに適用されるネットワーク セキュリティ グループを作成します。

この記事のサンプル スクリプトでは、NetworkController モジュールからエクスポートWindows PowerShellコマンドを使用します。 Windows Admin Centerを使用して、ネットワーク セキュリティ グループを構成および管理することもできます。

すべてのトラフィックを許可するようにデータセンター ファイアウォールを構成する

SDN をデプロイしたら、新しい環境で基本的なネットワーク接続をテストする必要があります。 これを行うには、制限なしですべてのネットワーク トラフィックを許可するデータセンター ファイアウォールの規則を作成します。

次の表の項目を使用して、すべての受信および送信ネットワーク トラフィックを許可する一連の規則を作成します。

発信元 IP 宛先 IP Protocol 発信元ポート 宛先ポート Direction アクション Priority
* * All * * 受信 Allow 100
* * All * * 送信 Allow 110

この例では、次の 2 つの規則を使用してネットワーク セキュリティ グループを作成します。

  1. AllowAll_Inbound - すべてのネットワーク トラフィックが、このネットワーク セキュリティ グループが構成されているネットワーク インターフェイスに渡すことができます。
  2. AllowAllOutbound - すべてのトラフィックにネットワーク インターフェースの外部に渡されることを許可します。 リソース ID "AllowAll-1" で識別されるこのネットワーク セキュリティ グループは、仮想サブネットとネットワーク インターフェイスで使用する準備ができました。

このコマンドは、ネットワーク コントローラー REST エンドポイントにアクセスできる任意のコンピューターから実行できます。 まず、PowerShell セッションを開きます。 この例では、 Enter-PSSession コマンドレットを使用し、 をネットワーク コントローラー REST エンドポイントを持つコンピューターの名前に置き換えます <computer-name>

Enter-PSSession <computer-name>

次に、次のスクリプトを実行して、ネットワーク セキュリティ グループを作成します。

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "100"
$ruleproperties.Type = "Inbound"
$ruleproperties.Logging = "Enabled"
$aclrule1 = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule1.Properties = $ruleproperties
$aclrule1.ResourceId = "AllowAll_Inbound"
$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "110"
$ruleproperties.Type = "Outbound"
$ruleproperties.Logging = "Enabled"
$aclrule2 = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule2.Properties = $ruleproperties
$aclrule2.ResourceId = "AllowAll_Outbound"
$acllistproperties = new-object Microsoft.Windows.NetworkController.AccessControlListProperties
$acllistproperties.AclRules = @($aclrule1, $aclrule2)
New-NetworkControllerAccessControlList -ResourceId "AllowAll" -Properties $acllistproperties -ConnectionUri <NC REST FQDN>

注意

ネットワーク コントローラーのWindows PowerShell コマンド リファレンスは、ネットワーク コントローラー コマンドレットにあります

ネットワーク セキュリティ グループを使用してサブネット上のトラフィックを制限する

この例では、192.168.0.0/24 サブネット内の仮想マシン (VM) が相互に通信できないようにするネットワーク セキュリティ グループを作成します。 この種のネットワーク セキュリティ グループは、攻撃者がサブネット内に横方向に分散する機能を制限しながら、VM がサブネットの外部から要求を受信し、他のサブネット上の他のサービスと通信できるようにする場合に役立ちます。

発信元 IP 宛先 IP Protocol 発信元ポート 宛先ポート Direction アクション Priority
192.168.0.1 * All * * 受信 Allow 100
* 192.168.0.1 All * * 送信 Allow 101
192.168.0.0/24 * All * * 受信 ブロック 102
* 192.168.0.0/24 All * * 送信 ブロック 103
* * All * * 受信 Allow 104
* * All * * 送信 Allow 105

次のサンプル スクリプトによって作成されたネットワーク セキュリティ グループは、リソース ID Subnet-192-168-0-0 で識別され、"192.168.0.0/24" サブネット アドレスを使用する仮想ネットワーク サブネットに適用できるようになりました。 その仮想ネットワーク サブネットに接続されているネットワーク インターフェイスでは、上記のネットワーク セキュリティ グループ規則が自動的に適用されます。

ネットワーク コントローラー REST API を使用してこのネットワーク セキュリティ グループを作成するスクリプトの例を次に示します。

import-module networkcontroller
$ncURI = "https://mync.contoso.local"
$aclrules = @()

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "192.168.0.1"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "100"
$ruleproperties.Type = "Inbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "AllowRouter_Inbound"
$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "192.168.0.1"
$ruleproperties.Priority = "101"
$ruleproperties.Type = "Outbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "AllowRouter_Outbound"
$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Deny"
$ruleproperties.SourceAddressPrefix = "192.168.0.0/24"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "102"
$ruleproperties.Type = "Inbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "DenySubnet_Inbound"
$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Deny"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "192.168.0.0/24"
$ruleproperties.Priority = "103"
$ruleproperties.Type = "Outbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "DenySubnet_Outbound"

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "104"
$ruleproperties.Type = "Inbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "AllowAll_Inbound"
$aclrules += $aclrule

$ruleproperties = new-object Microsoft.Windows.NetworkController.AclRuleProperties
$ruleproperties.Protocol = "All"
$ruleproperties.SourcePortRange = "0-65535"
$ruleproperties.DestinationPortRange = "0-65535"
$ruleproperties.Action = "Allow"
$ruleproperties.SourceAddressPrefix = "*"
$ruleproperties.DestinationAddressPrefix = "*"
$ruleproperties.Priority = "105"
$ruleproperties.Type = "Outbound"
$ruleproperties.Logging = "Enabled"

$aclrule = new-object Microsoft.Windows.NetworkController.AclRule
$aclrule.Properties = $ruleproperties
$aclrule.ResourceId = "AllowAll_Outbound"
$aclrules += $aclrule

$acllistproperties = new-object Microsoft.Windows.NetworkController.AccessControlListProperties
$acllistproperties.AclRules = $aclrules

New-NetworkControllerAccessControlList -ResourceId "Subnet-192-168-0-0" -Properties $acllistproperties -ConnectionUri $ncURI

ネットワーク インターフェイスにネットワーク セキュリティ グループを追加する

ネットワーク セキュリティ グループを作成して仮想サブネットに割り当てると、仮想サブネット上の既定のネットワーク セキュリティ グループを、個々のネットワーク インターフェイスの特定のネットワーク セキュリティ グループでオーバーライドできます。 Windows Server 2019 Datacenter 以降では、SDN 仮想ネットワークに加えて、SDN 論理ネットワークに接続されているネットワーク インターフェイスに特定のネットワーク セキュリティ グループを直接適用できます。 ネットワーク インターフェイスに接続されている仮想サブネットにネットワーク セキュリティ グループが設定されている場合は、両方のネットワーク セキュリティ グループが適用され、ネットワーク インターフェイス のネットワーク セキュリティ グループが仮想サブネット ネットワーク セキュリティ グループよりも優先されます。

この例では、ネットワーク セキュリティ グループを仮想ネットワークに追加する方法を示します。

ヒント

ネットワーク インターフェイスを作成するのと同時に、ネットワーク セキュリティ グループを追加することもできます。

  1. ネットワーク セキュリティ グループを追加するネットワーク インターフェイスを取得または作成します。

    $nic = get-networkcontrollernetworkinterface -ConnectionUri $uri -ResourceId "MyVM_Ethernet1"
    
  2. ネットワーク インターフェイスに追加するネットワーク セキュリティ グループを取得または作成します。

    $acl = get-networkcontrolleraccesscontrollist -ConnectionUri $uri -ResourceId "AllowAllACL"
    
  3. ネットワーク セキュリティ グループをネットワーク インターフェイスの AccessControlList プロパティに割り当てます。

     $nic.properties.ipconfigurations[0].properties.AccessControlList = $acl
    
  4. ネットワーク コントローラーにネットワーク インターフェイスを追加します。

    new-networkcontrollernetworkinterface -ConnectionUri $uri -Properties $nic.properties -ResourceId $nic.resourceid
    

ネットワーク インターフェイスからネットワーク セキュリティ グループを削除する

この例では、ネットワーク インターフェイスからネットワーク セキュリティ グループを削除する方法を示します。 ネットワーク セキュリティ グループを削除すると、既定の規則セットがネットワーク インターフェイスに適用されます。 既定のルール セットでは、すべての送信トラフィックが許可されますが、すべての受信トラフィックはブロックされます。 すべての受信トラフィックを許可する場合は、前の に従って、すべての受信トラフィックとすべての送信トラフィックを許可するネットワーク セキュリティ グループを追加する必要があります。

  1. ネットワーク セキュリティ グループを削除するネットワーク インターフェイスを取得します。

    $nic = get-networkcontrollernetworkinterface -ConnectionUri $uri -ResourceId "MyVM_Ethernet1"
    
  2. ipConfiguration の AccessControlList プロパティに $null を割り当てます。

    $nic.properties.ipconfigurations[0].properties.AccessControlList = $null
    
  3. ネットワーク コントローラーにネットワーク インターフェイス オブジェクトを追加します。

    new-networkcontrollernetworkinterface -ConnectionUri $uri -Properties $nic.properties -ResourceId $nic.resourceid
    

ファイアウォールの監査

データセンター ファイアウォールのファイアウォール監査機能は、SDN ファイアウォール規則によって処理されたすべてのフローを記録します。 ログ記録が有効になっているすべてのネットワーク セキュリティ グループが記録されます。 ログ ファイルは、Azure Network Watcher フロー ログと一致する構文に含まれている必要があります。 これらのログは、診断に使用したり、後で分析するためにアーカイブしたりすることができます。

ホスト サーバーでファイアウォール監査を有効にするサンプル スクリプトを次に示します。 最初にある変数を更新し、ネットワーク コントローラーが展開された Azure Stack HCI クラスターでこれを実行します。

$logpath = "C:\test\log1"
$servers = @("sa18n22-2", "sa18n22-3", "sa18n22-4")
$uri = "https://sa18n22sdn.sa18.nttest.microsoft.com"

# Create log directories on the hosts
invoke-command -Computername $servers  {
    param(
        $Path
    )
    mkdir $path    -force
} -argumentlist $LogPath

# Set firewall auditing settings on Network Controller
$AuditProperties = new-object Microsoft.Windows.NetworkController.AuditingSettingsProperties
$AuditProperties.OutputDirectory = $logpath
set-networkcontrollerauditingsettingsconfiguration -connectionuri $uri -properties $AuditProperties -force  | out-null

# Enable logging on each server
$servers = get-networkcontrollerserver -connectionuri $uri
foreach ($s in $servers) {
    $s.properties.AuditingEnabled = @("Firewall")
    new-networkcontrollerserver -connectionuri $uri -resourceid $s.resourceid -properties $s.properties -force | out-null
}

有効にすると、各ホストの指定したディレクトリに新しいファイルが 1 時間ごとに 1 つずつ追加されます。 これらのファイルを定期的に処理し、ホストから削除する必要があります。 現在のファイルは長さが 0 で、次の 1 時間にフラッシュされるまでロックされます。

PS C:\test\log1> dir

    Directory: C:\test\log1

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----        7/19/2018   6:28 AM          17055 SdnFirewallAuditing.d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a.20180719TL122803093.json
-a----        7/19/2018   7:28 AM           7880 SdnFirewallAuditing.d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a.20180719TL132803173.json
-a----        7/19/2018   8:28 AM           7867 SdnFirewallAuditing.d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a.20180719TL142803264.json
-a----        7/19/2018   9:28 AM          10949 SdnFirewallAuditing.d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a.20180719TL152803360.json
-a----        7/19/2018   9:28 AM              0 SdnFirewallAuditing.d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a.20180719TL162803464.json

これらのファイルには、フロー イベントのシーケンスが含まれています。次に例を示します。

{
    "records": [
        {
            "properties":{
                "Version":"1.0",
                "flows":[
                    {
                        "flows":[
                            {
                                "flowTuples":["1531963580,192.122.0.22,192.122.255.255,138,138,U,I,A"],
                                "portId":"9",
                                "portName":"7290436D-0422-498A-8EB8-C6CF5115DACE"
                            }
                        ],
                        "rule":"Allow_Inbound"
                    }
                ]
            },
            "operationName":"NetworkSecurityGroupFlowEvents",
            "resourceId":"394f647d-2ed0-4c31-87c5-389b8c0c8132",
            "time":"20180719:L012620622",
            "category":"NetworkSecurityGroupFlowEvent",
            "systemId":"d8b3b697-5355-40e2-84d2-1bf2f0e0dc4a"
            },

ログ記録が行われるのは、LoggingEnabled に設定されているルールだけです。次に例を示します。

{
    "Tags":  null,
    "ResourceRef":  "/accessControlLists/AllowAll",
    "InstanceId":  "4a63e1a5-3264-4986-9a59-4e77a8b107fa",
    "Etag":  "W/\"1535a780-0fc8-4bba-a15a-093ecac9b88b\"",
    "ResourceMetadata":  null,
    "ResourceId":  "AllowAll",
    "Properties":  {
                       "ConfigurationState":  null,
                       "ProvisioningState":  "Succeeded",
                       "AclRules":  [
                                        {
                                            "ResourceMetadata":  null,
                                            "ResourceRef":  "/accessControlLists/AllowAll/aclRules/AllowAll_Inbound",
                                            "InstanceId":  "ba8710a8-0f01-422b-9038-d1f2390645d7",
                                            "Etag":  "W/\"1535a780-0fc8-4bba-a15a-093ecac9b88b\"",
                                            "ResourceId":  "AllowAll_Inbound",
                                            "Properties":  {
                                                               "Protocol":  "All",
                                                               "SourcePortRange":  "0-65535",
                                                               "DestinationPortRange":  "0-65535",
                                                               "Action":  "Allow",
                                                               "SourceAddressPrefix":  "*",
                                                               "DestinationAddressPrefix":  "*",
                                                               "Priority":  "101",
                                                               "Description":  null,
                                                               "Type":  "Inbound",
                                                               "Logging":  "Enabled",
                                                               "ProvisioningState":  "Succeeded"
                                                           }
                                        },
                                        {
                                            "ResourceMetadata":  null,
                                            "ResourceRef":  "/accessControlLists/AllowAll/aclRules/AllowAll_Outbound",
                                            "InstanceId":  "068264c6-2186-4dbc-bbe7-f504c6f47fa8",
                                            "Etag":  "W/\"1535a780-0fc8-4bba-a15a-093ecac9b88b\"",
                                            "ResourceId":  "AllowAll_Outbound",
                                            "Properties":  {
                                                               "Protocol":  "All",
                                                               "SourcePortRange":  "0-65535",
                                                               "DestinationPortRange":  "0-65535",
                                                               "Action":  "Allow",
                                                               "SourceAddressPrefix":  "*",
                                                               "DestinationAddressPrefix":  "*",
                                                               "Priority":  "110",
                                                               "Description":  null,
                                                               "Type":  "Outbound",
                                                               "Logging":  "Enabled",
                                                               "ProvisioningState":  "Succeeded"
                                                           }
                                        }
                                    ],
                       "IpConfigurations":  [

                                            ],
                       "Subnets":  [
                                       {
                                           "ResourceMetadata":  null,
                                           "ResourceRef":  "/virtualNetworks/10_0_1_0/subnets/Subnet1",
                                           "InstanceId":  "00000000-0000-0000-0000-000000000000",
                                           "Etag":  null,
                                           "ResourceId":  null,
                                           "Properties":  null
                                       }
                                   ]
                   }
}

次のステップ

関連情報については、以下をご覧ください。