def check(cfunc, ctree_item): if ctree_item.citype == idaapi.VDI_EXPR: expression = ctree_item.it.to_specific_type child = None while expression and expression.op not in (idaapi.cot_asg, idaapi.cit_return, idaapi.cot_call): child = expression.to_specific_type expression = cfunc.body.find_parent_of(expression) if expression: expression = expression.to_specific_type if expression.op == idaapi.cot_asg and \ expression.x.op in (idaapi.cot_var, idaapi.cot_obj, idaapi.cot_memptr, idaapi.cot_memref) \ and expression.y.op == idaapi.cot_cast: if expression.x.op == idaapi.cot_var: variable = cfunc.get_lvars()[expression.x.v.idx] idaapi.update_action_label( RecastItemLeft.name, 'Recast Variable "{0}"'.format(variable.name)) return RECAST_LOCAL_VARIABLE, expression.y.x.type, variable elif expression.x.op == idaapi.cot_obj: idaapi.update_action_label(RecastItemLeft.name, 'Recast Global') return RECAST_GLOBAL_VARIABLE, expression.y.x.type, expression.x.obj_ea elif expression.x.op == idaapi.cot_memptr: idaapi.update_action_label(RecastItemLeft.name, 'Recast Field') return RECAST_STRUCTURE, expression.x.x.type.get_pointed_object( ).dstr(), expression.x.m, expression.y.x.type elif expression.x.op == idaapi.cot_memref: idaapi.update_action_label(RecastItemLeft.name, 'Recast Field') return RECAST_STRUCTURE, expression.x.x.type.dstr( ), expression.x.m, expression.y.x.type elif expression.op == idaapi.cit_return: child = child or expression.creturn.expr if child.op == idaapi.cot_cast: idaapi.update_action_label(RecastItemLeft.name, "Recast Return") return RECAST_RETURN, child.x.type, None elif expression.op == idaapi.cot_call: if child and child.op == idaapi.cot_cast: if child.cexpr.x.op == idaapi.cot_memptr: idaapi.update_action_label( RecastItemLeft.name, 'Recast Virtual Function') return RECAST_STRUCTURE, child.cexpr.x.x.type.get_pointed_object( ).dstr(), child.cexpr.x.m, child.type arg_index, _ = Helper.get_func_argument_info( expression, child.cexpr) idaapi.update_action_label(RecastItemLeft.name, "Recast Argument") return (RECAST_ARGUMENT, arg_index, expression.x.type.get_pointed_object(), child.x.type, expression.x.obj_ea)
def check(cfunc, ctree_item): if ctree_item.citype != idaapi.VDI_EXPR: return False expression = ctree_item.it.to_specific_type if expression.op == idaapi.cot_var: lvar = ctree_item.get_lvar() # Check if it's either variable with user name or argument with not standard `aX` name if lvar.has_user_name or lvar.is_arg_var and re.search("a\d*$", lvar.name) is None: parent = cfunc.body.find_parent_of(expression).to_specific_type if parent.op == idaapi.cot_call: arg_index, _ = Helper.get_func_argument_info(parent, expression) func_tinfo = parent.x.type.get_pointed_object() func_data = idaapi.func_type_data_t() func_tinfo.get_func_details(func_data) if arg_index < func_tinfo.get_nargs() and lvar.name.lstrip('_') != func_data[arg_index].name: return func_tinfo, parent.x.obj_ea, arg_index, lvar.name.lstrip('_')
def check(cfunc, ctree_item): if ctree_item.citype != idaapi.VDI_EXPR: return False expression = ctree_item.it.to_specific_type if expression.op == idaapi.cot_var: lvar = ctree_item.get_lvar() parent = cfunc.body.find_parent_of(expression).to_specific_type if parent.op == idaapi.cot_call: arg_index, _ = Helper.get_func_argument_info(parent, expression) func_tinfo = parent.x.type.get_pointed_object() if func_tinfo.get_nargs() < arg_index: return func_data = idaapi.func_type_data_t() func_tinfo.get_func_details(func_data) name = func_data[arg_index].name if name and re.search("a\d*$", name) is None and name != 'this' and name != lvar.name: return name, lvar
def check(cfunc, ctree_item): if ctree_item.citype == idaapi.VDI_EXPR: expression = ctree_item.it.to_specific_type child = None while expression and expression.op not in (idaapi.cot_asg, idaapi.cit_return, idaapi.cot_call): child = expression.to_specific_type expression = cfunc.body.find_parent_of(expression) if expression: expression = expression.to_specific_type if expression.op == idaapi.cot_asg and \ expression.x.op in (idaapi.cot_var, idaapi.cot_obj, idaapi.cot_memptr, idaapi.cot_memref): right_expr = expression.y right_tinfo = right_expr.x.type if right_expr.op == idaapi.cot_cast else right_expr.type # Check if both left and right parts of expression are of the same types. # If no then we can recast then. if right_tinfo.dstr() == expression.x.type.dstr(): return if expression.x.op == idaapi.cot_var: variable = cfunc.get_lvars()[expression.x.v.idx] idaapi.update_action_label( RecastItemLeft.name, 'Recast Variable "{0}"'.format(variable.name)) return RECAST_LOCAL_VARIABLE, right_tinfo, variable elif expression.x.op == idaapi.cot_obj: idaapi.update_action_label(RecastItemLeft.name, 'Recast Global') return RECAST_GLOBAL_VARIABLE, right_tinfo, expression.x.obj_ea elif expression.x.op == idaapi.cot_memptr: idaapi.update_action_label(RecastItemLeft.name, 'Recast Field') return RECAST_STRUCTURE, expression.x.x.type.get_pointed_object( ).dstr(), expression.x.m, right_tinfo elif expression.x.op == idaapi.cot_memref: idaapi.update_action_label(RecastItemLeft.name, 'Recast Field') return RECAST_STRUCTURE, expression.x.x.type.dstr( ), expression.x.m, right_tinfo elif expression.op == idaapi.cit_return: idaapi.update_action_label(RecastItemLeft.name, "Recast Return") child = child or expression.creturn.expr if child.op == idaapi.cot_cast: return RECAST_RETURN, child.x.type, None func_tinfo = idaapi.tinfo_t() cfunc.get_func_type(func_tinfo) rettype = func_tinfo.get_rettype() print func_tinfo.get_rettype().dstr(), child.type.dstr() if func_tinfo.get_rettype().dstr() != child.type.dstr(): return RECAST_RETURN, child.type, None elif expression.op == idaapi.cot_call: if expression.x.op == idaapi.cot_memptr: # TODO: Recast arguments of virtual functions return if child and child.op == idaapi.cot_cast: if child.cexpr.x.op == idaapi.cot_memptr: idaapi.update_action_label( RecastItemLeft.name, 'Recast Virtual Function') return RECAST_STRUCTURE, child.cexpr.x.x.type.get_pointed_object( ).dstr(), child.cexpr.x.m, child.type arg_index, _ = Helper.get_func_argument_info( expression, child.cexpr) idaapi.update_action_label(RecastItemLeft.name, "Recast Argument") return (RECAST_ARGUMENT, arg_index, expression.x.type.get_pointed_object(), child.x.type, expression.x.obj_ea)