Beispiel #1
0
    def __setstate__(self, state):
        ownername, index, name, (cmtt, cmtf), ofs, t = state
        fullname = '.'.join((owername, name))

        identifier = idaapi.get_struc_id(ownername)
        if identifier == idaapi.BADADDR:
            logging.warn(
                "{:s}.instance({:s}).member_t : Creating structure {:s} -- [{:#x}] {:s}{:s}"
                .format(
                    __name__, ownername, ownername, ofs, name,
                    " // {:s}".format(cmtt or cmtf) if cmtt or cmtf else ''))
            identifier = idaapi.add_struc(idaapi.BADADDR, ownername)
        self.__owner = owner = instance(identifier, offset=0)

        flag, mytype, nbytes = t

        # FIXME: handle .strtype (strings), .ec (enums), .cd (custom)
        opinfo = idaapi.opinfo_t()
        opinfo.tid = 0 if mytype is None else mytype.id

        res = idaapi.add_struc_member(owner.ptr, name, ofs, flag, opinfo,
                                      nbytes)

        # FIXME: handle these errors properly
        # duplicate name
        if res == idaapi.STRUC_ERROR_MEMBER_NAME:
            if idaapi.get_member_by_name(owner.ptr, name).soff != ofs:
                newname = "{:s}_{:x}".format(name, ofs)
                logging.warn(
                    "{:s}.instace({:s}).member_t : Duplicate name found for {:s}, renaming to {:s}."
                    .format(__name__, ownername, name, newname))
                idaapi.set_member_name(owner.ptr, ofs, newname)
            else:
                logging.info(
                    "{:s}.instance({:s}).member_t : Field at {:+#x} contains the same name {:s}."
                    .format(__name__, ownername, ofs, name))
        # duplicate field
        elif res == idaapi.STRUC_ERROR_MEMBER_OFFSET:
            logging.info(
                "{:s}.instance({:s}).member_t : Field already found at {:+#x}. Overwriting with {:s}."
                .format(__name__, ownername, ofs, name))
            idaapi.set_member_type(owner.ptr, ofs, flag, opinfo, nbytes)
            idaapi.set_member_name(owner.ptr, ofs, name)
        # invalid size
        elif res == idaapi.STRUC_ERROR_MEMBER_SIZE:
            logging.warn(
                "{:s}.instance({:s}).member_t : Issue creating structure member {:s} : {:#x}"
                .format(__name__, ownername, fullname, res))
        # unknown
        elif res != idaapi.STRUC_ERROR_MEMBER_OK:
            logging.warn(
                "{:s}.instance({:s}).member_t : Issue creating structure member {:s} : {:#x}"
                .format(__name__, ownername, fullname, res))

        self.__index = index
        self.__owner = owner

        idaapi.set_member_cmt(self.ptr, cmtt, True)
        idaapi.set_member_cmt(self.ptr, cmtf, False)
        return
Beispiel #2
0
    def __setstate__(self, state):
        ownername, index, name, (cmtt, cmtf), ofs, t = state

        identifier = idaapi.get_struc_id(ownername)
        if identifier == idaapi.BADADDR:
            logging.warn('member_t : Creating structure %s -- [%x] %s%s' %
                         (ownername, ofs, name, ' // %s' %
                          (cmtt or cmtf) if cmtt or cmtf else ''))
            identifier = idaapi.add_struc(idaapi.BADADDR, ownername)
        self.__owner = owner = instance(identifier, offset=0)

        flag, mytype, nbytes = t

        # FIXME: handle .strtype (strings), .ec (enums), .cd (custom)
        opinfo = idaapi.opinfo_t()
        opinfo.tid = 0 if mytype is None else mytype.id

        res = idaapi.add_struc_member(owner.ptr, name, ofs, flag, opinfo,
                                      nbytes)

        # FIXME: handle these errors properly
        # duplicate name
        if res == idaapi.STRUC_ERROR_MEMBER_NAME:
            if idaapi.get_member_by_name(owner.ptr, name).soff != ofs:
                newname = '%s_%x' % (name, ofs)
                logging.warn(
                    'structure_t(%s).member_t : Duplicate name found for %s, renaming to %s'
                    % (ownername, name, newname))
                idaapi.set_member_name(owner.ptr, ofs, newname)
            else:
                logging.info(
                    'structure_t(%s).member_t : Field at %x contains the same name %s'
                    % (ownername, ofs, name))
        # duplicate field
        elif res == idaapi.STRUC_ERROR_MEMBER_OFFSET:
            logging.info(
                'structure_t(%s).member_t : Field already found at %x. Overwriting with %s'
                % (ownername, ofs, name))
            idaapi.set_member_type(owner.ptr, ofs, flag, opinfo, nbytes)
            idaapi.set_member_name(owner.ptr, ofs, name)
        # invalid size
        elif res == idaapi.STRUC_ERROR_MEMBER_SIZE:
            logging.warn(
                'member_t : Issue creating structure member %s.%s : %x' %
                (ownername, name, res))
        # unknown
        elif res != idaapi.STRUC_ERROR_MEMBER_OK:
            logging.warn(
                'member_t : Issue creating structure member %s.%s : %x' %
                (ownername, name, res))

        self.__index = index
        self.__owner = owner

        idaapi.set_member_cmt(self.ptr, cmtt, True)
        idaapi.set_member_cmt(self.ptr, cmtf, False)
        return
Beispiel #3
0
 def __call__(self):
     sptr = idaapi.get_struc(idc.get_struc_id(self.sname.encode('utf-8')))
     if self.smname:
         mptr = idaapi.get_member_by_name(sptr, self.smname.encode('utf-8'))
         idaapi.set_member_cmt(mptr,
                               self.cmt.encode('utf-8') if self.cmt else '',
                               self.repeatable_cmt)
     else:
         idaapi.set_struc_cmt(sptr.id,
                              self.cmt.encode('utf-8') if self.cmt else '',
                              self.repeatable_cmt)
Beispiel #4
0
def handle_structs(delta, segs):
    for idx in range(idaapi.get_struc_qty()):
        tid = idaapi.get_struc_by_idx(idx)
        for cmt_type in (True, False):
            cmt = idaapi.get_struc_cmt(tid, cmt_type)
            if cmt:
                new_cmt = rebase_comment(segs, delta, cmt)
                if new_cmt:
                    idaapi.set_struc_cmt(tid, new_cmt, cmt_type)
        s = idaapi.get_struc(tid)
        for midx in range(s.memqty):
            m = s.get_member(midx)
            for cmt_type in (True, False):
                cmt = idaapi.get_member_cmt(m.id, cmt_type)
                if cmt:
                    new_cmt = rebase_comment(segs, delta, cmt)
                    if new_cmt:
                        idaapi.set_member_cmt(m, new_cmt, cmt_type)
Beispiel #5
0
def add_struc_descr(sid, structure, rep):
    """ Insert a (repeatable) comment descripting the structure whose id is sid.
    And name address in added segment annotated with structure description.
    
    Arguments:
    sid -- structure id which the added comment is describing
    structure -- structure object holding data
    rep -- add repeatable comment (True\False)
    
    Return:
    True -- if success; False otherwise
    """
    
    # TODO correct or not
    descr = format_comment(structure.description) + '\n'
    
    if idc.SetStrucComment(sid, descr, rep):       
                                         
        frm = [x.frm for x in idautils.XrefsTo(sid)]
            
        for ea in frm:
            # Added comment for global %structure.name% variable or pointer
            if ea > idc.MaxEA():
                # getting 'member_t' using ea as 'mid'
                mptr = idaapi.get_member_by_id(ea)
                                
                # IDA 6.8: setting member comment using 'mptr' as index
                idaapi.set_member_cmt(mptr, descr, rep)
                
                # IDA 6.9: mptr is type of list
                #idaapi.set_member_cmt(mptr[0], descr, rep)
            else:    
                if not rep:
                    idc.MakeComm(ea, descr)
                else:
                    idc.MakeRptCmt(ea, descr)
            
        return True
        
    else:
        return False
Beispiel #6
0
def add_struc_descr(sid, structure, rep):
    """ Insert a (repeatable) comment descripting the structure whose id is sid.
    And name address in added segment annotated with structure description.
    
    Arguments:
    sid -- structure id which the added comment is describing
    structure -- structure object holding data
    rep -- add repeatable comment (True\False)
    
    Return:
    True -- if success; False otherwise
    """

    # TODO correct or not
    descr = format_comment(structure.description) + '\n'

    if idc.SetStrucComment(sid, descr, rep):

        frm = [x.frm for x in idautils.XrefsTo(sid)]

        for ea in frm:
            # Added comment for global %structure.name% variable or pointer
            if ea > idc.MaxEA():
                # getting 'member_t' using ea as 'mid'
                mptr = idaapi.get_member_by_id(ea)

                # IDA 6.8: setting member comment using 'mptr' as index
                idaapi.set_member_cmt(mptr, descr, rep)

                # IDA 6.9: mptr is type of list
                #idaapi.set_member_cmt(mptr[0], descr, rep)
            else:
                if not rep:
                    idc.MakeComm(ea, descr)
                else:
                    idc.MakeRptCmt(ea, descr)

        return True

    else:
        return False
Beispiel #7
0
 def comment(self, value):
     return idaapi.set_member_cmt(self.ptr, value, True)
Beispiel #8
0
 def comment(self, value):
     return idaapi.set_member_cmt(self.ptr, value, True)
Beispiel #9
0
 def comment(self, value):
     '''Set the member's repeatable comment.'''
     return idaapi.set_member_cmt(self.ptr, value, True)