示例#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
示例#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
示例#3
0
    def name(self, string):
        '''Set the member's name to ``string``.'''
        if isinstance(string, tuple):
            string = interface.tuplename(*string)

        res = idaapi.validate_name2(buffer(string)[:])
        if string and string != res:
            logging.warn(
                "{:s}.name : Stripping invalid chars from structure \"{:s}\" member {:d} name {!r}. : {!r}"
                .format('.'.join((__name__, self.__class__.__name__)),
                        self.__owner.name, self.__index, string, res))
            string = res
        return idaapi.set_member_name(
            self.__owner.ptr, self.offset - self.__owner.members.baseoffset,
            string)
示例#4
0
 def name(self, value):
     return idaapi.set_member_name(self.owner.ptr, self.offset, value)
示例#5
0
 def name(self, value):
     return idaapi.set_member_name(self.owner.ptr, self.offset, value)
示例#6
0
 def name(self, value):
     return idaapi.set_member_name(
         self.__owner.ptr, self.offset - self.__owner.members.baseoffset,
         value)
示例#7
0
    def fill_function(self, ida_func, user=None, state=None):
        """
        Grab all relevant information from the specified user and fill the @ida_func.
        """

        # == function name === #
        _func = self.pull_function(ida_func, user=user, state=state)
        if _func is None:
            return

        if compat.get_func_name(ida_func.start_ea) != _func.name:
            compat.set_ida_func_name(ida_func.start_ea, _func.name)

        # === comments === #
        # set the func comment
        func_comment = self.pull_comment(_func.addr, user=user, state=state)
        if func_comment is None:
            func_comment = ""
            #idc.set_func_cmt(_func.addr, func_comment, 1)
            #compat.set_ida_comment(_func.addr, func_comment, 1, func_cmt=True)

        # set the disassembly comments
        func_cmt_end = "\n"
        for start_ea, end_ea in idautils.Chunks(ida_func.start_ea):
            for head in idautils.Heads(start_ea, end_ea):
                if head == _func.addr:
                    continue

                comment = self.pull_comment(head, user=user, state=state)
                if comment is not None:
                    func_cmt_end += f"\n{hex(head)}: {comment}"
                    #compat.set_decomp_comments(_func.addr, {head: comment})
                    #compat.set_ida_comment(head, comment, 0, func_cmt=False)
        func_comment += func_cmt_end
        compat.set_ida_comment(_func.addr, func_comment, 1, func_cmt=True)

        # === stack variables === #
        existing_stack_vars = {}
        frame = idaapi.get_frame(ida_func.start_ea)
        if frame is None or frame.memqty <= 0:
            _l.debug(
                "Function %#x does not have an associated function frame. Skip variable name sync-up.",
                ida_func.start_ea)
            return

        frame_size = idc.get_struc_size(frame)
        last_member_size = idaapi.get_member_size(
            frame.get_member(frame.memqty - 1))

        for i in range(frame.memqty):
            member = frame.get_member(i)
            stack_offset = member.soff - frame_size + last_member_size
            existing_stack_vars[stack_offset] = member

        for offset, stack_var in self.pull_stack_variables(
                ida_func, user=user, state=state).items():
            ida_offset = stack_var.get_offset(StackOffsetType.IDA)
            # skip if this variable already exists
            if ida_offset in existing_stack_vars:
                type_str = self._get_type_str(
                    existing_stack_vars[ida_offset].flag)
            else:
                type_str = None

            if ida_offset in existing_stack_vars:
                if idc.get_member_name(frame.id, existing_stack_vars[ida_offset].soff) == stack_var.name \
                        and type_str is not None \
                        and stack_var.type == type_str:
                    continue
                # rename the existing variable
                idaapi.set_member_name(frame,
                                       existing_stack_vars[ida_offset].soff,
                                       stack_var.name)
                # TODO: retype the existing variable

        # ===== update the psuedocode ==== #
        compat.refresh_pseudocode_view(_func.addr)
示例#8
0
 def __call__(self):
     sptr = idaapi.get_struc(idc.get_struc_id(self.sname.encode('utf-8')))
     idaapi.set_member_name(sptr, self.offset, self.newname.encode('utf-8'))