Example #1
0
def show_microcode():
    """Generates and displays microcode for an address range.
    An address range can be a selection of code or that of
    the current function."""
    sel, sea, eea = kw.read_range_selection(None)
    pfn = ida_funcs.get_func(kw.get_screen_ea())
    if not sel and not pfn:
        return (False, "Position cursor within a function or select range")

    if not sel and pfn:
        sea = pfn.start_ea
        eea = pfn.end_ea

    addr_fmt = "%016x" if ida_ida.inf_is_64bit() else "%08x"
    fn_name = (ida_funcs.get_func_name(pfn.start_ea) if pfn else "0x%s-0x%s" %
               (addr_fmt % sea, addr_fmt % eea))
    F = ida_bytes.get_flags(sea)
    if not ida_bytes.is_code(F):
        return (False, "The selected range must start with an instruction")

    text, mmat, mba_flags = ask_desired_maturity()
    if text is None and mmat is None:
        return (True, "Cancelled")

    if not sel and pfn:
        mbr = hr.mba_ranges_t(pfn)
    else:
        mbr = hr.mba_ranges_t()
        mbr.ranges.push_back(ida_range.range_t(sea, eea))

    hf = hr.hexrays_failure_t()
    ml = hr.mlist_t()
    mba = hr.gen_microcode(mbr, hf, ml, hr.DECOMP_WARNINGS, mmat)
    if not mba:
        return (False, "0x%s: %s" % (addr_fmt % hf.errea, hf.desc()))
    vp = printer_t()
    mba.set_mba_flags(mba_flags)
    mba._print(vp)
    mcv = microcode_viewer_t()
    if not mcv.Create(
            mba, "0x%s-0x%s (%s)" %
        (addr_fmt % sea, addr_fmt % eea, text), text, fn_name, vp.get_mc()):
        return (False, "Error creating viewer")

    mcv.Show()
    return (True, "Successfully generated microcode for 0x%s..0x%s" %
            (addr_fmt % sea, addr_fmt % eea))
Example #2
0
    def set_addr(self, ea, new_cursor=None):
        _ea = ea

        selection, start, end = ida_kernwin.read_range_selection(None)
        if selection:
            _ea = start

        base = self.bh.get_base(_ea)
        self._set_base(base)
        self._set_offs(_ea - base)

        if new_cursor:
            self.set_cursor_offset(new_cursor)
            if self.highlight_cursor:
                highlight_item(_ea)

        self.repaint()
Example #3
0
#      Copyright (c) 2007-2020 by Hex-Rays, [email protected]
#      ALL RIGHTS RESERVED.
#
#      Sample script for Hex-Rays Decompiler.
#      It generates microcode for selection and dumps it to the output window.
#
#      This is a rewrite in Python of the vds13 example that comes with hexrays sdk.
#

import ida_bytes
import ida_range
import ida_kernwin
import ida_hexrays

if ida_hexrays.init_hexrays_plugin():
    sel, sea, eea = ida_kernwin.read_range_selection(None)
    w = ida_kernwin.warning
    if sel:
        F = ida_bytes.get_flags(sea)
        if ida_bytes.is_code(F):
            hf = ida_hexrays.hexrays_failure_t()
            mbr = ida_hexrays.mba_ranges_t()
            mbr.ranges.push_back(ida_range.range_t(sea, eea))
            mba = ida_hexrays.gen_microcode(mbr, hf, None,
                                            ida_hexrays.DECOMP_WARNINGS)
            if mba:
                print("Successfully generated microcode for 0x%08x..0x%08x\n" %
                      (sea, eea))
                vp = ida_hexrays.vd_printer_t()
                mba._print(vp)
            else:
Example #4
0
def get_selected_range_or_line():
    selection, startaddr, endaddr = ida_kernwin.read_range_selection(None)
    if selection:
        return startaddr, endaddr
    else:
        return ida_kernwin.get_screen_ea(), None
Example #5
0
    def paintEvent(self, event):
        if not self.fm:
            return

        # set leftmost x-coordinate of graph
        zoom_level = self.get_zoom()
        self.rect_x_width = self.get_width() * zoom_level
        self.rect_x = (self.rect().width() / 2) - (self.rect_x_width / 2)

        self.qp.begin(self)

        # what is a good default font for OSX/Linux?
        self.qp.setFont(QFont(FONT_DEFAULT))

        # fill background
        self.qp.fillRect(self.rect(), Qt.black)

        content_addr = content_size = None
        if self.fm.support_selection:
            selected, start, end = ida_kernwin.read_range_selection(None)
            if selected:
                content_addr = start  #min(start, end)
                content_size = end - start  #max(start, end) - content_addr

        # use colorfilter to render image
        img = self.render_image(addr=content_addr, buf_size=content_size)

        if img:
            """
            if zoom_level > 6:
                opacity = self.qp.opacity()
                full_opacity_zoom = 40.0
                cur_opacity = (1.0 - (full_opacity_zoom - float(min(zoom_level-1, full_opacity_zoom)))/full_opacity_zoom)
                self.qp.setOpacity(1.0-cur_opacity)
            """
            # draw image
            self.qp.drawImage(
                QRect(
                    QPoint(self.rect_x, 0),
                    QPoint(self.rect_x + self.get_width() * zoom_level,
                           (self.get_pixels_total() / self.get_width()) *
                           zoom_level)), img)

            # TODO: pen color contrast
            # TODO: data export: render data
            # TODO: default fonts / OS?
            # TODO: optimization
            # FIXME: there's a bug with gaps/unmapped buffers
            if self.render_data and not self.fm.disable_data and zoom_level > 6:
                self.qp.setPen(QColor(Qt.white))
                fontsize = self.qp.font().pointSize()
                font = self.qp.font()

                font.setPointSize(zoom_level / 3)
                self.qp.setFont(font)

                opacity = self.qp.opacity()
                full_opacity_zoom = 28
                cur_opacity = (
                    1.0 - (full_opacity_zoom -
                           float(min(zoom_level - 1, full_opacity_zoom))) /
                    full_opacity_zoom)
                self.qp.setOpacity(cur_opacity)

                m = self.qp.fontMetrics()
                x = y = 0
                num_pixels_per_row = self.get_width()

                if self.cur_formatter_idx == 0:
                    sample = "%c" % (ord('X'))
                    cwidth = m.width(sample)
                    cheight = m.height()

                    for mapped, buf in self.buffers:
                        for i in range(len(buf)):
                            if mapped:
                                b = ord(buf[i])
                                data = "%c" % (chr(b) if b in range(
                                    0x20, 0x7E) else ".")

                                self.qp.drawStaticText(
                                    self.rect_x + x * zoom_level +
                                    (zoom_level - cwidth) / 2, y * zoom_level +
                                    (zoom_level - cheight) / 2,
                                    QStaticText(data))

                            x = (i + 1) % num_pixels_per_row
                            if not x:
                                y = y + 1

                elif self.cur_formatter_idx == 1:
                    sample = "%02X" % (ord('X'))
                    cwidth = m.width(sample)
                    cheight = m.height()

                    for mapped, buf in self.buffers:
                        for i in range(len(buf)):
                            if mapped:
                                data = "%02X" % ord(buf[i])

                                self.qp.drawStaticText(
                                    self.rect_x + x * zoom_level +
                                    (zoom_level - cwidth) / 2, y * zoom_level +
                                    (zoom_level - cheight) / 2,
                                    QStaticText(data))

                            x = (i + 1) % num_pixels_per_row
                            if not x:
                                y = y + 1

                self.qp.setOpacity(opacity)
                font.setPointSize(fontsize)
                self.qp.setFont(font)

        if self.show_address_range and self.fm.link_pixel:
            self.render_slider(addr=content_addr, buf_size=content_size)

        # get and draw annotations and pointers
        annotations = self.fm.on_get_annotations(
            content_addr if content_addr else self.get_address(),
            self.get_pixels_total(), self.mouseOffs)

        if annotations:
            self.render_annotations(annotations)

        self.qp.end()
        return
Example #6
0
    def paintEvent(self, event):
        if not self.fm:
            return

        # set leftmost x-coordinate of graph
        zoom_level = self.get_zoom()
        self.rect_x_width = self.get_pixel_qty_per_line() * zoom_level
        self.rect_x = (self.rect().width() / 2) - (self.rect_x_width / 2)

        self.qp.begin(self)

        # what is a good default font for OSX/Linux?
        self.qp.setFont(QFont(FONT_DEFAULT))

        # fill background
        self.qp.fillRect(self.rect(), Qt.black)

        content_addr = content_size = None
        if self.fm.support_selection:
            selected, start, end = ida_kernwin.read_range_selection(None)
            if selected:
                content_addr = start  #min(start, end)
                content_size = end - start  #max(start, end) - content_addr

        # use colorfilter to render image
        img = self.paint_image(addr=content_addr, buf_size=content_size)

        if img:
            """
            if zoom_level > 6:
                opacity = self.qp.opacity()
                full_opacity_zoom = 40.0
                cur_opacity = (1.0 - (full_opacity_zoom - float(min(zoom_level-1, full_opacity_zoom)))/full_opacity_zoom)
                self.qp.setOpacity(1.0-cur_opacity)
            """
            # draw image
            self.qp.drawImage(
                QRect(
                    QPoint(self.rect_x, 0),
                    QPoint(
                        self.rect_x +
                        self.get_pixel_qty_per_line() * zoom_level,
                        (self.get_pixel_qty() / self.get_pixel_qty_per_line())
                        * zoom_level)), img)

            # TODO: pen color contrast
            # TODO: data export: render data
            # TODO: default fonts / OS?
            # TODO: optimization
            # FIXME: there's a bug with gaps/unmapped buffers
            if (self.cur_formatter_idx and not self.fm.disable_data
                    and zoom_level >= 10 and self.get_pixel_qty() < 70 * 70):

                self.qp.setPen(QColor(Qt.white))
                fontsize = self.qp.font().pointSize()
                font = self.qp.font()

                font.setPointSize(zoom_level / 3)
                #font.setPixelSize(zoom_level)
                self.qp.setFont(font)

                opacity = self.qp.opacity()
                full_opacity_zoom = 28
                cur_opacity = (
                    1.0 - (full_opacity_zoom -
                           float(min(zoom_level - 1, full_opacity_zoom))) /
                    full_opacity_zoom)
                self.qp.setOpacity(cur_opacity)

                m = self.qp.fontMetrics()
                x = y = 0
                num_pixels_per_line = self.get_pixel_qty_per_line()

                cm = self.qp.compositionMode()
                self.qp.setCompositionMode(
                    self.composition_modes[self.cur_compos_mode][0])

                if self.formatters[self.cur_formatter_idx][0] == 1:
                    fmt = lambda c: "%c" % c if c in range(0x20, 0x7e) else "."
                elif self.formatters[self.cur_formatter_idx][0] == 2:
                    fmt = lambda c: "%02X" % c

                for mapped, buf in self.buffers:
                    for i in range(len(buf)):
                        if mapped:
                            b = buf[i]
                            data = fmt(b)

                            self.qp.drawText(self.rect_x + x * zoom_level,
                                             y * zoom_level, zoom_level,
                                             zoom_level, Qt.AlignCenter, data)

                        x = (i + 1) % num_pixels_per_line
                        if not x:
                            y = y + 1

                # restore attributes
                self.qp.setCompositionMode(cm)
                self.qp.setOpacity(opacity)
                font.setPointSize(fontsize)
                self.qp.setFont(font)

        if self.show_address_range and self.fm.link_pixel:
            self.paint_slider(addr=content_addr, buf_size=content_size)

        # get and draw annotations and pointers
        annotations = self.fm.on_get_annotations(
            content_addr if content_addr else self.get_address(),
            self.get_pixel_qty(), self.mouseOffs)

        if annotations:
            self.paint_annotations(annotations)

        self.paint_status()

        if self.textbox_content:
            self.paint_text_box()

        self.qp.end()
        return