Skip to content

Commit e3dc140

Browse files
committed
initial porting to python3
1 parent b0c75b4 commit e3dc140

File tree

8 files changed

+171
-168
lines changed

8 files changed

+171
-168
lines changed

plugins/alleycat/alleycat.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def add_to_namespace(namespace, source, name, variable):
1919
'''
2020
Add a variable to a different namespace, likely __main__.
2121
'''
22+
import importlib
2223
importer_module = sys.modules[namespace]
23-
if source in sys.modules.keys():
24-
reload(sys.modules[source])
24+
if source in list(sys.modules.keys()):
25+
importlib.reload(sys.modules[source])
2526
else:
26-
import importlib
2727
m = importlib.import_module(source, None)
2828
sys.modules[source] = m
2929

@@ -56,8 +56,8 @@ def __init__(self, start, end, quiet=False):
5656
# We work backwards via xrefs, so we start at the end and end at the
5757
# start
5858
if not self.quiet:
59-
print "Generating call paths from %s to %s..." % (self._name(end),
60-
self._name(start))
59+
print("Generating call paths from %s to %s..." % (self._name(end),
60+
self._name(start)))
6161
self._build_paths(start, end)
6262

6363
def _name(self, ea):
@@ -442,7 +442,7 @@ def OnDblClick(self, node_id):
442442
xref_locations = []
443443
node_ea = self.get_ea_by_name(self[node_id])
444444

445-
if self.edges.has_key(node_id):
445+
if node_id in self.edges:
446446
for edge_node_id in self.edges[node_id]:
447447

448448
edge_node_name = self[edge_node_id]
@@ -457,14 +457,14 @@ def OnDblClick(self, node_id):
457457
if xref_locations:
458458
xref_locations.sort()
459459

460-
print ""
461-
print "Path Xrefs from %s:" % self[node_id]
462-
print "-" * 100
460+
print("")
461+
print("Path Xrefs from %s:" % self[node_id])
462+
print("-" * 100)
463463
for (xref_ea, dst_ea) in xref_locations:
464-
print "%-50s => %s" % (self.get_name_by_ea(xref_ea),
465-
self.get_name_by_ea(dst_ea))
466-
print "-" * 100
467-
print ""
464+
print("%-50s => %s" % (self.get_name_by_ea(xref_ea),
465+
self.get_name_by_ea(dst_ea)))
466+
print("-" * 100)
467+
print("")
468468

469469
ida_shims.jumpto(xref_locations[0][0])
470470
else:
@@ -571,15 +571,15 @@ def _find_and_plot_paths(self, sources, targets, klass=AlleyCatFunctionPaths):
571571
s = time.time()
572572
r = klass(source, target).paths
573573
e = time.time()
574-
print "Found %d paths in %f seconds." % (len(r), (e-s))
574+
print("Found %d paths in %f seconds." % (len(r), (e-s)))
575575

576576
if r:
577577
results += r
578578
else:
579579
name = ida_shims.get_name(target)
580580
if not name:
581581
name = "0x%X" % target
582-
print "No paths found to", name
582+
print("No paths found to", name)
583583

584584
if results:
585585
# Be sure to close any previous graph before creating a new one.

plugins/codatify/codatify.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, ea, n=16):
6666
struct2 = StructCast(address, n)
6767
this_pattern = [x.type for x in struct2.entries[p1:p2+1]]
6868
if this_pattern == pattern_to_match:
69-
entry_element_count = (address - ea) / self.element_size
69+
entry_element_count = int((address - ea) / self.element_size)
7070
break
7171

7272
if entry_element_count is not None and entry_element_count > 0:
@@ -120,7 +120,7 @@ def get_entry_pair(self, structure):
120120
class Struct(object):
121121

122122
def __init__(self, **kwargs):
123-
for (k, v) in kwargs.iteritems():
123+
for (k, v) in kwargs.items():
124124
setattr(self, k, v)
125125

126126

@@ -159,8 +159,8 @@ def valid_function_name(self, name):
159159
def search(self):
160160
patterns = []
161161

162-
print "Searching for data structure arrays from %s to %s" % \
163-
(hex(self.start), hex(self.stop))
162+
print("Searching for data structure arrays from %s to %s" % \
163+
(hex(self.start), hex(self.stop)))
164164

165165
ea = self.start
166166
while ea < self.stop:
@@ -186,14 +186,14 @@ def search(self):
186186
else:
187187
j += 1
188188

189-
print "Found an array of %d structures at 0x%X - 0x%X. Each " \
189+
print("Found an array of %d structures at 0x%X - 0x%X. Each " \
190190
"entry has %d elements of %d bytes each." % \
191191
(patterns[i].num_entries, patterns[i].start, patterns[i].stop,
192-
patterns[i].num_elements, patterns[i].element_size)
192+
patterns[i].num_elements, patterns[i].element_size))
193193

194-
print "Array element #%d is the address pointer, and element #%d " \
194+
print("Array element #%d is the address pointer, and element #%d " \
195195
"is the address pointer name.\n" % \
196-
(patterns[i].function_element, patterns[i].name_element)
196+
(patterns[i].function_element, patterns[i].name_element))
197197

198198
i += 1
199199

@@ -213,29 +213,29 @@ def parse_function_tables(self):
213213
ea + (pattern.function_element * pattern.element_size))
214214

215215
new_function_name = ida_shims.get_strlit_contents(
216-
string_address)
216+
string_address).decode("utf8")
217217
current_function_name = ida_shims.get_name(function_address)
218218

219219
if not self.valid_function_name(new_function_name):
220-
print "ERROR: '%s' is not a valid function name. This is " \
220+
print("ERROR: '%s' is not a valid function name. This is " \
221221
"likely not a function table, or I have parsed it " \
222-
"incorrectly!" % new_function_name
223-
print " Ignoring all entries in the structures " \
222+
"incorrectly!" % new_function_name)
223+
print(" Ignoring all entries in the structures " \
224224
"between 0x%X and 0x%X.\n" % (pattern.start,
225-
pattern.stop)
225+
pattern.stop))
226226
name2func = {}
227227
break
228228
elif current_function_name.startswith("sub_"):
229229
name2func[new_function_name] = function_address
230230

231231
ea += (pattern.num_elements * pattern.element_size)
232232

233-
for (name, address) in name2func.iteritems():
234-
print "0x%.8X => %s" % (address, name)
233+
for (name, address) in name2func.items():
234+
print("0x%.8X => %s" % (address, name))
235235
ida_shims.set_name(address, name)
236236
count += 1
237237

238-
print "Renamed %d functions!" % count
238+
print("Renamed %d functions!" % count)
239239

240240

241241
class FunctionNameology(object):
@@ -254,16 +254,16 @@ def rename_functions(self, debug=True, dry_run=False):
254254
'''
255255
count = 0
256256

257-
for (function_address, function_name) in self.func2str_mappings().iteritems():
257+
for (function_address, function_name) in self.func2str_mappings().items():
258258
if ida_shims.get_name(function_address).startswith("sub_"):
259259
if dry_run or ida_shims.set_name(function_address, function_name):
260260
if debug:
261-
print "0x%.8X => %s" % (function_address,
262-
function_name)
261+
print("0x%.8X => %s" % (function_address,
262+
function_name))
263263
count += 1
264264

265265
if debug:
266-
print "Renamed %d functions based on unique string xrefs!" % count
266+
print("Renamed %d functions based on unique string xrefs!" % count)
267267

268268
return count
269269

@@ -280,12 +280,12 @@ def func2str_mappings(self):
280280
if self.is_valid_function_name(str(string)):
281281
function_address = self.str2func(string.ea)
282282
if function_address is not None:
283-
if not function_map.has_key(function_address):
283+
if function_address not in function_map:
284284
function_map[function_address] = []
285285
function_map[function_address].append(str(string))
286286

287287
# Each function must have only one candidate string
288-
for function_address in function_map.keys():
288+
for function_address in list(function_map.keys()):
289289
if len(function_map[function_address]) == 1:
290290
function_map[function_address] = function_map[function_address][0]
291291
else:
@@ -363,23 +363,23 @@ def stringify(self):
363363
if ea == idc.BADADDR:
364364
ea = ida_shims.get_first_seg()
365365

366-
print "Looking for possible strings starting at: 0x%X..." % ea,
366+
print("Looking for possible strings starting at: 0x%X..." % ea, end=' ')
367367

368368
for s in idautils.Strings():
369369
if s.ea > ea:
370370
if not ida_shims.is_strlit(ida_shims.get_full_flags(s.ea)) \
371371
and ida_shims.create_strlit(s.ea, 0):
372372
n += 1
373373

374-
print "created %d new ASCII strings" % n
374+
print("created %d new ASCII strings" % n)
375375

376376
# Converts remaining data into DWORDS.
377377
def datify(self):
378378
ea = self.get_start_ea(self.DATA)
379379
if ea == idc.BADADDR:
380380
ea = ida_shims.get_first_seg()
381381

382-
print "Converting remaining data to DWORDs...",
382+
print("Converting remaining data to DWORDs...", end=' ')
383383

384384
while ea != idc.BADADDR:
385385
flags = ida_shims.get_full_flags(ea)
@@ -391,14 +391,14 @@ def datify(self):
391391

392392
ea = ida_shims.next_addr(ea)
393393

394-
print "done."
394+
print("done.")
395395

396396
self._fix_data_offsets()
397397

398398
def pointify(self):
399399
counter = 0
400400

401-
print "Renaming pointers...",
401+
print("Renaming pointers...", end=' ')
402402

403403
for (name_ea, name) in idautils.Names():
404404
for xref in idautils.XrefsTo(name_ea):
@@ -415,13 +415,13 @@ def pointify(self):
415415
#else:
416416
# print "Failed to create name '%s'!" % new_name
417417

418-
print "renamed %d pointers" % counter
418+
print("renamed %d pointers" % counter)
419419

420420
def _fix_data_offsets(self):
421421
ea = 0
422422
count = 0
423423

424-
print "Fixing unresolved offset xrefs...",
424+
print("Fixing unresolved offset xrefs...", end=' ')
425425

426426
while ea != idaapi.BADADDR:
427427
(ea, n) = idaapi.find_notype(ea, idaapi.SEARCH_DOWN)
@@ -435,7 +435,7 @@ def _fix_data_offsets(self):
435435
(idaapi.dr_O | idaapi.XREF_USER))
436436
count += 1
437437

438-
print "created %d new data xrefs" % count
438+
print("created %d new data xrefs" % count)
439439

440440
# Creates functions and code blocks
441441
def codeify(self, ea=idc.BADADDR):
@@ -447,8 +447,8 @@ def codeify(self, ea=idc.BADADDR):
447447
if ea == idc.BADADDR:
448448
ea = ida_shims.get_first_seg()
449449

450-
print "\nLooking for undefined code starting at: %s:0x%X" % \
451-
(ida_shims.get_segm_name(ea), ea)
450+
print("\nLooking for undefined code starting at: %s:0x%X" % \
451+
(ida_shims.get_segm_name(ea), ea))
452452

453453
while ea != idc.BADADDR:
454454
try:
@@ -466,8 +466,8 @@ def codeify(self, ea=idc.BADADDR):
466466

467467
ea = ida_shims.next_addr(ea)
468468

469-
print "Created %d new functions and %d new code blocks\n" % \
470-
(func_count, code_count)
469+
print("Created %d new functions and %d new code blocks\n" % \
470+
(func_count, code_count))
471471

472472

473473
try:

plugins/funcprofiler/funcprofiler.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class IDAProfilerXref(object):
1919
def __init__(self, **kwargs):
20-
for (k, v) in kwargs.iteritems():
20+
for (k, v) in kwargs.items():
2121
setattr(self, k, v)
2222

2323

@@ -36,7 +36,7 @@ def _build_string_xrefs(self):
3636
func = idaapi.get_func(xref.frm)
3737
if func:
3838
start_ea = ida_shims.start_ea(func)
39-
if not self.functions.has_key(start_ea):
39+
if start_ea not in self.functions:
4040
self.functions[start_ea] = list()
4141

4242
xref = IDAProfilerXref(ea=string.ea, string=key_string,
@@ -49,15 +49,15 @@ def _build_function_xrefs(self):
4949
func = idaapi.get_func(xref.frm)
5050
if func:
5151
start_ea = ida_shims.start_ea(func)
52-
if not self.functions.has_key(start_ea):
52+
if start_ea not in self.functions:
5353
self.functions[start_ea] = list()
5454

5555
self.functions[start_ea].append(IDAProfilerXref(
5656
ea=function, string=ida_shims.get_name(function),
5757
xref=xref.frm, type=callable))
5858

5959
def _sort_functions(self):
60-
for function in self.functions.keys():
60+
for function in list(self.functions.keys()):
6161
self.functions[function] = sorted(self.functions[function],
6262
key=attrgetter('xref'))
6363

@@ -124,7 +124,7 @@ def set_internal_filter(self, functions=set(), strings=set()):
124124
def populate_items(self):
125125
self.items = []
126126

127-
for (func_ea, xrefs) in self.profile.functions.iteritems():
127+
for (func_ea, xrefs) in self.profile.functions.items():
128128
if not self.function_filters or func_ea in self.function_filters:
129129
orig_items_len = len(self.items)
130130

@@ -184,12 +184,12 @@ def rename_regex(self, n, regex_str="", dryrun=False):
184184

185185
if regex_str:
186186
if dryrun:
187-
print "Testing regex rename rule: '%s'" % regex_str
187+
print("Testing regex rename rule: '%s'" % regex_str)
188188

189189
regex = re.compile(regex_str)
190190

191191
# Look at all the profiled functions
192-
for (function, xrefs) in self.profile.functions.iteritems():
192+
for (function, xrefs) in self.profile.functions.items():
193193
new_function_name = ""
194194

195195
# Don't rename functions that have already been renamed
@@ -225,14 +225,14 @@ def rename_regex(self, n, regex_str="", dryrun=False):
225225
n += 1
226226

227227
if dryrun:
228-
print "%s => %s" % (idc.Name(function),
229-
new_function_name)
228+
print("%s => %s" % (idc.Name(function),
229+
new_function_name))
230230
count += 1
231231
else:
232232
if ida_shims.set_name(function, new_function_name):
233233
count += 1
234234

235-
print "Renamed %d functions" % count
235+
print("Renamed %d functions" % count)
236236

237237

238238
def from_function_profiler(arg=None):
@@ -248,15 +248,15 @@ def from_function_profiler(arg=None):
248248
"because 0x%X is not inside a function!" % cur_loc)
249249
chooser.show()
250250
except Exception as e:
251-
print "IDAFunctionProfiler ERROR: %s" % str(e)
251+
print("IDAFunctionProfiler ERROR: %s" % str(e))
252252

253253

254254
def all_functions_profiler(arg=None):
255255
try:
256256
chooser = IDAFunctionProfilerChooser()
257257
chooser.show()
258258
except Exception as e:
259-
print "IDAFunctionProfiler ERROR: %s" % str(e)
259+
print("IDAFunctionProfiler ERROR: %s" % str(e))
260260

261261

262262
try:

0 commit comments

Comments
 (0)