-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocked_user_bot.ps1
397 lines (360 loc) · 14.7 KB
/
locked_user_bot.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
<#
.NOTES
Filename: locked_user_bot.ps1
Prerequisites: PowerShell 7+
.DESCRIPTION
Alert techs on users locked out and allow them to unlock remotely.
#>
# Passed parameters from event log 4740
param (
$TargetUserName,
$TargetDomainName
)
# Variables
$botToken = ""
$chatID = ""
$zendeskDomain = ""
$zendeskEmail = ""
$zendeskToken = ""
# Grab other AD user vairables
$lockedUser = Get-ADUser -Identity $TargetUserName -Properties Name, lockedout, lockoutTime | Select-Object Name, lockedout, @{ Name = "LockoutTime"; Expression = { ([datetime]::FromFileTime($_.lockoutTime).ToLocalTime()) } }
$name = $lockedUser.Name
$lockoutTime = ($lockedUser.LockoutTime.ToString("hh:mm:ss tt"))
# Functions
function Send-TelegramMsg {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true,
HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$BotToken,
[Parameter(Mandatory = $true,
HelpMessage = '-#########')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$ChatID,
[Parameter(Mandatory = $true,
HelpMessage = 'Text of the message to be sent')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory = $false,
HelpMessage = 'HTML vs Markdown for message formatting')]
[ValidateSet('Markdown', 'MarkdownV2', 'HTML')]
[string]$ParseMode = 'HTML', #set to HTML by default
[Parameter(Mandatory = $false,
HelpMessage = 'Custom or inline keyboard object')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[psobject]$Keyboard,
[Parameter(Mandatory = $false,
HelpMessage = 'Disables link previews')]
[switch]$DisablePreview, #set to false by default
[Parameter(Mandatory = $false,
HelpMessage = 'Send the message silently')]
[switch]$DisableNotification
)
#------------------------------------------------------------------------
$results = $true
#------------------------------------------------------------------------
$PL = @{
chat_id = $ChatID
text = $Message
parse_mode = $ParseMode
disable_web_page_preview = $DisablePreview.IsPresent
disable_notification = $DisableNotification.IsPresent
}
if ($Keyboard) { $PL.Add('reply_markup', $Keyboard) }
#------------------------------------------------------------------------
$invokeRestMethod = @{
Uri = ('https://api.telegram.org/bot{0}/sendMessage' -f $BotToken)
Body = ([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -Compress -InputObject $PL -Depth 8)))
ErrorAction = 'Stop'
ContentType = 'application/json'
Method = 'Post'
}
#------------------------------------------------------------------------
try {
$Response = Invoke-RestMethod @invokeRestMethod
if ($Response.ok) {
return $Response.result.message_id
} else {
Write-Host "Telegram API returned an error: $($Response.description)"
return $null
}
} catch {
Write-Host "Exception caught: $_"
return $null
}
#------------------------------------------------------------------------
}#function Send-TelegramMsg
function Edit-TelegramMsg {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true,
HelpMessage = '#########:xxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxx')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$botToken,
[Parameter(Mandatory = $true,
HelpMessage = '-#########')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$chatID,
[Parameter(Mandatory = $true,
HelpMessage = '#######')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$MessageID,
[Parameter(Mandatory = $true,
HelpMessage = 'Text of the message to be sent')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Message,
[Parameter(Mandatory = $false,
HelpMessage = 'HTML vs Markdown for message formatting')]
[ValidateSet('Markdown', 'MarkdownV2', 'HTML')]
[string]$ParseMode = 'HTML', #set to HTML by default
[Parameter(Mandatory = $false,
HelpMessage = 'Custom or inline keyboard object')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[psobject]$Keyboard,
[Parameter(Mandatory = $false,
HelpMessage = 'Disables link previews')]
[switch]$DisablePreview #set to false by default
)
#------------------------------------------------------------------------
$results = $true
#------------------------------------------------------------------------
$PL = @{
chat_id = $chatID
message_id = $MessageID
text = $Message
parse_mode = $ParseMode
disable_web_page_preview = $DisablePreview.IsPresent
}
if ($Keyboard) { $PL.Add('reply_markup', $Keyboard) }
#------------------------------------------------------------------------
$invokeRestMethod = @{
Uri = ('https://api.telegram.org/bot{0}/editMessageText' -f $botToken)
Body = ([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -Compress -InputObject $PL -Depth 8)))
ErrorAction = 'Stop'
ContentType = 'application/json'
Method = 'Post'
}
#------------------------------------------------------------------------
try {
Write-Verbose -Message 'Editing Telegram message...'
$results = Invoke-RestMethod @invokeRestMethod
}#try_messageSend
catch {
Write-Warning -Message 'An error was encountered editing the Telegram message:'
Write-Error $_
$results = $false
}#catch_messageSend
return $results
#------------------------------------------------------------------------
}#function Edit-TelegramMsg
function Get-TelegramUpdates {
# API Request
$Uri = "https://api.telegram.org/bot$($botToken)/getUpdates"
$BotUpdates = Invoke-WebRequest -Uri $Uri
$BotUpdatesJson = $BotUpdates.Content | ConvertFrom-Json
return $BotUpdatesJson.result
}#function Get-TelegramUpdates
function Create-Zendesk_Ticket {
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $true,
HelpMessage = 'Title of Zendesk Ticket')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Subject,
[Parameter(Mandatory = $true,
HelpMessage = 'Main Body, of ticket to be created within Zendesk')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$Comment,
[Parameter(Mandatory = $false,
HelpMessage = 'Set Zendesk Ticket Comment Public or Private')]
[string]$IsPublic,
[Parameter(Mandatory = $false,
HelpMessage = 'Zendesk Group to be assigned to ticket')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[string]$GroupID,
[Parameter(Mandatory = $false,
HelpMessage = 'Set Zendesk Ticket Type problem, incident, question, or task')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateSet('problem', 'incident', 'question', 'task')]
[string]$Type,
[Parameter(Mandatory = $false,
HelpMessage = 'Set Zendesk Ticket Priority urgent, high, normal, or low')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateSet('urgent', 'high', 'normal', 'low')]
[string]$Priority,
[Parameter(Mandatory = $false,
HelpMessage = 'Set Zendesk Ticket Status new, open, pending, hold, solved, or closed')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateSet('new', 'open', 'pending', 'hold', 'solved', 'closed')]
[string]$Status,
[Parameter(Mandatory = $false,
HelpMessage = 'Set your tags for Zendesk')]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[array]$Tags
)
#------------------------------------------------------------------------
$results = $true
#------------------------------------------------------------------------
$header = @{
Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($zendeskEmail):$($zendeskToken)"));
}
#------------------------------------------------------------------------
$PL = @{
ticket = @{
subject = $Subject
comment = @{
body = $Comment
}
}
}
if ($GoupID) { $PL.ticket.group_id = $GroupID }
if ($Type) { $PL.ticket.type = $Type }
if ($Priority) { $PL.ticket.priority = $Priority }
if ($Status) { $PL.ticket.status = $Status }
if ($Tags) { $PL.ticket.tags = $Tags }
if ($DueDate) { $PL.ticket.due_at = $DueDate }
#------------------------------------------------------------------------
$invokeRestMethod = @{
Uri = "https://$zendeskDomain.zendesk.com/api/v2/tickets.json"
Header = $header
Body = ([System.Text.Encoding]::UTF8.GetBytes((ConvertTo-Json -Compress -InputObject $PL -Depth 8)))
ErrorAction = 'Stop'
ContentType = 'application/json'
Method = 'Post'
}
#------------------------------------------------------------------------
try {
Write-Verbose -Message 'Creating Zendesk Ticket...'
$results = Invoke-RestMethod @invokeRestMethod
}#try_messageSend
catch {
Write-Warning -Message 'An error was encountered creating the Zendesk Ticket:'
Write-Error $_
$results = $false
}#catch_messageSend
return $results
#------------------------------------------------------------------------
}#function Create-Zendesk_Ticket
#Initial Telegram Lockout Message
$Row1 = @(
@{
text = "🔓 Unlock";
callback_data = "Unlock $name $lockoutTime"
},
@{
text = "🚨 Create Ticket";
callback_data = "Ticket $name $lockoutTime"
}
)
$Row2 = @(
)
$Keyboard = @{
inline_keyboard = @(
$Row1, $Row2
)
}
$Message = @{
botToken = $botToken
chatID = $chatID
Message = "<b>Lockout Notification:</b>`nUser: <i>$name</i>`nLockout Time: $lockoutTime `nSource Machine: $TargetDomainName"
Keyboard = $Keyboard
}
$messageId = Send-TelegramMsg @Message
# Keywords
$userUnlock = "Unlock $name $lockoutTime"
$userTicket = "Ticket $name $lockoutTime"
# Define the timeout duration in minutes
$timeoutMinutes = 50
$startTime = Get-Date
# Loop flag to identify if the loop ended due to a condition
$conditionMet = $false
# Main loop to continuously check for updates allotted time in minutes
while ((Get-Date) -lt $startTime.AddMinutes($timeoutMinutes) -and -not $conditionMet) {
$BotUpdateArray = Get-TelegramUpdates
foreach ($update in $BotUpdateArray) {
if ($update.callback_query) {
$callbackQuery = $update.callback_query
# Check for unlocking user
if ($callbackQuery.message.chat.id -eq $chatID -and $callbackQuery.message.message_id -eq $messageId -and $callbackQuery.data -eq $userUnlock) {
# Query user who selected the action
$actionUserFirstName = $callbackQuery.from.first_name
$actionUserLastName = $callbackQuery.from.last_name
# Unlock User
Unlock-ADAccount $TargetUserName
Write-Host "$ActionUserFirstName $ActionUserLastName unlocked $name who was locked out at $lockoutTime"
# Edit Telegram Message
$unlockMessage = @{
botToken = $botToken
chatID = $chatID
MessageID = $messageID
Message = "$actionUserFirstName $actionUserLastName unlocked $name"
}
Edit-TelegramMsg @unlockMessage
$conditionMet = $true # Stop main loop
break # Stop the inner loop
}
# Check for creating a ticket
if ($callbackQuery.message.chat.id -eq $chatID -and $callbackQuery.message.message_id -eq $messageId -and $callbackQuery.data -eq $userTicket) {
# Query user who selected the action
$actionUserFirstName = $callbackQuery.from.first_name
$actionUserLastName = $callbackQuery.from.last_name
# Create Zendesk Ticket
$ticket = @{
Subject = "Locked User - $name"
Comment = "$actionUserFirstName $actionUserLastName wants to investigate $name further to see why they are continually getting locked out."
Type = 'task'
Priority = 'high'
Status = 'new'
Tags = 'internal, lockout'
}
Create-Zendesk_Ticket @ticket
#Update Telegram Message
$ticketMessage = @{
botToken = $botToken
chatID = $chatID
MessageID = $messageID
Message = "$actionUserFirstName $actionUserLastName created an escalated ticket for $name"
}
Edit-TelegramMsg @ticketMessage
$conditionMet = $true # Stop main loop
break # Stop the inner loop
}
}
}
# Wait for 3 seconds before the next update check
Start-Sleep -Seconds 3
}
# After the loop, check why it was stopped
if (-not $conditionMet) {
Write-Host Timeout has been reached, no action was taken on $name.
#Update Telegram Message
$timeoutMessage = @{
botToken = $botToken
chatID = $chatID
MessageID = $messageID
Message = "<b>Lockout Notification:</b>`nThe 50 minute timeout has been reached, no action was taken on $name that was locked out at $lockoutTime."
}
Edit-TelegramMsg @timeoutMessage
} else {
Write-Host "Action taken based on the condition met inside the loop."
}