diff --git a/basicutils_7x.py b/basicutils_7x.py index c66574f..387982c 100644 --- a/basicutils_7x.py +++ b/basicutils_7x.py @@ -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 _) @@ -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 "" \ No newline at end of file + return "" diff --git a/cc_base.py b/cc_base.py index 006faa4..3ef74d5 100644 --- a/cc_base.py +++ b/cc_base.py @@ -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 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(): diff --git a/modnaming.py b/modnaming.py index 4f970ba..598ba82 100644 --- a/modnaming.py +++ b/modnaming.py @@ -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 diff --git a/snap_cg.py b/snap_cg.py index 8e87c30..8d66d2d 100644 --- a/snap_cg.py +++ b/snap_cg.py @@ -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 \ No newline at end of file + return UGraph