<# : Co-CISO Microsoft 365 check __CC_VERSION__ - READ-ONLY. Double-click to run on a Windows PC. @echo off title Co-CISO Microsoft 365 check set "CCDIR=%~dp0" set "CCSELF=%~f0" powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ([IO.File]::ReadAllText($env:CCSELF))" echo. pause exit /b #> # ------------------------------------------------------------------------------------------------ # Co-CISO Microsoft 365 / Entra ID check. READ-ONLY: a Microsoft 365 administrator signs in on # Microsoft's own page (device code) with read permissions; the script reads security settings and # COUNTS only (no names, e-mails or files) and writes ONE report file (HTML). Nothing is changed. # It uses Microsoft's own "Microsoft Graph Command Line Tools" sign-in app. No module is installed. # Published by Co-CISO ยท Template source: https://co-ciso.online/collectors/templates/ # ------------------------------------------------------------------------------------------------ $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $cid = '14d82eec-204b-4c2f-b7e8-296a70dab67e' $g = 'https://graph.microsoft.com/' $scope = (@('Policy.Read.All', 'Directory.Read.All', 'AuditLog.Read.All', 'Reports.Read.All', 'SharePointTenantSettings.Read.All', 'SecurityEvents.Read.All') | ForEach-Object { $g + $_ }) -join ' ' Write-Host '' Write-Host ' Co-CISO - __CC_MSG_M365_INTRO__' -ForegroundColor Cyan $dc = Invoke-RestMethod -Method Post -Uri 'https://login.microsoftonline.com/organizations/oauth2/v2.0/devicecode' -Body @{ client_id = $cid; scope = "$scope offline_access" } Write-Host '' Write-Host " __CC_MSG_M365_CODE__ $($dc.user_code)" -ForegroundColor Yellow Write-Host " $($dc.verification_uri)" try { Set-Clipboard $dc.user_code } catch {} Start-Process $dc.verification_uri $tok = $null $until = (Get-Date).AddSeconds([int]$dc.expires_in) while (-not $tok -and (Get-Date) -lt $until) { Start-Sleep -Seconds ([int]$dc.interval) try { $tok = Invoke-RestMethod -Method Post -Uri 'https://login.microsoftonline.com/organizations/oauth2/v2.0/token' -Body @{ grant_type = 'urn:ietf:params:oauth:grant-type:device_code'; client_id = $cid; device_code = $dc.device_code } } catch { $e = $null; try { $e = ($_.ErrorDetails.Message | ConvertFrom-Json).error } catch {} if ($e -and $e -ne 'authorization_pending' -and $e -ne 'slow_down') { Write-Host " Sign-in failed: $e" -ForegroundColor Red; return } } } if (-not $tok) { Write-Host ' Sign-in timed out.' -ForegroundColor Red; return } Write-Host ' __CC_MSG_RUNNING__' -ForegroundColor Cyan $H = @{ Authorization = "Bearer $($tok.access_token)"; ConsistencyLevel = 'eventual' } $collectionErrors = @() function G([string]$path) { try { Invoke-RestMethod -Headers $H -Uri ($g + 'v1.0/' + $path) -TimeoutSec 30 } catch { $script:collectionErrors += $path; $null } } function GAll([string]$path, [int]$max = 5000) { $items = @(); $u = $g + 'v1.0/' + $path while ($u -and $items.Count -lt $max) { try { $r = Invoke-RestMethod -Headers $H -Uri $u -TimeoutSec 30; $items += $r.value; $u = $r.'@odata.nextLink' } catch { $script:collectionErrors += $path; $u = $null } } if ($u) { $script:collectionErrors += $path } return $items } $org = G 'organization?$select=displayName' $sd = G 'policies/identitySecurityDefaultsEnforcementPolicy' $ca = @(GAll 'identity/conditionalAccess/policies' 200 | Where-Object { $_.state -eq 'enabled' }) $caMfa = @($ca | Where-Object { $_.conditions.users.includeUsers -contains 'All' -and $_.conditions.applications.includeApplications -contains 'All' -and ($_.grantControls.builtInControls -contains 'mfa' -or $_.grantControls.authenticationStrength) }).Count -gt 0 $caLegacy = @($ca | Where-Object { ($_.conditions.clientAppTypes -contains 'exchangeActiveSync' -or $_.conditions.clientAppTypes -contains 'other') -and $_.grantControls.builtInControls -contains 'block' }).Count -gt 0 $reg = @(GAll 'reports/authenticationMethods/userRegistrationDetails?$top=999') $users = @($reg | Where-Object { $_.userType -ne 'guest' }) $mfaPct = if ($users.Count) { [math]::Round(100 * @($users | Where-Object isMfaRegistered).Count / $users.Count) } else { $null } $adminsNoMfa = @($users | Where-Object { $_.isAdmin -and -not $_.isMfaRegistered }).Count $ga = @(GAll "directoryRoles(roleTemplateId='62e90394-69f5-4237-9190-012177145e10')/members" 200).Count $ap = G 'policies/authorizationPolicy' $grants = @($ap.defaultUserRolePermissions.permissionGrantPoliciesAssigned) $consent = if ($grants -match 'legacy') { 'all' } elseif ($grants -match 'low') { 'limited' } elseif ($ap) { 'none' } else { $null } $sp = G 'admin/sharepoint/settings' $sharing = switch ($sp.sharingCapability) { 'externalUserAndGuestSharing' { 'anyone' } 'externalUserSharingOnly' { 'guests' } 'existingExternalUserSharingOnly' { 'existing_guests' } 'disabled' { 'internal_only' } default { $null } } $guests = try { Invoke-RestMethod -Headers $H -Uri ($g + 'v1.0/users/$count?$filter=userType eq ''Guest''') } catch { $null } $ss = try { (Invoke-RestMethod -Headers $H -Uri ($g + 'v1.0/security/secureScores?$top=1')).value[0] } catch { $null } $out = [ordered]@{ co_ciso_collector = 'm365'; version = '__CC_VERSION__'; collected_at = (Get-Date).ToUniversalTime().ToString('o') tenant = [ordered]@{ name = $org.value[0].displayName; security_defaults = if ($sd) { [bool]$sd.isEnabled } else { $null }; ca_policies_enabled = $ca.Count; ca_mfa_all = if ($collectionErrors -contains 'identity/conditionalAccess/policies') { $null } else { $caMfa }; ca_block_legacy = if ($collectionErrors -contains 'identity/conditionalAccess/policies') { $null } else { $caLegacy } users = $users.Count; mfa_registered_pct = if ($collectionErrors -contains 'reports/authenticationMethods/userRegistrationDetails?$top=999') { $null } else { $mfaPct }; admins_without_mfa = if ($collectionErrors -contains 'reports/authenticationMethods/userRegistrationDetails?$top=999') { $null } else { $adminsNoMfa }; global_admins = if ($collectionErrors -contains "directoryRoles(roleTemplateId='62e90394-69f5-4237-9190-012177145e10')/members") { $null } else { $ga } user_consent = $consent; guest_invites = "$($ap.allowInvitesFrom)"; sharing = $sharing; guests = $guests secure_score = if ($ss) { [math]::Round(100 * $ss.currentScore / [math]::Max(1, $ss.maxScore)) } else { $null } } collection_errors = @($collectionErrors | Sort-Object -Unique) collection_scope = 'Tenant settings and bounded API results; unavailable or truncated responses remain unassessed.' host = [ordered]@{ model = "Microsoft 365: $($org.value[0].displayName)" } } $json = $out | ConvertTo-Json -Depth 6 -Compress $b64 = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($json)) $head = @' __CC_HEAD__ '@ $tail = @' __CC_TAIL__ '@ $dir = if ($env:CCDIR) { $env:CCDIR } else { [Environment]::GetFolderPath('Desktop') } $file = Join-Path $dir ("Co-CISO-Report-M365-{0}.html" -f (Get-Date -Format 'yyyyMMdd-HHmm')) [IO.File]::WriteAllText($file, $head + $b64 + $tail, (New-Object Text.UTF8Encoding $false)) $tok = $null; $H = $null Write-Host '' Write-Host ' __CC_MSG_DONE__' -ForegroundColor Green Write-Host " $file" Write-Host ' __CC_MSG_UPLOAD__' -ForegroundColor Yellow Start-Process $file