예제 #1
0
파일: sample2.py 프로젝트: lebek/pyudis86
def main():
    ud_obj = udis86.init()
    ud_obj.set_input_buffer('\xccH\x8d\x05\x8b\x00\x00\x00')
    ud_obj.set_mode(udis86.MOD_64)
    ud_obj.set_pc(4524773054L)
    ud_obj.set_syntax(udis86.UD_SYN_INTEL)

    while ud_obj.disassemble():
        print "%016d %-16s %-24s" % (
            ud_obj.insn_off(), ud_obj.insn_hex(), ud_obj.insn_asm())
예제 #2
0
def main():
    ud_obj = udis86.init()
    ud_obj.set_input_file(sys.stdin)
    ud_obj.set_mode(udis86.MOD_64)
    ud_obj.set_pc(0)
    ud_obj.set_syntax(udis86.UD_SYN_INTEL)

    while True:
        ud_obj.disassemble()
        print "%016d %-16s %-24s" % (
            ud_obj.insn_off(), ud_obj.insn_hex(), ud_obj.insn_asm()
        )
예제 #3
0
파일: hexasm.py 프로젝트: cybojanek/hexasm
    def disassemble(self):
        """Disassemble the buffer and update pad

        """
        self.u = udis86.init()
        self.u.set_input_buffer(self.buffer)
        self.u.set_mode(udis86.MOD_64)
        self.u.set_pc(0)
        self.u.set_syntax(udis86.UD_VENDOR_INTEL)
        self.off = []
        self.data = []
        self.ins = []
        self.modrm = []
        self.primary_opcode = []
        while self.u.disassemble():
            self.off.append("%08x" % self.u.insn_off())
            self.data.append(self.u.insn_hex())
            self.ins.append(self.u.insn_asm())
            self.modrm.append((self.u.have_modrm, self.u.modrm))
            self.primary_opcode.append(self.u.primary_opcode)

        # Redo pad on line count change
        if self.pad is not None and self.pad.getmaxyx()[0] != len(self.data) + 2:
            self.pad.clear()
            self.stdscr.clear()
            self.stdscr.refresh()
            self.redraw_base()
            self.pad = None
        elif self.pad is not None:  # Erase and draw redraw borders
            self.pad.erase()
            self.pad.border()
            self.redraw_base()

        if self.pad is None:  # First time
            self.pad = curses.newpad(len(self.data) + 2, self.width)
            self.pad.border()
            self.redraw_base()

        # Fill pad buffer
        y = 1
        for i, (off, data, asm) in enumerate(zip(self.off, self.data, self.ins)):
            x = 1
            # Write offset
            for h in off:
                self.pad.addch(y, x, h, curses.color_pair(1))
                x += 1
            x += 2
            # Write hex
            can_prefix = True
            can_rex = False
            can_op = False
            can_mod = False
            can_sib = False
            can_other = False
            for h in group(data, 2):
                hit = False
                h = "".join(h)
                color = 0
                if can_prefix:
                    if h in prefixes:
                        color = curses.color_pair(6)
                    else:
                        can_prefix = False
                        can_rex = True
                if can_rex:
                    if h[0] == "4":
                        color = curses.color_pair(6)
                        hit = True
                    can_rex = False
                    can_op = True
                if can_op and not hit:
                    # if int("0x%s" % h, base=16) == self.primary_opcode[self.y]:
                    color = curses.color_pair(2)
                    hit = True
                    can_op = False
                    can_mod = True
                if can_mod and not hit:
                    if self.modrm[i][0]:
                        color = curses.color_pair(4)
                        hit = True
                    can_mod = False
                    can_sib = True
                if can_sib and not hit:
                    if self.modrm[i][0]:
                        m = hex(self.modrm[i][1])[2:]
                        m = "0" * (2 - len(m)) + m
                        b = bin(int("0x%s" % m, base=16))[2:]
                        b = "0" * (8 - len(b)) + b
                        if b[0:2] == "11" or b[5:] != "100":
                            color = curses.color_pair(4)
                            hit = True
                    can_sib = False
                    can_other = True
                if can_other and not hit:
                    color = curses.color_pair(3)
                if asm.rstrip().lstrip() == "invalid":
                    color = curses.color_pair(5)
                self.pad.addstr(y, x, h, color)
                x += 3
            # Write instructions
            x = 42
            x += 5
            p = asm.split()
            # Write out rest
            for i in p:
                color = None
                stripped = i.lstrip(',').rstrip(',').strip().rstrip()
                if re.match("^[\w]+$", stripped):
                    color = curses.color_pair(2)
                if stripped == "invalid":
                    color = curses.color_pair(5)
                # Highlight hex color
                if hex_re.match(stripped):  # Strip comma
                    color = curses.color_pair(3)
                # Highlight reg
                if reg_re.match(stripped):  # Strip comma
                    color = curses.color_pair(4)
                if i[-1] == ',':
                    self.pad.addstr(y, x, i[0:-1], color or 0)
                    self.pad.addstr(y, x + len(i) - 1, i[-1], 0)
                else:
                    self.pad.addstr(y, x, i, color or 0)
                x += len(i) + 1
            y += 1