-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertFrom-F5Log.ps1
More file actions
181 lines (146 loc) · 6.52 KB
/
Copy pathConvertFrom-F5Log.ps1
File metadata and controls
181 lines (146 loc) · 6.52 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
function ConvertFrom-F5Log {
<#
.SYNOPSIS
Converts F5 log files into PowerShell objects.
.DESCRIPTION
Converts F5 log files into PowerShell objects.
.EXAMPLE
ConvertFrom-F5Log -Path C:\path\to\audit.log
Converts 'audit.log' into PowerShell objects.
.EXAMPLE
ConvertFrom-F5Log -Path C:\path\to\var\log -IncludeRegEx tmsh -FileFilter audit*
Converts all files under 'C:\path\to\var\log' named 'audit*' into PowerShell objects.
Filters the output to only entries related to F5 Traffic Management Shell (tmsh).
.EXAMPLE
# Search ltm logs for attacks and calculate the duration of each one.
ConvertFrom-F5Log -Path 'C:\path\to\var\log' -FileFilter ltm* -IncludeRegEx attack | ForEach-Object {
$AttackID = $_.Message.Split(' ')[-1] -replace '\.'
$AttackVector = [regex]::Matches($($_.Message),'vector\s.+(?=,)').Value.ToString() -replace 'vector\s'
Add-Member -InputObject $_ -MemberType NoteProperty -Name AttackID -Value $AttackID
Add-Member -InputObject $_ -MemberType NoteProperty -Name AttackVector -Value $AttackVector -PassThru
} | Group-Object -Property AttackID | Where-Object { $_.Count -eq 2 } | ForEach-Object {
$TheEvents = $_.Group | Sort-Object -Property TimeStamp
$TotalSeconds = (New-TimeSpan -Start $TheEvents[0].TimeStamp -End $TheEvents[-1].TimeStamp).TotalSeconds
New-Object -TypeName psobject -Property @{
Hostname = $TheEvents[0].Hostname
AttackID = $TheEvents[0].AttackID
AttackVector = $TheEvents[0].AttackVector
StartTime = $TheEvents[0].TimeStamp
StopTime = $TheEvents[-1].TimeStamp
TotalSeconds = [int]$TotalSeconds
}
} | Tee-Object -Variable Attacks |
Select-Object -Property Hostname,AttackID,AttackVector,StartTime,StopTime,TotalSeconds
.EXAMPLE
$Attacks | Group-Object -Property AttackVector | Sort-Object -Property Count -Descending
Assuming you already ran example 3, summarize attack totals by vector.
.PARAMETER Path
Path to the file(s) or folder(s) to be converted.
.PARAMETER IncludeRegEx
RegEx filter for logs to include. Defaults to '.*'
.PARAMETER ExcludeRegEx
RegEx filter for logs to exclude. Defaults to '^$'
.PARAMETER FileFilter
Path filter for input files. Defaults to '*'
.PARAMETER TimeStampToString
Switch to convert TimeStamp parameter from [datetime] to [string].
.PARAMETER Year
The year as a string.
.INPUTS
System.Object
.OUTPUTS
System.Object
.NOTES
###########################################################################################
Author: State of Oregon, Enterprise Information Services, Cybersecurity Assessment Team
Version: 1.0
###########################################################################################
License: https://unlicense.org/UNLICENSE
###########################################################################################
.LINK
https://github.com/stateoforegon-eis-css/F5LogTools
.FUNCTIONALITY
Converts F5 log files into powershell objects.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[Alias('PSPath','FullName')]
[string[]]
$Path,
[string]
$IncludeRegEx,
[string]
$ExcludeRegEx,
[string]
$FileFilter,
[switch]
$TimeStampToString,
[string]
$Year
) #param
begin {
if (-not ($PSBoundParameters['FileFilter'])) {
$FileFilter = '*'
} #if FileFilter
if (-not ($PSBoundParameters['IncludeRegEx'])) {
$IncludeRegEx = '.*'
} #if IncludeRegEx
if (-not ($PSBoundParameters['ExcludeRegEx'])) {
$ExcludeRegEx = '^$'
} #if ExcludeRegEx
if (-not ($PSBoundParameters['Year'])) {
$Year = (Get-Date).Year.ToString()
} #if Year
$DateFormatRegEx = '^\w\w\w\s+\d+\s\d\d:\d\d:\d\d'
$LogSeverityRegEx = '(emerg|alert|crit|err|warning|notice|info|debug)'
} #begin
process {
$Path | ForEach-Object {
$EachItem = $_ | Get-Item
if (-not $EachItem.PSIsContainer) {
$EachItem | Get-Content | Where-Object {
$_ -match $DateFormatRegEx -and
$_ -match $LogSeverityRegEx -and
$_ -match $IncludeRegEx -and
$_ -notmatch $ExcludeRegEx
}
} elseif ($EachItem.PSIsContainer) {
$EachItem | Get-ChildItem -Recurse -File -Filter $FileFilter | Get-Content | Where-Object {
$_ -match $DateFormatRegEx -and
$_ -match $LogSeverityRegEx -and
$_ -match $IncludeRegEx -and
$_ -notmatch $ExcludeRegEx
}
} #if
} | ForEach-Object {
$ValueArray = $_.SubString(16).Split(' ')
$Hostname = $ValueArray[0]
$Level = $ValueArray[1]
$Source = $ValueArray[2].Split('[')[0].Split('(')[0] -replace '(-|:)'
$MessageArray = ($ValueArray | Select-Object -Skip 4)
$MetaArray = $MessageArray | Where-Object { $_ -match '=' }
$UserEnum = $MetaArray | Where-Object { $_ -match 'user' }
if ($UserEnum) {
$User = ($UserEnum).Split('=')[-1].Split('(')[0]
} else {
$User = '-'
} #if
$Date = Get-Date -Date "$("$($_.SubString(0,6)) $Year" -replace '\s\s',' ') $($_.SubString(7,8))"
if ($TimeStampToString) {
$TimeStamp = [string]$Date.GetDateTimeFormats('s')
} else {
$TimeStamp = $Date
} #if
$MessageText = (($MessageArray | Where-Object { $_ -notmatch '=' }) -join ' ').Split(']')[-1].Trim()
New-Object -TypeName psobject -Property @{
TimeStamp = $TimeStamp
Hostname = $Hostname
Level = $Level
Source = $Source
User = $User
Message = $MessageText
}
} | Select-Object -Property TimeStamp,Hostname,Level,Source,User,Message | Where-Object { $_.Message }
} #process
} #function ConvertFrom-F5Log