-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpselasticsearch.psm1
More file actions
511 lines (458 loc) · 18.7 KB
/
pselasticsearch.psm1
File metadata and controls
511 lines (458 loc) · 18.7 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
function Ignore-certificate {
if($islinux){
$GLOBAL:PSDefaultParameterValues = @{"Invoke-Restmethod:SkipCertificateCheck"=$True}
}else{
Add-Type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
# Set Tls versions
$allProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $allProtocols
}
}
function Get-Elasticdata {
param(
$index,
$body,
$server=$ENV:ELASTICSERVER,
[string]$port = "9200",
[switch]$scroll,
[switch]$count,
$size = 100,
$simplequery,
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
if ($scroll) {
#Send query and get scroll id for retrieval
if ($simplequery -and !$body) {
#if check for simple or complex query
$scrollrequest = Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_search/?q=$simplequery&scroll=1m" -Method get -ContentType 'application/json' -Headers $header
}
else {
$scrollrequest = Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_search/?scroll=1m" -Body $body -Method post -ContentType 'application/json' -Headers $header
}
#build object for scroll result retrival
$scrollgetbody = [pscustomobject]@{
scroll = "1m"
scroll_id = "$($scrollrequest._scroll_id)"
} | ConvertTo-Json
#loop all scroll results
#output hits from initial request
[System.Collections.Generic.List[Object]]$_scroll_id += $scrollrequest._scroll_id
[System.Collections.Generic.List[Object]]$timed_out += $scrollrequest.timed_out.tostring()
[System.Collections.Generic.List[Object]]$_shards += $scrollrequest._shards
[System.Collections.Generic.List[Object]]$hits += $scrollrequest.hits
[System.Collections.Generic.List[Object]]$aggregations += $scrollrequest.aggregations
[int]$took += [int]$scrollreqresult.took #temp to calculate total time
do {
#$scrollreqresult=$null #reset variable so that end of results can be detected
$scrollreqresult = Invoke-RestMethod -Uri "$protocol`://$server`:$port/_search/scroll" -Body $scrollgetbody -Method post -ContentType 'application/json' -Headers $header #get scroll results 10 at a time
$_scroll_id += $scrollreqresult._scroll_id
$timed_out += if($scrollreqresult.timed_out){$scrollreqresult.timed_out.tostring()}else{$null}
$_shards += $scrollreqresult._shards
$hits += $scrollreqresult.hits
[int]$took += [int]$scrollreqresult.took #temp to calculate total time
#$scrollreqresult #output scroll results
}while ($scrollreqresult.hits.hits)#loop to output scroll results while there are results being delivered by elastic
[pscustomobject]@{
_scroll_id = $_scroll_id
took = $took
timed_out = $timed_out
_shards = $_shards
hits = $hits
aggregations = $aggregations
}
#Delete scroll after done
$scrolldeletebody=[pscustomobject]@{
scroll_id = "$($scrollrequest._scroll_id)"
} | ConvertTo-Json
Invoke-RestMethod -Uri "$protocol`://$server`:$port/_search/scroll" -Body $scrolldeletebody -Method Delete -ContentType 'application/json' -Headers $header
}
elseif ($count) {
#If count do query and return count
if ($simplequery -and !$body) {
#if check for simple or complex query
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_count/?q=$simplequery" -Method get -ContentType 'application/json' -Headers $header
}
else {
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_count/" -Body $body -Method post -ContentType 'application/json' -Headers $header
}
}
else {
#If no scroll do query and return specified number of results
if ($simplequery -and !$body) {
#if check for simple or complex query
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_search/?q=$simplequery&size=$size" -Method get -ContentType 'application/json' -Headers $header
}
else {
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_search/?size=$size" -Body $body -Method post -ContentType 'application/json' -Headers $header
}
}
}
function Convert-Elasticdata {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline)]$item,
$inputtype,
$resulttype
)
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
switch ($inputtype) {
netflow {
switch ($resulttype) {
toptalkers {
$item.aggregations.source.buckets | ForEach-Object {
[pscustomobject]@{
Host = $_.key
Megabytes = [math]::round($_.totalbytes.value / 1MB, 0)
}
}
}
topprotocols {
$item.aggregations.source.buckets | ForEach-Object {
[pscustomobject]@{
Protocol = $_.key
Megabytes = [math]::round($_.totalbytes.value / 1MB, 0)
}
}
}
}
}
pfsense {
switch ($resulttype) {
failedlogons {
$logon = $item.hits.hits._source | where { $_.message -like "*authentication error*" }
$failedlogons = $logon | ForEach-Object {
[pscustomobject]@{
username = $_.pfsense_USER
source_IP = $_.message.split(":")[2]
}
}
$failedlogons.username | sort | Get-Unique | ForEach-Object {
$tinput = $_
[pscustomobject]@{
username = $_
source_IP = ($failedlogons | where { $_.username -eq $tinput }).source_IP | Get-Unique
attempts = ($failedlogons | where { $_.username -eq $tinput }).count
}
}
}
openvpnfailedlogons {
$logon = $item.hits.hits._source
$logon | ForEach-Object {
[pscustomobject]@{
username = $_.message.split("'")[1]
time = $_."@timestamp" | get-date
}
}
}
firewallblocktop {
$item.aggregations.source.buckets | foreach-object {
[PSCustomObject]@{
IP = $_.key
Attempts = $_.doc_count
}
}
}
}
}
gcp {
switch ($resulttype) {
fileDLP {
$item.hits.hits._source | ForEach-Object {
[pscustomobject]@{
user = $_.protoPayload.authenticationInfo.principalEmail
path = "$($_.resource.labels.project_id)/$($_.protoPayload.resourceName)"
time = "$($_."@timestamp" | get-date)"
audit = $_.protoPayload.methodName
}
} | sort $_.path | Get-Unique -AsString
}
}
}
windows {
switch ($resulttype) {
failedlogons {
$events = $item.hits.hits._source | ForEach-Object {
[pscustomobject]@{
Username = $_.event_data.targetusername
Time = ($_."@timestamp" | get-date -Format "dd.MM.yyyy HH:mm:ss").ToString()
}
}
$uniquevents = $events | sort time | Get-Unique -AsString
#totalcount
[pscustomobject]@{
Username = "Total"
"Failed attempts" = $uniquevents.count
}
$uniquevents.username | sort | get-unique -asstring | ForEach-Object {
$tinput = $_
$count = ($uniquevents | where { $_.username -eq $tinput }).count
if (!$count) { $count = 1 } #For error where 1 count is recorded as NULL
[pscustomobject]@{
Username = $_
"Failed attempts" = $count
}
} | sort "Failed attempts" -Descending
}
fileDLP {
$item.hits.hits._source | ForEach-Object {
[pscustomobject]@{
user = $_.event_data.SubjectUserName
path = $_.event_data.ObjectName
time = "$($_."@timestamp" | get-date)"
audit = $_.keywords.replace("Audit ", "")
}
} | sort $_.path | Get-Unique -AsString
}
fsPerms {
$item.hits.hits._source | ForEach-Object {
$OldACLObject = New-Object -TypeName System.Security.AccessControl.DirectorySecurity
$OldACLObject.SetSecurityDescriptorSddlForm($_.event_data.oldsd)
$NewACLObject = New-Object -TypeName System.Security.AccessControl.DirectorySecurity
$NewACLObject.SetSecurityDescriptorSddlForm($_.event_data.newsd)
[PSCustomObject]@{
Changedby = $_.event_data.subjectusername
Item = $_.event_data.ObjectName
OldPerms = $OldACLObject
NewPerms = $NewACLObject
Diff = $newaclobject.AccessToString.split([Environment]::NewLine) | foreach-object {
$_ | where { $oldaclobject.AccessToString.split([Environment]::NewLine) -notcontains $_ }
}
}
}
}
adchanges {
$item.hits.hits._source | ForEach-Object {
[pscustomobject]@{
Time = ($_."@timestamp" | get-date).tostring()
Changedby = $_.event_data.subjectusername
TargetUser = if ($_.event_data.oldtargetusername) { "$($_.event_data.oldtargetusername)->$($_.event_data.newtargetusername)" }else { $_.event_data.targetusername }
EventID = $_.event_id
Reason = $_.message.split([Environment]::NewLine)[0]
}
}
}
}
}
}
}
function New-Elasticindex{
param(
$index,
$shards=1,
$replicas=0,
$server=$ENV:ELASTICSERVER,
[string]$port = "9200",
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
$body= @{
settings =@{
number_of_shards = $shards
number_of_replicas = $replicas
}
} | ConvertTo-json
if($body){
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index" -Headers $header -Method put -ContentType 'application/json' -Body $body
}
}
function Get-Elasticindex{
param(
$server=$ENV:ELASTICSERVER,
[string]$port = "9200",
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$index,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
#Do webrequest for index list
if($index){Invoke-RestMethod -Uri "$protocol`://$server`:$port/_cat/indices/$index" -Headers $header -Method Get -ContentType 'application/json'}
else{
Invoke-RestMethod -Uri "$protocol`://$server`:$port/_cat/indices" -Headers $header -Method Get -ContentType 'application/json'
}
}
function Add-ElasticData{
param(
$index,
$body,
$server=$ENV:ELASTICSERVER,
[string]$port = "9200",
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$CreateIndexIfNotExist,
[switch]$ForceRefresh,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
if($CreateIndexIfNotExist){
if((Get-Elasticindex -server $server -port $port -username $username -password $password -index $index | where {$_ -like "*$index*"}).count -eq 1){
#index exists, do nothing
}else{
New-Elasticindex -server $server -port $port -username $username -password $password -index $index | out-null
}
}
if($body -and $ForceRefresh){
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/doc?refresh=wait_for" -Headers $header -Method post -ContentType 'application/json' -Body $body
}
elseif($body){
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/doc" -Headers $header -Method post -ContentType 'application/json' -Body $body
}
}
function Set-ElasticData{
param(
$index,
$body,
$server=$ENV:ELASTICSERVER,
$docid,
[string]$port = "9200",
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$CreateDocIfNotExist,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
if($body){
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_update/$docid" -Headers $header -Method post -ContentType 'application/json' -Body $body
}
}
function Remove-Elasticdoc{
param(
$index,
$server=$ENV:ELASTICSERVER,
$docid,
[string]$port = "9200",
[switch]$https,
$username=$ENV:ELASTICUSER,
$password=$ENV:ELASTICPASSWORD,
$path #Add support for path in the url
)
if($path){
$port="$port$path"
}
if ($ENV:ELASTICIGNORECERT){
if($islinux -or $IsMacOS){$PSDefaultParameterValues = @{"Invoke-RestMethod:SkipCertificateCheck"=$True}}else{Ignore-certificate}
}
#Set protocol for requests
If(($https) -or ($ENV:ELASTICHTTPS -EQ "TRUE")){
$protocol="https"
}else{$protocol="http"}
#if username and password is provided
if($username -and $password){
$server="$username`:$password@$server"
}
#cCreate header for auth
$header= @{
Authorization = "Basic $(ConvertTo-Base64 "$username`:$password")"
}
if($docid){
Invoke-RestMethod -Uri "$protocol`://$server`:$port/$index/_doc/$docid" -Headers $header -Method delete -ContentType 'application/json'
}
}