如何使用 Azure Virtual Network Manager 阻止网络流量 - Azure PowerShell

本文将向您展示如何创建安全规则,阻止向端口80和443的外发网络流量,并将其添加到你的规则集合中。 有关详细信息,请参阅安全管理规则

先决条件

在开始配置安全规则之前,请确认以下步骤:

创建安全管理员配置

通过使用 New-AzNetworkManagerSecurityAdminConfiguration. 创建安全管理员配置。 配置中包含了你在接下来章节创建的规则集合和规则。

$config = @{
    Name = 'SecurityConfig'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
}
$securityconfig = New-AzNetworkManagerSecurityAdminConfiguration @config

将网络组添加到配置组

安全管理员配置适用于一个或多个网络组。 这些步骤将网络组存储在变量中,并将其添加到配置组中。

  1. 通过使用 Get-AzNetworkManagerGroup变量存储网络组。

    $ng = @{
        Name = 'myNetworkGroup'
        ResourceGroupName = 'myAVNMResourceGroup'
        NetworkManagerName = 'myAVNM'
    }
    $networkgroup = Get-AzNetworkManagerGroup @ng   
    
  2. 使用 New-AzNetworkManagerSecurityGroupItem 为网络组创建安全组项。

    $groupItem = New-AzNetworkManagerSecurityGroupItem -NetworkGroupId $networkgroup.id
    
  3. 创建配置组,并添加在上一步骤中创建的组项。

    [System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSNetworkManagerSecurityGroupItem]]$configGroup = @()  
    $configGroup.Add($groupItem) 
    

创建规则集合

通过使用 New-AzNetworkManagerSecurityAdminRuleCollection。 创建安全管理员规则集合。 该集合适用于你在上一节创建的配置组。

$collection = @{
    Name = 'myRuleCollection'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
    ConfigName = 'SecurityConfig'
}
$rulecollection = New-AzNetworkManagerSecurityAdminRuleCollection @collection -AppliesToGroup $configGroup

为80号和443号端口创建拒绝规则

这些步骤定义规则的地址前缀和端口,然后创建一个规则, Block_HTTP_HTTPS 阻止端口80和443的出站流量。

  1. 使用 New-AzNetworkManagerAddressPrefixItem 定义源地址和目标地址前缀及端口。

    $sourceip = @{
        AddressPrefix = 'Internet'
        AddressPrefixType = 'ServiceTag'
    }
    $sourceprefix = New-AzNetworkManagerAddressPrefixItem @sourceip
    
    $destinationip = @{
        AddressPrefix = '10.0.0.0/24'
        AddressPrefixType = 'IPPrefix'
    }
    $destinationprefix = New-AzNetworkManagerAddressPrefixItem @destinationip
    
    [System.Collections.Generic.List[string]]$sourcePortList = @() 
    $sourcePortList.Add("65500") 
    
    [System.Collections.Generic.List[string]]$destinationPortList = @() 
    $destinationPortList.Add("80")
    $destinationPortList.Add("443")
    
  2. 通过使用 New-AzNetworkManagerSecurityAdminRule创建安全规则。

    $rule = @{
        Name = 'Block_HTTP_HTTPS'
        ResourceGroupName = 'myAVNMResourceGroup'
        NetworkManagerName = 'myAVNM'
        SecurityAdminConfigurationName = 'SecurityConfig'
        RuleCollectionName = 'myRuleCollection'
        Protocol = 'TCP'
        Access = 'Deny'
        Priority = '100'
        Direction = 'Outbound'
        SourceAddressPrefix = $sourceprefix
        SourcePortRange = $sourcePortList
        DestinationAddressPrefix = $destinationprefix
        DestinationPortRange = $destinationPortList
    }
    $securityrule = New-AzNetworkManagerSecurityAdminRule @rule
    

提交部署

通过使用 Deploy-AzNetworkManagerCommit,将安全配置提交到目标区域。 根据你之前创建的安全管理员配置构建$configIds列表,然后将其传递给提交操作。

[System.Collections.Generic.List[string]]$configIds = @()
$configIds.Add($securityconfig.Id)

$regions = @("chinanorth3")
$deployment = @{
    Name = 'myAVNM'
    ResourceGroupName = 'myAVNMResourceGroup'
    ConfigurationId = $configIds
    TargetLocation = $regions
    CommitType = 'SecurityAdmin'
}
Deploy-AzNetworkManagerCommit @deployment 

删除安全配置

如果您不再需要安全配置,请确保以下条件为正确,以便删除安全配置本身:

  • 未在任何区域中进行配置部署。
  • 删除与安全配置关联的规则集合中的所有安全规则。

删除安全配置部署

通过部署带有 Deploy-AzNetworkManagerCommit的配置来移除安全部署。

[System.Collections.Generic.List[string]]$configIds = @()
[System.Collections.Generic.List[string]]$regions = @()   
$regions.Add("chinanorth3")     
$removedeployment = @{
    Name = 'myAVNM'
    ResourceGroupName = 'myAVNMResourceGroup'
    ConfigurationId = $configIds
    TargetLocation = $regions
    CommitType = 'SecurityAdmin'
}
Deploy-AzNetworkManagerCommit @removedeployment

删除安全规则

使用 Remove-AzNetworkManagerSecurityAdminRule 删除你之前创建的安全规则。

$removerule = @{
    Name = 'Block_HTTP_HTTPS'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
    SecurityAdminConfigurationName = 'SecurityConfig'
    RuleCollectionName = 'myRuleCollection'
}
Remove-AzNetworkManagerSecurityAdminRule @removerule

删除安全规则集合

$removecollection = @{
    Name = 'myRuleCollection'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
    SecurityAdminConfigurationName = 'SecurityConfig'
}
Remove-AzNetworkManagerSecurityAdminRuleCollection @removecollection

删除配置

使用Remove-AzNetworkManagerSecurityAdminConfiguration删除安全配置。

$removeconfig = @{
    Name = 'SecurityConfig'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
}
Remove-AzNetworkManagerSecurityAdminConfiguration @removeconfig

后续步骤

详细了解安全管理规则