-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSophosCentral-Powershell.ps1
406 lines (295 loc) · 13.6 KB
/
SophosCentral-Powershell.ps1
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
### function ###
function Toggle-TamperProtection {
param (
## hostname ##
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[string]
$computerName
,
[Parameter(Mandatory=$false)]
[string]
$csv
,
[Parameter(Mandatory=$false)]
[string]
$all
,
[Parameter(Mandatory=$false)]
[switch]
$enable
,
[Parameter(Mandatory=$false)]
[switch]
$disable
)
$sophosApiResponse = Authenticate-SophosApi
if ($disable) {
$promptUserMessage = "disabling"
$json = @{"enabled" = "false"} | ConvertTo-Json
}
elseIf ($enable) {
$promptUserMessage = "enabling"
$json = @{"enabled" = "true"} | ConvertTo-Json
}
else {
Write-Host "[ERROR] please supply -enable or -disable flags when toggling tamper protection"
return
}
if ($all) {
$promptUser = Read-Host "$($promptUserMessage) + tamper protection from all devices. Press 'y' to continue."
if($promptUser -eq 'y') {
$endpoints = Get-SophosEndpoints -sophosApiResponse $sophosApiResponse
foreach ($endpoint in $endpoints) {
$endpointId = $endpoint.id
# build the uri for removing tamper protection from the specified $ComputerName (requires the $endpointId)
$uri = ($sophosApiResponse['dataRegionApiUri'] + "/endpoint/v1/endpoints/" + $endpointId + "/tamper-protection")
try {
# api request to toggle tamper protection
$tamperProtectionToggleResponse = Invoke-RestMethod -Method Post -Headers @{Authorization="Bearer $($sophosApiResponse['token_resp'].access_token)"; "X-Tenant-ID"=$sophosApiResponse['whoami_resp'].id} -ContentType "application/json" -Body $json -Uri $uri
Write-Host "$($promptUserMessage) tamper protection on device: $($endpoint.hosts)"
Start-Sleep -Milliseconds 250
}
catch {
Write-Warning "Failed to toggle tamper protection for device: $($endpoint.hosts) with id: $($endpointId)"
Write-Warning $Error[0]
}
}
}
else {
Write-Host "exiting..."
return
}
}
if ($csv) {
$endpointsCsv = Import-SophosEndpointHostList -csv $csv
$sophosEndpoints = Get-SophosEndpoints -sophosApiResponse $sophosApiResponse
foreach ($endpoint in $endpointsCsv) {
#Write-Host $endpoint.hosts
$endpointId = Get-SophosEndpointId -computerName $endpoint.hosts -sophosApiResponse $sophosApiResponse -sophosEndpoints $sophosEndpoints
# build the uri for removing tamper protection from the specified $ComputerName (requires the $endpointId)
$uri = ($sophosApiResponse['dataRegionApiUri'] + "/endpoint/v1/endpoints/" + $endpointId + "/tamper-protection")
try {
# api request to toggle tamper protection
$tamperProtectionToggleResponse = Invoke-RestMethod -Method Post -Headers @{Authorization="Bearer $($sophosApiResponse['token_resp'].access_token)"; "X-Tenant-ID"=$sophosApiResponse['whoami_resp'].id} -ContentType "application/json" -Body $json -Uri $uri
Write-Host "$($promptUserMessage) tamper protection on device: $($endpoint.hosts)"
Start-Sleep -Milliseconds 250
}
catch {
Write-Warning "Failed to toggle tamper protection for device: $($endpoint.hosts) with id: $($endpointId)"
Write-Warning $Error[0]
}
}
}
elseIf ($computerName) {
# get the associated endpointId for the specified $ComputerName
$endpointId = Get-SophosEndpointId $computerName
if ($endpointId -eq $null) {
return
}
# build the uri for removing tamper protection from the specified $ComputerName (requires the $endpointId)
$uri = ($sophosApiResponse['dataRegionApiUri'] + "/endpoint/v1/endpoints/" + $endpointId + "/tamper-protection")
# api request to remove tamper protection
try {
$tamperProtectionToggleResponse = Invoke-RestMethod -Method Post -Headers @{Authorization="Bearer $($sophosApiResponse['token_resp'].access_token)"; "X-Tenant-ID"=$sophosApiResponse['whoami_resp'].id} -Uri $uri -ContentType "application/json" -Body $json
}
catch {
Write-Warning "Failed to toggle tamper protection for device: $($computerName) with id: $($endpointId)"
Write-Warning $Error[0]
}
Write-Host "$($promptUserMessage) tamper protection on device: $($computerName)"
}
else {
Write-Host "no endpoints specified"
return
}
}
### function ###
function Get-SophosEndpoints {
param (
[Parameter(Mandatory=$false)]
$sophosApiResponse
,
[Parameter(Mandatory=$false)]
[switch]
$export
)
if (!($sophosApiResponse)) {
$sophosApiResponse = Authenticate-SophosApi
}
#Write-Host "token response is: $($sophosApiResponse["token_resp"].access_token)"
#Write-Host "whoami response: $($sophosApiResponse["whoami_resp"].id)"
#Write-Host "data region uri: $($sophosApiResponse["dataRegionApiUri"])"
$endpoint_key = $sophosApiResponse["endpoints_resp"].pages.nextKey
$sophosEndpoints = @()
$sophosEndpoints_noDupes = @()
Write-Host "grabbing updated list of endpoints from sophos api..."
Do {
#Write-Host $endpoint_key
$endpoints_resp = Invoke-RestMethod -Method Get -Headers @{Authorization="Bearer $($sophosApiResponse["token_resp"].access_token)"; "X-Tenant-ID"=$sophosApiResponse["whoami_resp"].id} ($($sophosApiResponse["dataRegionApiUri"])+"/endpoint/v1/endpoints?pageSize=500&pageTotal=true&pageFromKey=$($endpoint_key)")
# enumerate results and append to csv
$sophosEndpoints += @($endpoints_resp.items |
Select-Object -Property id,type,hostname,health,os,
@{name="ipv4Addresses"; expression={$_.ipv4Addresses | select -First 1}},
@{name="ipv6Addresses"; expression={$_.ipv6Addresses | Select -First 1}},
@{name="macAddresses"; expression={$_.macAddresses | Select -First 1}},
associatedPerson,tamperProtectionEnabled,
@{name="endpointProtection"; expression={$_.assignedProducts[0] | Where-Object -Property code -eq -Value "endpointProtection"}},
@{name="interceptX"; expression={$_.assignedProducts[1] | Where-Object -Property code -eq -Value "interceptX"}},
@{name="coreAgent"; expression={$_.assignedProducts[2] | Where-Object -Property code -eq -Value "coreAgent"}},
lastSeenAt)
#Write-Host $endpoints_resp.items
Start-Sleep -Seconds 2
$endpoint_key = $endpoints_resp.pages.nextKey
}
While ($endpoints_resp.pages.fromKey -ne "")
##----##
## remove duplicate devices ##
Write-Host "removing duplicates...."
# sort the endpoints using the "lastSeenAt" property descending and then group endpoints with the same hostname
$endpoints_grouped_duplicates_sorted = $sophosEndpoints | Sort-Object {$_."lastSeenAt" -as [datetime]} -Descending | Group-Object "hostname"
ForEach ($endpoint_group in $endpoints_grouped_duplicates_sorted) {
# expand group of duplicate endpoint objects
$duplicates = Select-Object -InputObject $endpoint_group -ExpandProperty "Group"
# select the unique endpoint from the duplicates
$unique_endpoint = $duplicates | select -First 1
$sophosEndpoints_noDupes += $unique_endpoint
}
if ($export) {
$sophosEndpoints_noDupes = $sophosEndpoints_noDupes | Export-Csv -Path .\endpoints.csv -NoTypeInformation -Encoding UTF8
} else {
$sophosEndpoints_noDupes = $sophosEndpoints_noDupes | sort "hostname"
}
return $sophosEndpoints_noDupes
}
### function ###
function Get-SophosEndpointId {
param (
[Parameter(Mandatory=$true,Position=0)]
[String]
$computerName
,
[Parameter(Mandatory=$false)]
$sophosApiResponse
,
[Parameter(Mandatory=$false)]
$sophosEndpoints
)
if (!($sophosEndpoints)) {
$sophosEndpoints = Get-SophosEndpoints
}
# loop through all devices on sophos to find matching id for current device
ForEach ($endpoint in $sophosEndpoints) {
#Write-Host $endpoint
if($computerName -eq $endpoint.hostname) {
return $endpoint.id
}
}
Write-Host "Device: $computerName not found. Please check spelling."
return
}
function Authenticate-SophosApi {
try {
$apiCredentials = Read-Host "Enter path to sophos api credentials file (\path\to\filename.json)"
$apiCredentials = Get-Content $apiCredentials | ConvertFrom-Json
} catch {
Write-Error "[ERROR] api credential file not found or is formatted improperly"
}
$client_id = $apiCredentials.client_id
$client_secret = $apiCredentials.client_secret
$sophosApiResponse = @{}
Write-Host "Authenticating with Sophos API...."
# authenticate with sophos (returns time/scope limited java web token)
$token_resp = Invoke-RestMethod -Method Post -ContentType "application/x-www-form-urlencoded" -Body "grant_type=client_credentials&client_id=$client_id&client_secret=$client_secret&scope=token" -Uri https://id.sophos.com/api/v2/oauth2/token
$whoami_resp = Invoke-RestMethod -Method Get -Headers @{Authorization="Bearer $($token_resp.access_token)"} https://api.central.sophos.com/whoami/v1
$dataRegionApiUri = $whoami_resp.apiHosts.dataRegion
# Get all endpoints within the tenant
$endpoints_resp = Invoke-RestMethod -Method Get -Headers @{Authorization="Bearer $($token_resp.access_token)"; "X-Tenant-ID"=$whoami_resp.id} ($($whoami_resp.apiHosts.dataRegion)+"/endpoint/v1/endpoints")
$sophosApiResponse["token_resp"] = $token_resp
$sophosApiResponse["whoami_resp"] = $whoami_resp
$sophosApiResponse["endpoints_resp"] = $endpoints_resp
$sophosApiResponse["dataRegionApiUri"] = $dataRegionApiUri
return $sophosApiResponse
}
# authenticate with sophos api and store api session info in $sophosApiResponse hashTable
#$sophosApiResponse = Authenticate-SophosApi
# update list of all sophos endpoints (requires sophosApiResponse as argument)
# updated list of endpoints will be placed in .\endpoints.csv
function Import-SophosEndpointHostList {
Param([string]$csv)
if ($csv) {
$isValidPath = (Test-Path $csv -PathType Leaf)
if ($isValidPath -eq $false) {
Write-Host "unable to load specified csv file. Please check to make sure it exists."
return
}
try {
$hostListCsv = Import-Csv -Path $csv -Encoding UTF8
}
catch {
Write-Error "Error Importing Csv"
}
if ($hostListCsv[0].psobject.properties.name -cne "hosts") {
Write-Error "csv file must use the field header 'hosts'"
return
}
return $hostListCsv
}
else {
Write-Error "No csv file provided"
return
}
}
#Remove-Item .\endpoints.csv
#Get-SophosEndpoints $sophosApiResponse
#$updatedEndpointsList = Import-Csv -Path .\endpoints.csv -Encoding UTF8
function Check-TamperProtectionStatus {
Param (
[Parameter(Mandatory=$false)]
[string]
$csv
,
[Parameter(Mandatory=$false,ValueFromPipeline=$true)]
[String]
$computerName
,
[Parameter(Mandatory=$false)]
$sophosApiResponse
,
[Parameter(Mandatory=$false)]
$sophosEndpoints
)
$sophosApiResponse = Authenticate-SophosApi
$sophosEndpoints = Get-SophosEndpoints -sophosApiResponse $sophosApiResponse
if($csv) {
$hostListCsv = Import-SophosEndpointHostList -csv $csv
ForEach ($hostname in $hostListCsv) {
ForEach ($endpoint in $sophosEndpoints) {
$tamperProtectionEnabled = [bool]::Parse($endpoint.tamperProtectionEnabled)
#Write-Host $endpoint.hostname
#Write-Host $hostname.hosts
if($endpoint.hostname -eq $hostname.hosts -And $tamperProtectionEnabled -eq $false) {
Write-Host "Tamper Protection is DISABLED for $($hostname.hosts)"
}
elseif ($endpoint.hostname -eq $hostname.hosts -And $tamperProtectionEnabled -eq $true) {
Write-Host "Tamper Protection is ENABLED for $($hostname.hosts)"
}
}
}
}
elseif($computerName) {
ForEach ($endpoint in $sophosEndpoints) {
$tamperProtectionEnabled = [bool]::Parse($endpoint.tamperProtectionEnabled)
#Write-Host $endpoint.hostname
#Write-Host $hostname.hosts
if($endpoint.hostname -eq $computerName -And $tamperProtectionEnabled -eq $false) {
Write-Host "Tamper Protection is DISABLED for $($computerName)"
}
elseif ($endpoint.hostname -eq $computerName -And $tamperProtectionEnabled -eq $true) {
Write-Host "Tamper Protection is ENABLED for $($computerName)"
}
}
}
else {
Write-Error "missing computerName or csv"
}
}