-
Notifications
You must be signed in to change notification settings - Fork 0
/
LRWC Log Beautify.ps1
501 lines (434 loc) · 22.5 KB
/
LRWC Log Beautify.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
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
# Banner start
Write-Host ""
Write-Host -ForegroundColor White '888 8888888b. 888 888 .d8888b. 888 888888b. 888 d8b .d888 '
Write-Host -ForegroundColor White '888 888 Y88b 888 o 888 d88P Y88b 888 888 "88b 888 Y8P d88P" '
Write-Host -ForegroundColor White '888 888 888 888 d8b 888 888 888 888 888 .88P 888 888 '
Write-Host -ForegroundColor White '888 888 d88P 888 d888b 888 888 888 .d88b. .d88b. 8888888K. .d88b. 8888b. 888 888 888888 888 888888 888 888 '
Write-Host -ForegroundColor White '888 8888888P" 888d88888b888 888 888 d88""88b d88P"88b 888 "Y88b d8P Y8b "88b 888 888 888 888 888 888 888 '
Write-Host -ForegroundColor White '888 888 T88b 88888P Y88888 888 888 888 888 888 888 888 888 888 88888888 .d888888 888 888 888 888 888 888 888 '
Write-Host -ForegroundColor White '888 888 T88b 8888P Y8888 Y88b d88P 888 Y88..88P Y88b 888 888 d88P Y8b. 888 888 Y88b 888 Y88b. 888 888 Y88b 888 '
Write-Host -ForegroundColor White '88888888 888 T88b 888P Y888 "Y8888P" 88888888 "Y88P" "Y88888 8888888P" "Y8888 "Y888888 "Y88888 "Y888 888 888 "Y88888 '
Write-Host -ForegroundColor White ' 888 888 '
Write-Host -ForegroundColor White 'Author: Cyb3rN8TE 2023 Y8b d88P Y8b d88P '
Write-Host -ForegroundColor White 'Version: 2.0.1 "Y88P" "Y88P" '
#Banner end
# Switch statement that detects which operating system is being used (Windows or Mac OS)
switch ($env:OS) {
# Windows OS Script
"Windows_NT" {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
try {
Add-Type -AssemblyName Microsoft.Office.Interop.Excel -ErrorAction Stop
Write-Host
Write-Host "Microsoft.Office.Interop.Excel assembly found. Proceeding..."
}
catch {
Write-Error "Microsoft.Office.Interop.Excel assembly not found. Please make sure that Microsoft Office is installed on your system"
Write-Host "Press ENTER key to exit..."
$null = Read-Host
Exit
}
# Set dimensions for PowerShell window
$Width = 170
$Height = 60
[Console]::SetWindowSize($Width, $Height)
# Check if ImportExcel Module is installed for the user and if not, install it
if (-not (Get-Module -Name ImportExcel -ListAvailable)) {
Write-Host ""
Write-Output "ImportExcel module not found. Installing module..."
Install-Module -Name ImportExcel -Scope CurrentUser -Force
} else {
Write-Host ""
Write-Output "ImportExcel module found."
}
# Import Module
Import-Module ImportExcel
Write-Host ""
Write-Host "Windows OS detected"
do {
Write-Host ""
Write-Host "Please select your file for processing.."
# Define array of values to check for in column headers
$valuesToDelete = @('Actions','Log Source Host','Host (Impacted) KBytes Rcvd','Host (Impacted) KBytes Sent','Host (Impacted) KBytes Total','Host (Impacted) Packets Rcvd','Host (Impacted) Packets Sent','Host (Impacted) Packets Total','User (Origin) Identity','User (Impacted) Identity','Rule Block','First Log Date','Last Log Date','Log Sequence Number','Log Count','Serial Number','Priority','Severity','Quantity','Amount','Size','Rate','Duration','Version')
# Create open file dialog
$openFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$openFileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
$openFileDialog.Filter = "CSV files (*.csv)|*.csv"
# Show the dialog and get the selected file
if ($openFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK)
{
Clear-Host
$filePath = $openFileDialog.FileName
# Load the CSV file
$data = Import-Csv -Path $filePath
# Move columns to reflect provided order
$orderedColumns = @(
"Log Source Entity",
"Log Date",
"Log Source Type",
"Log Source",
"Session",
"User (Origin)",
"Host (Origin)",
"MAC Address (Origin)",
"IP Address (Origin)",
"NAT IP Address (Origin)",
"Location (Origin)",
"Classification",
"Common Event",
"MPE Rule Name",
"Protocol",
"Application",
"Known Application",
"TCP/UDP Port (Origin)",
"Direction",
"TCP/UDP Port (Impacted)",
"User (Impacted)",
"Host (Impacted)",
"MAC Address (Impacted)",
"IP Address (Impacted)",
"NAT IP Address (Impacted)",
"Location (Impacted)",
"User Agent",
"Command",
"URL",
"Action",
"Subject",
"Reason",
"Response Code",
"Result",
"Status",
"Policy",
"Vendor Message ID",
"Vendor Info",
"Object",
"Object Name",
"Object Type",
"Process Name",
"Process ID",
"Parent Process ID",
"Parent Process Name",
"Parent Process Path",
"Hash",
"Threat Name",
"Threat ID",
"CVE",
"Sender",
"Recipient",
"Sender Identity",
"Recipient Identity",
"Session Type",
"Group",
"Country (Origin)",
"Country (Impacted)",
"Region (Origin)",
"Region (Impacted)",
"Zone (Origin)",
"Zone (Impacted)",
"Hostname (Origin)",
"Hostname (Impacted)",
"Known Host (Origin)",
"Known Host (Impacted)",
"Interface (Origin)",
"Interface (Impacted)",
"Network (Origin)",
"Network (Impacted)",
"NAT TCP/UDP Port (Origin)",
"NAT TCP/UDP Port (Impacted)",
"Domain (Impacted)",
"Domain (Origin)",
"Entity (Origin)",
"Entity (Impacted)",
"Log Message"
)
# Reorder columns
$data = $data | Select-Object $orderedColumns
# Define the column names that should be modified
$columnsToModify = @(
"URL",
"Subject",
"Host (Origin)",
"Host (Impacted)",
"IP Address (Origin)",
"IP Address (Impacted)",
"NAT IP Address (Origin)",
"NAT IP Address (Impacted)",
"Hostname (Origin)",
"Hostname (Impacted)",
"Known Host (Origin)",
"Known Host (Impacted)",
"Domain (Impacted)",
"Domain (Origin)",
"Protocol",
"Log Message"
)
# Convert Log Date format from MM/DD/YYYY to DD/MM/YYYY
foreach ($row in $data) {
if ($row.PSObject.Properties.Name -contains 'Log Date' -and ![string]::IsNullOrEmpty($row."Log Date")) {
$logDate = Get-Date $row."Log Date"
$formattedLogDate = $logDate.ToString("dd/MM/yyyy HH:mm:ss")
$row."Log Date" = $formattedLogDate
}
}
# Loop through each row of the data
foreach ($row in $data) {
# Remove columns containing specified values
foreach ($value in $valuesToDelete) {
if ($row.PSObject.Properties.Name -contains $value) {
$row.PSObject.Properties.Remove($value)
}
}
# Modify specified columns
foreach ($column in $columnsToModify) {
$value = $row.$column
# Check if the value is not empty
if (![string]::IsNullOrEmpty($value)) {
# Replace '.' and ':' with '[.]' and '[:]', respectively
$newValue = $value.Replace(".", "[.]").Replace(":", "[:]")
$row.$column = $newValue
}
}
}
# Initialise row count
$rowCount = $data.Count
# Convert the contents to an Excel workbook
$excel = New-Object -ComObject Excel.Application
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Sheets.Item(1)
# Write the CSV data to the worksheet
$rowIndex = 1
foreach ($row in $data) {
$colIndex = 1
foreach ($prop in $row.PSObject.Properties) {
$value = $prop.Value
# Write the header row to the worksheet
if ($rowIndex -eq 1) {
$sheet.Cells.Item($rowIndex, $colIndex) = $prop.Name
}
# Write the data rows to the worksheet
$sheet.Cells.Item($rowIndex + 1, $colIndex) = $value
$colIndex++
}
$rowIndex++
# Display progress bar
$percentComplete = [int]([Math]::Min($rowIndex / $rowCount * 100, 100))
Write-Progress -Activity "Processing File and Converting to Excel workbook..." -PercentComplete $percentComplete
}
# Resise the cells to fit the contents
$range = $sheet.UsedRange
$range.EntireColumn.AutoFit() | Out-Null
# Apply TableStyleMedium13
$table = $sheet.ListObjects.Add([Microsoft.Office.Interop.Excel.XlListObjectSourceType]::xlSrcRange, $range, $null, [Microsoft.Office.Interop.Excel.XlYesNoGuess]::xlYes)
$table.TableStyle = "TableStyleMedium13"
# Remove progress bar
Write-Progress -Activity "Converting to Excel workbook..." -Completed
Write-Host""
Write-Host "Processing Complete..."
# Show save dialog
$result = [System.Windows.Forms.MessageBox]::Show("Do you want to select where to save the Excel file?" + [Environment]::NewLine + "" + [Environment]::NewLine + "Note: Selecting no will save the file in the same location as the original CSV", "Save As", [System.Windows.Forms.MessageBoxButtons]::YesNo)
if ($result -eq [System.Windows.Forms.DialogResult]::Yes) {
# Create save file dialog
$saveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
$saveFileDialog.Filter = "Excel files (*.xlsx)|*.xlsx"
$saveFileDialog.FileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
$saveFileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($filePath)
# Show the dialog and get the selected filename and location
if ($saveFileDialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {
$excel.DisplayAlerts = $false
$workbook.SaveAs($saveFileDialog.FileName, 51)
$excel.DisplayAlerts = $true
$workbook.Close()
$excel.Quit()
# Release the Excel object from memory to prevent memory leaks
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
# Kill excel processes that are not visible
Get-Process Excel | Where-Object {$_.MainWindowTitle -eq ''} | Stop-Process
}
}
else {
$excel.DisplayAlerts = $false
$workbook.SaveAs($filePath + ".xlsx", 51)
$excel.DisplayAlerts = $true
$workbook.Close()
$excel.Quit()
# Release the Excel object from memory to prevent memory leaks
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
# Kill excel processes that are not visible
Get-Process Excel | Where-Object {$_.MainWindowTitle -eq ''} | Stop-Process
}
}
Clear-Host
# Prompt the user to process another file
$msgBoxTitle = "Process more files?"
$msgBoxMessage = "Do you want to process more CSV files?"
$msgBoxButtons = [System.Windows.Forms.MessageBoxButtons]::YesNo
$msgBoxIcon = [System.Windows.Forms.MessageBoxIcon]::Question
$processMoreFiles = [System.Windows.Forms.MessageBox]::Show($msgBoxMessage, $msgBoxTitle, $msgBoxButtons, $msgBoxIcon)
# Keep prompting user to process more CSV files until the user selects no
} while ($processMoreFiles -eq [System.Windows.Forms.DialogResult]::Yes)
}
# Mac OS Script
default {
do {
Write-Host ""
# Prompt the user for the input file path
do {
Write-Host "Please specify the input CSV file path..."
$inputFilePath = Read-Host
} while (-not (Test-Path $inputFilePath))
# Prompt the user for the output file path and name
do {
Write-Host "Please specify the output CSV file path and filename..."
$outputFilePath = Read-Host
} while (-not (Test-Path (Split-Path $outputFilePath)) -or (Split-Path $outputFilePath).Equals($null))
# If the output file name is not specified, use the input file name with "_modified" appended
if ($outputFilePath -eq "") {
$outputFilePath = [IO.Path]::ChangeExtension($inputFilePath, "csv")
$outputFilePath = [IO.Path]::Combine((Split-Path $inputFilePath), [IO.Path]::GetFileNameWithoutExtension($outputFilePath) + "_modified.csv")
}
# Define array of values to check for in column headers
$valuesToDelete = @('Actions','Log Source Host','Host (Impacted) KBytes Rcvd','Host (Impacted) KBytes Sent','Host (Impacted) KBytes Total','Host (Impacted) Packets Rcvd','Host (Impacted) Packets Sent','Host (Impacted) Packets Total','User (Origin) Identity','User (Impacted) Identity','Rule Block','First Log Date','Last Log Date','Log Sequence Number','Log Count','Serial Number','Priority','Severity','Quantity','Amount','Size','Rate','Duration','Version')
# Load the CSV file
$data = Import-Csv -Path $inputFilePath
# Move columns to reflect provided order
$orderedColumns = @(
"Log Source Entity",
"Log Date",
"Log Source Type",
"Log Source",
"Session",
"User (Origin)",
"Host (Origin)",
"MAC Address (Origin)",
"IP Address (Origin)",
"NAT IP Address (Origin)",
"Location (Origin)",
"Classification",
"Common Event",
"MPE Rule Name",
"Protocol",
"Application",
"Known Application",
"TCP/UDP Port (Origin)",
"Direction",
"TCP/UDP Port (Impacted)",
"User (Impacted)",
"Host (Impacted)",
"MAC Address (Impacted)",
"IP Address (Impacted)",
"NAT IP Address (Impacted)",
"Location (Impacted)",
"User Agent",
"Command",
"URL",
"Action",
"Subject",
"Reason",
"Response Code",
"Result",
"Status",
"Policy",
"Vendor Message ID",
"Vendor Info",
"Object",
"Object Name",
"Object Type",
"Process Name",
"Process ID",
"Parent Process ID",
"Parent Process Name",
"Parent Process Path",
"Hash",
"Threat Name",
"Threat ID",
"CVE",
"Sender",
"Recipient",
"Sender Identity",
"Recipient Identity",
"Session Type",
"Group",
"Country (Origin)",
"Country (Impacted)",
"Region (Origin)",
"Region (Impacted)",
"Zone (Origin)",
"Zone (Impacted)",
"Hostname (Origin)",
"Hostname (Impacted)",
"Known Host (Origin)",
"Known Host (Impacted)",
"Interface (Origin)",
"Interface (Impacted)",
"Network (Origin)",
"Network (Impacted)",
"NAT TCP/UDP Port (Origin)",
"NAT TCP/UDP Port (Impacted)",
"Domain (Impacted)",
"Domain (Origin)",
"Entity (Origin)",
"Entity (Impacted)",
"Log Message"
)
# Reorder columns
$data = $data | Select-Object $orderedColumns
# Define the column names that should be modified
$columnsToModify = @(
"URL",
"Subject",
"Host (Origin)",
"Host (Impacted)",
"IP Address (Origin)",
"IP Address (Impacted)",
"NAT IP Address (Origin)",
"NAT IP Address (Impacted)",
"Hostname (Origin)",
"Hostname (Impacted)",
"Known Host (Origin)",
"Known Host (Impacted)",
"Domain (Impacted)",
"Domain (Origin)",
"Protocol",
"Log Message"
)
# Loop through each row of the data
foreach ($row in $data) {
# Remove columns containing specified values
foreach ($value in $valuesToDelete) {
if ($row.PSObject.Properties.Name -contains $value) {
$row.PSObject.Properties.Remove($value)
}
}
# Modify specified columns
foreach ($column in $columnsToModify) {
$value = $row.$column
# Check if the value is not empty
if (![string]::IsNullOrEmpty($value)) {
# Defang values
$newValue = $value.Replace(".", "[.]").Replace(":", "[:]")
$row.$column = $newValue
}
}
}
# Convert Log Date format from MM/DD/YYYY to DD/MM/YYYY
$logDate = [DateTime]::ParseExact($row.'Log Date', 'MM/dd/yyyy HH:mm:ss tt', [System.Globalization.CultureInfo]::InvariantCulture)
$row.'Log Date' = $logDate.ToString('dd/MM/yyyy HH:mm:ss tt')
# Export the modified data to a CSV file
$data | Export-Csv -Path $outputFilePath -NoTypeInformation
# Prompt user to process more CSV files
$answer = Read-Host "Do you want to process more CSV files? (Y/N)"
if ($answer -eq "Y") {
continue
} elseif ($answer -eq "N") {
break
} else {
Write-Host "Invalid input. Please enter Y or N."
}
} while ($true)
}
}
# Prompt the user to exit
Write-Host ""
Write-Host "Press ENTER key to exit..."
$null = Read-Host