Microsoft Entra
Overview
This integration uses both a bash script and PowerShell script to execute. The PowerShellpowershell script managesis designed to manage named locations in Microsoft Azure Conditional Access policies via the Microsoft Graph API. It allows users to add, delete, or flush named locations related to specific IP addresses. The primary use case is to automate the management of named locations in a secure and efficient manner.
This integration with knocknoc enables IP whitelisting within Microsoft 365. An example of a user's experience can be found below:
- User attempts to access Microsoft 365 services:
- User must authenticate with knocknoc and will see their granted ACL on the right hand side:
- User can now access Microsoft 365 services:
Prerequisites
Before running this script, ensure that you have the following prerequisites installed and configured:
- Powershell - Install PowerShell on your system. For Linux, follow these steps:
# Update the list of packages sudo apt-get update # Install pre-requisite packages sudo apt-get install -y wget apt-transport-https software-properties-common # Download the Microsoft repository GPG keys wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb # Register the Microsoft repository GPG keys sudo dpkg -i packages-microsoft-prod.deb # Update the list of packages after we added the Microsoft repository sudo apt-get update # Install PowerShell sudo apt-get install -y powershell
- Azure Enterprise Application: An enterprise application must be created to allow knocknoc to authenticate and change conditional access policies and named
locationslocations.inCreate an enterprise application, create a client secret, and copy the application's client id, azure tenant id and secret. You'll need these later. -
Microsoft
AzureGraphusingAPI Permissions: Ensure that the application has the necessary permissions to access the Microsoft GraphAPI.API,Itparticularlysupportsforadding, removing, and flushing IP-based named locations within conditional access policies. The script is designed to optimize API calls by caching policy and location data.Prerequisites• Azure PowerShell Module• Microsoft Graph PowerShell Module• Azure AD app registration with appropriate permissions to managemanaging conditional access policies and named locations. - Conditional Access Policy/Policies: Knocknoc needs to be able to distinguish between policies it can amend and policies it cannot. Therefore, knocknoc looks for a prepending "knocknoc_" prior to the name of the ACL. For example, a conditional access policy might be named "knocknoc_financedepartment" with specific rules around applications and services that group can access. These must be created PRIOR to configuring anything in the knocknoc admin portal.
-
Credentials File: Ensure that a credentials file is present at
/opt/knocknoc-agent/etc/entra-credentials.sh
with the following content:entra_clientid="" entra_tenantid="" entra_clientsecret=""
You will need to input the necessary ClientID for your Azure Enterprise Application, your Azure Tenant ID and your application's client secret.
Script Parameters
•
$action:action
: Specifies the action to perform. AcceptableValid values are `add`add
, `remove`del
, and `flush`flush
.
•acl
: $acl:The Namename of the access control listlist.(ACL)ip
: to modify.
• $ip:The IP address to add or removedelete (required for add
and del
actions).
ClientId
: The client ID of your Azure AD application (sourced from the namedcredentials location.
Functions
TenantId
: Get-ClientSecret
The functiontenant Get-ClientSecretID {of paramyour Azure AD (sourced [string]$secretNamefrom )the returncredentials "your-secure-secret"file).
ClientSecret
: Retrieves aThe client secret securelyof your Azure AD application (sourced from the credentials file).
Script Details
- The script begins by initializing a
storagelocationsystemcacheliketoAzurestoreKeyexistingVault.named
locations.Get-NamedLocationId
functionDepending Get-NamedLocationId {
param ([string]$displayName)
# Implementation...
}
Retrieveson the IDspecified ofaction (add
, del
, or flush
), the script performs the corresponding operation.
add
action, the script creates a named location by its display name, caching the result to minimize repeated API calls.
Update-NamedLocationTrustStatus
function Update-NamedLocationTrustStatus {
param (
[string]$NamedLocationId,
[bool]$IsTrusted,
[string]$LocationType = "#microsoft.graph.ipNamedLocation"
)
# Implementation...
}
Updates the trust status of a named location.
Get-ConditionalAccessPolicy
function Get-ConditionalAccessPolicy {
param ([string]$displayName)
# Implementation...
}
Retrieves a conditional access policy by its display name, caching the result to minimize repeated API calls.
Update-ConditionalAccessPolicy
function Update-ConditionalAccessPolicy {
param (
[string]$PolicyId,
[array]$LocationIds
)
# Implementation...
}
Updates a conditional access policy with a list of named location IDs.
Add-NamedLocationAndModifyPolicy
```powershellfunction Add-NamedLocationAndModifyPolicy { # Implementation...}```
Adds a named location if it does not exist and updates the specified conditional access policy to includeexclude this location.
newdel
namedaction, location.
Remove-LocationFromPolicies
script ```powershellfunctionremoves Remove-LocationFromPoliciesthe { param ([string]$NamedLocationId) # Implementation...}```
Removes aspecified named location from all conditional access policies and then deletes the named location.
Flush-NamedLocationsFromPolicy
itself.
```powershellfunction Flush-NamedLocationsFromPolicy { # Implementation...}```
Removes all named locations matchingpatternflush
`knocknoc_*` from all conditional access policies and then deletes these named locations.
Usage
Add a Named Location and Modify Policy
```powershell.\Script.ps1 -action add -acl "aclName" -ip "192.168.1.1"```
Addsaction, the specifiedscript IP address as a named location and modifies the specified access control list to include this named location.
Remove a Named Location from Policies
```powershell.\Script.ps1 -action remove -acl "aclName" -ip "192.168.1.1"```
Removes the specified IP address from the named locations in all policies.
Flush Named Locations from Policies
```powershell.\Script.ps1 -action flush -acl "aclName"```
Removesremoves all named locations that match the pattern `knocknoc_*
from all policies.`
Authentication
The script requires authentication with Microsoft Graph API. This is achieved by creating a PSCredential object using the client ID, tenant ID, and client secret.
Example
```powershellparam ( [ValidateSet('add', 'remove', 'flush')][string]$action, [string]$acl, [ValidatePattern('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')][string]$ip)# Authentication$ClientId = "client"$TenantId = "tenant"$ClientSecret = "secret"try { $ClientSecretPass = ConvertTo-SecureString -String $ClientSecret -AsPlainText -Force $ClientSecretCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $ClientId, $ClientSecretPass Connect-MgGraph -TenantId $TenantId -ClientSecretCredential $ClientSecretCredential -NoWelcome} catch { Write-Host "Failed to authenticate with Microsoft Graph. Error: $_" exit 1}# Action map$actionMap = @{ 'add' = { Add-NamedLocationAndModifyPolicy } 'remove' = { $NamedLocationId = Get-NamedLocationId "knocknoc_$ip" if ($null -eq $NamedLocationId) { Write-Host "Named location 'knocknoc_$ip' not found." } else { Remove-LocationFromPolicies -NamedLocationId $NamedLocationId } } 'flush' = { Flush-NamedLocationsFromPolicy }}# Execute actionif ($actionMap.ContainsKey($action)) { & $actionMap[$action]} else { Write-Host "Invalid action specified"}```
Script
Admin NotesConfiguration
• Ensure