2007-10-20 09:03:51 +02:00
|
|
|
#------------------------------------------------------------
|
|
|
|
# IDAPython - Python plugin for Interactive Disassembler Pro
|
|
|
|
#
|
2009-09-16 16:00:31 +02:00
|
|
|
# Copyright (c) 2004-2009 Gergely Erdelyi <dyce@d-dome.net>
|
2007-10-20 09:03:51 +02:00
|
|
|
#
|
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# For detailed copyright information see the file COPYING in
|
|
|
|
# the root of the distribution archive.
|
|
|
|
#------------------------------------------------------------
|
|
|
|
"""
|
|
|
|
idautils.py - High level utility functions for IDA
|
|
|
|
"""
|
2008-04-12 11:08:11 +02:00
|
|
|
import idaapi
|
2008-11-02 09:37:57 +01:00
|
|
|
import idc
|
2009-09-22 16:36:55 +02:00
|
|
|
import types
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
def refs(ea, funcfirst, funcnext):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Generic reference collector - INTERNAL USE ONLY.
|
|
|
|
"""
|
|
|
|
ref = funcfirst(ea)
|
2009-04-27 20:05:14 +02:00
|
|
|
while ref != idaapi.BADADDR:
|
|
|
|
yield ref
|
|
|
|
ref = funcnext(ea, ref)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def CodeRefsTo(ea, flow):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of code references to 'ea'
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Target address
|
|
|
|
@param flow: Follow normal code flow or not
|
|
|
|
@type flow: Boolean (0/1, False/True)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of references (may be empty list)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
Example::
|
|
|
|
|
|
|
|
for ref in CodeRefsTo(ScreenEA(), 1):
|
|
|
|
print ref
|
|
|
|
"""
|
|
|
|
if flow == 1:
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_cref_to, idaapi.get_next_cref_to)
|
2008-06-15 16:39:43 +02:00
|
|
|
else:
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_fcref_to, idaapi.get_next_fcref_to)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def CodeRefsFrom(ea, flow):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of code references from 'ea'
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Target address
|
|
|
|
@param flow: Follow normal code flow or not
|
|
|
|
@type flow: Boolean (0/1, False/True)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of references (may be empty list)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
Example::
|
|
|
|
|
|
|
|
for ref in CodeRefsFrom(ScreenEA(), 1):
|
|
|
|
print ref
|
|
|
|
"""
|
|
|
|
if flow == 1:
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_cref_from, idaapi.get_next_cref_from)
|
2008-06-15 16:39:43 +02:00
|
|
|
else:
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_fcref_from, idaapi.get_next_fcref_from)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def DataRefsTo(ea):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of data references to 'ea'
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Target address
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of references (may be empty list)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
Example::
|
|
|
|
|
|
|
|
for ref in DataRefsTo(ScreenEA(), 1):
|
|
|
|
print ref
|
|
|
|
"""
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_dref_to, idaapi.get_next_dref_to)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def DataRefsFrom(ea):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of data references from 'ea'
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Target address
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of references (may be empty list)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
Example::
|
|
|
|
|
|
|
|
for ref in DataRefsFrom(ScreenEA(), 1):
|
|
|
|
print ref
|
|
|
|
"""
|
2009-09-16 16:00:31 +02:00
|
|
|
return refs(ea, idaapi.get_first_dref_from, idaapi.get_next_dref_from)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
2008-06-15 11:36:30 +02:00
|
|
|
def XrefTypeName(typecode):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Convert cross-reference type codes to readable names
|
|
|
|
|
|
|
|
@param typecode: cross-reference type code
|
|
|
|
"""
|
2009-09-16 16:00:31 +02:00
|
|
|
ref_types = {
|
2008-06-15 16:39:43 +02:00
|
|
|
0 : 'Data_Unknown',
|
2009-09-16 16:00:31 +02:00
|
|
|
1 : 'Data_Offset',
|
2008-06-15 16:39:43 +02:00
|
|
|
2 : 'Data_Write',
|
|
|
|
3 : 'Data_Read',
|
|
|
|
4 : 'Data_Text',
|
|
|
|
5 : 'Data_Informational',
|
|
|
|
16 : 'Code_Far_Call',
|
|
|
|
17 : 'Code_Near_Call',
|
|
|
|
18 : 'Code_Far_Jump',
|
|
|
|
19 : 'Code_Near_Jump',
|
|
|
|
20 : 'Code_User',
|
|
|
|
21 : 'Ordinary_Flow'
|
|
|
|
}
|
|
|
|
assert typecode in ref_types, "unknown reference type %d" % typecode
|
|
|
|
return ref_types[typecode]
|
2008-06-15 11:36:30 +02:00
|
|
|
|
|
|
|
|
2008-06-16 20:47:02 +02:00
|
|
|
def _copy_xref(xref):
|
|
|
|
""" Make a private copy of the xref class to preserve its contents """
|
|
|
|
class _xref:
|
|
|
|
pass
|
|
|
|
|
|
|
|
xr = _xref()
|
|
|
|
for attr in [ 'frm', 'to', 'iscode', 'type', 'user' ]:
|
|
|
|
setattr(xr, attr, getattr(xref, attr))
|
|
|
|
return xr
|
|
|
|
|
|
|
|
|
2008-06-15 11:36:30 +02:00
|
|
|
def XrefsFrom(ea, flags=0):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Return all references from address 'ea'
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Reference address
|
|
|
|
@param flags: any of idaapi.XREF_* flags
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2009-09-13 19:37:43 +02:00
|
|
|
Example::
|
2008-06-15 16:39:43 +02:00
|
|
|
for xref in XrefsFrom(here(), 0):
|
|
|
|
print xref.type, XrefTypeName(xref.type), \
|
2008-06-15 11:36:30 +02:00
|
|
|
'from', hex(xref.frm), 'to', hex(xref.to)
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
xref = idaapi.xrefblk_t()
|
|
|
|
if xref.first_from(ea, flags):
|
2008-06-16 20:47:02 +02:00
|
|
|
yield _copy_xref(xref)
|
2008-06-15 16:39:43 +02:00
|
|
|
while xref.next_from():
|
2008-06-16 20:47:02 +02:00
|
|
|
yield _copy_xref(xref)
|
2008-06-15 11:36:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
def XrefsTo(ea, flags=0):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Return all references to address 'ea'
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: Reference address
|
|
|
|
@param flags: any of idaapi.XREF_* flags
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2009-09-13 19:37:43 +02:00
|
|
|
Example::
|
2008-06-15 16:39:43 +02:00
|
|
|
for xref in XrefsTo(here(), 0):
|
|
|
|
print xref.type, XrefTypeName(xref.type), \
|
2008-06-15 11:36:30 +02:00
|
|
|
'from', hex(xref.frm), 'to', hex(xref.to)
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
xref = idaapi.xrefblk_t()
|
|
|
|
if xref.first_to(ea, flags):
|
2008-06-16 20:47:02 +02:00
|
|
|
yield _copy_xref(xref)
|
2008-06-15 16:39:43 +02:00
|
|
|
while xref.next_to():
|
2008-06-16 20:47:02 +02:00
|
|
|
yield _copy_xref(xref)
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2009-10-05 12:35:27 +02:00
|
|
|
def Threads():
|
|
|
|
"""Returns all thread IDs"""
|
|
|
|
for i in xrange(0, idc.GetThreadQty()):
|
|
|
|
yield idc.GetThreadId(i)
|
2008-06-15 11:36:30 +02:00
|
|
|
|
2009-04-27 20:20:02 +02:00
|
|
|
def Heads(start=idaapi.cvar.inf.minEA, end=idaapi.cvar.inf.maxEA):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of heads (instructions or data)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2009-04-27 20:20:02 +02:00
|
|
|
@param start: start address (default: inf.minEA)
|
|
|
|
@param end: end address (default: inf.maxEA)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of heads between start and end
|
|
|
|
"""
|
|
|
|
ea = start
|
2009-04-27 20:20:02 +02:00
|
|
|
if not idc.isHead(idc.GetFlags(ea)):
|
|
|
|
ea = idaapi.next_head(ea, end)
|
2009-04-27 20:05:14 +02:00
|
|
|
while ea != idaapi.BADADDR:
|
|
|
|
yield ea
|
2008-06-15 16:39:43 +02:00
|
|
|
ea = idaapi.next_head(ea, end)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
2009-04-27 20:20:02 +02:00
|
|
|
def Functions(start=idaapi.cvar.inf.minEA, end=idaapi.cvar.inf.maxEA):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of functions
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2009-04-27 20:20:02 +02:00
|
|
|
@param start: start address (default: inf.minEA)
|
|
|
|
@param end: end address (default: inf.maxEA)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of heads between start and end
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@note: The last function that starts before 'end' is included even
|
2009-07-12 16:35:13 +02:00
|
|
|
if it extends beyond 'end'. Any function that has its chunks scattered
|
|
|
|
in multiple segments will be reported multiple times, once in each segment
|
|
|
|
as they are listed.
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
func = idaapi.get_func(start)
|
2009-06-25 18:11:17 +02:00
|
|
|
if not func:
|
|
|
|
func = idaapi.get_next_func(start)
|
2009-04-27 20:05:14 +02:00
|
|
|
while func and func.startEA < end:
|
|
|
|
yield func.startEA
|
|
|
|
func = idaapi.get_next_func(func.startEA)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
2008-06-13 22:29:05 +02:00
|
|
|
def Chunks(start):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get a list of function chunks
|
2008-06-13 22:29:05 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param start: address of the function
|
2008-06-13 22:29:05 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: list of funcion chunks (tuples of the form (start_ea, end_ea))
|
|
|
|
belonging to the function
|
|
|
|
"""
|
|
|
|
func_iter = idaapi.func_tail_iterator_t( idaapi.get_func( start ) )
|
|
|
|
status = func_iter.main()
|
|
|
|
while status:
|
|
|
|
chunk = func_iter.chunk()
|
2009-04-27 20:05:14 +02:00
|
|
|
yield (chunk.startEA, chunk.endEA)
|
2008-06-15 16:39:43 +02:00
|
|
|
status = func_iter.next()
|
2008-06-13 22:29:05 +02:00
|
|
|
|
|
|
|
|
2007-10-20 09:03:51 +02:00
|
|
|
def Segments():
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get list of segments (sections) in the binary image
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: List of segment start addresses.
|
|
|
|
"""
|
2009-09-16 16:00:31 +02:00
|
|
|
for n in xrange(idaapi.get_segm_qty()):
|
2008-06-15 16:39:43 +02:00
|
|
|
seg = idaapi.getnseg(n)
|
2009-04-27 20:05:14 +02:00
|
|
|
if seg:
|
|
|
|
yield seg.startEA
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def GetDataList(ea, count, itemsize=1):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Get data list - INTERNAL USE ONLY
|
|
|
|
"""
|
|
|
|
if itemsize == 1:
|
|
|
|
getdata = idaapi.get_byte
|
2009-09-23 09:03:16 +02:00
|
|
|
elif itemsize == 2:
|
2008-06-15 16:39:43 +02:00
|
|
|
getdata = idaapi.get_word
|
2009-09-23 09:03:16 +02:00
|
|
|
elif itemsize == 4:
|
2008-08-30 18:10:00 +02:00
|
|
|
getdata = idaapi.get_long
|
2009-09-23 09:03:16 +02:00
|
|
|
elif itemsize == 8:
|
|
|
|
getdata = idaapi.get_qword
|
|
|
|
else:
|
|
|
|
raise ValueError, "Invalid data size! Must be 1, 2, 4 or 8"
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2009-09-24 12:42:09 +02:00
|
|
|
endea = ea + itemsize * count
|
|
|
|
curea = ea
|
|
|
|
while curea < endea:
|
2008-08-30 18:10:00 +02:00
|
|
|
yield getdata(curea)
|
2009-09-24 12:42:09 +02:00
|
|
|
curea += itemsize
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
2009-09-07 19:08:30 +02:00
|
|
|
def FuncItems(start):
|
|
|
|
"""
|
|
|
|
Get a list of function items
|
|
|
|
|
|
|
|
@param start: address of the function
|
|
|
|
|
|
|
|
@return: ea of each item in the function
|
|
|
|
"""
|
|
|
|
func = idaapi.get_func(start)
|
|
|
|
if not func:
|
|
|
|
return
|
|
|
|
fii = idaapi.func_item_iterator_t()
|
|
|
|
ok = fii.set(func)
|
|
|
|
while ok:
|
|
|
|
yield fii.current()
|
|
|
|
ok = fii.next_code()
|
|
|
|
|
|
|
|
|
|
|
|
def DecodeInstruction(ea):
|
|
|
|
"""
|
|
|
|
Decodes an instruction and returns a insn_t like class
|
|
|
|
|
|
|
|
@param ea: address to decode
|
|
|
|
|
|
|
|
@return: None or an insn_t like structure
|
|
|
|
"""
|
|
|
|
inslen = idaapi.decode_insn(ea)
|
|
|
|
if inslen == 0:
|
|
|
|
return None
|
|
|
|
insn = idaapi.get_current_instruction()
|
|
|
|
if not insn:
|
|
|
|
return None
|
|
|
|
class dup:
|
|
|
|
def __init__(self, op):
|
|
|
|
for x in dir(op):
|
|
|
|
if x.startswith("__") and x.endswith("__"):
|
|
|
|
continue
|
|
|
|
setattr(self, x, getattr(op, x))
|
|
|
|
r = dup(insn)
|
|
|
|
r.Operands = []
|
2009-09-16 16:00:31 +02:00
|
|
|
for n in xrange(0, idaapi.UA_MAXOP):
|
2009-09-07 19:08:30 +02:00
|
|
|
t = idaapi.get_instruction_operand(insn, n)
|
|
|
|
if t.type == idaapi.o_void:
|
|
|
|
break
|
|
|
|
r.Operands.append(dup(t))
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
2008-08-30 18:10:00 +02:00
|
|
|
def PutDataList(ea, datalist, itemsize=1):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Put data list - INTERNAL USE ONLY
|
|
|
|
"""
|
|
|
|
putdata = None
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
if itemsize == 1:
|
|
|
|
putdata = idaapi.patch_byte
|
|
|
|
if itemsize == 2:
|
|
|
|
putdata = idaapi.patch_word
|
|
|
|
if itemsize == 4:
|
2008-08-30 18:10:00 +02:00
|
|
|
putdata = idaapi.patch_long
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-08-30 18:10:00 +02:00
|
|
|
assert putdata, "Invalid data size! Must be 1, 2 or 4"
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-08-30 18:10:00 +02:00
|
|
|
for val in datalist:
|
2008-06-15 16:39:43 +02:00
|
|
|
putdata(ea, val)
|
|
|
|
ea = ea + itemsize
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def MapDataList(ea, length, func, wordsize=1):
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Map through a list of data words in the database
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@param ea: start address
|
|
|
|
@param length: number of words to map
|
|
|
|
@param func: mapping function
|
|
|
|
@param wordsize: size of words to map [default: 1 byte]
|
2007-10-20 09:03:51 +02:00
|
|
|
|
2008-06-15 16:39:43 +02:00
|
|
|
@return: None
|
|
|
|
"""
|
|
|
|
PutDataList(ea, map(func, GetDataList(ea, length, wordsize)), wordsize)
|
2007-10-20 09:03:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
def GetInputFileMD5():
|
2008-06-15 16:39:43 +02:00
|
|
|
"""
|
|
|
|
Return the MD5 hash of the input binary file
|
|
|
|
|
|
|
|
@return: MD5 string or None on error
|
|
|
|
"""
|
2008-11-02 09:37:57 +01:00
|
|
|
return idc.GetInputMD5()
|
2009-01-27 18:37:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class _cpu:
|
|
|
|
"Simple wrapper around GetRegValue/SetRegValue"
|
|
|
|
def __getattr__(self, name):
|
|
|
|
#print "cpu.get(%s)"%name
|
|
|
|
return idc.GetRegValue(name)
|
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
#print "cpu.set(%s)"%name
|
|
|
|
return idc.SetRegValue(value, name)
|
|
|
|
|
2009-09-16 16:00:31 +02:00
|
|
|
class Strings:
|
|
|
|
"""
|
|
|
|
Returns the string list.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
s = Strings()
|
|
|
|
|
|
|
|
for i in s:
|
|
|
|
print "%x: len=%d type=%d -> '%s'" % (i.ea, i.length, i.type, str(i))
|
|
|
|
|
|
|
|
"""
|
|
|
|
class StringItem:
|
|
|
|
"""
|
|
|
|
Class representing each string item.
|
|
|
|
The attributes are:
|
|
|
|
ea - string ea
|
|
|
|
type - string type (ASCSTR_xxxxx)
|
|
|
|
str() - returns the actual string
|
|
|
|
"""
|
|
|
|
def __init__(self, si):
|
|
|
|
self.ea = si.ea
|
|
|
|
self.type = si.type
|
|
|
|
self.length = si.length
|
|
|
|
def __str__(self):
|
|
|
|
return idc.GetString(self.ea, self.length, self.type)
|
|
|
|
|
|
|
|
STR_C = 0x0001 # C-style ASCII string
|
|
|
|
STR_PASCAL = 0x0002 # Pascal-style ASCII string (length byte)
|
|
|
|
STR_LEN2 = 0x0004 # Pascal-style, length is 2 bytes
|
|
|
|
STR_UNICODE = 0x0008 # Unicode string
|
|
|
|
STR_LEN4 = 0x0010 # Pascal-style, length is 4 bytes
|
|
|
|
STR_ULEN2 = 0x0020 # Pascal-style Unicode, length is 2 bytes
|
|
|
|
STR_ULEN4 = 0x0040 # Pascal-style Unicode, length is 4 bytes
|
|
|
|
|
2009-10-17 22:33:09 +02:00
|
|
|
def clear_cache(self):
|
2009-09-16 16:00:31 +02:00
|
|
|
"""Clears the strings list cache"""
|
|
|
|
self.refresh(0, 0) # when ea1=ea2 the kernel will clear the cache
|
|
|
|
|
|
|
|
def __init__(self, default_setup=True):
|
|
|
|
if default_setup:
|
|
|
|
self.setup()
|
|
|
|
self._si = idaapi.string_info_t()
|
|
|
|
|
|
|
|
def refresh(self, ea1=idaapi.cvar.inf.minEA, ea2=idaapi.cvar.inf.maxEA):
|
|
|
|
"""Refreshes the strings list"""
|
|
|
|
idaapi.refresh_strlist(ea1, ea2)
|
|
|
|
self.size = idaapi.get_strlist_qty()
|
|
|
|
|
|
|
|
def setup(self, strtypes=STR_C, minlen=5, only_7bit=True, ignore_instructions=False, ea1=idaapi.cvar.inf.minEA, ea2=idaapi.cvar.inf.maxEA, display_only_existing_strings=False):
|
|
|
|
t = idaapi.strwinsetup_t()
|
|
|
|
t.strtypes = strtypes
|
|
|
|
t.minlen = minlen
|
|
|
|
t.only_7bit = only_7bit
|
|
|
|
t.ea1 = ea1
|
|
|
|
t.ea2 = ea2
|
|
|
|
t.display_only_existing_strings = display_only_existing_strings
|
|
|
|
idaapi.set_strlist_options(t)
|
|
|
|
# automatically refreshes
|
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
def __getitem__(self, index):
|
|
|
|
"""Returns string items"""
|
|
|
|
if index >= self.size:
|
|
|
|
raise StopIteration
|
|
|
|
if idaapi.get_strlist_item(index, self._si):
|
|
|
|
return Strings.StringItem(self._si)
|
|
|
|
return None
|
|
|
|
|
2009-09-22 16:36:55 +02:00
|
|
|
|
|
|
|
def _Assemble(ea, line):
|
|
|
|
"""
|
|
|
|
Please refer to Assemble() - INTERNAL USE ONLY
|
|
|
|
"""
|
|
|
|
if type(line) == types.StringType:
|
|
|
|
lines = [line]
|
|
|
|
else:
|
|
|
|
lines = line
|
|
|
|
ret = []
|
|
|
|
for line in lines:
|
|
|
|
seg = idaapi.getseg(ea)
|
|
|
|
if not seg:
|
|
|
|
return (False, "No segment at ea")
|
|
|
|
ip = ea - (idaapi.ask_selector(seg.sel) << 4)
|
|
|
|
buf = idaapi.AssembleLine(ea, seg.sel, ip, seg.bitness, line)
|
|
|
|
if not buf:
|
|
|
|
return (False, "Assembler failed: " + line)
|
|
|
|
ea += len(buf)
|
|
|
|
ret.append(buf)
|
|
|
|
|
|
|
|
if len(ret) == 1:
|
|
|
|
ret = ret[0]
|
|
|
|
return (True, ret)
|
|
|
|
|
|
|
|
|
|
|
|
def Assemble(ea, line):
|
|
|
|
"""
|
|
|
|
Assembles one or more lines (does not display an message dialogs)
|
|
|
|
If line is a list then this function will attempt to assemble all the lines
|
|
|
|
This function will turn on batch mode temporarily so that no messages are displayed on the screen
|
|
|
|
|
|
|
|
@param ea: start address
|
|
|
|
@return: (False, "Error message") or (True, asm_buf) or (True, [asm_buf1, asm_buf2, asm_buf3])
|
|
|
|
"""
|
|
|
|
old_batch = idc.Batch(1)
|
|
|
|
ret = _Assemble(ea, line)
|
|
|
|
idc.Batch(old_batch)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2009-01-27 18:37:10 +01:00
|
|
|
cpu = _cpu()
|