Repository navigation
Expand file tree
/
Copy pathGet-UserLogonEvent.ps1
More file actions
177 lines (147 loc) · 5.81 KB
/
Copy pathGet-UserLogonEvent.ps1
File metadata and controls
177 lines (147 loc) · 5.81 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
<#
.SYNOPSIS
Holt Anmelde-Events bestimmter Event-IDs oder Benutzer von allen Domain
Controllern.
.DESCRIPTION
Baut einen XML-Filter fuer einen Zeitraum (Tage rueckwaerts) und fragt
damit reihum jeden Domain Controller ab. Filtern laesst sich nach
Event-ID und Benutzername.
.NOTES
Der Filter wird serverseitig angewendet, die Abfrage bleibt dadurch auch
bei grossen Sicherheitsprotokollen handhabbar.
Fuer fehlgeschlagene Anmeldungen mit aufbereiteten Statuscodes ist
Get-ADAuthEventsAllDCs.ps1 im selben Ordner die umfangreichere Variante.
#>
# This Script returns all logon events of specific EventIDs or user names
# within a specific time frame (days backwards)
function get-DC-WinEvents {
param (
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string]$filterXML,
[array]$domainControllers
)
# Iterate through each domain controller and retrieve logon attempts
foreach ($dc in $domainControllers) {
$dcName = $dc.HostName
Write-Host("Verarbeite $dcName ...") -ForegroundColor Green
$events = Get-WinEvent -ComputerName $dc.HostName -FilterXml $filterXML -ErrorAction SilentlyContinue
# Extract specific columns and return them as a custom object
$logonAttempts += $events | ForEach-Object {
# Extract Fehlerursache from the Message property
$Fehlerursache = if ($_.Message -match 'Fehlerursache:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Anmeldeprozess = if ($_.Message -match 'Anmeldeprozess:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Arbeitsstationsname = if ($_.Message -match 'Arbeitsstationsname:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Quellnetzwerkadresse = if ($_.Message -match 'Quellnetzwerkadresse:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Authentifizierungspaket = if ($_.Message -match 'Authentifizierungspaket:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Status = if ($_.Message -match 'Status:\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$Unterstatus = if ($_.Message -match 'Unterstatus::\s+(.*)') {
$matches[1].Trim()
} else {
"N/A"
}
$pattern = 'Kontoname:\s+(.+)'
$erg = $_.Message -split "`n" | Select-String -Pattern $pattern | ForEach-Object { $_.Matches.Groups[1].Value.Trim() }
$Kontoname = if ($erg[1]) { $erg[1] } else { "N/A" }
[PSCustomObject]@{
"Domaincontroller" = $dcName
"EventID" = $_.Id
"LogonTime" = $_.TimeCreated
"Quellnetzwerkadresse" = $Quellnetzwerkadresse
"Arbeitsstationsname" = $Arbeitsstationsname
"Kontoname" = $Kontoname
"Fehlerursache" = $Fehlerursache
"Anmeldeprozess" = $Anmeldeprozess
"Authentifizierungspaket" = $Authentifizierungspaket
"Status" = $Status
"Unterstatus" = $Unterstatus
}
}
}
# Output the custom object
return $logonAttempts #| Sort-Object -Property LogonTime -Descending | Out-GridView -Title "4624 (Erfolgreich) oder 4625 (Fehler) zum Filtern eingeben"
}
# Initialize an empty array to store logon attempts
$logonAttempts = @()
# Prompt user for username input
$username = Read-Host -Prompt "Enter the username without domainname ([Alle User = * Standard)"
if (-not $username) {
$username = '*'
}
$username = $username.Trim()
if ($username -ne '*') {
$userxml = @"
and
*[EventData[Data[@Name='TargetUserName']='$username']]
"@
} else {
$userxml = ""
}
# Prompt user for number of days backward and convert it to an integer
$days = Read-Host -Prompt "Enter the number of days backward (1 = Standard)"
if (-not $days) {
$days = 1
} else {
$days = [int]$days # Convert input to an integer
}
# Calculate time difference in milliseconds
$timeDiff = $days * 24 * 60 * 60 * 1000 # days * hours/day * minutes/hour * seconds/minute * milliseconds/second
$eventids = Read-Host -Prompt "Which EventIDs? [1] 4624=Only Successful Logons, [2] 4625=Only Errors, [3] Both (2 = Standard)"
if (-not $eventids) {
$eventids = 2
}
if ([int]$eventids -eq 3) {
$eventxml = "EventID=4624 or EventID=4625"
}
if ([int]$eventids -eq 2) {
$eventxml = "EventID=4625"
}
if ([int]$eventids -eq 1) {
$eventxml = "EventID=4624"
}
Write-Host("User: $username") -ForegroundColor Yellow
Write-Host("Days: $days") -ForegroundColor Yellow
Write-Host("Eventids: $eventxml") -ForegroundColor Yellow
if (($eventids -in (1, 2, 3)) -and ($days -ge 1) -and ($username -ne "")) {
# Construct the filter for the event log query
$filterXML = @"
<QueryList>
<Query Id="0" Path="Security">
<Select Path="Security">
*[System[Provider[@Name='Microsoft-Windows-Security-Auditing'] and ($eventxml)]]
$userxml
and
*[System[TimeCreated[timediff(@SystemTime) <= '$timeDiff']]]
</Select>
</Query>
</QueryList>
"@
Write-Host($filterXML) -ForegroundColor Magenta
# Get all domain controllers in the domain
$domainControllers = Get-ADDomainController -Filter *
$logonAttempts = get-DC-WinEvents -filterXML $filterXML -domainControllers $domainControllers
$logonAttempts | Sort-Object -Property LogonTime -Descending | Out-GridView -Title "input 4624 (successful) oder 4625 (error) or search for something else "
}