Azure Network Security Groups not working? (attached to subnet) - azure

I am trying to secure some subnets in a virtual network.
I have Virtual Network 1 with Subnets A, B, C.
I have a VM in each subnet with default endpoints (RDP and WinRM).
I used the following commands to create and attach the Network Security Group to subnet C:
$SGName = 'SecurityGroupC'
$location = 'West US'
$virtualNetwork = '1'
$subnet = 'C'
New-AzureNetworkSecurityGroup -Name $SGName -Location $Location -Label $SGName
Get-AzureNetworkSecurityGroup -Name $SGName | Set-AzureNetworkSecurityGroupToSubnet -VirtualNetworkName $VirtualNetwork -SubnetName $Subnet
I can see the default rules by running:
Get-AzureNetworkSecurityGroup -Name $SGName -Detailed
Which shows the expected default rules:
Name : SecurityGroupC
Rules :
Type: Inbound
Name Priority Action Source Address Source Port Destination Destination Protocol
Prefix Range Address Prefix Port Range
---- -------- ------ --------------- ------------- ---------------- -------------- --------
ALLOW VNET INBOUND 65000 Allow VIRTUAL_NETWORK * VIRTUAL_NETWORK * *
ALLOW AZURE LOAD 65001 Allow AZURE_LOADBALAN * * * *
BALANCER INBOUND CER
DENY ALL INBOUND 65500 Deny * * * * *
Type: Outbound
Name Priority Action Source Address Source Port Destination Destination Protocol
Prefix Range Address Prefix Port Range
---- -------- ------ --------------- ------------- ---------------- -------------- --------
ALLOW VNET OUTBOUND 65000 Allow VIRTUAL_NETWORK * VIRTUAL_NETWORK * *
ALLOW INTERNET 65001 Allow * * INTERNET * *
OUTBOUND
DENY ALL OUTBOUND 65500 Deny * * * * *
Based on these rules my RDP endpoint on my VM in subnet C should stop working. However I am still able to RDP directly to my VM from the internet. Is there something I am missing?

When you create a VM it will create a RDP endpoint automatically. It appears that this setting overrides your Network Security Group values.
I usually add an ACL to it "0.0.0.0/0" "DENY" so I can re-enable it if I need to.

Per the function of Network Security Groups:
"Network security groups are different than endpoint-based ACLs. Endpoint ACLs work only on the public port that is exposed through the Input endpoint. An NSG works on one or more VM instances and controls all the traffic that is inbound and outbound on the VM."
The first inbound rule = "ALLOW VNET INBOUND 65000 Allow VIRTUAL_NETWORK * VIRTUAL_NETWORK * "
This allows all traffic inside to the virtual machine. Since there is no endpoint ACL on the VM and that RDP endpoint is enabled, then traffic can get to the VM.
Update: You are correct. It should not allow RDP access. As per this link under FAQ: http://azure.microsoft.com/blog/2014/11/04/network-security-groups/
4. I have defined RDP endpoint for my VM and I am using a Network Security Group do I need a Access control rule to connect to the RDP port from Internet?
Yes, the default rules in Network Security Group does not allow access to any port from Internet, the users have to create a specific rule to allow RDP traffic.

I have just found the same thing. I also found that deleting and recreating the endpoint then allows the NSG to function as expected, i.e. it seems that if the NSG is created/linked after the endpoint, it doesn't work but if the NSG is done first, it does!

You have to apply the changes, that's why you're not getting the expected behaviour:
Set-AzureRmVirtualNetwork -VirtualNetwork $virtualNetwork
Hope this helps!

Related

Azure Network : Prevent subnet to subnet communication

I have the following subnets
Subnet_1 = 10.2.3.0
Subnet_1 = 10.2.4.0
I want to prevent subnet to subnet communication. As far as I see, it can be done using the Network Security Group. However I am not sure about the required change.
I tried to reproduce the same in my environment to prevent communication between 2 Subnet
I have created 2 subnets in my Vnet, like below.
Subnet 1: Prod-Subnet (10.0.1.0/24)
Subnet 2: default (10.0.0.0/24)
In order to prevent communication between both the VNet, you need to create an inbound and outbound rule in same NSG group, like below.
I have created 2 virtual machines and attached above subnets to 2 virtual machines, like below.
Prod-subnet attached to VM1
Default Subnet attached to VM2
Create inbound and outbound rule in Network Security Group to block the communicatioin ,like below.
Inbound Rule:
Source IP: 10.0.0.9/24
Source Port: * ( for all)
Destination IP: 10.0.1.0/24
Destination Port: *( for all)
Service: custom
(Note: If you want block particular service and port, mention the port details and protocol type)
Outbound Rule:
Source IP: 10.0.1.0/24
Source Port: * ( for all)
Destination IP: 10.0.0.0/24
Destination Port: *( for all)
Service: custom
Tested communication and its getting denied from the subnets.
VM 1 Result
VM 2 Result

ApplicationGatewaySubnetInboundTrafficBlockedByNetworkSecurityGroup error when destroying resources with terraform

I am creating Azure infra using terraform. I am able to create AppGateway in gateway subnet. The AppGateway required NSG rule to all access on ports 65200 - 65535, I have added the NSG. I am able to communicate with app behind AppGateway. But my jenkins pipeline fails when I try to destroy the complete setup, it says -
Error: Deleting Security Rule: (Name "AllowGatewayManagerInbound" / Network Security Group
Name "gateway" / Resource Group "primary"): network.SecurityRulesClient#Delete: Failure
sending request: StatusCode=400 -- Original Error:
Code="ApplicationGatewaySubnetInboundTrafficBlockedByNetworkSecurityGroup" Message="Network
security group /subscriptions/****/resourceGroups/primary/providers/Microsoft.Network/networkSecurityGroups
/gateway blocks incoming internet traffic on ports 65200 - 65535 to subnet
/subscriptions/****/resourceGroups/primary/providers/Microsoft.Network/virtualNetworks/primary/subnets/gateway,
associated with Application Gateway subscriptions/****/resourceGroups/primary/providers/Microsoft.Network/applicationGateways/primary-centralus.
This is not permitted for Application Gateways that have V2 Sku." Details=[]
Terraform code to create subnet, NSG and create AppGateway.
resource "azurerm_network_security_group" "gateway" {
name = "gateway"
location = var.location
resource_group_name = azurerm_resource_group.app.name
tags = var.tags
}
resource "azurerm_network_security_rule" "gateway_allow_gateway_manager_https_inbound" {
name = "AllowGatewayManagerInbound"
description = "Allow Azure application GatewayManager on management ports"
resource_group_name = azurerm_network_security_group.gateway.resource_group_name
network_security_group_name = azurerm_network_security_group.gateway.name
priority = 2510
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
source_address_prefix = "GatewayManager"
destination_port_range = "65200-65535"
destination_address_prefix = "*"
}
module "app_gateway" {
source = "../../modules/app_gateway"
name = "${azurerm_resource_group.app.name}-${var.location}"
location = azurerm_resource_group.app.location
resource_group_name = azurerm_resource_group.app.name
vnet_subnet_id = azurerm_subnet.gateway.id
app_public_dns_zone = local.app_public_dns_zone
a_record_domain_name = local.a_record_subdomain
key_vault = local.key_vault
ssl_certificates = local.ssl_certificates
env = local.suffix
tags = var.tags
depends_on = [
azurerm_network_security_group.gateway
]
}
I have added depends_on relationship between AppGateway and NSG as AppGateway depends on NSG.
I need help to destry these resources using terraform.
• The ‘Destroy’ task through the terraform code that you are using is failing because inbound connectivity from the Jenkins pipeline is not possible through the NSG to the Azure resources, i.e., Application gateway in this case since the NSG is blocking the Jenkins pipeline access to the Azure resources on ports 65200 – 65535.
Thus, since you have deployed the ‘Application gateway’ in the ‘Gateway’ subnet and you have already allowed inbound network connectivity through the NSG to the application deployed behind the application gateway.
• Therefore, ensure that this allow rule’s priority is set higher than the deny rules for the same category. Also, allow TCP ports 65200 - 65535 for the application gateway v2 SKU with the destination subnet as ‘Any’ and source as ‘GatewayManager’ service tag for the communication between the Jenkin pipeline and the Azure Resource manager to happen.
Do check and ensure that the below rules in the NSG are set correctly: -
a) Outbound Internet connectivity can't be blocked. Default outbound rules in the NSG allow Internet connectivity.
b) Don't remove the default outbound rules.
c) Don't create other outbound rules that deny any outbound connectivity.
d) Traffic from the ‘AzureLoadBalancer’ tag with the destination subnet as Any must be allowed.
Finally, do check the priority for all the above stated rules and configurations for if the priority of the inbound rules is set higher than the deny rules, then they won’t be effective. Please find the below snapshot for your reference: -

To enable pings between 2 VMs in a subnet, Should I configure an inbound or outbound rule in an Azure Network Security Group?

I have 2 virtual machines (say 'A', 'B') in a subnet within a virtual network.
In the future, I can have multiple VMs in the same subnet.
I want to ensure that only virtual machine 'A' can ping 'B'.
Should I create an Inbound security role in the NSG (Network Security Group) with Source as A's private IP, protocol as ICMP, rule as "Allow", and destination as B's private IP?
Or
Should I be creating an Outbound rule in NSG with the same configuration as explained above for the inbound rule?
I see configuring the Inbound rule is the solution. Is it correct?
Go to VM B's NSG -> Inbound rule -> Add 1) Source = IP Addresses 2)
Source IP addresses/CIDR ranges = private ip address of VM A 3) Source
port ranges = 22 4) Destination = Any 5) Service = Custom 6) Protocol
= ICMP 7) Action = Deny
This will block all ping request from VM A to VM B, if you want to block and
ip ranges you can also paste it on number 2 section.

Azure VM - Creating TCP inbound rules

Please, I am trying to do some that should be simple... but it is not working.
I have 03 VMs in the same subnet.
The Subnet has a security group that I created HTTP/80 inbound OK.
Now, I need to open SQL to my second VM in the same subnet.
I already try to change the security group of my VM running SQL to the same of the IIS server.
I did do my Windows firewall inbound rule too. No way.
I tried to created another rule to test if my security group was forwarding correctly, without success.
This is my SQLIN rule:
Priority: 2100 / Source: Any / Protocol: TCP / Source port: 1433 / Target: CIDR Block: x.x.x.x/32 (server vm azure ip (internal) / Target port: 1433 / Action: ALLOW.
I can access my SQL through my VPN, but I need to open to the Internet.
The another test to check if my security group is doing what I create in the rules... is... I try to open RDP through port 3390... and redirect to 3389 (because in this security group I already have 3389 published to another server...)
The rule
Priority: 2120 / Source: Any / Protocol: TCP / Source port: 3390 / Target: CIDR block: x.x.x.x/32 (server vm azure ip interrna) / Target port: 3389 / Action: ALLOW.
I did not have sucess in both rules.
Again: Subnet is associated to this security group, and BOTH VMs are associated to this sec group.
If the 2 VMs are on the same subnet then you don't need to open up the NSG for the machines to talk to each other - you should just be able to use the windows firewall rules. Make the SQL VM private by making sure it doesn't have a public IP, or use the NSG here. I suspect the problem is with windows firewall from the IIS box or into the SQL box.

Azure Cloud Service Endpoint for VM to only allow internal access

I have a VM running SQL Server. I have port 1433 open on the Windows firewall and an Endpoint on the corresponding cloud service forwarding port 57501 to 1433. I haven't specified any ACLs on that endpoint. I want the endpoint to only be accessible from a specific subnet in the VNet where the VM exists.
If I set that in the ACL, it doesn't work - the ACL only seems to care about the public IP of the client. Since the public IP may change, this isn't an option.
What's the recommended approach here? Note that I don't want to connect directly to the VM hostname because I want to use the CNAME that the cloud service sets up for me (the actual Windows computer name is a random long string).
NSGs apply rules on the incoming/outgoing traffic at a VM or cloud-service-role-instance level.
Note that NSGs default rules allow traffic within the virtual network, and outbound to Internet. All other traffic is denied by default. You need to explicitly specify rules to change this behavior or allow any other traffic in/out.
You can create an NSG rule like below to allow only traffic from a specific subnet within a VNet.
Get-AzureNetworkSecurityGroup -Name "NSG-FrontEnd" `
| Set-AzureNetworkSecurityRule -Name rdp-rule `
-Action Allow -Protocol TCP -Type Inbound -Priority 100 `
-SourceAddressPrefix 192.168.1.0/24 -SourcePortRange '*' `
-DestinationAddressPrefix '*' -DestinationPortRange '1433'

Resources