diff options
| -rw-r--r-- | example/ida/ctype_propagation.py | 120 |
1 files changed, 97 insertions, 23 deletions
diff --git a/example/ida/ctype_propagation.py b/example/ida/ctype_propagation.py index b2c7d5ab..9b9c2e95 100644 --- a/example/ida/ctype_propagation.py +++ b/example/ida/ctype_propagation.py @@ -1,6 +1,3 @@ -import os -import tempfile - import ida_kernwin import idc import ida_funcs @@ -8,17 +5,14 @@ import ida_funcs from miasm2.core.bin_stream_ida import bin_stream_ida from miasm2.expression import expression as m2_expr from miasm2.expression.simplifications import expr_simp -from miasm2.analysis.depgraph import DependencyGraph from miasm2.ir.ir import IRBlock, AssignBlock from miasm2.arch.x86.ctype import CTypeAMD64_unk, CTypeX86_unk from miasm2.arch.msp430.ctype import CTypeMSP430_unk -from miasm2.expression.expression import ExprId from miasm2.core.objc import CTypesManagerNotPacked, ExprToAccessC, CHandler from miasm2.core.ctypesmngr import CAstTypes -from miasm2.expression.expression import ExprMem, ExprId, ExprInt, ExprOp, ExprAff +from miasm2.expression.expression import ExprId, ExprInt, ExprOp, ExprAff from miasm2.ir.symbexec_types import SymbExecCType from miasm2.expression.parser import str_to_expr -from miasm2.ir.symbexec import SymbolicExecutionEngine, SymbolicState from miasm2.analysis.cst_propag import add_state, propagate_cst_expr from utils import guess_machine @@ -32,27 +26,96 @@ class TypePropagationForm(ida_kernwin.Form): default_types_info = r"""ExprId("RDX", 64): char *""" archs = ["AMD64_unk", "X86_32_unk", "msp430_unk"] + func = ida_funcs.get_func(idc.ScreenEA()) + func_addr = func.startEA + + start_addr = idc.SelStart() + if start_addr == idc.BADADDR: + start_addr = idc.ScreenEA() + end_addr = idc.SelEnd() + ida_kernwin.Form.__init__(self, r"""BUTTON YES* Launch BUTTON CANCEL NONE -Dependency Graph Settings -<##Header file :{headerFile}> -<Architecture/complator:{arch}> -<Types informations:{strTypesInfo}> +Type Propagation Settings + +{FormChangeCb} +Analysis scope: +<Whole function:{rFunction}> +<From an address to the end of function:{rAddr}> +<Between two addresses:{r2Addr}>{cScope}> + +<Target function:{functionAddr}> +<Start address :{startAddr}> +<End address :{endAddr}> + +<Architecture/compilator :{arch}> + +<##Header file :{headerFile}> +<Use a file for type informations:{rTypeFile}>{cTypeFile}> +<##Types informations :{typeFile}> +<Types informations :{strTypesInfo}> + <Unalias stack:{rUnaliasStack}>{cUnalias}> """, { - 'headerFile': ida_kernwin.Form.FileInput(swidth=20, open=True), + 'FormChangeCb': ida_kernwin.Form.FormChangeCb(self.OnFormChange), + 'cScope': ida_kernwin.Form.RadGroupControl( + ("rFunction", "rAddr", "r2Addr")), + 'functionAddr': ida_kernwin.Form.NumericInput( + tp=ida_kernwin.Form.FT_RAWHEX, + value=func_addr), + 'startAddr': ida_kernwin.Form.NumericInput( + tp=ida_kernwin.Form.FT_RAWHEX, + value=start_addr), + 'endAddr': ida_kernwin.Form.NumericInput( + tp=ida_kernwin.Form.FT_RAWHEX, + value=end_addr), 'arch': ida_kernwin.Form.DropdownListControl( items=archs, readonly=False, selval=archs[0]), + 'headerFile': ida_kernwin.Form.FileInput(swidth=20, open=True), + 'cTypeFile': ida_kernwin.Form.ChkGroupControl(("rTypeFile",)), + 'typeFile': ida_kernwin.Form.FileInput(swidth=20, open=True), 'strTypesInfo': ida_kernwin.Form.MultiLineTextControl(text=default_types_info, flags=ida_kernwin.Form.MultiLineTextControl.TXTF_FIXEDFONT), 'cUnalias': ida_kernwin.Form.ChkGroupControl(("rUnaliasStack",)), }) form, args = self.Compile() form.rUnaliasStack.checked = True - + form.rTypeFile.checked = True + + def OnFormChange(self, fid): + if fid == -1: # INIT + self.EnableField(self.functionAddr, True) + self.EnableField(self.startAddr, False) + self.EnableField(self.endAddr, False) + self.EnableField(self.strTypesInfo, False) + self.EnableField(self.typeFile, True) + elif fid == self.cTypeFile.id: + if self.GetControlValue(self.cTypeFile) == 0: + self.EnableField(self.strTypesInfo, True) + self.EnableField(self.typeFile, False) + elif self.GetControlValue(self.cTypeFile) == 1: + self.EnableField(self.strTypesInfo, False) + self.EnableField(self.typeFile, True) + elif fid == self.cScope.id: + # "Whole function" scope + if self.GetControlValue(self.cScope) == 0: + self.EnableField(self.functionAddr, True) + self.EnableField(self.startAddr, False) + self.EnableField(self.endAddr, False) + # "From an address" scope + elif self.GetControlValue(self.cScope) == 1: + self.EnableField(self.functionAddr, False) + self.EnableField(self.startAddr, True) + self.EnableField(self.endAddr, False) + # "Between two addresses" scope + elif self.GetControlValue(self.cScope) == 2: + self.EnableField(self.functionAddr, False) + self.EnableField(self.startAddr, True) + self.EnableField(self.endAddr, True) + return 1 def get_types_mngr(headerFile, arch): text = open(headerFile).read() @@ -193,21 +256,25 @@ def analyse_function(): ir_arch = iraCallStackFixer(mdis.symbol_pool) - # Get the current function - func = ida_funcs.get_func(idc.ScreenEA()) - addr = func.startEA - blocks = mdis.dis_multiblock(addr) - # Generate IR - for block in blocks: - ir_arch.add_block(block) - - # Get settings settings = TypePropagationForm(ir_arch) ret = settings.Execute() if not ret: return + if settings.cScope.value == 0: + addr = settings.functionAddr.value + else: + addr = settings.startAddr.value + if settings.cScope.value == 2: + end = settings.endAddr + mdis.dont_dis = [end] + + blocks = mdis.dis_multiblock(addr) + # Generate IR + for block in blocks: + ir_arch.add_block(block) + cst_propag_link = {} if settings.cUnalias.value: init_infos = {ir_arch.sp: ir_arch.arch.regs.regs_init[ir_arch.sp] } @@ -217,7 +284,14 @@ def analyse_function(): types_mngr = get_types_mngr(settings.headerFile.value, settings.arch.value) mychandler = MyCHandler(types_mngr, {}) infos_types = {} - for line in settings.strTypesInfo.value.split('\n'): + infos_types_raw = [] + + if settings.cTypeFile.value: + infos_types_raw = open(settings.typeFile.value).read().split('\n') + else: + infos_types_raw = settings.strTypesInfo.value.split('\n') + + for line in infos_types_raw: if not line: continue expr_str, ctype_str = line.split(':') |