-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathendpoint-api-svbm.ps1
More file actions
104 lines (81 loc) · 3.59 KB
/
Copy pathendpoint-api-svbm.ps1
File metadata and controls
104 lines (81 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<# CIAOPS
Script provided as is. Use at own risk. No guarantees or warranty provided.
Description - Connect to the Defender for Endpoint API and return software vulnerabilities by machine
Source - https://github.com/directorcia/Office365/blob/master/endpoint-api-svbm.ps1
Documentation - https://blog.ciaops.com/2021/06/15/using-the-defender-for-endpoint-api-and-powershell/
Prerequisites = 1
1. Azure AD app setup per - https://blog.ciaops.com/2019/04/17/using-interactive-powershell-to-access-the-microsoft-graph/
2. Pass Client Id, Tenant Id and Client Secret as parameters when running the script
More scripts available by joining http://www.ciaopspatron.com
#>
param(
[Parameter(Mandatory = $true)]
[string]$ClientId,
[Parameter(Mandatory = $true)]
[string]$TenantId,
[Parameter(Mandatory = $true)]
[SecureString]$ClientSecret,
[Parameter(Mandatory = $false)]
[string]$CsvOutput
)
## Variables
$systemmessagecolor = "cyan"
$processmessagecolor = "green"
$errormessagecolor = "red"
Clear-Host
Write-Host -ForegroundColor $systemmessagecolor "Script started`n"
try {
# Decode the secure client secret for the token request body
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($ClientSecret)
$plainSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
# Construct token URI and body
$tokenUri = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
$tokenBody = @{
client_id = $ClientId
scope = "https://api.securitycenter.microsoft.com/.default"
client_secret = $plainSecret
grant_type = "client_credentials"
}
Write-Host -ForegroundColor $processmessagecolor "Getting OAuth 2.0 token"
$tokenResponse = Invoke-RestMethod -Method Post -Uri $tokenUri -ContentType "application/x-www-form-urlencoded" -Body $tokenBody -ErrorAction Stop
$token = $tokenResponse.access_token
}
catch {
Write-Host -ForegroundColor $errormessagecolor "Failed to acquire token: $($_.Exception.Message)"
exit 1
}
finally {
if ($bstr) { [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) }
}
$headers = @{ Authorization = "Bearer $token" }
$apiUri = "https://api.securitycenter.microsoft.com/api/machines/SoftwareVulnerabilitiesByMachine"
$allResults = [System.Collections.Generic.List[object]]::new()
try {
Write-Host -ForegroundColor $processmessagecolor "Querying Defender for Endpoint API (with pagination)"
do {
$response = Invoke-RestMethod -Method Get -Uri $apiUri -ContentType "application/json" -Headers $headers -ErrorAction Stop
if ($response.value) {
$allResults.AddRange($response.value)
}
$apiUri = $response.'@odata.nextLink'
} while ($apiUri)
}
catch {
Write-Host -ForegroundColor $errormessagecolor "API query failed: $($_.Exception.Message)"
exit 1
}
Write-Host -ForegroundColor $processmessagecolor "Total records returned: $($allResults.Count)"
$selected = $allResults |
Select-Object DeviceName, CveId, LastSeenTimestamp, SoftwareName, SoftwareVendor, SoftwareVersion, VulnerabilitySeverityLevel |
Sort-Object DeviceName, LastSeenTimestamp
$selected | Format-Table -AutoSize
if ($CsvOutput) {
try {
$selected | Export-Csv -Path $CsvOutput -NoTypeInformation -Encoding UTF8
Write-Host -ForegroundColor $processmessagecolor "Results exported to: $CsvOutput"
}
catch {
Write-Host -ForegroundColor $errormessagecolor "CSV export failed: $($_.Exception.Message)"
}
}
Write-Host -ForegroundColor $systemmessagecolor "`nScript Completed`n"