soteria-security / 365Inspect

A PowerShell script that automates the security assessment of Microsoft 365 environments.
https://soteria.io/solutions/soteria-inspect/
MIT License
582 stars 109 forks source link

Error connecting using Valid Certiticate thumbprint #101

Closed dovelus closed 3 weeks ago

dovelus commented 2 months ago

Describe the bug Using APPID connection is not correctly working

To Reproduce Steps to reproduce the behavior:

  1. Install powershell 7.4.5
  2. Install the modules as described in #99 image
  3. Run .\365Inspect.ps1 -OutPath ..\365_report -UserPrincipalName user@tenant.onmicrosoft.com -Auth APP
  4. See error

Screenshots Full Error image Certificate in Azure APP Certificate on azure: image

Desktop (please complete the following information):

Additional context To create the app i used this command: Register-PnPEntraIDApp -ApplicationName "PnP Rocks" -Tenant [yourtenant].onmicrosoft.com -OutPath c:\mycertificates -DeviceLogin

ThoughtContagion commented 2 months ago

Just to verify, after creating the certificate, you installed the certificate on the machine you're executing the tool from? You can validate this by using the following command:

Get-ChildItem -Path Cert:\ -Recurse | Where-Object { $_.Thumbprint -eq $thumbprint }
dovelus commented 2 months ago

I checked the certificates but it seems that the thumbprint is different from my local machine and azure even when i used the same cert

Ok i rerunned the command by suppling the local thumbprint and now it passed the cert check, image

ThoughtContagion commented 2 months ago

The PNP documentation states that the provided command creates the certificates and they can be used to authenticate by issuing the certificate path. By installing the .cer version of the generated certificate locally, and ensuring the thumbprint matches what is reflected in the registered application, the thumbprint authentication should work.

In the error messages in your provided screenshots, the service cannot find a matching certificate on the registered application. Based on your provided command, try navigating to c:\mycertificates and uploading the .cer file to the app in your tenant.

If this still fails, it may be better to create your own certificate and upload it to the registered application.

ThoughtContagion commented 2 months ago

You will also want to ensure that you have granted the appropriate application permissions for all other services to the registered application as outlined in the Application Authentication Requirements in this repos README file.

dovelus commented 2 months ago

I created another self signed certificate but the error still remain the same: image Now the thumb print are the same: image image

ThoughtContagion commented 2 months ago

Are you able to connect to the PnP service using this thumbprint?

Connect-PnPOnline -Url "https://[your_org_name]-admin.sharepoint.com" -ClientId $appID -Thumbprint $thumbprint -Tenant [your_tenant_domain]

For example:

Connect-PnPOnline -Url "https://mycompany-admin.sharepoint.com" -ClientId $appID -Thumbprint $thumbprint -Tenant mycompany.com
dovelus commented 2 months ago

Here is the output yep i think her can't find the certificate and just for context the Cert is under APP Registration -> 365 Inspect -> Manage -> Certificates image

ThoughtContagion commented 2 months ago

This error appears to be related to your local machine and not the application in Microsoft Entra.

Does this command return any output?

Get-ChildItem -Path Cert:\CurrentUser\My | Where-Object { $_.Thumbprint -eq $thumbprint }

image

dovelus commented 2 months ago

This machine is freshly installed windows VM so i exclude any problem related directly to my system image

ThoughtContagion commented 2 months ago

The error in this screenshot appears to be related to the certificate creation.

I would recommend creating a new certificate using the following process:

PowerShell Execution Policy PowerShell Execution Policy on the client machine should be configured to allow the scripts to run. Typically, the Execution Policy should be set to ‘RemoteSigned’. This can be accomplished by running the following command from an administrative PowerShell prompt: Set-ExecutionPolicy RemoteSigned -Force -Confirm:$false

Certificate Creation A certificate is required to run the tool unattended, as it will be used to authenticate the scripts to the tenant. Creating a self-signed certificate is recommended. It is important to note that existing policies on creation and use of self-signed certificates may be prescribed by the organization. While the examples in this document present one possible solution, it is recommended to adhere to established internal policies in creation of the certificate. The .cer certificate should be installed on the machine after creation.

Creation of the certificate can be accomplished via an administrative PowerShell window using the following example commands:

Create certificate

$mycert = New-SelfSignedCertificate -DnsName "Enter your organization here" -CertStoreLocation "cert:\CurrentUser\My" -NotAfter (Get-Date).AddYears(1) -KeySpec KeyExchange

Export certificate to .pfx file

$mycert | Export-PfxCertificate -FilePath mycert.pfx -Password $(ConvertTo-SecureString -String "P@ssw0Rd1234" -AsPlainText -Force)

Export certificate to .cer file

$mycert | Export-Certificate -FilePath mycert.cer Certificate Installation The certificate can be installed on the machine by right-clicking the certificate and selecting Install. The certificate defaults to the Current User Store Location, click Next Click Next ensuring the Certificate Import Wizard radio button selected is Automatically select the certificate store based on the type of certificate Click Finish

dovelus commented 2 months ago

Thanks it worked beautifully the Cert problem is solved.

ThoughtContagion commented 1 month ago

Are the permissions granted using the 'Grant Admin Consent' button? Your application's permissions should look like this: image