-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMain_alyac.py
387 lines (309 loc) · 10.8 KB
/
Main_alyac.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
384
385
386
387
# -*- coding: utf-8 -*-
from pydbg import *
from pydbg.defines import *
import utils
import random
import threading
import os
import shutil
import time
import sys
import DOC_fuzzer
import PE_fuzzer
import COMP_fuzzer
import subprocess
import re
class file_fuzzer:
def __init__(self, exe_path):
self.mutate_count = 100
self.mutate_list = []
self.selected_list = [] # 크래시 트래킹에 사용할 리스트
self.eip_list = [] #크래시 중복체크 (EIP 기준)
self.exe_path = exe_path
self.orig_file = None
self.sample_dir = "C:\\fuzz\\in_alyac"
self.tmp_file = None
self.tmp_dir = "C:\\fuzz\\temp_alyac"
self.count = 0
self.max = 0
self.crash = None
self.crash_tracking = False # 크래시 추적 활성화 체크
self.crash_count = None # 크래시 번호 저장
self.tracking_count = 0 # 트래킹 카운트 저장(무한루프 방지)
self.check = False
self.pid = None
self.in_accessv_handler = False
self.dbg = None
self.running = False
self.filename = ""
self.ord_ads = False
self.pid_exe = None
self.running_alyac = False
self.running_aye = False
self.running_cra = False
self.pid_aye = None
self.dbg_aye = None
def wincmd(self, cmd):
return subprocess.Popen(cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
def file_picker_setting(self):
cmd = "dir " + self.sample_dir
pipe = self.wincmd(cmd)
output, errors = pipe.communicate()
pipe.stdin.close()
self.max = int(re.findall('\d+', output.split("\n")[-3])[0])
# 파일 선택
def file_picker(self):
file_list = os.listdir(self.sample_dir)
file_num = self.count % self.max
sel_file = str(time.time()).replace(".", "") + "-" + str(file_num) + "-" + file_list[file_num]
self.tmp_file = self.tmp_dir + "\\" + sel_file
self.orig_file = self.sample_dir + "\\" + file_list[file_num]
## shutil.copy(self.orig_file, self.tmp_file)
return
# 에러를 추적하고 정보를 저장하기 위한 접근 위반 핸들러
def handler_access_violation(self, pydbg):
self.running_cra = True
print "\n[-] Access_violation Crash!!\n"
print "[-] Woot! Handling an access violation!"
print "[-] EIP : 0x%08x" % self.dbg_aye.context.Eip
# eip 리스트에 추가
self.eip_list.append(self.dbg_aye.context.Eip)
# 트래킹 활성화
# self.crash_tracking = True
# self.in_accessv_handler = True
crash_bin = utils.crash_binning.crash_binning()
crash_bin.record_crash(self.dbg_aye)
self.crash = crash_bin.crash_synopsis()
# 크래시 일 때 카운트정보를 작성한다.
self.crash_count = self.count
# 크래시 정보 로깅
crash_fd = open("C:\\fuzz\\crash_alyac\\" + self.tmp_file.split("\\")[-1] + "-%d.log" % self.count,"w")
crash_fd.write(self.crash)
crash_fd.close()
# 원본 파일을 백업한다.
shutil.copy(self.tmp_file, "C:\\fuzz\\crash_alyac\\" + self.tmp_file.split("\\")[-1].split(".")[0] + "-" + str(self.count) + "." + self.tmp_file.split(".")[-1] )
self.dbg_aye.terminate_process()
self.dbg_aye.close_handle(self.dbg_aye.h_process)
self.dbg_aye.detach()
self.pid_aye = None
self.running_aye = False
print "[*] Restart AYRTSrv"
self.running_aye = False
restart_thread = threading.Thread(target=self.kill_AYRTSrv)
restart_thread.setDaemon(0)
restart_thread.start()
while self.running_aye == False:
while self.running_alyac == False:
time.sleep(0.5)
time.sleep(10)
os.system("taskkill /F /IM ALYac.aye")
time.sleep(2)
pydbg_aye_thread = threading.Thread(target=self.start_AYRTSrv_debugger)
pydbg_aye_thread.setDaemon(0)
pydbg_aye_thread.start()
print "[-]Fin save crash & restart AYRTSrv"
self.running_cra = False
self.running = False
return DBG_EXCEPTION_NOT_HANDLED
def fuzz(self):
self.file_picker_setting()
# adssvc.exe에 디버거
debugger_thread = threading.Thread(target=self.start_AYRTSrv_debugger)
debugger_thread.setDaemon(0)
debugger_thread.start()
print "started debugger"
while self.pid_aye == None:
time.sleep(0.5)
while 1:
self.count +=1
while self.running or self.running_cra:
time.sleep(1)
self.running = True
print "[*] Starting Antivirus for iteration: %d" % self.count
# 크래시 추적 활성화 여부 체크
if self.crash_tracking == False:
# 먼저 변형을 가할 파일을 선택한다.
self.file_picker()
self.mutate_file()
else: #크래시 추적이 활성화 되었으면
print "[ * ] Crash Tracking Start !!!", self.orig_file
# 크래시 난 파일 복사
shutil.copy(self.orig_file, self.tmp_file)
# 트래킹하는 뮤테이션 함수 호출
self.mutate_track()
# 실행파일 쓰레드 실행
pydbg_thread = threading.Thread(target=self.start_exe)
pydbg_thread.setDaemon(0)
pydbg_thread.start()
'''
# Attack AYRTSrv.exe
if self.count == 5:
attack_thread = threading.Thread(target=self.attack_debugger)
attack_thread.setDaemon(0)
attack_thread.start()
'''
while self.running_cra:
time.sleep(1)
# 모니터링 쓰레드 실행
monitor_thread = threading.Thread(target=self.monitor_exe)
monitor_thread.setDaemon(0)
monitor_thread.start()
if (self.count % 100) == 99:
time.sleep(5)
print "[*] Restart AYRTSrv"
self.running_aye = False
restart_thread = threading.Thread(target=self.kill_AYRTSrv)
restart_thread.setDaemon(0)
restart_thread.start()
while self.running_aye == False:
while self.running_alyac == False:
time.sleep(0.5)
time.sleep(10)
os.system("taskkill /F /IM ALYac.aye")
time.sleep(2)
pydbg_aye_thread = threading.Thread(target=self.start_AYRTSrv_debugger)
pydbg_aye_thread.setDaemon(0)
pydbg_aye_thread.start()
def kill_AYRTSrv(self):
print "[-] Start to kill process"
while True:
self.running_alyac = False
os.system("taskkill /F /IM AYCon.exe")
time.sleep(0.5)
os.system("taskkill /F /IM AYRTSrv.aye")
time.sleep(3)
os.system("taskkill /F /IM ALYac.aye")
time.sleep(3)
self.running_alyac = True
os.system( "\"C:\\Program Files\\ESTsoft\\Alyac\\ALYac.aye\"" )
cmd = "tasklist /FI \"IMAGENAME eq ALYac.aye\" /FO LIST"
pipe = self.wincmd(cmd)
output1, errors1 = pipe.communicate()
pipe.stdin.close()
cmd = "tasklist /FI \"IMAGENAME eq AYRTSrv.exe\" /FO LIST"
pipe = self.wincmd(cmd)
output2, errors2 = pipe.communicate()
pipe.stdin.close()
if output2.split("\n")[0].encode("hex") == "0d":
self.running_aye = True
break;
# 대상 어플리케이션을 실행시키는 디버거 쓰레드
def start_AYRTSrv_debugger(self):
self.running_aye = True
self.dbg_aye = pydbg()
cmd = "tasklist /FI \"IMAGENAME eq AYRTSrv.aye\" /FO LIST"
pipe = self.wincmd(cmd)
output, errors = pipe.communicate()
pipe.stdin.close()
if output.find("PID") < 0:
print "[-] Error on start"
else:
self.pid_aye = output.split("\n")[2].split(" ")[-1]
print self.pid_aye
self.dbg_aye.set_callback(EXCEPTION_ACCESS_VIOLATION, self.handler_access_violation )
self.dbg_aye.attach(int(self.pid_aye,10))
print "[+] Attach debugger to AYRTSrv : " + str(self.pid_aye)
self.dbg_aye.run()
def attack_debugger(self):
print "[!] Start attack : " + str( self.dbg_aye.pid )
# self.dbg_aye.suspend_all_threads()
for thread_id in self.dbg_aye.enumerate_threads():
thread_handle = self.dbg_aye.open_thread(thread_id)
thread_context = self.dbg_aye.get_thread_context(thread_handle)
# print "Eip = 0x%08x" % thread_context.Eip
thread_context.Eip=0xdeadbeef
self.dbg_aye.set_thread_context(thread_context,0,thread_id)
thread_context = self.dbg_aye.get_thread_context(thread_handle)
# print "new Eip = 0x%08x" % thread_context.Eip
# self.dbg_aye.resume_all_threads()
# pydbg.debug_event_loop(self.dbg_aye)
print "[!] Fin attack : "
# 대상 어플리케이션을 실행
def start_exe(self):
self.running = True
print self.tmp_file
while True :
cmd = "\"" + self.exe_path + "\" -s " + self.tmp_file
# print cmd
pipe = self.wincmd(cmd)
pipe.stdin.close()
cmd = "tasklist /FI \"IMAGENAME eq AYCon.exe\" /FO LIST"
pipe = self.wincmd(cmd)
output, errors = pipe.communicate()
pipe.stdin.close()
if output.find("PID") > 0:
self.pid_exe = output.split("\n")[2].split(" ")[-1]
print "PID is " + self.pid_exe
break
else:
print "no aycon"
print "hello"
# 어플레킹션을 몇 초 동안 실행 되게 한 다음 종료시키는 모니터링 쓰레드
def monitor_exe(self):
while self.pid_exe == None:
time.sleep(0.5)
'''
counter = 0
print "[*] waiting ",
while counter < 5 and self.pid_exe != None:
time.sleep(1)
print ".",
counter += 1
print "\n"
#if self.in_accessv_handler != True:
os.system("taskkill /F /IM AYCon.exe")
#else:
# while self.pid_exe != None:
# time.sleep(0.5)
'''
while(1):
cmd = "tasklist /FI \"IMAGENAME eq AYCon.exe\" /FO LIST"
pipe = self.wincmd(cmd)
output, errors = pipe.communicate()
pipe.stdin.close()
if output.find("PID") > 0:
continue
else:
break
self.in_accessv_handler = False
self.running = False
def mutate_file( self ):
DOC_list = ["hwp", "doc", "ppt", "xls", "pdf", "chm", "rtf"]
PE_list = ["exe"]
COMP_list = ["zip", "gz", "7z", "rar"]
print "[*] Selected file : %s" % self.orig_file
ext = self.orig_file.split(".")[-1]
if(ext in COMP_list):
#print self.sample_dir
#print self.tmp_dir+ "\\" + self.tmp_file.split("\\")[-1].split("-")[0]
#print self.tmp_file
fuzzer = COMP_fuzzer.COMP_FUZZ(self.sample_dir + "\\", self.tmp_dir+ "\\" + self.tmp_file.split("\\")[-1].split("-")[0] + "-" + self.tmp_file.split("\\")[-1].split("-")[1] + "-" , self.tmp_file.split("-")[-1])
fuzzer.Mutation()
if(ext in PE_list):
#print self.sample_dir
#print self.tmp_dir
#print self.orig_filee
#print self.tmp_file
fuzzer = PE_fuzzer.PE_FUZZ(self.sample_dir + "\\", self.tmp_dir+ "\\" + self.tmp_file.split("\\")[-1].split("-")[0] + "-" + self.tmp_file.split("\\")[-1].split("-")[1] + "-", self.tmp_file.split("-")[-1])
fuzzer.Mutation()
if(ext in DOC_list):
#print self.sample_dir
#print self.tmp_dir
#print self.orig_file
fuzzer = DOC_fuzzer.DOC_FUZZ(self.sample_dir + "\\", self.tmp_dir+ "\\" + self.tmp_file.split("\\")[-1].split("-")[0] + "-" + self.tmp_file.split("\\")[-1].split("-")[1] + "-", self.tmp_file.split("-")[-1])
fuzzer.Mutation()
print "[*] Fin Fuzz"
return
if __name__ == "__main__":
os.system( "mkdir C:\\fuzz\\in_alyac C:\\fuzz\\temp_alyac C:\\fuzz\\crash_alyac" )
print "[*] File Fuzzer."
exe_path = ("C:\\Program Files\\ESTsoft\\Alyac\\AYCon.exe")
if exe_path is not None:
fuzzer = file_fuzzer( exe_path)
fuzzer.fuzz()
else:
"[+] Error!"