-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebsiteFailedLogins.psm1
320 lines (230 loc) · 9.54 KB
/
WebsiteFailedLogins.psm1
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
# Require TLS1.2 for all communications
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Function Invoke-WebsiteFailedLogins
{
<#
.SYNOPSIS
Launches WebsiteFailedLogins.
.DESCRIPTION
Generates an alert for:
- Each IP address meeting or exceeding the threshold FailedLoginsPerIP
- When the total failed logins threshold (TotalFailedLogins) is met or exceeded
See wiki for details.
.LINK
https://github.com/phbits/WebsiteFailedLogins/wiki
.EXAMPLE
$results = Invoke-WebsiteFailedLogins -Configuration D:\WFL\W3SVC1.ini
.EXAMPLE
$results = Invoke-WebsiteFailedLogins -Configuration D:\WFL\W3SVC1.ini -RunningConfig
.INPUTS
System.String
.OUTPUTS
System.Collections.Hashtable
#>
[CmdletBinding()]
[OutputType('System.Collections.Hashtable')]
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path -LiteralPath $_})]
[string]
# Path to configuration file.
$Configuration
,
[Parameter(Mandatory=$false)]
[switch]
# Performs the minimum validation checks against the configuration file. Use this switch after all configuration errors have been resolved.
$RunningConfig
)
$returnValue = @{
'FailedLoginsPerIP' = @{}
'TotalFailedLogins' = @{}
'HasError' = $false
'HasResults' = $false
'Configuration' = @{}
'ErrorMessages' = @()
}
$iniConfig = Get-IniConfig -Path $Configuration
$configTestResult = Assert-ValidIniConfig -IniConfig $iniConfig -RunningConfig:$($RunningConfig)
$returnValue.Configuration = $configTestResult.Configuration
if ($configTestResult.HasError)
{
$returnValue.HasError = $true
$returnValue.ErrorMessages = $configTestResult.ErrorMessages
$alertData = $returnValue
$alertData.Remove('FailedLoginsPerIP')
$alertData.Remove('TotalFailedLogins')
Submit-Alert -IniConfig $returnValue.Configuration -AlertData $alertData -TerminatingError
} else {
$lpQuery = Get-LogparserQuery -IniConfig $returnValue.Configuration
$returnValue.Configuration.Logparser.Add('WebsiteFailedLoginsQuery', $lpQuery)
$allFailedLogins = Get-WebsiteFailedLogins -IniConfig $returnValue.Configuration
$returnValue.Configuration.Script.Add('EndTimeTSZ', (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ'))
if ([System.String]::IsNullOrEmpty($allFailedLogins) -eq $false)
{
[Int] $totalFailedLogins = 0
[Int] $bottom20Percent = [System.Math]::Round($(.2 * $returnValue.Configuration.Website.TotalFailedLogins))
[Hashtable] $totalFailedLoginClientIpList = @{}
foreach ($entry in $allFailedLogins)
{
if ([Int] $entry.FailedLoginCount -ge [Int] $returnValue.Configuration.Website.FailedLoginsPerIP)
{
$clientIpResult = Get-FailedLoginsPerIpResult -IniConfig $returnValue.Configuration `
-ClientIP $entry.ClientIP `
-FailedLogins $entry.FailedLoginCount
$returnValue.FailedLoginsPerIP.Add($entry.ClientIP, $clientIpResult)
$returnValue.HasResults = $true
}
if ([Int] $entry.FailedLoginCount -gt $bottom20Percent)
{
$totalFailedLoginClientIpList.Add($entry.ClientIP, [Int] $entry.FailedLoginCount)
}
$totalFailedLogins += [Int] $entry.FailedLoginCount
}
if ($totalFailedLogins -ge [Int] $returnValue.Configuration.Website.TotalFailedLogins)
{
$returnValue.TotalFailedLogins = Get-TotalFailedLoginsResult -IniConfig $returnValue.Configuration `
-TotalFailedLogins $totalFailedLogins `
-ClientIpList $totalFailedLoginClientIpList
$returnValue.HasResults = $true
}
}
# send alerts
if ($returnValue.HasResults -eq $true -and $returnValue.Configuration.Alert.Method -imatch "(Smtp|WinEvent)")
{
if ($returnValue.FailedLoginsPerIP.Count -gt 0)
{
foreach ($key in $returnValue.FailedLoginsPerIP.Keys)
{
Submit-Alert -IniConfig $returnValue.Configuration `
-AlertData $($returnValue.FailedLoginsPerIP[$key])
}
}
if ($returnValue.TotalFailedLogins.Count -gt 0)
{
Submit-Alert -IniConfig $returnValue.Configuration `
-AlertData $returnValue.TotalFailedLogins
}
}
}
return $returnValue
} # End Function Invoke-WebsiteFailedLogins
Function Get-WebsiteFailedLoginsREADME
{
<#
.SYNOPSIS
Gets the WebsiteFailedLogins README file.
#>
[OutputType('System.String[]')]
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[System.String]
# Section to return.
$SectionKeyword
)
try {
$readMePath = Join-Path -Path $PSScriptRoot -ChildPath 'README.md'
$readMeFile = Get-Item -LiteralPath $readMePath -ErrorAction Stop
$readMeContent = Get-Content -LiteralPath $readMeFile.FullName
if ([System.String]::IsNullOrEmpty($SectionKeyword))
{
$readMeContent | foreach-Object{ Write-Output $_ }
} else {
$printLine = $false
$sectionKeywordLower = $SectionKeyword.ToLower().Trim()
for ($i=0; $i -lt $readMeContent.Length; $i++)
{
$line = $readMeContent[$i]
if ($line.Trim().StartsWith('#'))
{
$printLine = $false
}
if ([System.String]::IsNullOrEmpty($line) -eq $false)
{
if ($line.ToLower().Contains($sectionKeywordLower))
{
if ($printLine -eq $false)
{
Write-Output $readMeContent[$i - 1]
}
$printLine = $true
}
}
if ($printLine)
{
Write-Output $line
}
}
}
} catch {
$e = $_
Write-Error -Message "$('[ERROR][Exception] {0}' -f $e.Exception.Message)"
}
} # End Function Get-WebsiteFailedLoginsReadme
Function Copy-WebsiteFailedLoginsREADME
{
<#
.SYNOPSIS
Copy the WebsiteFailedLogins README file (README.md) to the destination folder.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType Container})]
[string]
# Destination folder to copy README.md
$DestinationFolder = (Get-Location).Path
)
try {
$readMePath = Join-Path -Path $PSScriptRoot -ChildPath 'README.md'
$readMeFile = Get-Item -LiteralPath $readMePath -ErrorAction Stop
Copy-Item -Path $readMeFile.FullName -Destination $DestinationFolder
} catch {
$e = $_
Write-Error -Message "$('[ERROR][Exception] {0}' -f $e.Exception.Message)"
}
} # End Function Copy-WebsiteFailedLoginsReadme
Function Get-WebsiteFailedLoginsDefaultConfiguration
{
<#
.SYNOPSIS
Gets the WebsiteFailedLogins default configuration file.
#>
[OutputType('System.String[]')]
[CmdletBinding()]
param( )
try {
$defaultConfigPathFolder = Join-Path -Path $PSScriptRoot -ChildPath 'Resources'
$defaultConfigPath = Join-Path -Path $defaultConfigPathFolder -ChildPath 'WebsiteFailedLogins_default.ini'
$configFile = Get-Item -LiteralPath $defaultConfigPath -ErrorAction Stop
[string[]] $configContents = Get-Content -LiteralPath $configFile.FullName
return $configContents
} catch {
$e = $_
Write-Error -Message "$('[ERROR][Exception] {0}' -f $e.Exception.Message)"
}
} # End Function Get-WebsiteFailedLoginsDefaultConfiguration
Function Copy-WebsiteFailedLoginsDefaultConfiguration
{
<#
.SYNOPSIS
Copy the WebsiteFailedLogins default configuration file to the destination folder.
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[ValidateScript({Test-Path -LiteralPath $_ -PathType Container})]
[string]
# Destination folder to copy WebsiteFailedLogins.ini
$DestinationFolder = (Get-Location).Path
)
try {
$defaultConfigPathFolder = Join-Path -Path $PSScriptRoot -ChildPath 'Resources'
$defaultConfigPath = Join-Path -Path $defaultConfigPathFolder -ChildPath 'WebsiteFailedLogins_default.ini'
$configFile = Get-Item -LiteralPath $defaultConfigPath -ErrorAction Stop
Copy-Item -Path $configFile.FullName -Destination $DestinationFolder
} catch {
$e = $_
Write-Error -Message "$('[ERROR][Exception] {0}' -f $e.Exception.Message)"
}
} # End Function Copy-WebsiteFailedLoginsDefaultConfiguration