Ported to Python 3.X

This commit is contained in:
joxeankoret@yahoo.es
2019-12-09 18:52:28 +01:00
parent 94201ea3d8
commit 4cfba49c5e
8 changed files with 84 additions and 83 deletions

View File

@@ -109,7 +109,7 @@ def ForEveryFuncInSeg( seg, fun ):
f = start
while (f < end):
"""print "ev: %#x" % f"""
print f
print(f)
fun(f)
f=NextFunction(f)
@@ -158,7 +158,7 @@ def ProgramAddrRange() :
return ida_funcs.get_prev_func(ida_idaapi.BADADDR) - ida_funcs.get_next_func(0)
def MemCopy( dest, src, length ) :
for i in xrange(0, length):
for i in range(0, length):
#if (i < 20):
# print "set byte at %#x to %#x" % (dest+i, idc.Byte(src+i))
ida_bytes.patch_byte(dest+i,ida_bytes.get_byte(src+i))
@@ -169,7 +169,7 @@ def PrefixRange(start, end, prefix) :
n = idc.get_func_name(x)
if n.startswith("sub_"):
nn = prefix + n
print "Renaming %s to %s\n" % (n, nn)
print("Renaming %s to %s\n" % (n, nn))
ida_name.set_name(x,nn)
x = NextFunction(x)
@@ -240,7 +240,7 @@ def GetCanonicalName(f):
#Put function in canonical format, given the function name and module name
def NameCanonical(f,mod_name,func_name):
n = "%s_%s_%08x" % (mod_name,func_name,f)
print "Renaming %s to %s\n" % (idc.get_func_name(f),n)
print("Renaming %s to %s\n" % (idc.get_func_name(f),n))
ida_name.force_name(f,n)
#Put function in canonical format when it doesn't have a name, but you know the module name
@@ -264,7 +264,7 @@ def CanonicalFuncRename(f,name):
n = idc.get_func_name(f)
parts = n.split("_")
new_name = "%s_%s_%08x" % (parts[0],name,f)
print "Renaming %s to %s\n" % (n, new_name)
print("Renaming %s to %s\n" % (n, new_name))
ida_name.set_name(f,new_name)
#Rename the module name without changing the function name
@@ -272,7 +272,7 @@ def RenameFuncWithNewMod(f,mod):
n = idc.get_func_name(f)
parts = n.split("_")
new_name = "%s_%s_%08x" % (mod,parts[1],f)
print "Renaming %s to %s\n" % (n, new_name)
print("Renaming %s to %s\n" % (n, new_name))
ida_name.set_name(f,new_name)
#Rename a module (all functions that start with <mod>_)
@@ -336,7 +336,7 @@ def CompileTextFromRange(start,end,sep):
#print "Found ref at %x: %x " % (faddr[c],d)
t = ida_nalt.get_str_type(d)
if ((t==0) or (t==3)):
s += " " + sep + " " + GetStrLitContents(d)
s += " " + sep + " " + GetStrLitContents(d).decode("utf-8")
x = NextFunction(x)
return s
@@ -363,4 +363,4 @@ def GetStrLitContents(ea):
if(potential_len > 0):
return ida_bytes.get_strlit_contents(ea, potential_len, ida_nalt.STRTYPE_C)
#print("Error! %lu not a string" % (ea))
return ""
return ""

View File

@@ -74,7 +74,7 @@ def gen_mod_graph(module_list, suffix):
c+=1
root_name = basicutils.GetRootName()
file = open(root_name + "_" + suffix + "_mod_graph.gv", "wb")
file = open(root_name + "_" + suffix + "_mod_graph.gv", "w")
file.write("digraph g {\n")
@@ -94,7 +94,7 @@ def gen_rename_script(module_list, suffix):
c=0
root_name = basicutils.GetRootName()
file = open(root_name + "_" + suffix + "_labels.py", "wb")
file = open(root_name + "_" + suffix + "_labels.py", "w")
#if (IDA_VERSION < 7):
# file.write("import basicutils_6x as basicutils\n");
@@ -120,7 +120,7 @@ def gen_map_file(module_list, suffix):
c=0
root_name = basicutils.GetRootName()
file = open(root_name + "_" + suffix + "_map.map", "wb")
file = open(root_name + "_" + suffix + "_map.map", "w")
while (c<len(module_list)):
m=module_list[c]
@@ -137,7 +137,7 @@ def gen_map_file(module_list, suffix):
def print_results(function_list, module_list1, module_list2):
c=0
root_name = basicutils.GetRootName()
file = open(root_name + "_cc_results.csv", "wb")
file = open(root_name + "_cc_results.csv", "w")
#write header
file.write("Function,Function #,LFA Score 1,LFA Score 2,LFA Total,LFA Edge,MC Edge,Function Name,Suggested Mod Name (LFA), Suggested Mod Name(MC),Source Str Ref\n");

View File

@@ -23,6 +23,7 @@ import modnaming
import cc_base
import basicutils_7x as basicutils
import snap_cg
import imp
def go():
@@ -52,11 +53,11 @@ def go():
return True
if __name__ == "__main__":
reload(modnaming)
reload(module)
reload(cc_base)
reload(lfa)
reload(maxcut)
reload(snap_cg)
reload(basicutils)
imp.reload(modnaming)
imp.reload(module)
imp.reload(cc_base)
imp.reload(lfa)
imp.reload(maxcut)
imp.reload(snap_cg)
imp.reload(basicutils)
go()

6
lfa.py
View File

@@ -158,7 +158,7 @@ def func_call_weight(f_start, f_end):
#if both scores are 0 (i.e. no references for the function or all refs are above the threshold)
#then skip the function altogether
if (score_1 == 0) and (score_2 == 0):
print "Skipping 0x%08x\n" % f
print("Skipping 0x%08x\n" % f)
prevscore_1 = 0
prevscore_2 = 0
z1 = 1
@@ -187,7 +187,7 @@ def func_call_weight(f_start, f_end):
total_score = score_1 + score_2
#Output scores in log window
print "0x%08x, %d , %f, %f, %f" % (f, c,score_1, score_2, total_score)
print("0x%08x, %d , %f, %f, %f" % (f, c,score_1, score_2, total_score))
#Add scores to the global function score list
finf = module.func_info(f,score_1,score_2)
@@ -218,7 +218,7 @@ def get_last_three(index):
if (c==3):
return p[0],p[1],p[2]
else:
print "Error: could not find 3 scored entries before index: %d (%d,%d)" % (index, i, c)
print("Error: could not find 3 scored entries before index: %d (%d,%d)" % (index, i, c))
return 0,0,0
def get_lfa_start():

View File

@@ -109,14 +109,14 @@ def map_parse(f,mlist):
#Print both ground truth and LFA map output
def map_print(n):
if (n==1):
print "Map 1 (ground truth):"
print("Map 1 (ground truth):")
mod_list = g_mod_list1
else:
print "Map 2:"
print("Map 2:")
mod_list = g_mod_list2
print "# of modules: %d" % len(mod_list)
for x in xrange(len(mod_list)):
print "Name: %s Offset: %x Len: %x" % (mod_list[x].name,mod_list[x].offset,mod_list[x].mlen)
print("# of modules: %d" % len(mod_list))
for x in range(len(mod_list)):
print("Name: %s Offset: %x Len: %x" % (mod_list[x].name,mod_list[x].offset,mod_list[x].mlen))
#score_underlap(module1,module2):
@@ -164,9 +164,9 @@ def mod_collapse(m1,m2):
#Print a single module
def mod_print(m):
#print "%s: %08x - %08x" % (m.name,m.offset,m.reach),
print "%08x - %08x" % (m.offset,m.reach),
print("%08x - %08x" % (m.offset,m.reach), end=' ')
if (m.gap != 0):
print " gap: %x" % m.gap,
print(" gap: %x" % m.gap, end=' ')
#rec_list_print():
#Print side by side the reconciled module lists
@@ -174,12 +174,12 @@ def rec_list_print():
i1 = len(g_rec_list1)
i2 = len(g_rec_list2)
if (i1 != i2):
print "Error: List lengths don't match, not fully reconciled (%d and %d)." % (i1,i2)
print("Error: List lengths don't match, not fully reconciled (%d and %d)." % (i1,i2))
return
for i in xrange(i1):
for i in range(i1):
mod_print(g_rec_list1[i])
mod_print(g_rec_list2[i])
print "u: %x" % (score_underlap(g_rec_list1[i],g_rec_list2[i]))
print("u: %x" % (score_underlap(g_rec_list1[i],g_rec_list2[i])))
#final_score():
#Determine the scores by iterating through the reconciled module lists
@@ -190,17 +190,17 @@ def final_score():
i1 = len(g_rec_list1)
i2 = len(g_rec_list2)
if (i1 != i2):
print "Error: List lengths don't match, not fully reconciled (%d and %d)." % (i1,i2)
print("Error: List lengths don't match, not fully reconciled (%d and %d)." % (i1,i2))
return
s=0
g=0
for i in xrange(0,i1):
for i in range(0,i1):
s+=score_underlap(g_rec_list1[i],g_rec_list2[i])
#only count gaps from the "compare" map file (the one we generate with LFA)
g+=g_rec_list2[i].gap
#Area of overlap - total area - (underlaps + gaps)
good_area = (end-start) - (s+g)
print "Length: 0x%x Good: 0x%x (%2f) Underlap: 0x%x (%2f) Gaps: 0x%x (%2f)" % (end-start,good_area, good_area*100.0/(end-start),s,s*100.0/(end-start),g,g*100.0/(end-start))
print("Length: 0x%x Good: 0x%x (%2f) Underlap: 0x%x (%2f) Gaps: 0x%x (%2f)" % (end-start,good_area, good_area*100.0/(end-start),s,s*100.0/(end-start),g,g*100.0/(end-start)))
return (s+g)/1.0/(end-start)
#map_reconcile():
@@ -229,11 +229,11 @@ def map_reconcile():
po = mod_underlap(m1,m2)
pc = 0x10000000000
print " m1 (%d): " % i1,
print(" m1 (%d): " % i1, end=' ')
mod_print(m1)
print " m2 (%d): " % i2,
print(" m2 (%d): " % i2, end=' ')
mod_print(m2)
print " underlap: %x" % (po)
print(" underlap: %x" % (po))
d=0
#module 1 is longer than module 2, so attempt to collapse modules in list 2 to optimize
@@ -244,17 +244,17 @@ def map_reconcile():
pnm2 = nm2
nm2 = mod_collapse(nm2,g_mod_list2[i2+1])
pc = mod_underlap(m1, nm2)
print "nm2 (%d): (%x)" % (i2+1,pc),
print("nm2 (%d): (%x)" % (i2+1,pc), end=' ')
mod_print(nm2)
print ""
print("")
if (pc < po):
po = pc
i2+=1
else:
d=1
print "Collapsed m2 (%d): " % i2,
print("Collapsed m2 (%d): " % i2, end=' ')
mod_print(pnm2)
print ""
print("")
#add final collapsed modules to reconciled list
g_rec_list1.append(m1)
@@ -266,55 +266,55 @@ def map_reconcile():
pnm1 = nm1
nm1 = mod_collapse(nm1,g_mod_list1[i1+1])
pc = mod_underlap(nm1, m2)
print "nm1 (%d): (%x)" % (i1 + 1, pc),
print("nm1 (%d): (%x)" % (i1 + 1, pc), end=' ')
mod_print(nm1)
print ""
print("")
if (pc < po):
po = pc
i1 += 1
else:
d=1
print "Collapsed m1 (%d): " % i1,
print("Collapsed m1 (%d): " % i1, end=' ')
mod_print(pnm1)
print ""
print("")
g_rec_list1.append(pnm1)
g_rec_list2.append(m2)
i1+=1
i2+=1
print ""
print("")
#end case
#if we've got one module left on either side,
#collapse all the other modules on the other side to match
if (i1 == len(g_mod_list1)-1):
m1 = g_mod_list1[i1]
print "end m1 (%d):" % (i1),
print("end m1 (%d):" % (i1), end=' ')
mod_print(m1)
print ""
print("")
nm2 = g_mod_list2[i2]
i2 += 1
while (i2 < len(g_mod_list2)):
nm2 = mod_collapse(nm2,g_mod_list2[i2])
print "end nm2 (%d):" % (i2),
print("end nm2 (%d):" % (i2), end=' ')
mod_print(nm2)
print ""
print("")
i2 += 1
g_rec_list1.append(m1)
g_rec_list2.append(nm2)
if (i2 == len(g_mod_list2)-1):
m2 = g_mod_list2[i2]
print "end m2 (%d):" % (i2),
print("end m2 (%d):" % (i2), end=' ')
mod_print(m2)
print ""
print("")
nm1 = g_mod_list1[i1]
i1 += 1
while (i1 < len(g_mod_list1)):
nm1 = mod_collapse(nm1,g_mod_list1[i1])
print "end nm1 (%d):" % (i1),
print("end nm1 (%d):" % (i1), end=' ')
mod_print(nm1)
print ""
print("")
i1 += 1
g_rec_list1.append(nm1)
g_rec_list2.append(m2)
@@ -338,6 +338,6 @@ map_reconcile()
rec_list_print()
#Print score
print "Score: %f" % (final_score())
print("Score: %f" % (final_score()))
f.close()
f2.close()

View File

@@ -33,7 +33,7 @@ g_maxcut_modlist = []
#or terminate outside the region)
def make_subgraph(region_start,region_end, graph):
print "make_subgraph: start: 0x%x and end: 0x%x" % (region_start,region_end)
print("make_subgraph: start: 0x%x and end: 0x%x" % (region_start,region_end))
NIdV = snap.TIntV()
#this would be much faster if we had a linear list of functions (nodes)
for Node in graph.Nodes():
@@ -51,7 +51,7 @@ def make_subgraph(region_start,region_end, graph):
#closest to the middle of the region is returned.
def make_cut(region_start, region_end, graph):
print "make_cut: start: 0x%x end: 0x%x" % (region_start,region_end)
print("make_cut: start: 0x%x end: 0x%x" % (region_start,region_end))
weight = {}
z = 0
@@ -79,7 +79,7 @@ def make_cut(region_start, region_end, graph):
#If we have a place where we have no edges crossing - keep track of it
#We will pick the place closest to the center of the module
if edge_count == 0:
print " returning 0 weight count at: 0x%0x" % cut_address
print(" returning 0 weight count at: 0x%0x" % cut_address)
z+=1
zeroes.append(cut_address)
weight[cut_address] = 0
@@ -89,27 +89,27 @@ def make_cut(region_start, region_end, graph):
#if we had edges with zero crossings, pick the one closest to the center
if (z > 0):
print " total of %d zero weight counts" % (z)
print(" total of %d zero weight counts" % (z))
center = region_start + ((region_end-region_start)/2)
min_dist = sys.maxint
for i in xrange(z):
min_dist = sys.maxsize
for i in range(z):
dist = abs(center - zeroes[i])
if dist < min_dist:
min_dist = dist
min_zero = zeroes[i]
print " returning zero cut at addr: %x" % min_zero
print(" returning zero cut at addr: %x" % min_zero)
return min_zero
#otherwise pick the edge with the maximum weight score
max_weight=0
#print " weight table:"
for addr,w in weight.iteritems():
for addr,w in weight.items():
#print " %x: %x" % (addr,w)
if w > max_weight:
max_addr = addr
max_weight = w
print " returning max weight: %x at addr: 0x%x" % (max_weight,max_addr)
print(" returning max weight: %f at addr: 0x%x" % (max_weight,max_addr))
return max_addr
#do_cutting()
@@ -118,7 +118,7 @@ def make_cut(region_start, region_end, graph):
#Stop if the area being cut is below a particular threshold
def do_cutting(start, end, graph):
nodes = graph.GetNodes()
print "do_cutting: start: 0x%x end: 0x%x nodes: 0x%x" % (start, end, nodes)
print("do_cutting: start: 0x%x end: 0x%x nodes: 0x%x" % (start, end, nodes))
THRESHOLD = 0x1000
#THRESHOLD = 0x2000
@@ -131,7 +131,7 @@ def do_cutting(start, end, graph):
do_cutting(start,cut_address,graph1)
do_cutting(cut_address+1,end,graph2)
else:
print "Module 0x%x to 0x%x" % (start, end)
print("Module 0x%x to 0x%x" % (start, end))
b_mod = module.bin_module(start,end,0,"")
g_maxcut_modlist.append(b_mod)
@@ -147,7 +147,7 @@ def func_list_annotate(flist):
#print "F: %08x M: %08x" % (flist[c].loc, start)
c+=1
if (c == len(flist)):
print "Error: Maxcut module list does not reconcile with function list"
print("Error: Maxcut module list does not reconcile with function list")
return None
flist[c].edge[1]=1
#print "MC: Set %08x func edge to 1" % flist[c].loc
@@ -161,7 +161,7 @@ def analyze(flist):
sys.setrecursionlimit(5000)
UGraph = snap_cg.create_snap_cg()
g_min_node=sys.maxint
g_min_node=sys.maxsize
g_max_node=0
for Node in UGraph.Nodes():

View File

@@ -111,9 +111,9 @@ def bracket_strings(start,end,b_brack,e_brack):
if (b_contents != "0x%x"):
b.append(tk[1:tk.find(e_brack)])
print "bracket_strings tokens:"
print tokens
print b
print("bracket_strings tokens:")
print(tokens)
print(b)
u_gram=""
u_gram_score=0
@@ -157,9 +157,9 @@ def source_file_strings(start,end):
ntk = tk
b.append(ntk)
print "source_file_strings tokens:"
print("source_file_strings tokens:")
#print tokens
print b
print(b)
#a better way to do this (if there are multiple)
#would be to sort, uniquify, and then make the name foo.c_and_bar.c
@@ -193,8 +193,8 @@ def common_strings(start,end):
else:
c+=1
print "common_strings tokens:"
print tokens
print("common_strings tokens:")
print(tokens)
if len(u_tokens) < CS_THRESHOLD:
#print "%08x - %08x : %s" % (start,end,"no string")
@@ -303,7 +303,7 @@ def guess_module_names(module_list):
unk_mod+=1
module_list[c].name = name
module_list[c].score = scr
print "%08x - %08x : %s (%d)" % (m.start,m.end,name,scr)
print("%08x - %08x : %s (%d)" % (m.start,m.end,name,scr))
c+=1
return module_list

View File

@@ -37,13 +37,13 @@ def add_edge(f, t):
n = basicutils.GetFunctionName(f)
if n != "":
#since we're only doing one edge for each xref, we'll do weight based on distance from the middle of the caller to the callee
f_start = idc.GetFunctionAttr(f,idc.FUNCATTR_START)
f_start = idc.get_func_attr(f, idc.FUNCATTR_START)
if (not UGraph.IsNode(f_start)):
print "Error: had to add node (to): %08x" % f_start
print("Error: had to add node (to): %08x" % f_start)
UGraph.AddNode(f_start)
print "%08x -> %08x" % (f_start, t)
print("%08x -> %08x" % (f_start, t))
UGraph.AddEdge(t,f_start)
#print "s_%#x -> s_%#x" % (f_start,t)," [len = ",get_weight(func_mid, t), "]"
@@ -61,7 +61,7 @@ def create_snap_cg():
basicutils.ForEveryFuncInSeg(".text",add_node)
for NI in UGraph.Nodes():
print "node id 0x%x with out-degree %d and in-degree %d" %(
NI.GetId(), NI.GetOutDeg(), NI.GetInDeg())
print("node id 0x%x with out-degree %d and in-degree %d" %(
NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
return UGraph
return UGraph