-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dade7c9
commit 2a0ea1c
Showing
5 changed files
with
92 additions
and
20 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Modules/CIPPCore/Public/Entrypoints/Invoke-ExecBreachSearch.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using namespace System.Net | ||
|
||
Function Invoke-ExecBreachSearch { | ||
<# | ||
.FUNCTIONALITY | ||
Entrypoint | ||
.ROLE | ||
CIPP.Core.Read | ||
#> | ||
[CmdletBinding()] | ||
param($Request, $TriggerMetadata) | ||
|
||
$APIName = $TriggerMetadata.FunctionName | ||
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug' | ||
$TenantFilter = $Request.query.TenantFilter | ||
#Move to background job | ||
New-BreachTenantSearch -TenantFilter $TenantFilter | ||
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ | ||
StatusCode = [HttpStatusCode]::OK | ||
Body = @{ Results = "Executing Search for $TenantFilter" } | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
function Get-BreachInfo { | ||
[CmdletBinding()] | ||
param( | ||
[Parameter()] | ||
$TenantFilter | ||
) | ||
$Data = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/domains' -tenantid $TenantFilter | ForEach-Object { | ||
$uri = 'https://geoipdb.azurewebsites.net/api/Breach?func=domain&domain=limenetworks.nl' | ||
Invoke-RestMethod -Uri $uri | ||
} | ||
return $Data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
function Get-HIBPRequest { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter()]$endpoint | ||
|
||
param( | ||
[Parameter()] | ||
$endpoint | ||
) | ||
$uri = "https://haveibeenpwned.com/api/v3/$endpoint" | ||
try { | ||
Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth) | ||
return Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth) | ||
} catch { | ||
#If the error is a 404, it means no breach has been found. Return an empty object. | ||
if ($_.Exception.Response.StatusCode -eq 404) { | ||
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 404) { | ||
return @() | ||
} elseif ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 429) { | ||
Write-Host 'Rate limited hit for hibp.' | ||
return @{ | ||
Wait = ($_.Exception.Response.headers | Where-Object -Property key -EQ 'Retry-After').value | ||
'rate-limit' = $true | ||
} | ||
} else { | ||
throw "Failed to connect to HIBP: $($_.Exception.Message)" | ||
} | ||
throw "Failed to connect to HIBP: $($_.Exception.Message)" | ||
} | ||
throw "Failed to connect to HIBP after $maxRetries retries." | ||
} |
36 changes: 36 additions & 0 deletions
36
Modules/CippExtensions/Public/HIBP/New-BreachTenantSearch.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function New-BreachTenantSearch { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter()]$TenantFilter, | ||
[Parameter()][switch]$Force | ||
) | ||
|
||
$Table = Get-CIPPTable -TableName UserBreaches | ||
$LatestBreach = Get-BreachInfo -TenantFilter $TenantFilter | ||
|
||
$usersResults = foreach ($domain in $LatestBreach) { | ||
$ExistingBreaches = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$TenantFilter'" | ||
if ($null -eq $domain.result) { | ||
Write-Host "No breaches found for domain $($domain.domain)" | ||
continue | ||
} | ||
$SumOfBreaches = ($LatestBreach | Measure-Object -Sum -Property found).sum | ||
if ($ExistingBreaches.sum -eq $SumOfBreaches -and $Force.IsPresent -eq $false) { | ||
Write-Host "No new breaches found for tenant $TenantFilter" | ||
continue | ||
} | ||
|
||
@{ | ||
RowKey = $domain.domain | ||
PartitionKey = $TenantFilter | ||
breaches = "$($LatestBreach.Result | ConvertTo-Json)" | ||
sum = $SumOfBreaches | ||
} | ||
} | ||
|
||
#Add user breaches to table | ||
if ($usersResults) { | ||
$entity = Add-CIPPAzDataTableEntity @Table -Entity $usersResults -Force | ||
Write-Host "Added $($usersResults.Count) breaches to table for tenant $TenantFilter" | ||
} | ||
} |