forked from Yanivmd/TRACY
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathx86Analyzer.py
324 lines (224 loc) · 11.9 KB
/
x86Analyzer.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
#-------------------------------------------------------------------------------
# Name: module1
# Purpose:
#
# Author: user
#
# Created: 17/08/2013
# Copyright: (c) user 2013
# Licence: <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python
import difflib
import inspect
import myutils
import itertools
import logging
import sys
def seperateCmd(cmd):
cmdFields = filter(None,cmd.split(" "))
if len(cmdFields) >= 3:
if "," in cmdFields[-2]:
#print cmd
#if "mov" in cmd:
# if cmd.count("mov") > 1:
# print "too much in one cmd:" + cmd
# assert(False)
last = cmdFields[-1]
cmdFields = cmdFields[0:-1]
cmdFields[-1] += " " + last
return cmdFields
def isRegisterStr(Str):
# the s: is a really nasty hack to get "ds:DWORD" and "cs:DWORD" to count as r
#"s:"
Registers = ["ax","bx","cx","dx","si","di","bp","sp"]
return Str in Registers or Str[1:] in Registers
def isVarStr(Str):
return Str.startswith("var_")
def isOffset(Str):
return Str.startswith("offset") or Str.startswith("(offset")
def isCall(cmdStr):
return cmdStr.startswith("call ")
def alwaysTrue(x):
return True
class RWEngineBase():
def getRW(self):
raise NotImplementedError('This method must be overriden')
class X86AnalyzerBase(RWEngineBase):
printMatchedCmds = False
printCmdMatchErrors = False
FUNCNAME = "FunctionNames"
REGISTER = "Registers"
VAR = "Var"
OTHER = "Other"
def getEmptyDict(self):
# this will be the dict of cmds type
rewriteDict = dict()
rewriteDict[self.FUNCNAME] = ({'testerFunction':alwaysTrue,'Mergeable':True,'isArgument':False,'useAble':True})
rewriteDict[self.REGISTER] = ({'testerFunction':isRegisterStr,'Mergeable':False,'isArgument':True,'useAble':True})
rewriteDict[self.VAR] = ({'testerFunction': isVarStr,'Mergeable':True,'isArgument':True,'useAble':True})
rewriteDict[self.OTHER] = ({'testerFunction': alwaysTrue,'Mergeable':True,'isArgument':True,'useAble':False})
return rewriteDict
# this gets MatchedCmds tuple, (rest = target (tarStr) and reference (refStr) , for debug)
def __init__(self,nodeGradesInfos=[]):
self.rewriteDict = self.getEmptyDict()
self.BlacklistDict = self.getEmptyDict()
self.generation = 0
self.nodeGradesInfos = nodeGradesInfos
if len(nodeGradesInfos)==0:
return
if (self.printMatchedCmds):
# TODO fix this with chain..
raise hell
for match in self.matchedCmds:
print match
# this will add recorded value to dict, even if there is a conflict it will be recorded...
def insertToDictWithType(self,tarCmdNum,fromStr,refCmdNum,toStr,typeStr,dict2insert=None):
raise NotImplementedError('This method must be overriden')
def __insertToDict(self,tarCmdNum,fromStr,refCmdNum,toStr,dict2insert=None):
if dict2insert == None:
dict2insert = self.rewriteDict
for typeDict in [x for x in dict2insert.keys() if dict2insert[x]['isArgument']==True ]:
res = myutils.iff(dict2insert[typeDict]['testerFunction'],fromStr,toStr)
if res < 0:
return
if res == 1:
self.insertToDictWithType(tarCmdNum,fromStr,refCmdNum,toStr,typeDict,dict2insert)
return
if res == 0:
continue
# the ref and tar are needed here only for debug - to present the context the cmd was extracted from
def createRewrite(self):
tarBase = 0
refBase = 0
for nodeInfo in self.nodeGradesInfos:
for cmd in nodeInfo['matchedCmds']:
tarCmdNum = tarBase + cmd['tarCmdNum']
refCmdNum = refBase + cmd['refCmdNum']
#ref = self.refCode
#tar = self.tarCode
res = myutils.iff(isCall,cmd['ref'],cmd['tar'])
if res < 0:
if self.printCmdMatchErrors:
print "bad command match" + "(refcmd=" + cmd['ref'] + ",tarcmd=" + cmd['tar'] + ")"
continue
elif res == 1:
self.insertToDictWithType(tarCmdNum,cmd['tar'][len("call "):],refCmdNum,cmd['ref'][len("call "):],self.FUNCNAME,self.rewriteDict)
continue
res = myutils.iff(lambda x: x.startswith("<"),cmd['ref'],cmd['tar'])
if res < 0:
if self.printCmdMatchErrors:
print "bad command match"
continue
elif res == 1:
continue # we do nothing with this now...
else:
tmpDict = self.getEmptyDict()
cmdPartsRef = cmd['ref'].split(" ")
cmdPartsTar = cmd['tar'].split(" ")
if cmdPartsRef[0] != cmdPartsTar[0]:
if self.printCmdMatchErrors:
print "bad command match" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
if len(cmdPartsRef) != len(cmdPartsTar):
if self.printCmdMatchErrors:
print "bad command match in cmd parts" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
for i in range(1,len(cmdPartsRef)):
res = myutils.iff(lambda x: x != "",cmdPartsRef[i],cmdPartsTar[i])
if res < 0:
if self.printCmdMatchErrors:
print "bad command match in cmd parts one empty one not" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
#no args, nothing to learn here.
if res==0:
if self.printCmdMatchErrors:
print "two empty commands" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
#else they are both not empty
argsRef = cmdPartsRef[i].split(",")
argsTar = cmdPartsTar[i].split(",")
if len(argsRef) != len(argsTar):
if self.printCmdMatchErrors:
print "bad command match in args" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
for j in range(0,len(argsRef)):
res = myutils.iff(lambda x: x.startswith("[") and x.endswith("]"),argsRef[j],argsTar[j])
if res < 0 :
if self.printCmdMatchErrors:
print "bad command match in args one mem one not" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
elif res == 1:
#both mem operations
singleArgRef = argsRef[j][1:-1]
singleArgTar = argsTar[j][1:-1]
paramsRef = singleArgRef.split("+")
paramsTar = singleArgTar.split("+")
if len(paramsRef) == len(paramsTar):
for k in range(0,len(paramsRef)):
self.__insertToDict(tarCmdNum,paramsTar[k],refCmdNum,paramsRef[k],tmpDict)
else:
if self.printCmdMatchErrors:
print "bad command match - number of params is not the same" + "(ref=" + cmd['ref'] + ",tar=" + cmd['tar'] + ")"
continue
else:
#both without mem
self.__insertToDict(tarCmdNum,argsTar[j],refCmdNum,argsRef[j],tmpDict)
# we passed the parameted\argument compare and the cmds have the same structure. we commit the changes.
self.commitChanges(tmpDict)
tarBase += nodeInfo['tarCode'].count(";")
refBase += nodeInfo['refCode'].count(";")
def commitChanges(self,tmpDict):
raise NotImplementedError('This method must be overriden')
#returns a rewrite or the original str if not found or conflict.
def getRewriteWithType(self,tarCmdNum,fromStr,typeStr,FoundBlacklistElement):
raise NotImplementedError('This method must be overriden')
#returns a rewrite or the original str if not found or conflict.
def __getRewrite(self,tarCmdNum,fromStr,FoundBlacklistElement):
for typeDict in [x for x in self.rewriteDict.keys() if self.rewriteDict[x]['isArgument']==True and self.rewriteDict[x]['useAble']==True ]:
if not self.rewriteDict[typeDict]['testerFunction'](fromStr):
continue
return self.getRewriteWithType(tarCmdNum,fromStr,typeDict,FoundBlacklistElement)
# no match for any type, return original
return fromStr
def getRW(self):
tarBase = 0
for nodeInfo in self.nodeGradesInfos:
cmdsStr = nodeInfo['tarCode']
rewrittenCmds = []
for lineNumber,cmd in enumerate(cmdsStr.split(";")):
tarCmdNum = tarBase + lineNumber + 1
FoundBlacklistElement = [False]
if cmd.startswith("<"):
#this is a goto cmd - TODO
rewrittenCmds.append(cmd)
elif cmd.startswith("call "):
#this is a call cmd - TODO
rewrittenCmds.append("call " + self.getRewriteWithType(tarCmdNum,cmd[len("call "):],self.FUNCNAME,FoundBlacklistElement))
else:
cmdParts = cmd.split(" ")
rewrittenCmdParts = []
#opcode will not be changed...
rewrittenCmdParts.append(cmdParts[0])
for i in range(1,len(cmdParts)):
if cmdParts[i] == "":
raise hell
args = cmdParts[i].split(",")
rewrittenArgs = []
for j in range(0,len(args)):
if args[j].startswith("[") and args[j].endswith("]"):
# mem operations
singleArg = args[j][1:-1]
params = singleArg.split("+")
rewrittenParams = []
for k in range(0,len(params)):
rewrittenParams.append(self.__getRewrite(tarCmdNum,params[k],FoundBlacklistElement))
rewrittenArgs.append("[" + "+".join(rewrittenParams) + "]")
else:
# without mem
rewrittenArgs.append(self.__getRewrite(tarCmdNum,args[j],FoundBlacklistElement))
rewrittenCmdParts.append(','.join(rewrittenArgs))
if FoundBlacklistElement[0] == False:
rewrittenCmds.append(' '.join(rewrittenCmdParts))
tarBase += nodeInfo['tarCode'].count(";")
yield ";".join(rewrittenCmds)