Skip to content

Commit 2a0ea1c

Browse files
addedbreach searches
1 parent dade7c9 commit 2a0ea1c

File tree

5 files changed

+92
-20
lines changed

5 files changed

+92
-20
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using namespace System.Net
2+
3+
Function Invoke-ExecBreachSearch {
4+
<#
5+
.FUNCTIONALITY
6+
Entrypoint
7+
.ROLE
8+
CIPP.Core.Read
9+
#>
10+
[CmdletBinding()]
11+
param($Request, $TriggerMetadata)
12+
13+
$APIName = $TriggerMetadata.FunctionName
14+
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15+
$TenantFilter = $Request.query.TenantFilter
16+
#Move to background job
17+
New-BreachTenantSearch -TenantFilter $TenantFilter
18+
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
19+
StatusCode = [HttpStatusCode]::OK
20+
Body = @{ Results = "Executing Search for $TenantFilter" }
21+
})
22+
23+
}

Modules/CIPPCore/Public/Entrypoints/Invoke-ListBreachesTenant.ps1

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ Function Invoke-ListBreachesTenant {
1010
[CmdletBinding()]
1111
param($Request, $TriggerMetadata)
1212

13-
$APIName = $TriggerMetadata.FunctionName
14-
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
15-
$users = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$select=UserPrincipalName,mail" -tenantid $Request.query.TenantFilter
16-
$usersResults = foreach ($user in $users) {
17-
$Results = Get-HIBPRequest "breachedaccount/$($user.UserPrincipalName)?truncateResponse=true"
18-
if ($null -eq $Results) {
19-
$Results = 'No breaches found.'
20-
}
21-
[PSCustomObject]@{
22-
user = $user.UserPrincipalName
23-
breaches = $Results
24-
}
13+
$TenantFilter = $Request.query.TenantFilter
14+
$Table = Get-CIPPTable -TableName UserBreaches
15+
if ($TenantFilter -ne 'AllTenants') {
16+
$filter = "PartitionKey eq '$TenantFilter'"
17+
} else {
18+
$filter = $null
2519
}
26-
20+
$usersResults = (Get-CIPPAzDataTableEntity @Table -Filter $filter).breaches | ConvertFrom-Json
2721

2822
# Associate values to output bindings by calling 'Push-OutputBinding'.
2923
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function Get-BreachInfo {
2+
[CmdletBinding()]
3+
param(
4+
[Parameter()]
5+
$TenantFilter
6+
)
7+
$Data = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/domains' -tenantid $TenantFilter | ForEach-Object {
8+
$uri = 'https://geoipdb.azurewebsites.net/api/Breach?func=domain&domain=limenetworks.nl'
9+
Invoke-RestMethod -Uri $uri
10+
}
11+
return $Data
12+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
function Get-HIBPRequest {
22
[CmdletBinding()]
3-
param (
4-
[Parameter()]$endpoint
5-
3+
param(
4+
[Parameter()]
5+
$endpoint
66
)
77
$uri = "https://haveibeenpwned.com/api/v3/$endpoint"
88
try {
9-
Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth)
9+
return Invoke-RestMethod -Uri $uri -Headers (Get-HIBPAuth)
1010
} catch {
11-
#If the error is a 404, it means no breach has been found. Return an empty object.
12-
if ($_.Exception.Response.StatusCode -eq 404) {
11+
if ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 404) {
1312
return @()
13+
} elseif ($_.Exception.Response -and $_.Exception.Response.StatusCode -eq 429) {
14+
Write-Host 'Rate limited hit for hibp.'
15+
return @{
16+
Wait = ($_.Exception.Response.headers | Where-Object -Property key -EQ 'Retry-After').value
17+
'rate-limit' = $true
18+
}
19+
} else {
20+
throw "Failed to connect to HIBP: $($_.Exception.Message)"
1421
}
15-
throw "Failed to connect to HIBP: $($_.Exception.Message)"
1622
}
23+
throw "Failed to connect to HIBP after $maxRetries retries."
1724
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function New-BreachTenantSearch {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter()]$TenantFilter,
5+
[Parameter()][switch]$Force
6+
)
7+
8+
$Table = Get-CIPPTable -TableName UserBreaches
9+
$LatestBreach = Get-BreachInfo -TenantFilter $TenantFilter
10+
11+
$usersResults = foreach ($domain in $LatestBreach) {
12+
$ExistingBreaches = Get-CIPPAzDataTableEntity @Table -Filter "RowKey eq '$TenantFilter'"
13+
if ($null -eq $domain.result) {
14+
Write-Host "No breaches found for domain $($domain.domain)"
15+
continue
16+
}
17+
$SumOfBreaches = ($LatestBreach | Measure-Object -Sum -Property found).sum
18+
if ($ExistingBreaches.sum -eq $SumOfBreaches -and $Force.IsPresent -eq $false) {
19+
Write-Host "No new breaches found for tenant $TenantFilter"
20+
continue
21+
}
22+
23+
@{
24+
RowKey = $domain.domain
25+
PartitionKey = $TenantFilter
26+
breaches = "$($LatestBreach.Result | ConvertTo-Json)"
27+
sum = $SumOfBreaches
28+
}
29+
}
30+
31+
#Add user breaches to table
32+
if ($usersResults) {
33+
$entity = Add-CIPPAzDataTableEntity @Table -Entity $usersResults -Force
34+
Write-Host "Added $($usersResults.Count) breaches to table for tenant $TenantFilter"
35+
}
36+
}

0 commit comments

Comments
 (0)