-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathGet-UserRDPLogon.ps1
315 lines (256 loc) · 12.9 KB
/
Get-UserRDPLogon.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
<#
.SYNOPSIS
A PowerShell module that searches Windows RDP Logon events for a specified user, on a specified system, for a specified timeframe.
Name: Get-UserRDPLogon
Author: Spencer Alessi (@techspence)
License: MIT License
Assumptions: Run as Administrator.
Requirements: 'Audit Logon Success and Failure' must be enabled in Group Policy for Security-Auditing 4624 Events.
*LocalSessionManager Events require no pre-requisites.
To import module, use: PS C:\>. .\Get-UserRDPLogon.ps1
.DESCRIPTION
The categories below describe which Events withing which Providers are searched:
=======================================================================
Microsoft-Windows-TerminalServices-LocalSessionManager/Operational
=======================================================================
Provider Name: Microsoft-Windows-TerminalServices-LocalSessionManager
Event ID: 21
Event Type: Logon
Log Size: 1028KB (~14 days of events for a terminal server)
Description: Remote Desktop Services: Session logon succeeded:
User: GLOBALDYNAMICS\jcarter
Session ID: 46
Source Network Address: 192.168.0.123
-----------------------------------------------------------------------
=======================================================================
Microsoft-Windows-Security-Auditing
=======================================================================
*WARNING: Searching 4624 events can take a while, especially if
querying multiple days*
Provider Name: Microsoft-Windows-Security-Auditing
Event ID: 4624
Event Type: Type 7 (Reconnection) & 10 (Remote Interactive)
Log Size: 20480KB (~6 days of events for a terminal server)
Description: An account was successfully logged on.
Subject:
Security ID: SYSTEM
Account Name: SARAH$
Account Domain: GLOBALDYNAMICS
Logon ID: 0x6A9
Logon Type: 10
Impersonation Level: Impersonation
New Logon:
Security ID: GLOBALDYNAMICS\jcarter
Account Name: jcarter
Account Domain: GLOBALDYNAMICS
Logon ID: 0x3G7C926C1
Logon GUID: {123a45c7-8901-d2e3-4cdb-d2e3af58d2e3}
Process Information:
Process ID: 0x652c
Process Name: C:\Windows\System32\winlogon.exe
Network Information:
Workstation Name: SARAH
Source Network Address: 192.168.0.123
Source Port: 0
-----------------------------------------------------------------------
=======================================================================
Microsoft-Windows-Security-Auditing
=======================================================================
Provider Name: Microsoft-Windows-Security-Auditing
Event ID: 4801
Event Type: Windows Unlock
Log Size: 20480KB (~6 days of events for a terminal server)
Description: The workstation was unlocked.
Subject:
Security ID: GLOBALDYNAMICS\jcarter
Account Name: jcarter
Account Domain: GLOBALDYNAMICS
Logon ID: 0x3AC282179
Session ID: 22
-----------------------------------------------------------------------
.PARAMETER User
This is the user you want to search logon events for.
.PARAMETER Server
This is the system (workstation or server) that you want to search.
.PARAMETER Days
This is the number of days prior to today you want to search.
.PARAMETER Max
This is the maximum number of events you want to search through.
.EXAMPLE
Get-UserRDPLogon -User jcarter -Server sarah -Days 5
.LINK
https://ponderthebits.com/2018/02/windows-rdp-related-event-logs-identification-tracking-and-investigation/
#>
function Get-UserRDPLogon {
param (
[Parameter(Mandatory = $true)]
[string]$User,
[Parameter(Mandatory = $true)]
[string]$Server,
[Parameter(Mandatory = $true)]
[int32]$Days,
[Parameter(Mandatory = $false)]
[int32]$Max = 100
)
$Output = @()
$SecOutput = @()
$UnlockOutput = @()
$Date = (Get-Date).AddDays(-$Days)
#########################################################################
#### PROVIDER: Microsoft-Windows-TerminalServices-LocalSessionManager ###
#### EVENT ID: 21 ###
#### TYPE: Logon ###
#########################################################################
try {
$LocalSessionEvents = Get-WinEvent -FilterHashtable @{logname='Microsoft-Windows-TerminalServices-LocalSessionManager/Operational'; id=21; starttime=$Date;} -ComputerName $Server -MaxEvents $Max -ErrorAction Stop
}
catch {
Write-Warning "Error obtaining LocalSessionManager events. There may be no results that match your criteria. Bummer.. :O("
}
Foreach ($Event in $LocalSessionEvents){
[array]$EventUser = $Event.Message | select-String "$User"
$Username = $EventUser.Matches.Value
[array]$MessageArray = $Event.message.split("`n")
$Message = $MessageArray[0]
$SessionID = $MessageArray[3].Split(':')[1].trim()
$IP = $MessageArray[4].Split(':')[1].trim()
$EventID = $Event.Id
$EventDate = $Event.TimeCreated
if ($Username -eq $User) {
$Eventobj = New-Object -TypeName PSObject -Property @{
"User" = $Username
"Server" = $Server
"Date" = $EventDate
"SourceIP" = $IP
"Message" = $Message
"SessionID" = $SessionID
"EventID" = $EventID
}
$Output += $Eventobj
} else {
}
}
#########################################################################
### PROVIDER: Microsoft-Windows-Security-Auditing ###
### EVENT ID: 4624 ###
### TYPE: 7 (Reconnection) & 10 (Remote Interactive) ###
#########################################################################
try {
$SecurityEvents = Get-WinEvent -FilterHashtable @{logname='Security'; id=4624; starttime=$Date;} -ComputerName $Server -MaxEvents $Max -ErrorAction Stop
}
catch {
Write-Warning "Error obtaining Security events. There may be no results that match your criteria. Bummer.. :O("
}
Foreach ($SecurityEvent in $SecurityEvents){
$LogonTypeString = "Logon Type:"
$idx = $SecurityEvent.Message.IndexOf($LogonTypeString)
$LogonType = $SecurityEvent.Message.Substring( $idx + $LogonTypeString.length, 7).trim()
[array]$MessageArray = $SecurityEvent.message.split("`n")
$Secusername = $MessageArray[14].Split(":")[1].trim()
if($Secusername -eq $User) {
$IP = $MessageArray[25].Split(':')[1].trim()
$EventID = $SecurityEvent.ID
$Message = $MessageArray[0]
$EventDate = $SecurityEvent.TimeCreated
if ($LogonType -eq 7 -or $LogonType -eq 10) {
$SecEventobj = New-Object -TypeName PSObject -Property @{
"User" = $User
"Server" = $Server
"Date" = $EventDate
"SourceIP" = $IP
"Message" = $Message
"EventID" = $EventID
"LogonType" = $LogonType
}
$SecOutput += $SecEventobj
} else {
}
} else {}
}
#########################################################################
### PROVIDER: Microsoft-Windows-Security-Auditing ###
### EVENT ID: 4801 ###
### TYPE: Unlock ###
#########################################################################
try {
$UnlockEvents = Get-WinEvent -FilterHashtable @{logname='Security'; id=4801; starttime=$Date;} -ComputerName $Server -MaxEvents $Max -ErrorAction Stop
}
catch {
Write-Warning "Error obtaining Unlock events. There may be no results that match your criteria. Bummer.. :O("
}
Foreach ($UnlockEvent in $UnlockEvents){
[array]$MessageArray = $UnlockEvents.message.split("`n")
$Username = $MessageArray[4].Split(':')[1].trim()
$Message = $MessageArray[0]
$SessionID = $MessageArray[7].Split(':')[1].trim()
$EventID = $UnlockEvent.Id
$EventDate = $UnlockEvent.TimeCreated
if($Username -eq $User) {
$UnlockEventobj = New-Object -TypeName PSObject -Property @{
"User" = $User
"Server" = $Server
"Date" = $EventDate
"SourceIP" = $IP
"Message" = $Message
"EventID" = $EventID
}
$UnlockOutput += $UnlockEventobj
} else {
}
}
#################
### FUNCTIONS ###
#################
function Display-LocalSession {
"`n#########################################################################"
"#### PROVIDER: Microsoft-Windows-TerminalServices-LocalSessionManager ###"
"#### EVENT ID: 21 ###"
"#### TYPE: Logon ###"
"#########################################################################"
($Output | FT -Wrap `
@{Name = "User"; Expression = {$_.User}; Alignment = "Left"},
@{Name = "Server"; Expression = {$_.Server}; Alignment = "Left"},
@{Name = "SourceIP"; Expression = {$_.SourceIP}; Alignment = "Left"},
@{Name = "SessionID"; Expression = {$_.SessionID}; Alignment = "center"},
@{Name = "EventID"; Expression = {$_.EventID}; Alignment = "center"},
@{Name = "Date"; Expression = {$_.Date}; Alignment = "Left"},
@{Name = "Message"; Expression = {$_.Message}; Alignment = "Left"} `
| Out-String).Trim()
}
function Display-SecurityAuditing {
"`n#########################################################################"
"### PROVIDER: Microsoft-Windows-Security-Auditing ###"
"### EventID: 4624 ###"
"### TYPE: 7 (Reconnection) & 10 (Remote Interactive) ###"
"#########################################################################"
($SecOutput | FT -Wrap `
@{Name = "User"; Expression = {$_.User}; Alignment = "Left"},
@{Name = "Server"; Expression = {$_.Server}; Alignment = "Left"},
@{Name = "SourceIP"; Expression = {$_.SourceIP}; Alignment = "Left"},
@{Name = "EventID"; Expression = {$_.EventID}; Alignment = "center"},
@{Name = "LogonType"; Expression = {$_.LogonType}; Alignment = "center"},
@{Name = "Date"; Expression = {$_.Date}; Alignment = "Left"},
@{Name = "Message"; Expression = {$_.Message}; Alignment = "Left"} `
| Out-String).Trim()
}
function Display-WindowsUnlock {
"`n#########################################################################"
"### PROVIDER: Microsoft-Windows-Security-Auditing ###"
"### EventID: 4801 ###"
"### TYPE: Unlock ###"
"#########################################################################"
($UnlockOutput | FT -Wrap `
@{Name = "User"; Expression = {$_.User}; Alignment = "Left"},
@{Name = "Server"; Expression = {$_.Server}; Alignment = "Left"},
@{Name = "EventID"; Expression = {$_.EventID}; Alignment = "center"},
@{Name = "Date"; Expression = {$_.Date}; Alignment = "Left"},
@{Name = "Message"; Expression = {$_.Message}; Alignment = "Left"} `
| Out-String).Trim()
}
#################
### DISPLAY ###
#################
Display-LocalSession
Display-SecurityAuditing
Display-WindowsUnlock
}