Example #1
0
 def _get_user_cmts(ea):
     user_cmts = idaapi.restore_user_cmts(ea)
     if user_cmts is None:
         user_cmts = idaapi.user_cmts_new()
     cmts = []
     it = idaapi.user_cmts_begin(user_cmts)
     while it != idaapi.user_cmts_end(user_cmts):
         tl = idaapi.user_cmts_first(it)
         cmt = idaapi.user_cmts_second(it)
         cmts.append(((tl.ea, tl.itp), str(cmt)))
         it = idaapi.user_cmts_next(it)
     idaapi.user_cmts_free(user_cmts)
     return cmts
Example #2
0
def decompile_and_get(ea):
    """
    For a given function EA, return the comparable decompiled result
    :param self:
    :param ea: the EA of the function
    :return: (pseudocode text, pseudocode hash, pseudocode comments)
    """
    decompiler_plugin = "hexarm"
    if not idaapi.init_hexrays_plugin() and not (
            load_plugin(decompiler_plugin) and idaapi.init_hexrays_plugin()):
        raise Exception("Could not load Hex-Rays!")
    f = get_func(ea)
    if f is None:
        return None, None, None

    cfunc = decompile(f)
    if cfunc is None:
        # Failed to decompile
        return None, None, None

    visitor = CAstVisitor(cfunc)
    visitor.apply_to(cfunc.body, None)
    pseudo_hash = visitor.primes_hash

    cmts = idaapi.restore_user_cmts(cfunc.entry_ea)
    pseudo_comments = {}
    if cmts is not None:
        for tl, cmt in cmts.iteritems():
            pseudo_comments[tl.ea -
                            self.get_base_address()] = [str(cmt), tl.itp]

    sv = cfunc.get_pseudocode()
    pseudo = []
    first_line = None
    for sline in sv:
        line = tag_remove(sline.line)
        if line.startswith("//"):
            continue

        if first_line is None:
            first_line = line
        else:
            pseudo.append(line)
    pseudo_text = "\n".join(pseudo)
    if pseudo_text:
        pseudo_text = get_cmp_pseudo_lines(pseudo_text)
    return pseudo_text, pseudo_hash, pseudo_comments
Example #3
0
def handle_function_comments(delta, segs):
    for f in get_all_funcs():
        for cmt_type in (True, False):
            cmt = idaapi.get_func_cmt(f, cmt_type)
            if cmt:
                new_cmt = rebase_comment(segs, delta, cmt)
                if new_cmt:
                    idaapi.set_func_cmt(f, cmt, cmt_type)
        cmts = idaapi.restore_user_cmts(f.start_ea)
        if not cmts:
            continue
        changed = False
        for (treeloc, citm) in cmts.items():
            if citm:
                citm = citm.c_str()
            new_cmt = rebase_comment(segs, delta, citm)
            if new_cmt:
                changed = True
                it = cmts.find(treeloc)
                cmts.erase(it)
                cmts.insert(treeloc, idaapi.citem_cmt_t(new_cmt))
        if changed:
            idaapi.save_user_cmts(f.start_ea, cmts)
Example #4
0
def run():
    
    cfunc = idaapi.decompile(idaapi.get_screen_ea())
    if not cfunc:
        print 'Please move the cursor into a function.'
        return
    
    entry_ea = cfunc.entry_ea
    print "Dump of user-defined information for function at %x" % (entry_ea, )
    
    # Display user defined labels.
    labels = idaapi.restore_user_labels(entry_ea);
    if labels is not None:
        print "------- %u user defined labels" % (len(labels), )
        for org_label, name in labels.iteritems():
            print "Label %d: %s" % (org_label, str(name))
        idaapi.user_labels_free(labels)
    
    # Display user defined comments
    cmts = idaapi.restore_user_cmts(entry_ea);
    if cmts is not None:
        print "------- %u user defined comments" % (len(cmts), )
        for tl, cmt in cmts.iteritems():
            print "Comment at %x, preciser %x:\n%s\n" % (tl.ea, tl.itp, str(cmt))
        idaapi.user_cmts_free(cmts)
    
    # Display user defined citem iflags
    iflags = idaapi.restore_user_iflags(entry_ea)
    if iflags is not None:
        print "------- %u user defined citem iflags" % (len(iflags), )
        for cl, t in iflags.iteritems():
            print "%a(%d): %08X%s" % (cl.ea, cl.op, f, " CIT_COLLAPSED" if f & CIT_COLLAPSED else "")
        idaapi.user_iflags_free(iflags)

    # Display user defined number formats
    numforms = idaapi.restore_user_numforms(entry_ea)
    if numforms is not None:
        print "------- %u user defined number formats" % (len(numforms), )
        for ol, nf in numforms.iteritems():
            
            print "Number format at %a, operand %d: %s" % (ol.ea, ol.opnum, "negated " if (nf.props & NF_NEGATE) != 0 else "")
            
            if nf.isEnum():
                print "enum %s (serial %d)" % (str(nf.type_name), nf.serial)
                
            elif nf.isChar():
                print "char"
                
            elif nf.isStroff():
                print "struct offset %s" % (str(nf.type_name), )
                
            else:
                print "number base=%d" % (idaapi.getRadix(nf.flags, ol.opnum), )
        
        idaapi.user_numforms_free(numforms)

    # Display user-defined local variable information
    # First defined the visitor class
    class dump_lvar_info_t(idaapi.user_lvar_visitor_t):
    
        def __init__(self):
            idaapi.user_lvar_visitor_t.__init__(self)
            self.displayed_header = False
            return
        
        def get_info_qty_for_saving(self):
            return 0
        
        def get_info_for_saving(self, lv):
            return False
        
        def handle_retrieved_info(self, lv):
            
            try:
                if not self.displayed_header:
                    self.displayed_header = True;
                    print "------- User defined local variable information"
                
                print "Lvar defined at %x" % (lv.ll.defea, )
                
                if len(str(lv.name)):
                    print "  Name: %s" % (str(lv.name), )
                
                if len(str(lv.type)):
                    #~ print_type_to_one_line(buf, sizeof(buf), idati, .c_str());
                    print "  Type: %s" % (str(lv.type), )
                
                if len(str(lv.cmt)):
                    print "  Comment: %s" % (str(lv.cmt), )
            except:
                traceback.print_exc()
            return 0
    
        def handle_retrieved_mapping(self, lm):
            return 0
        
        def get_info_mapping_for_saving(self):
            return None
    
    # Now iterate over all user definitions
    dli = dump_lvar_info_t();
    idaapi.restore_user_lvar_settings(entry_ea, dli)
    
    return
Example #5
0
def run():

    cfunc = idaapi.decompile(idaapi.get_screen_ea())
    if not cfunc:
        print 'Please move the cursor into a function.'
        return

    entry_ea = cfunc.entry_ea
    print "Dump of user-defined information for function at %x" % (entry_ea, )

    # Display user defined labels.
    labels = idaapi.restore_user_labels(entry_ea);
    if labels is not None:
        print "------- %u user defined labels" % (len(labels), )
        for org_label, name in labels.iteritems():
            print "Label %d: %s" % (org_label, str(name))
        idaapi.user_labels_free(labels)

    # Display user defined comments
    cmts = idaapi.restore_user_cmts(entry_ea);
    if cmts is not None:
        print "------- %u user defined comments" % (len(cmts), )
        for tl, cmt in cmts.iteritems():
            print "Comment at %x, preciser %x:\n%s\n" % (tl.ea, tl.itp, str(cmt))
        idaapi.user_cmts_free(cmts)

    # Display user defined citem iflags
    iflags = idaapi.restore_user_iflags(entry_ea)
    if iflags is not None:
        print "------- %u user defined citem iflags" % (len(iflags), )
        for cl, t in iflags.iteritems():
            print "%a(%d): %08X%s" % (cl.ea, cl.op, f, " CIT_COLLAPSED" if f & CIT_COLLAPSED else "")
        idaapi.user_iflags_free(iflags)

    # Display user defined number formats
    numforms = idaapi.restore_user_numforms(entry_ea)
    if numforms is not None:
        print "------- %u user defined number formats" % (len(numforms), )
        for ol, nf in numforms.iteritems():

            print "Number format at %a, operand %d: %s" % (ol.ea, ol.opnum, "negated " if (nf.props & NF_NEGATE) != 0 else "")

            if nf.is_enum():
                print "enum %s (serial %d)" % (str(nf.type_name), nf.serial)

            elif nf.is_char():
                print "char"

            elif nf.is_stroff():
                print "struct offset %s" % (str(nf.type_name), )

            else:
                print "number base=%d" % (idaapi.get_radix(nf.flags, ol.opnum), )

        idaapi.user_numforms_free(numforms)

    # Display user-defined local variable information
    lvinf = idaapi.lvar_uservec_t()
    if idaapi.restore_user_lvar_settings(lvinf, entry_ea):
        print "------- User defined local variable information\n"
        for lv in lvinf.lvvec:
            print "Lvar defined at %x" % (lv.ll.defea, )

            if len(str(lv.name)):
                print "  Name: %s" % (str(lv.name), )

            if len(str(lv.type)):
                #~ print_type_to_one_line(buf, sizeof(buf), idati, .c_str());
                print "  Type: %s" % (str(lv.type), )

            if len(str(lv.cmt)):
                print "  Comment: %s" % (str(lv.cmt), )


    return
Example #6
0
File: vds4.py Project: AmesianX/src
def run():

    cfunc = idaapi.decompile(idaapi.get_screen_ea())
    if not cfunc:
        print('Please move the cursor into a function.')
        return

    entry_ea = cfunc.entry_ea
    print("Dump of user-defined information for function at %x" % (entry_ea, ))

    # Display user defined labels.
    labels = idaapi.restore_user_labels(entry_ea);
    if labels is not None:
        print("------- %u user defined labels" % (len(labels), ))
        for org_label, name in labels.iteritems():
            print("Label %d: %s" % (org_label, str(name)))
        idaapi.user_labels_free(labels)

    # Display user defined comments
    cmts = idaapi.restore_user_cmts(entry_ea);
    if cmts is not None:
        print("------- %u user defined comments" % (len(cmts), ))
        for tl, cmt in cmts.iteritems():
            print("Comment at %x, preciser %x:\n%s\n" % (tl.ea, tl.itp, str(cmt)))
        idaapi.user_cmts_free(cmts)

    # Display user defined citem iflags
    iflags = idaapi.restore_user_iflags(entry_ea)
    if iflags is not None:
        print("------- %u user defined citem iflags" % (len(iflags), ))
        for cl, f in iflags.iteritems():
            print("%x(%d): %08X%s" % (cl.ea, cl.op, f, " CIT_COLLAPSED" if f & idaapi.CIT_COLLAPSED else ""))
        idaapi.user_iflags_free(iflags)

    # Display user defined number formats
    numforms = idaapi.restore_user_numforms(entry_ea)
    if numforms is not None:
        print("------- %u user defined number formats" % (len(numforms), ))
        for ol, nf in numforms.iteritems():

            print("Number format at %a, operand %d: %s" % (ol.ea, ol.opnum, "negated " if (nf.props & NF_NEGATE) != 0 else ""))

            if nf.is_enum():
                print("enum %s (serial %d)" % (str(nf.type_name), nf.serial))

            elif nf.is_char():
                print("char")

            elif nf.is_stroff():
                print("struct offset %s" % (str(nf.type_name), ))

            else:
                print("number base=%d" % (idaapi.get_radix(nf.flags, ol.opnum), ))

        idaapi.user_numforms_free(numforms)

    # Display user-defined local variable information
    lvinf = idaapi.lvar_uservec_t()
    if idaapi.restore_user_lvar_settings(lvinf, entry_ea):
        print("------- User defined local variable information\n")
        for lv in lvinf.lvvec:
            print("Lvar defined at %x" % (lv.ll.defea, ))

            if len(str(lv.name)):
                print("  Name: %s" % (str(lv.name), ))

            if len(str(lv.type)):
                #~ print_type_to_one_line(buf, sizeof(buf), idati, .c_str());
                print("  Type: %s" % (str(lv.type), ))

            if len(str(lv.cmt)):
                print("  Comment: %s" % (str(lv.cmt), ))


    return