forked from Infocyte/PowershellTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImport-HuntICLZs.ps1
358 lines (328 loc) · 13.4 KB
/
Import-HuntICLZs.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
# Script to upload manual .iclz file to hunt server.
Param(
[Parameter( Position = 0,
Mandatory = $true)]
[String]
$Path, # <folder containing the .iclz files to upload>
[String]
$TargetListName = "OfflineScans",
[String]
$Target = "localhost",
[String]
$HuntServer = "https://localhost:4443",
[PSCredential]
[System.Management.Automation.Credential()]
$HuntCredential = [System.Management.Automation.PSCredential]::Empty,
[PSCredential]
[System.Management.Automation.Credential()]
$ScanCredential = [System.Management.Automation.PSCredential]::Empty
)
Write-Host "PSVersion Check: $($PSVersionTable.PSVersion.tostring())"
$UploadDir = "C:\Program Files\Infocyte\Hunt\uploads"
# Automatically import the Infocyte API calls
# Makes it easier for users, so they don't have to do this separately
if (Get-Command New-ICToken -errorAction SilentlyContinue) {
# InfocyteAPIFunctions.ps1 already imported
} else {
if (Test-Path -Path "$PSScriptRoot\InfocyteAPIFunctions.ps1") {
Write-Host "Importing Infocyte API Functions ($PSScriptRoot\InfocyteAPIFunctions.ps1)"
. "$PSScriptRoot\InfocyteAPIFunctions.ps1"
} else {
Write-Host -ForegroundColor Red "You must import the InfocyteAPIFunctions.ps1 script."
Write-Host -ForegroundColor Red "Include it in the same folder as this script, and rerun this script with the same parameters."
return
}
}
if (-NOT (Test-Path -Path $UploadDir)) {
Write-Host -ForegroundColor Red "You are not on the Hunt Server. You must run this script on the Hunt server."
return
}
if (Test-Path $Path -PathType Container) {
if (-NOT (Get-ChildItem $Path -filter *.iclz)) {
Write-Host -ForegroundColor Red "ERROR: $Path does not contain .iclz files"
return
}
} else {
Write-Host -ForegroundColor Red "ERROR: $Path is not a directory"
return
}
# Hardcoded Credentials (unsafe in production but convenient for testing)
# Infocyte Credentials
# If a user did not add their credentials, use the default ones.
if ($HuntCredential -eq [System.Management.Automation.PSCredential]::Empty) {
$username = 'infocyte'
$password = 'hunt' | ConvertTo-SecureString -asPlainText -Force
$Script:HuntCredential = New-Object System.Management.Automation.PSCredential($username,$password)
}
# Query Credentials (Scanning Admin/Service Account)
# If a user did not add their credentials, use the default ones.
# This will not work unless it is on that specific machine, so make sure you add your credentials at the beginning.
if ($ScanCredential -eq [System.Management.Automation.PSCredential]::Empty) {
$username = 'galactica.int\administrator'
$password = 'hunt' | ConvertTo-SecureString -asPlainText -Force
$Script:ScanCredential = New-Object System.Management.Automation.PSCredential($username,$password)
}
if (-NOT (Test-Path $Path)) {
Write-Host -ForegroundColor Red "Path does not exist, place your ICLZ files in $Path"
return
}
elseif (-NOT (Get-ChildItem -Recurse -Path $Path -Filter *.iclz)) {
Write-Host -ForegroundColor Red "Path does not contain any .ICLZ files"
return
}
# Create new login Token and add it to Script variable
Write-Host "Connecting to $HuntServer using account $($HuntCredential.username)"
$NewToken = New-ICToken $HuntCredential $HuntServer
if ($NewToken.id) {
Write-Host "Login successful to $HuntServer"
Write-Host "Login Token id: $($NewToken.id)"
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
return
}
# Error if token no longer valid is:
# WARNING: Error: The underlying connection was closed: An unexpected error occurred on a send.
# Get Target List.
$TargetList = Get-ICTargetList
if ($TargetList -like "Error:*") {
Write-Host -ForegroundColor Red "$TargetList"
return
} else {
$TargetList = $TargetList | Where-Object { $_.name -eq $TargetListName -AND $_.deleted -eq $False}
if ($TargetList) {
Write-Host "TargetList $TargetListName is already created"
$TargetListId = $TargetList[0].id
} else {
# If our specified list isn't there, create it.
Write-Host "Creating TargetList named $TargetListName"
$TargetListId = (New-ICTargetList $TargetListName).id
}
}
# Get Credentials
$CredObjects = Get-ICCredentials
if ($CredObjects -like "Error:*") {
Write-Host -ForegroundColor Red "$CredObjects"
return
} else {
$CredObjects = $CredObjects | where { $_.name -eq "HuntLocal"}
if ($CredObjects) {
Write-Host "HuntLocal Credential is already loaded. Check the HUNT interface if you need to change the password or account."
$CredentialId = $CredObjects[0].id
} else {
#Create new Credential for target
Write-Host "Creating new Credential for the local Hunt Server: $($ScanCredential.username)"
$CredentialId = (New-ICCredential -Name "HuntLocal" -Cred $ScanCredential).id
}
}
# Get Queries
$Queries = Get-ICQuery $TargetListId
if ($Queries -like "Error:*") {
Write-Host -ForegroundColor Red "$Queries"
return
} else {
$Queries = $Queries | where { $_.value -eq $Target -AND $_.credentialId -eq $CredentialId}
if ($Queries) {
Write-Host "Query already created for $Target within TargetList $TargetListId"
$QueryId = $Queries[0].id
} else {
#Create new Query for target
Write-Host "Creating new Query for: $Target within TargetList $TargetListId"
$QueryId = (New-ICQuery -targetListId $TargetListId -credentialId $CredentialId -query $Target).id
}
}
# Initiate Enumeration
Write-Host "Enumerating $Target"
$enumtime = get-date
Invoke-ICEnumeration $TargetListId $QueryId
Start-Sleep 1
# Track Status of Enumeration
$active = $true
Write-Host "Waiting for enumeration to complete"
Write-Progress -Activity "Enumerating Target" -status "Initiating Enumeration"
while ($active) {
Start-Sleep 1
$status = Get-ICUserTasks
if ($status -like "Error:*") {
Write-Host -ForegroundColor Red "Error on Get-ICUserTasks: $status"
Write-Warning "Attempting to re-connecting to $HuntServer"
$NewToken = New-ICToken $HuntCredential $HuntServer
if ($NewToken.id) {
Write-Host "Login successful to $HuntServer"
Write-Host "Login Token id: $($NewToken.id)"
continue
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
return
}
} elseif ($status) {
$status = $status | Where-Object { $_.userid -eq $NewToken.userid -AND $_.type -eq "Enumerate" -AND [datetime]$_.createdon -gt $enumtime}
} else {
Write-Host -ForegroundColor Red "Error on Get-ICUserTasks: No Jobs have been started..."
Start-Sleep 1
continue
}
if ($status) {
$lastStatus = $status[0].message
if ($Status.message -match "error") {
$active = $false
Write-Host -ForegroundColor Red "ERROR: Could not enumerate Target: $($Status.message)"
return "ERROR: Could not enumerate Target: $($Status.message)"
}
if ($status.progress) {
$elapsedtime = "$($($status.elapsed)/1000)"
Write-Progress -Activity "Enumerating Target" -status "[Elapsed (seconds): $elapsedtime] $($status.message)" -percentComplete ($status.progress)
}
if ($status.status -eq "Completed") {
$active = $false
Write-Host "Enumeration Complete: $($lastStatus)"
}
} else {
Write-Host -ForegroundColor Red "Unhandled Error on enumeration Get-ICUserTasks: $Status"
$active = $false
}
}
Start-Sleep 1
$TargetListResults = Get-ICAddresses $TargetListId
if ($TargetListResults) {
if ($TargetListResults.accessibleAddressCount -eq 0) {
$failreason = (Get-ICAddresses $TargetListId).failureReason
Write-Host -ForegroundColor Red "ERROR: Enumeration was not successful ($failreason). Please check your ScanCredentials for the hunt server (HuntLocal) localhost within the Infocyte HUNT UI Credential Manager and try again"
return "ERROR: Enumeration was not successful ($failreason)"
} else {
Write-Host "Enumeration Successful!"
}
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get target list"
return
}
#Copy .iclz files into upload folder (temp dir)
# $LastFolder = (Get-ChildItem $UploadDir | Sort-Object LastWriteTime -Descending)[0].Name
$TempFolderName = "temp$([guid]::NewGuid())"
$iclznum = get-childitem $Path -filter *.iclz -recurse
Write-Host "Copying folder of $($iclznum.count) .iclz files from $Path to staging temp directory: $UploadDir\$TempFolderName"
try {
Copy-Item -Path $Path -Destination "$UploadDir\$TempFolderName" -recurse -ErrorAction Stop
} catch {
Write-Host -ForegroundColor Red "ERROR: Could not copy files from $Path to the infocyte upload directory: $UploadDir"
Write-Host -ForegroundColor Red "ERROR: $_"
return
}
<#
# TODO: Change this to grab the iclz files only and rename them using their md5 hash so we're not uploading the same iclz file twice (which would break everything)
Get-ChildItem $Path -filter *.iclz | Foreach-Object {
$newhash = (Get-Hashes -Path $_ -Type MD5).md5
Copy-Item -Path $_ -Destination $UploadDir\$TempFolderName\Survey-$newhash.json.iclz -recurse -Container
}
#>
$baseScanId = "NO_SCAN"
$scanId = $baseScanId
#Write-Host "Last Active ScanId: $baseScanId (Should say NO_SCAN if no scan is currently running)"
# Initiate Scan
Write-Host "Initiating Scan of $Target"
$scantime = get-date
$ScanTask = Invoke-ICScan $TargetListId
$ScanTask
Start-Sleep 1
if ($ScanTask -like "Error:*") {
Write-Host -ForegroundColor Red "Error on Invoke-ICScan: $ScanTask"
Write-Warning "Attempting to re-connecting to $HuntServer"
$NewToken = New-ICToken $HuntCredential $HuntServer
if ($NewToken.id) {
Write-Host "Login successful to $HuntServer"
Write-Host "Login Token id: $($NewToken.id)"
$ScanTask = Invoke-ICScan $TargetListId
Start-Sleep 1
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
return
}
}
# Wait for new scan to be created
$scanId = $baseScanId
while ($scanId -eq $baseScanId) {
Start-Sleep 1
$ScanJobs = Get-ICUserTasks
if ($ScanJobs -like "ERROR:*") {
Write-Host -ForegroundColor Red "Error on Get-ICUserTasks: $ScanJobs"
Write-Warning "Attempting to re-connecting to $HuntServer"
$NewToken = New-ICToken $HuntCredential $HuntServer
if ($NewToken.id) {
Write-Host "Login successful to $HuntServer"
Write-Host "Login Token id: $($NewToken.id)"
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
return "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
}
} elseif ($ScanJobs) {
$ScanJobs = $ScanJobs | Sort-Object timestamp -Descending | where { $_.userid -eq $NewToken.userid -AND [datetime]$_.createdon -gt $scantime -AND ($_.type -eq "Scan")}
if ($ScanJobs -AND ($ScanJobs -match "Error")) {
Write-Host -ForegroundColor Red "Error on scan. Check error message and investigate scan failure in HUNT server logs:"
Write-Host -ForegroundColor Red $ScanJobs
return "Error on last scan job. Check error message and investigate scan failure in HUNT server logs"
}
if ($ScanJobs | Where-Object { $_.status -eq "Active"}) {
$scanId = $ScanJobs[0].options.scanid
Write-Host "New ScanId created! Now: $scanId"
} else {
Write-Host "Waiting for new ScanId to be created... ScanID is currently $scanID as of $(Get-Date)"
}
} else {
Write-Host -ForegroundColor Red "No Active Scan! Waiting for scan to be initiated..."
}
}
Write-Host "Renaming $UploadDir\$TempFolderName Directory to $UploadDir\$ScanId"
if (Test-Path $UploadDir\$ScanId) {
Write-Host -ForegroundColor Red "Folder $UploadDir\$ScanId already exists!"
} else {
try {
Rename-Item -path $UploadDir\$TempFolderName -newname $ScanId -ErrorAction Stop
} catch {
Write-Host -ForegroundColor Red "ERROR: Could not rename temp folder ($UploadDir\$TempFolderName --> $UploadDir\$ScanId), survey results will not be processed"
Write-Host -ForegroundColor Red "$_"
return $_
}
}
Write-Host "Your HostSurvey results will be processed as the current scan of TargetList $TargetListName moves to the processing phase."
Start-Sleep 1
# Track Status of Scan processing
$active = $true
while ($active) {
Start-Sleep 0.5
$status = Get-ICUserTasks
if ($status -like "Error:*") {
Write-Host -ForegroundColor Red "$Status"
Write-Warning "Attempting to re-connecting to $HuntServer"
$NewToken = New-ICToken $HuntCredential $HuntServer
if ($NewToken.id) {
Write-Host "Login successful to $HuntServer"
Write-Host "Login Token id: $($NewToken.id)"
continue
} else {
Write-Host -ForegroundColor Red "ERROR: Could not get a token from $HuntServer using credentials $($HuntCredential.username)"
return
}
}
$status = $status | Where-Object { $_.options.ScanId -eq $scanId }
if ($status.status -eq "Active") {
$elapsedtime = ((Get-Date) - [datetime]$status.createdOn).TotalSeconds
$statusmessage = "[Elapsed (seconds): {0:N2} ] {1}" -f $elapsedtime, $status.message
if ($status.progress) {
Write-Progress -Activity "Waiting for scan to process" -status $statusmessage -percentComplete ($status.progress)
} else {
Write-Progress -Activity "Waiting for scan to process" -status $statusmessage
}
} else {
if ($status.status -eq "Error") {
Write-Host -ForegroundColor Red "ERROR: Could not complete scan and analysis."
Write-Host -ForegroundColor Red "$status.message"
$active = $false
} elseif ($status.status -eq "Completed") {
Write-Host "Scan Completed in $elapsedtime seconds"
$active = $false
} else {
Write-Host -ForegroundColor Red "[Unhandled Error] Something went wrong..."
Write-Host -ForegroundColor Red "$status.message"
$status
}
}
}