How to Find and Remove Application Owners from Disabled Applications

Owners of applications in Microsoft Entra inherently have full control over them applications, which poses some real risks, its something I’ve been conscious of for a long time. Late last year, I wrote an article advising admins how to disable applications in Microsoft Entra programmatically. For these applications which are disabled by admins, leaving owners assigned to them is just one of them risks which I will explore in this article.

Why you should remove owners from disabled applications

When an application owner is assign to an application (or app registration) in Microsoft Entra, they can pretty much do anything with it, create new credentials, update properties, delete it, even restore it! Now, that is a huge risk… Let take the example where an administrator disables an application because it starts to exhibit some malicious behaviour which was picked up by their monitoring systems. The application owner is blissfully unaware of this and when users start receiving log-in errors, the application owner re-enables the application, thus allowing it to continue its malicious behaviours. Now, of-course this happening means there would likely be a serious breakdown of communication, but emails, teams and calls are often missed in his stressful or busy environments, so it might happen to anyone…

Lets explore what re-enabling a disabled application looks like. Here I have an application which has been disabled by me, the administrator:

Disabled Application App1
Disabled Application App1

My standard user can login to the Microsoft Entra admin portal and see the same screen on the application he owns, advising him it is disabled. This user, if the organisation permits, can connect to Microsoft Graph PowerShell and re-enable the application with a few lines of code like below:

# Connects to Microsoft Graph
Connect-MgGraph

# Gets the application information
$uri = "/beta/applications/?`$filter=displayName eq 'App1'"
$app = Invoke-MgGraphRequest -Uri $uri | Select -Expand Value

# Enables the application
$uri2 = "/beta/applications/$($app.Id)"
$body = @{
    "isDisabled" = $false
}
Invoke-MgGraphRequest -Uri $uri2 -Method PATCH -Body $body

Now, it’s not always this simple. For the case above, the “application.readwrite.all” permission would need to be pre-consented to the Microsoft Graph Command Line Tools service principal for this to work, as well as the app being enabled and configured for you to sign-in. But, there is another way. Any user can simply open the developer tools of their browser and obtain the bearer token. This is a nice little token with lots of pre-consented permissions (including Application.ReadWrite.All) with the Microsoft Graph audience, the UX uses to be authorised to make requests to Microsoft Graph.

obtain bearer token

If you copy this token, then use the below code example, you can use this token to connect to Microsoft Graph in PowerShell, even with company restrictions in place. I talk about this in more detail in my article; How to bypass Microsoft Graph PowerShell access restrictions.

$token = "%PASTE TOKENH HERE%"
$SecureToken = ConvertTo-SecureString $token -AsPlainText -Force
connect-MgGraph -AccessToken $SecureToken

Then you can simply run the previous code example to enable or disable your application. This of-course could also be done directly from the Microsoft Entra admin center, however, some settings can only be modified programmatically.

Find all applications which are disabled with owners

Now, importantly, you should look to see if this scenario is currently a risk to you. So, to find all applications which are disabled and to view their owners, you can make a single request to Microsoft Graph, like in the example below. I wouldn’t expect there to be hundreds of applications in a disabled state, so I have left out any pagination from the request:

$uri = "/beta/applications?`$filter=isDisabled eq true&`$expand=Owners"
$apps = Invoke-MgGraphRequest -Method GET -Uri $uri -OutputType PSObject | Select -Expand Value
$applist = $apps | Select displayName, @{Name="Owners";Expression={$_.owners | Select -Expand displayName -Unique}}
$applist | Format-Table -AutoSize

The final line of the code in the example above will produce an output with a list of applications by name and their owners. You can use this to determine the validity of any owner assignments. Remember, the goal is to ensure that standard users are not assigned as owners to applications.

List of disabled apps and owners
List of disabled apps and owners

Remove all owners from disabled applications

Once you are happy with your list, you can loop through each application and remove all owners from it. Below I have provided a simple example loop which you can use to remove all users from all applications.

Foreach ($app in $apps) {
    $appId = $app.Id
    $owners = $app.Owners
    foreach ($owner in $owners) {
        $ownerId = $owner.id
        Write-Host "deleting owner $($owner.displayName) from application: $($app.displayName)"
        $uri = "/v1.0/applications/$appId/owners/$ownerId/`$ref"
        Invoke-MgGraphRequest -Method DELETE -Uri $uri
    }
}

Wrapping up

For organisations that monitor your apps and occasionally need to temporarily disable them, make sure you update your workflows to also remove owners. Leaving owners assigned to applications, especially owners who are not administrative users, can leave open an attack path which may have an huge impact on your organisation. For a comprehensive view of your application and the risks they may pose, check out my article to Create a free Enterprise App Permissions report in Microsoft Entra.

Daniel Bradley

Daniel Bradley is a UK-based technical architect and Microsoft MVP in Security. He specialises in Microsoft Entra, Intune, and Microsoft Graph PowerShell and is the author of Microsoft Graph PowerShell for Administrators. He writes and edits Our Cloud Network using experience gained from working with Microsoft cloud environments.

Leave a Reply