Skip to content

Commit

Permalink
remove trash prints
Browse files Browse the repository at this point in the history
  • Loading branch information
George Zaytsev authored and George Zaytsev committed Apr 22, 2018
1 parent a355e59 commit 078e7e4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 21 deletions.
12 changes: 10 additions & 2 deletions HRAST.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
NAME = 'log'

used_pats = []
DEBUG = True

def reLOAD():
global used_pats
Expand All @@ -69,6 +70,14 @@ def reLOAD():
print i[0]
used_pats.append((eval(i[0], globals(), locals()), i[1], i[2]))

def unLOAD():
global used_pats
used_pats = []

def deBUG():
global DEBUG
DEBUG = not DEBUG

def hexrays_events_callback_m(*args):
global LEV
global NAME
Expand All @@ -79,15 +88,14 @@ def hexrays_events_callback_m(*args):
level = args[2]
#print "Got level {}".format(CMAT_LEVEL[level])
if level == idaapi.CMAT_FINAL:
print used_pats
for i in used_pats:
fcnProc = FuncProcesser(fcn)
matcher = Matcher(fcnProc.fcn, None)
matcher.set_pattern(i[0])
matcher.chain = i[2]
matcher.replacer = i[1]
fcnProc.pattern = matcher
fcnProc.DEBUG = True
fcnProc.DEBUG = DEBUG
fcnProc.traverse_function()
return 0

Expand Down
19 changes: 10 additions & 9 deletions Matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ def __init__(self, ea, offset):

class SavedCTX(object):
"""Class which holds all collected context"""
def __init__(self):
def __init__(self, fcn):
self.names = fcn.lvars
self.obj = {}
self.vars = {}
self.memref = {}
Expand All @@ -38,9 +39,12 @@ def save_memref(self, key, ea, offset):

def save_var(self, idx, val, typ, mb):
#val is index in fcn.lvars
#print "[saving var]"
self.vars[idx] = SavedVar(val, typ, mb)


def get_var_name(self, idx):
return self.names[idx].name

def has_var(self, idx):
return idx in self.vars

Expand Down Expand Up @@ -68,20 +72,20 @@ def clear_ctx(self):
class Matcher(object):

def __init__(self, fcn, pattern):
self.names = fcn.lvars

self.pattern = pattern
self.node = 0
self.replacer = None
self.cnt = None
self.ctx = SavedCTX()
self.ctx = SavedCTX(fcn)
self.chain = False
self.fcn = fcn

def set_pattern(self, patt):
self.pattern = patt

def check(self, expr):
self.ctx.clear_ctx
self.ctx.clear_ctx()
return self.pattern.check(expr, self)

def check_chain(self, node):
Expand All @@ -92,9 +96,6 @@ def check_chain(self, node):
if self.is_finished():
pass
return ret

def get_name(self, idx):
return self.names[idx].name

def set_node(self, node):
self.node = node
Expand Down Expand Up @@ -142,4 +143,4 @@ def replace_if_need(self):
idx -= 1
cnt -= 1
self.replacer(self.blk.cblock.at(idx), self.ctx)
self.ctx = {}
self.ctx.clear_ctx()
6 changes: 0 additions & 6 deletions Patterns/Expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def __init__(self, name):

def check(self, expr, ctx):
if expr.opname == "memref":
print "yay"
if expr.x.opname == "obj":
if ctx.ctx.has_memref(self.name):
return ctx.ctx.get_memref(self.name).idx == expr.v.idx
Expand All @@ -40,7 +39,6 @@ def __init__(self, name):
self.name = name

def check(self, expr, ctx):
#print "Checking binded"
if expr.opname == "var":
if ctx.ctx.has_var(self.name):
return ctx.ctx.get_var(self.name).idx == expr.v.idx
Expand Down Expand Up @@ -77,12 +75,10 @@ def check(self, expr, ctx):
ln = len(self.args)
idx = 0
for i in expr.a:
print "Checking arg {} {}".format(idx, res)
if idx >= ln:
return False
res = res and self.args[idx].check(i, ctx)
idx += 1
print "Returning {}".format(res)
return res
return False

Expand All @@ -95,11 +91,9 @@ def __init__(self, addr):

def check(self, expr, ctx):
if expr.opname == "obj":
print "checking"
print "{:x}".format(expr.obj_ea)
print "{:x}".format(self.addr)
if expr.obj_ea == self.addr:
print "ex"
return True
return False

Expand Down
6 changes: 3 additions & 3 deletions ready_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

def replacer_strlen_global(idx, ctx):
var = ctx.get_var("res")
varname = ctx.ctx.get_name(var.idx)
varname = ctx.get_var_name(var.idx)
obj = ctx.get_obj("strlenarg")

varexp = make_var_expr(var.idx, var.typ, var.mba)
Expand All @@ -43,7 +43,7 @@ def replacer_strlen_global(idx, ctx):
del insn

#Third arg - is chain
#PATTERNS = [(strlen_global, replacer_strlen_global, True)]
PATTERNS = [(strlen_global, replacer_strlen_global, True)]
get_proc_addr = """ExprPattern(
AsgnPattern(
ObjBind("fcnPtr"),
Expand Down Expand Up @@ -96,4 +96,4 @@ def _f1(idx, ctx):
ida_struct.set_member_name(ida_struct.get_struc(ti.tid), obj.offset, name_str)


PATTERNS = [(global_struct_fields_sub, _f1, False)]
#PATTERNS = [(global_struct_fields_sub, _f1, False)]
1 change: 0 additions & 1 deletion traverse.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def traverse_node(self, node, shift = 0):
self.need_reanalyze_cblock = False
if self.pattern is not None:
if self.pattern.is_chain():
print "[chech chain]"
if self.pattern.check_chain(node):
#TODO: possible inner hiding?
if self.pattern.is_finished():
Expand Down

0 comments on commit 078e7e4

Please sign in to comment.