Public/Get-ALHADFailedLogonAttempt.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 |
<#PSScriptInfo
.VERSION 1.2.0 .GUID 6683d9ba-f92a-43d0-b84b-5b552fe9e123 .AUTHOR Dieter Koch .COMPANYNAME .COPYRIGHT (c) 2021-2023 Dieter Koch .TAGS .LICENSEURI https://github.com/admins-little-helper/ALH/blob/main/LICENSE .PROJECTURI https://github.com/admins-little-helper/ALH .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES 1.0.0 - Initial release 1.1.0 - Made script accept values for paramter ComputerName from pipeline. 1.2.0 - Added parameter 'TimeRange' to allow more user-friendly input #> <# .DESCRIPTION Contains a function to query the securtiy event log for event id 4625 and 4771 which are logged for failed logon attempts. #> function Get-ALHADFailedLogonAttempt { <# .SYNOPSIS Function to query the securtiy event log for event id 4625 and 4771 which are logged for failed logon attempts. .DESCRIPTION Function to query the securtiy event log for event id 4625 and 4771 which are logged for failed logon attempts. The function can query one or multiple computers for one, multiple or any user in a given timeframe. This helps to identify the source of the invalid logon attempts because the events contain the source IP address of the logon attempt. .PARAMETER DomainName The AD domain name in which the Domain Controller will be queried, if no value is specified for the -Compuername parameter. .PARAMETER Identity One or more usernames (samAccountName) to search for. If ommited, events for all users ("*") are searched. .PARAMETER StartTime The datetime to start searching from. If ommited, it's set for the last two hours. .PARAMETER ComputerName One or more computernames to search for. If ommited, the script tries to get the domain controller with the PDC emulator role for the current domain or the domain specified with the -DomainName parameter. .PARAMETER ResolveDNS If specified, the script will try to lookup the DNS hostname of the ip address found in the event log record. Note that this can be misleading because the ip address shown in the event can be assigned to anohter system at the time of the check. .PARAMETER CheckLockoutStatus If specified, the script will check the current lockout status of the user account found in the event. .PARAMETER Credential Credentials used to query the event log. If ommited, the credentials of the user running the script are used. .EXAMPLE Get-ALHADFailedLogonAttempt Get events for all users in the last 2 hours from the domain ctonroller with the PDC emulator role. .EXAMPLE Get-ALHADFailedLogonAttempt -Identity 'mike' -StartTime (Get-Date).AddHours(-8) Get events for user with samAccountName 'mike' within the last 8 hours. .EXAMPLE Get-ALHADFailedLogonAttempt -StartTime (Get-Date).AddDays(-1) -ComputerName dc1,dc2 Get events for any user within last 24 hours from a computers (Domain Controller) dc1 and dc2. .EXAMPLE Get-ALHADFailedLogonAttempt -Identity 'user1','user2' -StartTime (Get-Date).AddDays(-1) Get events for two users within the last 24 hours from Domain Controller running the PDC role. .EXAMPLE Get-Content -Path C:\Temp\Userlist.txt | Get-ALHADFailedLogonAttempt -StartTime (Get-Date).AddDays(-1) Get events for users from pipeline input within the last 24 hours from Domain Controller running the PDC role. .INPUTS Nothing .OUTPUTS Nothing .NOTES Author: Dieter Koch Email: diko@admins-little-helper.de .LINK https://github.com/admins-little-helper/ALH/blob/main/Help/Get-ALHADFailedLogonAttempt.txt #> [CmdletBinding()] param ( [ValidateNotNullOrEmpty()] [string]$DomainName = $env:USERDOMAIN, [Parameter(ValueFromPipeline, HelpMessage = 'Enter one or more user names')] [ValidateNotNullOrEmpty()] [string[]]$Identity, [Parameter(ParameterSetName = "DateTime")] [ValidateNotNullOrEmpty()] [datetime]$StartTime = (Get-Date).AddHours(-2), [Parameter(ParameterSetName = "TimeRange")] [ValidateSet("1h", "2h", "4h", "6h", "8h", "12h", "15h", "18h", "21h", "1d", "2d", "3d", "4d", "5d", "6d", "7d")] [ValidateNotNullOrEmpty()] [string]$TimeRange = "1d", [ValidateNotNullOrEmpty()] [string[]]$ComputerName = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain((New-Object System.DirectoryServices.ActiveDirectory.DirectoryContext('Domain', $DomainName))).PdcRoleOwner.Name, [ValidateNotNullOrEmpty()] [switch]$ResolveDns = $false, [ValidateNotNullOrEmpty()] [switch]$CheckLockoutStatus = $false, [ValidateNotNull()] [System.Management.Automation.PSCredential] [System.Management.Automation.Credential()] $Credential = [System.Management.Automation.PSCredential]::Empty ) begin { $RequiredModules = "ActiveDirectory" foreach ($RequiredModule in $RequiredModules) { if (-not [bool](Get-Module -Name $RequiredModule)) { if (-not [bool](Get-Module -Name $RequiredModule -ListAvailable)) { Write-Warning -Message "Module $RequiredModule not found. Stopping function." break } Write-Verbose -Message "Importing $RequiredModule Module" Import-Module ActiveDirectory } } if ($null -eq $Credential -or $Credential.Username -eq '') { $CredentialsOfUser = "$env:USERDOMAIN\$env:USERNAME" } else { $CredentialsOfUser = $Credential.Username } $ComputerTotal = $ComputerName.Count $UserTotal = $Identity.Count #"StatusHex;StatusDec;StatusText;StatusDesc $Event4625FailureReasons = @{ '-1073741715' = @{ StatusCodeHex = '0XC000006D' StatusText = 'STATUS_LOGON_FAILURE' StatusDescription = 'This is either due to a bad username or authentication information' } '-1073741714' = @{ StatusCodeHex = '0XC000006E' StatusText = 'STATUS_ACCOUNT_RESTRICTION' StatusDescription = 'Unknown user name or bad password' } '-1073741421' = @{ StatusCodeHex = '0XC0000193' StatusText = 'STATUS_ACCOUNT_EXPIRED' StatusDescription = 'Account Expired' } '-1073741428' = @{ StatusCodeHex = '0XC000018C' StatusText = 'STATUS_TRUSTED_DOMAIN_FAILURE' StatusDescription = 'The logon request failed because the trust relationship between the primary domain and the trusted domain failed' } '-1073741730' = @{ StatusCodeHex = '0XC000005E' StatusText = 'STATUS_NO_LOGON_SERVERS' StatusDescription = 'There are currently no logon servers available to service the logon request' } '-1073741604' = @{ StatusCodeHex = '0XC00000DC' StatusText = 'STATUS_INVALID_SERVER_STATE' StatusDescription = 'Indicates the Sam Server was in the wrong state to perform the desired operation' } '-1073741276' = @{ StatusCodeHex = '0XC0000224' StatusText = 'STATUS_PASSWORD_MUST_CHANGE' StatusDescription = 'User is required to change password at next logon' } '-1073741422' = @{ StatusCodeHex = '0XC0000192' StatusText = 'STATUS_NETLOGON_NOT_STARTED' StatusDescription = 'An attempt was made to logon, but the netlogon service was not started' } '-1073740781' = @{ StatusCodeHex = '0XC0000413' StatusText = 'STATUS_AUTHENTICATION_FIREWALL_FAILED' StatusDescription = 'Logon Failure: The machine you are logging onto is protected by an authentication firewall. The specified account is not allowed to authenticate to the machine' } '-1073741260' = @{ StatusCodeHex = '0xC0000234' StatusText = 'STATUS_ACCOUNT_LOCKED_OUT' StatusDescription = 'Account locked out' } } $Event4771FailureReasons = @{ '18' = @{ StatusCodeHex = '0x12' StatusText = 'Clients credentials have been revoked' StatusDescription = 'Account disabled, expired, locked out, logon hours' } '23' = @{ StatusCodeHex = '0x17' StatusText = 'Password has expired' StatusDescription = 'Password has expired' } '24' = @{ StatusCodeHex = '0x18' StatusText = 'Pre-authentication information was invalid' StatusDescription = 'Usually means bad password' } '37' = @{ StatusCodeHex = '0x25' StatusText = 'Clock skew too big' StatusDescription = 'Clock skew too big' } } if ($PsCmdlet.ParameterSetName -eq "TimeRange") { if ($TimeRange -match "^\d{1,2}[d]$") { $StartTime = (Get-Date).AddDays( - ($TimeRange -replace "d", "")) } elseif ($TimeRange -match "^\d{1,2}[h]$") { $TimeRange -replace "h", "" $StartTime = (Get-Date).AddHours( - ($TimeRange -replace "h", "")) } else { Write-Warning -Message "Not a valid timerange. Using default time range of 2h" $StartTime = (Get-Date).AddHours(-2) } } Write-Information -MessageData "Searching in domain : $DomainName" -InformationAction Continue Write-Information -MessageData "Checking security event log on cmputer(s) : $($Computername -join ';')" -InformationAction Continue Write-Information -MessageData "Search timeframe : $StartTime - $(Get-Date)" -InformationAction Continue Write-Information -MessageData "Running with credentials of user : $CredentialsOfUser" -InformationAction Continue Write-Information -MessageData "Searching for username(s) : $($Identity -join ';')" -InformationAction Continue Write-Information -MessageData "Resolving IP to hostname : $($ResolveDns.IsPresent)" -InformationAction Continue Write-Information -MessageData "Check current lockout status : $($CheckLockoutStatus.IsPresent)" -InformationAction Continue } process { $StartTimeProc = Get-Date if ($input) { $UserTotal = $Input.Count } foreach ($computer in $ComputerName) { $i = 0 Write-Progress -Activity "Searching on computer $computer" -PercentComplete ($i / $ComputerTotal * 100) -Status "Progress ->" -CurrentOperation OuterLoop $i++ foreach ($user in $Identity) { $j = 0 Write-Progress -Activity "Searching for user '$user'" -PercentComplete ($j / $UserTotal * 100) -Status "Progress ->" -CurrentOperation InnerLoop $j++ try { Write-Verbose -Message "Searching for user '$user'" $Events4625 = Get-WinEvent -ComputerName $computer -FilterHashtable @{LogName = 'Security'; Id = 4625; StartTime = $StartTime } -Credential $Credential -ErrorAction SilentlyContinue $StartTimeQuery4625 = Get-Date Write-Verbose -Message "Number events found for id 4625 before applying filter: $(($Events4625 | Measure-Object).Count)" if ($(($Events4625 | Measure-Object).Count) -gt 0) { if ($user -ne "*") { Write-Verbose -Message "Filtering for user $user" $Events4625 = $Events4625.where( { $_.Properties[5].Value -like "$user" } ) Write-Verbose -Message "Number events found for id 4625 after applying filter: $(($Events4625 | Measure-Object).Count)" } $Events4625 ` | Select-Object -Property TimeCreated, ` @{Name = 'UserName'; Expression = { $_.Properties[5].Value } }, ` @{Name = 'EventID'; Expression = { '4625' } } , @{Name = 'IpAddress'; Expression = { $_.Properties[19].Value } }, ` @{Name = 'ClientName'; Expression = { if ($ResolveDns.IsPresent) { ([system.net.dns]::GetHostByAddress($_.Properties[19].Value)).hostname } } }, @{Name = 'FailureCode'; Expression = { $_.Properties[7].Value } }, @{Name = 'FailureReason'; Expression = { $Event4625FailureReasons."$($_.Properties[7].Value)".StatusDescription } }, ` @{Name = 'CurrentlyLocked'; Expression = { if ($CheckLockoutStatus.IsPresent) { (Get-ADUser $_.Properties[5].Value -Properties LockedOut).LockedOut } } }, ` @{Name = 'Computer'; Expression = { $computer } } } $StartTimeFormat4625 = Get-Date $Events4771 = Get-WinEvent -ComputerName $computer -FilterHashtable @{LogName = 'Security'; Id = 4771; StartTime = $StartTime } -Credential $Credential -ErrorAction SilentlyContinue $StartTimeQuery4771 = Get-Date Write-Verbose -Message "Number events found for id 4771: $(($Events4771 | Measure-Object).Count)" if ($(($Events4771 | Measure-Object).Count) -gt 0) { if ($user -ne "*") { Write-Verbose -Message "Filtering for user $user" $Events4771 = $Events4771.where( { $_.Properties[0].Value -like "$user" } ) Write-Verbose -Message "Number events found for id 4771 after applying filter: $(($Events4771 | Measure-Object).Count)" } $Events4771 ` | Select-Object -Property TimeCreated, ` @{Name = 'UserName'; Expression = { $_.Properties[0].Value } }, ` @{Name = 'EventID'; Expression = { '4771' } }, @{Name = 'IpAddress'; Expression = { if ($_.Properties[6].Value -match "::ffff:") { $_.Properties[6].Value.Substring(7) } else { $_.Properties[6].Value } } }, ` @{Name = 'ClientName'; Expression = { if ($ResolveDns.IsPresent) { ([system.net.dns]::GetHostByAddress($_.Properties[6].Value)).hostname } else { '' } } }, @{Name = 'FailureCode'; Expression = { $_.Properties[4].Value } }, @{Name = 'FailureReason'; Expression = { $Event4771FailureReasons."$($_.Properties[4].Value)".StatusDescription } }, ` @{Name = 'CurrentlyLocked'; Expression = { if ($CheckLockoutStatus.IsPresent) { (Get-ADUser $_.Properties[5].Value -Properties LockedOut).LockedOut } else { '' } } }, ` @{Name = 'Computer'; Expression = { $computer } } } $StartTimeFormat4771 = Get-Date } catch [System.Exception] { if ($_.FullyQualifiedErrorID -eq 'NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand') { Write-Verbose -Message "No events returned in search" } else { $_ } } catch { $_ } } } } end { <# if ($($LockedOutUsers | Measure-Object).Count -eq 0) { Write-Information -MessageData "`n`nNo records found matching search criteria. Try extending the timeframe.`n" -InformationAction Continue } else { $LockedOutUsers | Sort-Object -Property Username, TimeCreated } #> Write-Verbose -Message "Elapsed time in seconds - TOTAL: $((((Get-Date) - $StartTimeProc).Totalseconds))" Write-Verbose -Message "Elapsed time in seconds - QUERY4625: $((($StartTimeQuery4625 - $StartTimeProc).Totalseconds))" Write-Verbose -Message "Elapsed time in seconds - FORMAT4625: $((($StartTimeFormat4625 - $StartTimeQuery4625).Totalseconds))" Write-Verbose -Message "Elapsed time in seconds - QUERY4771: $((($StartTimeQuery4771 - $StartTimeFormat4625).Totalseconds))" Write-Verbose -Message "Elapsed time in seconds - FORMAT4771: $((($StartTimeFormat4771 - $StartTimeQuery4771).Totalseconds))" } } #region EndOfScript <# ################################################################################ ################################################################################ # # ______ _ __ _____ _ _ # | ____| | | / _| / ____| (_) | | # | |__ _ __ __| | ___ | |_ | (___ ___ _ __ _ _ __ | |_ # | __| | '_ \ / _` | / _ \| _| \___ \ / __| '__| | '_ \| __| # | |____| | | | (_| | | (_) | | ____) | (__| | | | |_) | |_ # |______|_| |_|\__,_| \___/|_| |_____/ \___|_| |_| .__/ \__| # | | # |_| ################################################################################ ################################################################################ # created with help of http://patorjk.com/software/taag/ #> #endregion |