Logo

dev-resources.site

for different kinds of informations.

Set up Azure Network Security Perimeter with PowerShell

Published at
12/10/2024
Categories
azure
powershell
Author
omiossec
Categories
2 categories in total
azure
open
powershell
open
Author
8 person written this
omiossec
open
Set up Azure Network Security Perimeter with PowerShell

Azure Network Security Perimeter is one of the new features announced by Microsoft during the MS Ignite 2024 in Chicago.

Network Security Perimeter (or NSP) aims to offer public-faced PaaS services the equivalent of Network Security Group for IaaS. The NSP restricts inbound and outbound network access to pass services, and like NSG access can be logged.

The service is in preview and is only available in some US regions (East US, East US 2, North Central US, South Central US, West US, and West US 2). It is limited to a set of Azure services, Azure Monitor, Azure AI Search, Cosmo DB, Event Hubs, Key Vault, SQL DB, and Storage account.

The NSP itself acts as a container, it contains one or several profiles, and these profiles contain one or several rules and are associated with one or more PaaS resources. These rules define the traffic behavior.

A PaaS service can be associated with two modes, learning mode and enforcement mode.

Let’s try to make it work for a simple scenario by using a storage account, a key vault, and an Azure Function. The Azure Function can send data to the storage account and access the Key vault.

The first step is to register the preview feature.

Check first if the feature is registered.

Get-AzProviderFeature -FeatureName "AllowNSPInPublicPreview" -ProviderNamespace "Microsoft.Network"
Enter fullscreen mode Exit fullscreen mode

If not, register it.

Register-AzProviderFeature -FeatureName "AllowNSPInPublicPreview" -ProviderNamespace "Microsoft.Network"
Enter fullscreen mode Exit fullscreen mode

Then you need to re-register the Microsoft.Network provider in the subscription

Register-AzResourceProvider -ProviderNamespace Microsoft.Network
Enter fullscreen mode Exit fullscreen mode

The next step is to update the az.network PowerShell module. To Find the latest version of the az.network module.

Find-Module -Name Az.Network -Allversions -AllowPrerelease
Enter fullscreen mode Exit fullscreen mode

In my case, it was the 7.7.1-preview

Install-Module -Name Az.Network -AllowPrerelease -Force -RequiredVersion 7.7.1-preview 
Enter fullscreen mode Exit fullscreen mode

After that, you can import the module, but it is better to open a new shell.

import-Module Az.network -MinimumVersion "7.7.1"
Enter fullscreen mode Exit fullscreen mode

You need to test if new cmdlets for NSP are loaded.

get-help new-AzNetworkSecurityPerimeter
Enter fullscreen mode Exit fullscreen mode

The next step is to create a new NSP.

$demoNSP = New-AzNetworkSecurityPerimeter -Name demoNSP -Location westus2 -ResourceGroupName 02-testnetperimeter
Enter fullscreen mode Exit fullscreen mode

Then we need to create a profile in the new NSP.

$demoProfileNSP = New-AzNetworkSecurityPerimeterProfile -name dmoprofile -ResourceGroupName 02-testnetperimeter -SecurityPerimeterName demoNSP 
Enter fullscreen mode Exit fullscreen mode

Now, we need to associate resources with this profile. Let’s begin with the storage account and key vault.

$vaultId = "/subscriptions/XXXXXX"

New-AzNetworkSecurityPerimeterAssociation -AssociationName nsp-keyvault  -ResourceGroupName "02-testnetperimeter" -SecurityPerimeterName $demoNSP.name -ProfileId $demoProfileNSP.id -PrivateLinkResourceId  $vaultId -AccessMode Enforced

$storageAccountID = "/subscriptions/XXXXXX"

New-AzNetworkSecurityPerimeterAssociation -AssociationName nsp-storage  -ResourceGroupName "02-testnetperimeter" -SecurityPerimeterName $demoNSP.name -ProfileId $demoProfileNSP.id -PrivateLinkResourceId $storageAccountID -AccessMode Enforced
Enter fullscreen mode Exit fullscreen mode

If we look at the NSP in the Azure portal, we will see that both resources are added to the profile, but there is a warning for the storage account.

Image description

Image description

The access mode is enforced so only traffic inside the perimeter is allowed unless a rule is added.

The Azure Function app cannot access the key vault and the storage account. The same, if you try to get data from the storage account or the key vault from the portal you have an error.

An Access rule needs to be added. The inbound access rule has two options: IP address range or by subscription.

To add one or more subscriptions to an inbound rule, the New-AzNetworkSecurityPerimeterAccessRule cmdlet as a parameter Subscription that requires a special type System.Collections.Generic.List1[Microsoft.Azure.PowerShell.Cmdlets.NetworkSecurityPerimeter.Models.ISubscriptionId].

$subID1 =  @{ "ID" = "/subscriptions/a3cefae9-XXX"}

$subID2 =  @{ "ID" = "/subscriptions/6429c9df-XXX"}

$subIDList = [System.Collections.Generic.List[Microsoft.Azure.PowerShell.Cmdlets.NetworkSecurityPerimeter.Models.ISubscriptionId]]::new()

$subIDList.Add($subID1)
$subIDList.Add($subID2)


New-AzNetworkSecurityPerimeterAccessRule -Name "allowSubscription" -ResourceGroupName "02-testnetperimeter"  -ProfileName $demoProfileNSP.name -SecurityPerimeterName $demoNSP.name -Direction "Inbound" -Subscription $subIDList
Enter fullscreen mode Exit fullscreen mode

After that, the Function will access the key vault and the storage account.

In the same way, we can manage outbound access from PaaS services. In network security perimeter, you can only assign email addresses (this feature is not yet implemented) or FQDNs

For FQDNs

New-AzNetworkSecurityPerimeterAccessRule -Name "outboundFQDN" -ResourceGroupName "02-testnetperimeter"  -ProfileName $demoProfileNSP.name -SecurityPerimeterName $demoNSP.name -Direction "outbound"  -FullyQualifiedDomainName @("wwww.test.com", "www.test.net")
Enter fullscreen mode Exit fullscreen mode

For Emails (my trigger an error)

New-AzNetworkSecurityPerimeterAccessRule -Name "outbounEmails" -ResourceGroupName "02-testnetperimeter"  -ProfileName $demoProfileNSP.name -SecurityPerimeterName $demoNSP.name -Direction "outbound" -EmailAddress @("[email protected]")
Enter fullscreen mode Exit fullscreen mode
powershell Article's
30 articles in total
Favicon
Windows 上 VSCode 的 C/C++ 延伸模組處理編碼的問題
Favicon
PowerShell 的文字編碼
Favicon
笔记3
Favicon
TryHackMe | Windows PowerShell | RSCyberTech
Favicon
Configuring Hyper-V Global Default Directories
Favicon
How I Set Up an Awesome PowerShell Environment for Script Development
Favicon
Azure Function App (Flex Consumption) PowerShell Modules solution
Favicon
File Comparison Made Easy: Detecting New and Changed Files with PowerShell
Favicon
10 Best Practices of PowerShell Code Signing for Signing Your Script
Favicon
PowerShell Script Collection: Automation and Solutions for Everyday Tasks
Favicon
Video: List All Available Windows Features on Windows 11 using CMD & PowerShell
Favicon
Set up Azure Network Security Perimeter with PowerShell
Favicon
Automating Azure Project Setup with PowerShell and GitHub Actions
Favicon
Azure Function App (Flex Consumption) in private VNET via IaC
Favicon
Using PowerShell to Create Service Principals for RBAC
Favicon
Two ways to use Pester to Mock objects with strongly typed parameters
Favicon
PowerShell Automation: Execute Batch GitHub CLI Commands
Favicon
❓ Do you allow wrong input to enter your function?
Favicon
Introducing PowerShell Utility Scripts
Favicon
Cisco DHCP Pool conversion to Windows Server DHCP
Favicon
PowerShell | Script output garbled Chinese characters
Favicon
Rename Multiple Files in Sequence with Just One Click Using PowerShell in Windows! 🚀
Favicon
Redirect Out-File to TestDrive: in your PowerShell Pester test scripts with this one weird trick
Favicon
Using PowerShell for day to day stuff
Favicon
Automating SharePoint Embedded: Using PowerShell to Call Graph API Endpoints
Favicon
Install Ubuntu on WSL 2
Favicon
How to Install WSL from PowerShell on Windows 10 and 11
Favicon
How to Run PowerShell Script in Jenkins Pipeline
Favicon
Identify Which Files are Large.
Favicon
Quick guide to setting up an shortcut/alias in windows

Featured ones: