File tree Expand file tree Collapse file tree 5 files changed +92
-20
lines changed
CIPPCore/Public/Entrypoints
CippExtensions/Public/HIBP Expand file tree Collapse file tree 5 files changed +92
-20
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -10,20 +10,14 @@ Function Invoke-ListBreachesTenant {
10
10
[CmdletBinding ()]
11
11
param ($Request , $TriggerMetadata )
12
12
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
25
19
}
26
-
20
+ $usersResults = ( Get-CIPPAzDataTableEntity @Table - Filter $filter ).breaches | ConvertFrom-Json
27
21
28
22
# Associate values to output bindings by calling 'Push-OutputBinding'.
29
23
Push-OutputBinding - Name Response - Value ([HttpResponseContext ]@ {
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
function Get-HIBPRequest {
2
2
[CmdletBinding ()]
3
- param (
4
- [Parameter ()]$endpoint
5
-
3
+ param (
4
+ [Parameter ()]
5
+ $endpoint
6
6
)
7
7
$uri = " https://haveibeenpwned.com/api/v3/$endpoint "
8
8
try {
9
- Invoke-RestMethod - Uri $uri - Headers (Get-HIBPAuth )
9
+ return Invoke-RestMethod - Uri $uri - Headers (Get-HIBPAuth )
10
10
} 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 ) {
13
12
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 ) "
14
21
}
15
- throw " Failed to connect to HIBP: $ ( $_.Exception.Message ) "
16
22
}
23
+ throw " Failed to connect to HIBP after $maxRetries retries."
17
24
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments