fix: check both target and subject fields, 4624 type 7 is now an unlock event, 4634 now allows type 7 for logoff/session change, added security log access check
This commit is contained in:
@@ -100,6 +100,25 @@ Function Get-EventDataValue {
|
|||||||
Return [string]$Data.'#text'
|
Return [string]$Data.'#text'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function Get-EventDataValueFromList {
|
||||||
|
Param(
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[xml]$EventXml,
|
||||||
|
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string[]]$Names
|
||||||
|
)
|
||||||
|
|
||||||
|
ForEach ($Name in $Names) {
|
||||||
|
$Value = Get-EventDataValue -EventXml $EventXml -Name $Name
|
||||||
|
If (-not [string]::IsNullOrWhiteSpace($Value) -and $Value -ne '-') {
|
||||||
|
Return $Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Return $null
|
||||||
|
}
|
||||||
|
|
||||||
Function Test-UserLogonActivityUser {
|
Function Test-UserLogonActivityUser {
|
||||||
Param(
|
Param(
|
||||||
[AllowNull()]
|
[AllowNull()]
|
||||||
@@ -131,6 +150,16 @@ Function Format-UserName {
|
|||||||
Return "${DomainName}\${UserName}"
|
Return "${DomainName}\${UserName}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function Get-UserLogonActivitySecurityLogAccessError {
|
||||||
|
Try {
|
||||||
|
Get-WinEvent -ListLog Security -ErrorAction Stop | Out-Null
|
||||||
|
Return $null
|
||||||
|
}
|
||||||
|
Catch {
|
||||||
|
Return "Unable to read the Windows Security event log. Run PowerShell as administrator if access is denied. $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Function ConvertTo-UserLogonActivity {
|
Function ConvertTo-UserLogonActivity {
|
||||||
Param(
|
Param(
|
||||||
[Parameter(Mandatory=$true)]
|
[Parameter(Mandatory=$true)]
|
||||||
@@ -138,18 +167,23 @@ Function ConvertTo-UserLogonActivity {
|
|||||||
)
|
)
|
||||||
|
|
||||||
$EventXml = [xml]$Event.ToXml()
|
$EventXml = [xml]$Event.ToXml()
|
||||||
$UserName = Get-EventDataValue -EventXml $EventXml -Name 'TargetUserName'
|
$UserName = Get-EventDataValueFromList -EventXml $EventXml -Names @('TargetUserName', 'SubjectUserName')
|
||||||
$DomainName = Get-EventDataValue -EventXml $EventXml -Name 'TargetDomainName'
|
$DomainName = Get-EventDataValueFromList -EventXml $EventXml -Names @('TargetDomainName', 'SubjectDomainName')
|
||||||
$LogonType = Get-EventDataValue -EventXml $EventXml -Name 'LogonType'
|
$LogonType = Get-EventDataValue -EventXml $EventXml -Name 'LogonType'
|
||||||
$Activity = $null
|
$Activity = $null
|
||||||
|
|
||||||
Switch ($Event.Id) {
|
Switch ($Event.Id) {
|
||||||
4624 {
|
4624 {
|
||||||
If ($LogonType -notin @('2', '10', '11')) { Return $null }
|
Switch ($LogonType) {
|
||||||
$Activity = 'logon'
|
'2' { $Activity = 'logon' }
|
||||||
|
'7' { $Activity = 'unlock' }
|
||||||
|
'10' { $Activity = 'logon' }
|
||||||
|
'11' { $Activity = 'logon' }
|
||||||
|
Default { Return $null }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
4634 {
|
4634 {
|
||||||
If ($LogonType -notin @('2', '10', '11')) { Return $null }
|
If ($LogonType -notin @('2', '7', '10', '11')) { Return $null }
|
||||||
$Activity = 'logoff'
|
$Activity = 'logoff'
|
||||||
}
|
}
|
||||||
4647 {
|
4647 {
|
||||||
@@ -178,6 +212,12 @@ Function ConvertTo-UserLogonActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$SecurityLogAccessError = Get-UserLogonActivitySecurityLogAccessError
|
||||||
|
If (-not [string]::IsNullOrWhiteSpace($SecurityLogAccessError)) {
|
||||||
|
Write-Error $SecurityLogAccessError
|
||||||
|
Exit 1
|
||||||
|
}
|
||||||
|
|
||||||
$Filter = @{
|
$Filter = @{
|
||||||
LogName = 'Security'
|
LogName = 'Security'
|
||||||
Id = @(4624, 4634, 4647, 4800, 4801)
|
Id = @(4624, 4634, 4647, 4800, 4801)
|
||||||
|
|||||||
Reference in New Issue
Block a user