-
Notifications
You must be signed in to change notification settings - Fork 1
/
script3.py
469 lines (431 loc) · 18.1 KB
/
script3.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# Shachee Mishra #
import idaapi
from idautils import *
from idc import *
import sys
import inspect
import copy
from bisect import bisect
import csv
import os
import numpy
from numpy import ndarray
import time
import random
from datetime import timedelta
start_time = time.time()
MAX_RECURSION = 5
NUMPREVINST = 15
DEBUG = 0
'''
if DEBUG:
print idc.GetOpType(addr, 1)
print "_____"
print idc.o_reg # 1
print idc.o_imm # 5
print idc.o_mem # 2
print idc.o_phrase # 3
print idc.o_displ # 4
'''
'''
import_list = ["VirtualProtect","CreateThread"
#, "VirtualProtectEx", "VirtualAllocEx"
, "VirtualAlloc"
,"LoadLibraryExA"
"LoadLibraryExW"
,"CloseHandle"
, "CreateFileA", "CreateFileW", "CreateFileMappingA","WinExec"#,"WriteFile"
#, "GetProcAddress"
#, "ReadFile"
, "CreateFileMappingW", "CreateProcessA", "CreateProcessW"
#, "DeleteFileA", "DeleteFileW", "DuplicateHandle","ExitProcess","ExitThread","GetProcAddress","accept","bind","closesocket","connect","ioctlsocket"
, "DeleteFileA", "DeleteFileW", "DuplicateHandle","ExitProcess","ExitThread","accept","bind","closesocket","connect","ioctlsocket"
, "listen" , "recv" , "send" , "socket"
, "URLDownloadToFileA" , "URLDownloadToFileW"
, " __execv"#, "fclose"
, "fopen" , "fwrite"
, "InternetOpenA" , "InternetOpenUrlA" ,"InternetOpenUrlW", "InternetOpenW", "InternetReadFile"
, "CreateRemoteThread"
]
'''
import_list = ["VirtualAlloc","VirtualProtect"]
#'''
methods = dict()
#methods["VirtualProtect"] = ["lpflOldProtect", "flNewProtect", "dwSize"]#, "lpAddress"]
methods["VirtualProtect"] = ["aa","flNewProtect","aa", "dwSize"]#, "lpAddress"]
methods["CreateThread"] = ["lpThreadId", "dwCreationFlags", "lpParameter", "lpStartAddress", "dwStackSize", "lpThreadAttributes"]
methods["CreateFileW"] = ["hTemplateFile", "dwFlagsAndAttributes", "dwCreationDisposition", "lpSecurityAttributes", "dwShareMode", "dwDesiredAccess" , "lpFileName"]
methods["VirtualProtectEx"] = ["lpflOldProtect", "flNewProtect", "dwSize", "lpAddress", "hProcess"]
methods["CreateProcessW"] = ["ProcessInfo", "StartUpInfo", "CurrentDirectory", "lpEnvironment", "lpThreadAttributes", "lpProcessAttributes", "lpCommandLine", "lpApplicationName", "bIntehirtHandles", "dwCreationFlags"]
methods["LoadLibraryExA"] = ["dwFlags", "hFile"]
methods["LoadLibraryExW"] = ["dwFlags", "hFile", "lpLibFileName"]
methods["VirtualAllocEx"] = ["flProtect", "flAllocationType", "dwSize", "lpAddress", "hProcess"]
methods["VirtualAlloc"] = ["flProtect", "flAllocationType", "dwSize", "lpAddress"]
methods["CloseHandle"] = ["hObject"]
methods["CreateFileA"] = ["hTemplateFile", "dwFlagsAndAttributes", "dwCreationDisposition", "lpSecurityAttributes", "dwShareMode", "dwDesiredAccess", "lpFileName"]
methods["CreateFileMappingA"] = ["lpName","dwMaximumSizeLow","dwMaximumSizeHigh","flProtect","lpFileMappingAttributes","hFile"]
methods["CreateFileMappingW"] = ["lpName","dwMaximumSizeLow","dwMaximumSizeHigh","flProtect","lpFileMappingAttributes","hFile"]
methods["CreateProcessA"] = ["lpProcessInformation","lpStartupInfo","lpCurrentDirectory","lpEnvironment","dwCreationFlags","bInheritHandles","lpThreadAttributes","lpProcessAttributes","lpCommandLine","lpApplicationName"]
methods["CreateProcessW"] = ["lpProcessInformation","lpStartupInfo","lpCurrentDirectory","lpEnvironment","dwCreationFlags","bInheritHandles","lpThreadAttributes","lpProcessAttributes","lpCommandLine","lpApplicationName"]
methods["CreateRemoteThread"] = ["hProcess", "lpThreadAttributes", "dwStackSize" , "lpStartAddress", "lpParameter","dwCreationFlags", "lpThreadId"]
methods["DeleteFileA"] = ["lpFileName"]
methods["DeleteFileW"] = ["lpFileName"]
methods["DuplicateHandle"] = ["dwOptions","bInheritHandle","dwDesiredAccess","lpTargetHandle","hTargetProcessHandle","hSourceHandle","hSourceProcessHandle"]
methods["ExitProcess"] = ["uExitCode"]
methods["ExitThread"] = ["dwExitCode"]
methods["GetProcAddress"] = ["lpProcName","hModule"]
methods["accept"] = ["addrlen","addr","s"]
methods["bind"] = ["namelen","name","s"]
methods["closesocket"] = ["s"]
methods["connect"] = ["namelen","name","s"]
methods["ioctlsocket"] = ["argp","cmd","s"]
methods["listen"] = ["backlog","s"]
methods["recv"] = ["flags","len","buf","s"]
methods["send"] = ["flags","len","buf","s"]
methods["socket"] = ["protocol","type","af"]
methods["WinExec"] = ["uCmdShow","lpCmdLine"]
methods["WriteFile"] = ["lpOverlapped","lpNumberOfBytesWritten","nNumberOfBytesToWrite","lpBuffer","hFile"]
methods["ReadFile"] = ["lpOverlapped","lpNumberOfBytesRead","nNumberOfBytesToRead","lpBuffer","hFile"]
methods["URLDownloadToFileA"] = ["lpfnCB","dwReserved","szFileName","szURL","pCaller"]
methods["URLDownloadToFileW"] = ["lpfnCB","dwReserved","szFileName","szURL","pCaller"]
methods["__execv"] = ["argv","cmdname"]
methods["fclose"] = ["stream"]
methods["fopen"] = ["mode","filename"]
methods["fwrite"] = ["stream","count","size","buffer"]
methods["InternetOpenA"] = ["dwFlags","lpszProxyBypass" , "lpszProxyName" , "dwAccessType" ,"lpszAgent"]
methods["InternetOpenUrlA"] = ["dwContext" , "dwFlags" , "dwHeadersLength" , "lpszHeaders" , "lpszUrl" , "hInternet"]
methods["InternetOpenUrlW"] = ["dwContext" , "dwFlags" , "dwHeadersLength" , "lpszHeaders" , "lpszUrl" , "hInternet"]
methods["InternetOpenW"] = ["dwFlags","lpszProxyBypass" , "lpszProxyName" , "dwAccessType" ,"lpszAgent"]
methods["InternetReadFile"] = ["lpdwNumberOfBytesRead" , "dwNumberOfBytesToRead" , "lpBuffer" , "hFile"]
methods["WSASocketW"] = ["dwFlags" , "g" , "lpProtocolInfo" , "protocol" , "type", "af"]
methods["WSASocketA"] = ["dwFlags" , "g" , "lpProtocolInfo" , "protocol" , "type", "af"]
methods["WSAStartup "] = ["lpWSAData","wVersionRequested"]
outfile = "out.csv"
handle = open(outfile,"w")
intStrArg1 = ["rcx","rdx","r8d", "r9d"]
intStrArg2 = ["ecx","edx","r8", "r9"]
intStrArg3 = ["rcx","rdx","r8d", "r9d"]
floatArg = ["xmm0","xmm1","xmm2", "xmm3"]
# Wrapper to operate on sorted basic blocks.
class BBWrapper(object):
def __init__(self, ea, bb):
self.ea_ = ea
self.bb_ = bb
def get_bb(self):
return self.bb_
def __lt__(self, other):
return self.ea_ < other.ea_
# Creates a basic block cache for all basic blocks in the given function.
class BBCache(object):
def __init__(self, f):
self.bb_cache_ = []
for bb in idaapi.FlowChart(f):
self.bb_cache_.append(BBWrapper(bb.startEA, bb))
self.bb_cache_ = sorted(self.bb_cache_)
def find_block(self, ea):
#i = bisect_right(self.bb_cache_, BBWrapper(ea, None))
i = bisect(self.bb_cache_, BBWrapper(ea, None))
if i:
return self.bb_cache_[i-1].get_bb()
else:
return None
def get_prev_BBs(addr, cur_bb, bb_dep, bb_startEA, bb_endEA):
prevAdd = idc.PrevHead(addr)
if addr != bb_startEA[str(cur_bb)] :
return cur_bb
else:
if str(cur_bb) in bb_dep:
prev_bb = bb_dep[str(cur_bb)]
#print "Reurning: %s for %s" %(prev_bb,cur_bb)
return list(prev_bb)
else:
#print "Returing blank for %s" % (cur_bb)
return None
def ret_bb_deps(addr):
#Get Flowchart of function, the current address belongs to
f = idaapi.FlowChart(idaapi.get_func(addr))#here()))
#Check Successor n predecessor blocks for each
bb_dep = dict()
bb_startEA = dict()
bb_endEA = dict()
bb_cache = BBCache(idaapi.get_func(addr))
found = bb_cache.find_block(addr)
#Finding Basic Block dependencies
for block in f:
bb_startEA[str(block.id)] = block.startEA
bb_endEA[str(block.id)] = idc.PrevHead(block.endEA)
for succ_block in block.succs():
#print "Block id: %s, succ id: %s" % (block.id, succ_block.id)
if not str(succ_block.id) in bb_dep.keys():
bb_dep[str(succ_block.id)] = set()
bb_dep[str(succ_block.id)].add(block.id)
for pred_block in block.preds():
if not str(block.id) in bb_dep.keys():
bb_dep[str(block.id)] = set()
#print "Block id: %s, prev id: %s" % (block.id, pred_block.id)
bb_dep[str(block.id)].add(pred_block.id)
return bb_dep, bb_startEA, bb_endEA, found
def getDRegName(addr):
return GetOpnd(addr,0)
def getArgLoc(reg):
if reg=="rcx" or reg=="ecx" or reg=="xmm0":
return 1
elif reg=="rdx" or reg=="edx" or reg=="xmm1":
return 2
elif reg=="r8d" or reg=="r8" or reg=="xmm2":
return 3
elif reg=="r9d" or reg=="r9" or reg=="xmm3":
return 4
else:
if reg.find('rsp+')!=-1:
return 5
return 0
#Function to find register values
def find_value(addr, reg_name, bb, bb_dep, bb_startEA, bb_endEA,depth):
if depth>=MAX_RECURSION:
#print "returning NF 4"
return "NF"
pred_bb_iter = -1
'''
if DEBUG:
for i in bb_dep:
print "BlockID: %s, {%X,%X}" % (i, bb_startEA[str(i)], bb_endEA[str(i)])
print bb_dep[i]
'''
#rn = random.random()
ret_set = list()
if DEBUG:
print "Cur BB : %s , lookingf for : %s" % (bb, reg_name)
while True:
inst = idc.GetMnem(addr)
dest = idc.GetOpnd(addr, 0)
if dest == reg_name or dest == equiReg(reg_name):
if inst=="xor":
ret_set.insert(0,0)
if DEBUG:
print "Setting for xor"
return ret_set
break
elif inst == "mov":
if dest==reg_name:
new_bb = get_prev_BBs(addr, bb, bb_dep, bb_startEA, bb_endEA)
if new_bb == bb:
addr = idc.PrevHead(addr)
#Pred Basic block
else:
if new_bb == None:
if ret_set == []:
return None
return ret_set
pred_bb_iter = pred_bb_iter + 1
while pred_bb_iter < int(len(new_bb)):
bb_this_iter = new_bb[pred_bb_iter] #str(next(iter(new_bb)))
#print "new bB: %s , origin : %s" % (bb_this_iter, bb)
if int(bb_this_iter) >= int(bb):
if DEBUG:
print " continuing"
pred_bb_iter = pred_bb_iter + 1
continue;
#print "This bb_iter: " + str(bb_this_iter)
addr = bb_endEA[str(bb_this_iter)]
#print "Bfre %X" %(addr)
rTemp = find_value(addr, reg_name, bb_this_iter, bb_dep, bb_startEA, bb_endEA,depth+1)
if rTemp!=None:
ret_set.insert(0,rTemp)
#print "return for " + str(bb) + " dep:" + str(bb_this_iter) + "this ::: " + str(pred_bb_iter)
pred_bb_iter = pred_bb_iter + 1
else:
continue
else:
return ret_set
break
def equiReg(name):
if name=="ecx":
return "rcx"
if name=="edx":
return "rdx"
if name=="rdx":
return "edx"
if name=="rcx":
return "ecx"
def getRegUsed(name):
if name.find('rcx+') != -1:
return name[name.find('rcx+'):3]
if name.find('rdx+') != -1:
return name[name.find('rdx+'):3]
def get_reg_arg(addr, name):
functions = copy.deepcopy(methods)
handle.write("\n" + name + "\n")
if DEBUG:
print "*************"
print "for" + name
s = ()
#print functions[name]
numArg = len(functions[name])
if DEBUG:
print numArg
arrArg = ["O"] * numArg
stackArg = 5
if DEBUG:
print "#### StackArg: " + str(stackArg)
bb_dep, bb_startEA, bb_endEA, found = ret_bb_deps(addr)
while True:
if numArg<=0:
#print "Break Now"
break
addr = idc.PrevHead(addr)
inst = idc.GetMnem(addr)
#print "Left Args:: " + str(numArg)
if DEBUG:
print "instruction: " + inst
if inst == "xor":
regName = getDRegName(addr)
argLoc = getArgLoc(regName)
if DEBUG:
print "[xor] Setting : " + str(argLoc)
if argLoc <=4:
arrArg[argLoc-1] = "I"
else:
arrArg[stackArg-1] = "I"
stackArg+=1
#print arrArg
numArg-=1
elif inst == "mov" or inst == "lea" or inst =="test":
regName = getDRegName(addr)
if DEBUG:
print "Register here: " + regName
if regName==0:
continue
argLoc = getArgLoc(regName)
if DEBUG:
print "******Location::" + str(argLoc)
if argLoc>4:
if DEBUG:
print "In stack stackArg" + str(stackArg)
argLoc=stackArg
stackArg+=1
if idc.GetOpType(addr, 1) == idc.o_reg:
if DEBUG:
print "argLoc here: ::::::::: " + str(argLoc)
reg_val = find_value(addr, regName, found.id, bb_dep, bb_startEA, bb_endEA, 0)
#print "REG --------------------------"
if DEBUG:
print ":" + str(reg_val)
arrArg[argLoc - 1] = "R"
#print "Register found"
elif idc.GetOpType(addr, 1) == idc.o_imm:
arrArg[argLoc - 1] = "I"
if DEBUG:
print "Immediate value found "
print arrArg
elif idc.GetOpType(addr, 1) == idc.o_mem:
arrArg[argLoc - 1] = "A"
if DEBUG:
print "Memory address found"
elif idc.GetOpType(addr, 1) == idc.o_phrase:
arrArg[argLoc - 1] = "F"
if DEBUG:
print "Memory Ref found"
elif idc.GetOpType(addr, 1) == idc.o_displ:
arrArg[argLoc - 1] = "D"
regUsed = getRegUsed(regName);
reg_val = find_value(addr, regName, found.id, bb_dep, bb_startEA, bb_endEA, 0)
if reg_val==None:
reg_val = find_value(addr, equiReg(regName), found.id, bb_dep, bb_startEA, bb_endEA, 0)
if reg_val:
arrArg[argLoc-1] = "I"
if DEBUG:
print "Memory (base+reg+disp) found"
print arrArg
else:
arrArg[argLoc - 1] = "0"
if DEBUG:
print "None found"
if DEBUG:
print str(GetOpnd(addr, 1)) + "=>" + str(GetOperandValue(addr,1)) #GetDisasm(addr).split(";")[0]
numArg-=1
elif inst=="and" or inst=="sub" or inst=="add" or inst=="or":
reg1= getDRegName(addr)
reg2 = GetOpnd(addr, 1)
argLoc = getArgLoc(reg1)
arrArg[argLoc-1] = "L"
#Find Regs 1,2
if DEBUG:
print "To Find now: " + reg1 + " " + inst + " " + reg2
reg1_val = find_value(addr, reg1, found.id, bb_dep, bb_startEA, bb_endEA, 0)
if idc.GetOpType(addr, 1) == idc.o_reg:
if DEBUG:
print "reg2: " + str(reg2)
reg2_val = find_value(addr, reg2, found.id, bb_dep, bb_startEA, bb_endEA, 0)
elif idc.GetOpType(addr, 1) == idc.o_imm:
reg2_val = GetOpnd(addr, 1)
else:
reg2 = "NF"
if DEBUG:
print "reg1: " + str(reg1_val)
print "reg2: " + str(reg2_val)
numArg-=1
else:
continue
s = s + (str(arrArg),)
print frozenset(s)
# return frozenset(s)
return s
def find_xrefs(ea, name):
setArgs = set()
for addr in XrefsTo(ea, flags=0):
if str(XrefTypeName(addr.type))=="Code_Near_Call":
#print hex(addr.frm)
#print "%08x" % (addr.frm)
stack_args = get_reg_arg(addr.frm, name)
#print stack_args
#setArgs.add(stack_args)
#for x in setArgs:
# target.writerow(list(x))
#mat = numpy.row_stack(setArgs)
numOfUniqueInstances = len(setArgs)
numArgs = len(methods[name])
print "numArgs " + str(numArgs)
toCompare = ''
predNum = 0
for i in range(0, numOfUniqueInstances):
toCompare = toCompare + str("I")
for c in range(0,numArgs ):
txt = ''
for r in range(0, numOfUniqueInstances):
txt = txt + mat[r][c]
if txt==toCompare:
predNum = predNum + 1
print "predNums " + str(predNum)
target.writerow([name,numArgs,predNum])
def imp_cb(ea, name, ord):
if not name:
print "%08x: ord#%d" % (ea, ord)
else:
if name in import_list:
print "%08x: %s (ord#%d)" % (ea, name, ord)
find_xrefs(ea, name)
# True -> Continue enumeration
# False -> Stop enumeration
return True
nimps = idaapi.get_import_module_qty()
print "Found %d import(s)..." % nimps
#CSV File To Write
fileName = idaapi.get_root_filename().split('.')[0] +'.csv'
filePath = os.path.expanduser('~') + "\\Documents\\shredder\\Shredder64\\" + fileName
try:
os.remove(filePath)
except OSError:
pass
target = csv.writer(open(filePath, 'wb'))
for i in xrange(0, nimps):
name = idaapi.get_import_module_name(i)
if not name:
print "Failed to get import module name for #%d" % i
continue
#print "Walking-> %s" % name
#if name=="KERNEL32":
idaapi.enum_import_names(i, imp_cb)
print "All done..."
print("--- %s seconds ---" % (time.time() - start_time))
handle.close()