在本部分,将创建包含两个子网和一个 Azure Bastion 主机的虚拟网络。 第一个子网用于虚拟机,第二个子网用于 Bastion 主机。 你还可以创建网络安全组并将其应用于第一个子网。
在门户顶部的搜索框中,输入“网络安全组”。 从搜索结果中选择“网络安全组”。
在网络安全组列表中,选择“myVM-nsg”。
在“设置”下,选择“入站安全规则”。
选择“+ 添加”。 在“网络”选项卡中,输入或选择以下值:
| 设置 |
值 |
| 源 |
选择“服务标记”。 |
| 源服务标记 |
选择“VirtualNetwork”。 |
| 源端口范围 |
输入 *。 |
| 目标 |
选择“任何”。 |
| 服务 |
选择“自定义”。 |
| 目标端口范围 |
输入 *。 |
| 协议 |
选择“任何”。 |
| 操作 |
选择“拒绝”。 |
| 优先级 |
输入“1000”。 |
| 名称 |
输入“DenyVnetInBound”。 |
选择 添加 。
使用 Add-AzNetworkSecurityRuleConfig 创建用于拒绝来自虚拟网络的流量的安全规则。 然后使用 Set-AzNetworkSecurityGroup 和新的安全规则来更新网络安全组。
# Place the network security group configuration into a variable.
$networkSecurityGroup = Get-AzNetworkSecurityGroup -Name 'myVM-nsg' -ResourceGroupName 'myResourceGroup'
# Create a security rule that denies inbound traffic from the virtual network service tag.
Add-AzNetworkSecurityRuleConfig -Name 'DenyVnetInBound' -NetworkSecurityGroup $networkSecurityGroup `
-Access 'Deny' -Protocol '*' -Direction 'Inbound' -Priority '1000' `
-SourceAddressPrefix 'virtualNetwork' -SourcePortRange '*' -DestinationAddressPrefix '*' -DestinationPortRange '*'
# Updates the network security group.
Set-AzNetworkSecurityGroup -NetworkSecurityGroup $networkSecurityGroup
使用 az network nsg rule create 向网络安全组添加用于拒绝来自虚拟网络的流量的安全规则。
# Add to the network security group a security rule that denies inbound traffic from the virtual network service tag.
az network nsg rule create --name 'DenyVnetInBound' --resource-group 'myResourceGroup' --nsg-name 'myVM-nsg' --priority '1000' \
--access 'Deny' --protocol '*' --direction 'Inbound' --source-address-prefixes 'virtualNetwork' --source-port-ranges '*' \
--destination-address-prefixes '*' --destination-port-ranges '*'
在门户顶部的搜索栏中,搜索并选择“网络观察程序”。
在“网络诊断工具”下,选择“NSG 诊断”。
在“NSG 诊断”页上,输入或选择以下值:
| 设置 |
值 |
|
目标资源 |
|
| 目标资源类型 |
选择“虚拟机”。 |
| 虚拟机 |
选择 myVM 虚拟机。 |
|
流量详细信息 |
|
| 协议 |
选择“TCP”。 其他可用选项包括:“任何”、“UDP”和“ICMP”。 |
| 方向 |
选择“入站”。 其他可用选项为“出站”。 |
| 源类型 |
选择“IPv4 地址/CIDR”。 其他可用选项为“服务标记”。 |
| IPv4 地址/CIDR |
输入“10.0.1.0/26”,这是 Bastion 子网的 IP 地址范围。 可接受的值包括:单个 IP 地址、多个 IP 地址、单个 IP 前缀、多个 IP 前缀。 |
| 目标 IP 地址 |
保留默认值 10.0.0.4,它是 myVM 的 IP 地址。 |
| 目标端口 |
输入 * 以包含所有端口。 |
选择“运行 NSG 诊断”以运行测试。 当 NSG 诊断完成对所有安全规则的检查后,它会显示结果。
结果显示有三个安全规则评估了来自 Bastion 子网的入站连接:
-
GlobalRules:此安全管理规则使用 Azure Virtual Network Manage 在虚拟网络级别进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
mySubnet-nsg:此网络安全组在子网级别(虚拟机子网)进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
myVM-nsg:此网络安全组在网络接口 (NIC) 级别进行应用。 该规则拒绝从 Bastion 子网到虚拟机的入站 TCP 流量。
选择“myVM-nsg”的“查看详细信息”,以详细了解此网络安全组包含的安全规则以及哪些规则拒绝了流量。
在 myVM-nsg 网络安全组中,安全规则 DenyVnetInBound 拒绝了从 VirtualNetwork 服务标记的地址空间流向虚拟机的任何流量。 Bastion 主机使用地址范围 10.0.1.0/26 中的 IP 地址(包含在 VirtualNetwork 服务标记中)连接到虚拟机。 因此,来自 Bastion 主机的连接将被 DenyVnetInBound 安全规则拒绝。
使用 Invoke-AzNetworkWatcherNetworkConfigurationDiagnostic 启动 NSG 诊断会话。
# Create a profile for the diagnostic session.
$profile = New-AzNetworkWatcherNetworkConfigurationDiagnosticProfile -Direction Inbound -Protocol Tcp -Source 10.0.1.0/26 -Destination 10.0.0.4 -DestinationPort *
# Place the virtual machine configuration into a variable.
$vm = Get-AzVM -Name 'myVM' -ResourceGroupName 'myResourceGroup'
# Start the the NSG diagnostics session.
Invoke-AzNetworkWatcherNetworkConfigurationDiagnostic -Location 'eastus' -TargetResourceId $vm.Id -Profile $profile | Format-List
将返回类似于以下示例输出的输出:
Results : {Microsoft.Azure.Commands.Network.Models.PSNetworkConfigurationDiagnosticResult}
ResultsText : [
{
"Profile": {
"Direction": "Inbound",
"Protocol": "Tcp",
"Source": "10.0.1.0/26",
"Destination": "10.0.0.4",
"DestinationPort": "*"
},
"NetworkSecurityGroupResult": {
"SecurityRuleAccessResult": "Deny",
"EvaluatedNetworkSecurityGroups": [
{
"NetworkSecurityGroupId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/NetworkAdmin/providers/Microsoft.Network/networkManagers/GlobalRules",
"MatchedRule": {
"RuleName": "VirtualNetwork",
"Action": "Allow"
},
"RulesEvaluationResult": [
{
"Name": "VirtualNetwork",
"ProtocolMatched": true,
"SourceMatched": true,
"SourcePortMatched": true,
"DestinationMatched": true,
"DestinationPortMatched": true
}
]
},
{
"NetworkSecurityGroupId": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mySubnet-nsg",
"MatchedRule": {
"RuleName": "DefaultRule_AllowVnetInBound",
"Action": "Allow"
},
"RulesEvaluationResult": [
{
"Name": "DefaultRule_AllowVnetInBound",
"ProtocolMatched": true,
"SourceMatched": true,
"SourcePortMatched": true,
"DestinationMatched": true,
"DestinationPortMatched": true
}
]
},
{
"NetworkSecurityGroupId": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myVM-nsg",
"MatchedRule": {
"RuleName": "UserRule_DenyVnetInBound",
"Action": "Deny"
},
"RulesEvaluationResult": [
{
"Name": "UserRule_DenyVnetInBound",
"ProtocolMatched": true,
"SourceMatched": true,
"SourcePortMatched": true,
"DestinationMatched": true,
"DestinationPortMatched": true
}
]
}
]
}
}
]
结果显示有三个安全规则评估了来自 Bastion 子网的入站连接:
-
GlobalRules:此安全管理规则使用 Azure Virtual Network Manage 在虚拟网络级别进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
mySubnet-nsg:此网络安全组在子网级别(虚拟机子网)进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
myVM-nsg:此网络安全组在网络接口 (NIC) 级别进行应用。 该规则拒绝从 Bastion 子网到虚拟机的入站 TCP 流量。
在 myVM-nsg 网络安全组中,安全规则 DenyVnetInBound 拒绝了从 VirtualNetwork 服务标记的地址空间流向虚拟机的任何流量。 Bastion 主机使用 10.0.1.0/26 中的 IP 地址(包含在 VirtualNetwork 服务标记中)连接到虚拟机。 因此,来自 Bastion 主机的连接将被 DenyVnetInBound 安全规则拒绝。
使用 az network watcher run-configuration-diagnostic 启动 NSG 诊断会话。
# Start the the NSG diagnostics session.
az network watcher run-configuration-diagnostic --resource 'myVM' --resource-group 'myResourceGroup' --resource-type 'virtualMachines' --direction 'Inbound' --protocol 'TCP' --source '10.0.1.0/26' --destination '10.0.0.4' --port '*'
将返回类似于以下示例输出的输出:
{
"results": [
{
"networkSecurityGroupResult": {
"evaluatedNetworkSecurityGroups": [
{
"appliedTo": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet",
"matchedRule": {
"action": "Allow",
"ruleName": "VirtualNetwork"
},
"networkSecurityGroupId": "/subscriptions/aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e/resourceGroups/NetworkAdmin/providers/Microsoft.Network/networkManagers/GlobalRules",
"rulesEvaluationResult": [
{
"destinationMatched": true,
"destinationPortMatched": true,
"name": "VirtualNetwork",
"protocolMatched": true,
"sourceMatched": true,
"sourcePortMatched": true
}
]
},
{
"appliedTo": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNet/subnets/mySubnet",
"matchedRule": {
"action": "Allow",
"ruleName": "DefaultRule_AllowVnetInBound"
},
"networkSecurityGroupId": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/mySubnet-nsg",
"rulesEvaluationResult": [
{
"destinationMatched": true,
"destinationPortMatched": true,
"name": "DefaultRule_AllowVnetInBound",
"protocolMatched": true,
"sourceMatched": true,
"sourcePortMatched": true
}
]
},
{
"appliedTo": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkInterfaces/myvm36",
"matchedRule": {
"action": "Deny",
"ruleName": "UserRule_DenyVnetInBound"
},
"networkSecurityGroupId": "/subscriptions/bbbb1b1b-cc2c-dd3d-ee4e-ffffff5f5f5f/resourceGroups/myResourceGroup/providers/Microsoft.Network/networkSecurityGroups/myVM-nsg",
"rulesEvaluationResult": [
{
"destinationMatched": true,
"destinationPortMatched": true,
"name": "UserRule_DenyVnetInBound",
"protocolMatched": true,
"sourceMatched": true,
"sourcePortMatched": true
}
]
}
],
"securityRuleAccessResult": "Deny"
},
"profile": {
"destination": "10.0.0.4",
"destinationPort": "*",
"direction": "Inbound",
"protocol": "TCP",
"source": "10.0.1.0/26"
}
}
]
}
结果显示有三个安全规则评估了来自 Bastion 子网的入站连接:
-
GlobalRules:此安全管理规则使用 Azure Virtual Network Manage 在虚拟网络级别进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
mySubnet-nsg:此网络安全组在子网级别(虚拟机子网)进行应用。 该规则允许从 Bastion 子网到虚拟机的入站 TCP 流量。
-
myVM-nsg:此网络安全组在网络接口 (NIC) 级别进行应用。 该规则拒绝从 Bastion 子网到虚拟机的入站 TCP 流量。
在 myVM-nsg 网络安全组中,安全规则 DenyVnetInBound 拒绝了从 VirtualNetwork 服务标记的地址空间流向虚拟机的任何流量。 Bastion 主机使用 10.0.1.0/26 中的 IP 地址(包含在 VirtualNetwork 服务标记中)连接到虚拟机。 因此,来自 Bastion 主机的连接将被 DenyVnetInBound 安全规则拒绝。