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

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

本文介绍如何创建一个安全规则来阻止发送到端口 80 和 443 的出站网络流量,可以将此规则添加到规则集合中。 有关详细信息,请参阅安全管理规则

先决条件

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

创建安全管理配置

  1. 使用 New-AzNetworkManagerSecurityAdminConfiguration 创建一个新的安全管理配置。

    $config = @{
        Name = 'SecurityConfig'
        ResourceGroupName = 'myAVNMResourceGroup'
        NetworkManagerName = 'myAVNM'
    }
    $securityconfig = New-AzNetworkManagerSecurityAdminConfiguration @config
    
    
  2. 使用 Get-AzNetworkManagerGroup 将网络组存储到变量。

    $ng = @{
        Name = 'myNetworkGroup'
        ResourceGroupName = 'myAVNMResourceGroup'
        NetworkManagerName = 'myAVNM'
    }
    $networkgroup = Get-AzNetworkManagerGroup @ng   
    
  3. 使用 New-AzNetworkManagerSecurityGroupItem 创建要将网络组添加到的连接组项。

    $gi = @{
        NetworkGroupId = "$networkgroup.Id"
    }
    
    $groupItem = New-AzNetworkManagerSecurityGroupItem -NetworkGroupId $networkgroup.id
    
  4. 创建配置组,并添加在上一步骤中创建的组项。

    [System.Collections.Generic.List[Microsoft.Azure.Commands.Network.Models.PSNetworkManagerSecurityGroupItem]]$configGroup = @()  
    $configGroup.Add($groupItem) 
    
    $configGroup = @($groupItem)
    
  5. 使用 New-AzNetworkManagerSecurityAdminRuleCollection 创建一个安全管理规则集合。

    $collection = @{
        Name = 'myRuleCollection'
        ResourceGroupName = 'myAVNMResourceGroup'
        NetworkManager = 'myAVNM'
        ConfigName = 'SecurityConfig'
        AppliesToGroup = "$configGroup"
    }
    $rulecollection = New-AzNetworkManagerSecurityAdminRuleCollection @collection -AppliesToGroup $configGroup
    
  6. 使用 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”)
    
  7. 使用 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 将安全配置提交到目标区域。

$regions = @("westus")
$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("westus")     
$removedeployment = @{
    Name = 'myAVNM'
    ResourceGroupName = 'myAVNMResourceGroup'
    ConfigurationId = $configIds
    TargetLocation = $regions
    CommitType = 'SecurityAdmin'
}
Deploy-AzNetworkManagerCommit @removedeployment

删除安全规则

使用 Remove-AzNetworkManagerSecurityAdminRule 删除安全规则。

$removerule = @{
    Name = 'Block80'
    ResourceGroupName = 'myAVNMResourceGroup'
    NetworkManagerName = 'myAVNM'
    SecurityAdminConfigurationName = 'SecurityConfig'
}
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

后续步骤

详细了解安全管理规则