Skip to content

Commit 04f2bdb

Browse files
Update Invoke-ListCVEManagement.ps1
Send live requests to report Signed-off-by: DamienMatthys <damien@pcunplug.com>
1 parent da83e21 commit 04f2bdb

1 file changed

Lines changed: 17 additions & 169 deletions

File tree

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/Endpoint/MEM/Invoke-ListCVEManagement.ps1

Lines changed: 17 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -12,176 +12,24 @@ function Invoke-ListCVEManagement {
1212
$TenantFilter = $Request.Query.tenantFilter
1313
$UseReportDB = $Request.Query.UseReportDB
1414

15-
if ($UseReportDB -eq 'true') {
16-
try {
17-
$GraphRequest = Get-CIPPCVEReport -TenantFilter $TenantFilter -ErrorAction Stop
18-
$StatusCode = [HttpStatusCode]::OK
19-
$SortedCves = $GraphRequest
20-
Write-LogMessage -API 'ListCVEManagement' -tenant $TenantFilter -message 'running cve report' -sev 'info'
21-
} catch {
22-
Write-Host "Error retrieving CVEs from report database: $($_.Exception.Message)"
23-
$StatusCode = [HttpStatusCode]::InternalServerError
24-
$GraphRequest = $_.Exception.Message
25-
Write-LogMessage -API 'ListCVEManagement' -tenant $TenantFilter -message 'Error retrieving' -sev 'info'
26-
}
27-
} else {
28-
try {
29-
Write-LogMessage -API 'ListCVEManagement' -tenant $TenantFilter -message 'retrieving CVEs' -sev 'info'
30-
$GraphRequest = get-DefenderCVEs -TenantFilter $TenantFilter
31-
32-
# Retrieve Exceptions from Exception database
33-
$CveExceptionsTable = Get-CIPPTable -TableName 'CveExceptions'
34-
$AllExceptions = Get-CIPPAzDataTableEntity @CveExceptionsTable
35-
$ExceptionsByCve = @{}
36-
37-
# Retrieve CVEs from database
38-
$RawCveItems = $GraphRequest
39-
$AllCachedCves = $RawCveData
40-
41-
$TenantList = Get-Tenants | Where-Object defaultDomainName -EQ $TenantFilter
42-
43-
if ($RawCveItems.Count -eq 0) {
44-
return @()
45-
}
46-
47-
foreach ($Ex in $AllExceptions) {
48-
if ($TenantList.defaultDomainName -contains $Ex.customerId -or $Ex.customerId -eq 'ALL') {
49-
if (-not $ExceptionsByCve.ContainsKey($Ex.cveId)) {
50-
$ExceptionsByCve[$Ex.cveId] = [System.Collections.Generic.List[object]]::new()
51-
}
52-
53-
[void]$ExceptionsByCve[$Ex.cveId].Add([PSCustomObject]@{
54-
cveId = $Ex.cveId
55-
customerId = $Ex.customerId
56-
exceptionType = $Ex.exceptionType
57-
exceptionSource = $Ex.exceptionSource
58-
exceptionComment = $Ex.exceptionComment
59-
exceptionCreatedBy = $Ex.exceptionCreatedBy
60-
exceptionDate = $Ex.exceptionReadableDate
61-
exceptionExpiry = $Ex.exceptionExpiry
62-
})
63-
}
64-
}
65-
66-
# Merge all results
67-
$CveMasterTable = @{}
68-
69-
foreach ($Item in $RawCveItems) {
70-
$CveId = $Item.PartitionKey
71-
72-
if (-not $CveMasterTable.ContainsKey($CveId)) {
73-
$CveMasterTable[$CveId] = @{
74-
cveId = $CveId
75-
vulnerabilitySeverityLevel = $Item.vulnerabilitySeverityLevel
76-
exploitabilityLevel = $Item.exploitabilityLevel
77-
softwareName = $Item.softwareName
78-
softwareVendor = $Item.softwareVendor
79-
softwareVersion = $Item.softwareVersion
80-
lastUpdated = $Item.lastUpdated
81-
TotalDeviceCount = 0
82-
AffectedTenantsList = [System.Collections.Generic.List[object]]::new()
83-
AffectedDevicesList = [System.Collections.Generic.List[object]]::new()
84-
DiskPathList = [System.Collections.Generic.List[object]]::new()
85-
RegistryPathList = [System.Collections.Generic.List[object]]::new()
86-
ExceptionMatchCount = 0
87-
TotalTenantGroupCount = 0
88-
ExceptionSources = [System.Collections.Generic.HashSet[string]]::new()
89-
}
90-
}
91-
92-
$CveGroup = $CveMasterTable[$CveId]
93-
$CveGroup.TotalTenantGroupCount++
94-
95-
[void]$CveGroup.AffectedTenantsList.Add(@{ customerId = $Item.customerId })
96-
97-
# Unpack the device JSON details from the row
98-
if ($Item.deviceDetailsJson) {
99-
$Devices = ConvertFrom-Json $Item.deviceDetailsJson | Sort-Object -Property deviceName -Unique
100-
foreach ($Dev in $Devices) {
101-
[void]$CveGroup.AffectedDevicesList.Add(@{ deviceName = $Dev.deviceName })
102-
if ($Dev.registryPaths) {
103-
[void]$CveGroup.RegistryPathList.Add(@{ deviceName = $Dev.deviceName
104-
registryPaths = $Dev.registryPaths
105-
})
106-
}
107-
if ($Dev.diskPaths) {
108-
[void]$CveGroup.DiskPathList.Add(@{ deviceName = $Dev.deviceName
109-
diskPaths = $Dev.diskPaths
110-
})
111-
}
112-
$CveGroup.TotalDeviceCount ++
113-
}
114-
}
115-
}
116-
117-
# Combine filtered results
118-
$SortedCves = [System.Collections.Generic.List[PSCustomObject]]::new()
119-
120-
foreach ($CveKey in $CveMasterTable.Keys) {
121-
$Target = $CveMasterTable[$CveKey]
122-
$ExceptionStatus = 'None'
123-
$HasException = $false
124-
$Exceptions = @{}
125-
$ExceptionType = ''
126-
$ExceptionComment = ''
127-
$ExceptionCreatedBy = ''
128-
$ExceptionDate = ''
129-
$ExceptionExpiry = ''
130-
131-
if ($ExceptionsByCve.ContainsKey($CveKey)) {
132-
$Exceptions = @($ExceptionsByCve[$CveKey])
133-
$HasException = $true
134-
$ExceptionStatus = if ($Exceptions.customerId -contains 'ALL') { 'All' } else { 'Partial' }
135-
$ExceptionType = @{ customerId = $Exceptions.customerId
136-
exceptionType = $Exceptions.exceptionType
137-
}
138-
$ExceptionComment = @{ customerId = $Exceptions.customerId
139-
exceptionComment = $Exceptions.exceptionComment
140-
}
141-
$ExceptionCreatedBy = @{ customerId = $Exceptions.customerId
142-
exceptionCreatedBy = $Exceptions.exceptionCreatedBy
143-
}
144-
$ExceptionDate = @{ customerId = $Exceptions.customerId
145-
exceptionDate = $Exceptions.exceptionDate
146-
}
147-
$ExceptionExpiry = @{ customerId = $Exceptions.customerId
148-
exceptionExpiry = $Exceptions.exceptionExpiry
149-
}
150-
}
151-
152-
[void]$SortedCves.Add([PSCustomObject]@{
153-
cveId = $Target.cveId
154-
vulnerabilitySeverityLevel = $Target.vulnerabilitySeverityLevel
155-
exploitabilityLevel = $Target.exploitabilityLevel
156-
softwareName = $Target.softwareName
157-
softwareVendor = $Target.softwareVendor
158-
softwareVersion = $Target.softwareVersion
159-
deviceCount = $Target.TotalDeviceCount
160-
tenantCount = $Target.TotalTenantGroupCount
161-
registryPaths = $Target.RegistryPathList
162-
diskPaths = $Target.DiskPathList
163-
exceptionStatus = $ExceptionStatus
164-
hasException = $HasException
165-
affectedTenants = $Target.AffectedTenantsList
166-
affectedDevices = $Target.AffectedDevicesList
167-
exceptionType = $ExceptionType
168-
exceptionComment = $ExceptionComment
169-
exceptionCreatedBy = $ExceptionCreatedBy
170-
exceptionDate = $ExceptionDate
171-
exceptionExpiry = $ExceptionExpiry
172-
cacheTimeStamp = $Target.lastUpdated
173-
})
174-
$StatusCode = [HttpStatusCode]::OK
175-
}
176-
177-
} catch {
178-
Write-Host "Error retrieving CVEs: $($_.Exception.Message)"
179-
$StatusCode = [HttpStatusCode]::InternalServerError
180-
$SortedCves = $_.Exception.Message
15+
try {
16+
if ($UseReportDB -eq 'true') {
17+
$GraphRequest = Get-CIPPCVEReport -TenantFilter $TenantFilter -UseReportDB $UseReportDB -ErrorAction Stop
18+
} else {
19+
$GraphRequest = Get-CIPPCVEReport -TenantFilter $TenantFilter -UseReportDB 'false' -ErrorAction Stop
18120
}
21+
$StatusCode = [HttpStatusCode]::OK
22+
$SortedCves = $GraphRequest
23+
Write-LogMessage -API 'ListCVEManagement' -tenant $TenantFilter -message "running cve report" -sev 'info'
24+
} catch {
25+
Write-Host "Error retrieving CVEs from report database: $($_.Exception.Message)"
26+
$StatusCode = [HttpStatusCode]::InternalServerError
27+
$GraphRequest = $_.Exception.Message
28+
Write-LogMessage -API 'ListCVEManagement' -tenant $TenantFilter -message "Error retrieving CVEs: $GraphRequest" -sev 'info'
18229
}
183-
return [HttpResponseContext]@{
184-
StatusCode = $StatusCode
185-
Body = @($SortedCves | Sort-Object -Property cveId)
30+
31+
Return [HttpResponseContext]@{
32+
StatusCode = $StatusCode
33+
Body = @( $SortedCves )
18634
}
18735
}

0 commit comments

Comments
 (0)