Exemplo n.º 1
0
def miasm_dis(r2_op, r2_address, r2_buffer, r2_length):
    """Disassemble an instruction using miasm."""

    # Cast radare2 variables
    rasmop = ffi.cast("RAsmOp_r2m2*", r2_op)
    opcode = ffi.cast("char*", r2_buffer)

    # Prepare the opcode
    opcode = ffi.unpack(opcode, r2_length)

    # Get the miasm2 machine
    machine = miasm_machine()
    if machine is None:
        return

    # Disassemble the opcode
    loc_db = LocationDB()
    try:
        mode = machine.dis_engine().attrib
        instr = machine.mn().dis(opcode, mode)
        instr.offset = r2_address
        if instr.dstflow():
            # Remember ExprInt arguments sizes
            args_size = list()
            for i in range(len(instr.args)):
                if isinstance(instr.args[i], ExprInt):
                    args_size.append(instr.args[i].size)
                else:
                    args_size.append(None)

            # Adjust arguments values using the instruction offset
            instr.dstflow2label(loc_db)

            # Convert ExprLoc to ExprInt
            for i in range(len(instr.args)):
                if args_size[i] is None:
                    continue
                if isinstance(instr.args[i], ExprLoc):
                    addr = loc_db.get_location_offset(instr.args[i].loc_key)
                    instr.args[i] = ExprInt(addr, args_size[i])

        dis_str = str(instr)
        dis_len = instr.l
    except Exception:
        dis_str = "/!\ Can't disassemble using miasm /!\\"
        dis_len = 2  # GV: seems fischy !

    # Remaining bytes
    buf_hex = opcode[0:dis_len].encode("hex")

    # Check buffer sizes
    if len(dis_str)-1 > 256:
        dis_str = "/!\ Disassembled instruction is too long /!\\"
    if len(buf_hex)-1 > 256:
        buf_hex = buf_hex[:255]

    # Fill the RAsmOp structure
    rasmop.size = dis_len
    set_rbuf(rasmop.buf_asm, dis_str)
    set_rbuf(rasmop.buf_hex, buf_hex)
Exemplo n.º 2
0
def miasm_dis(r2_buffer, r2_length, r2_op):
    """Disassemble an instruction using miasm."""

    # Cast radare2 variables
    rasmop = ffi.cast("RAsmOp_r2m2*", r2_op)
    opcode = ffi.cast("char*", r2_buffer)

    # Prepare the opcode
    opcode = ffi.unpack(opcode, r2_length)

    # Get the miasm2 machine
    machine = miasm_machine()
    if machine is None:
        return

    # Disassemble the opcode
    try:
        mode = machine.dis_engine().attrib
        instr = machine.mn().dis(opcode, mode)
        dis_str = str(instr)
        dis_len = instr.l
    except:
        dis_str = "/!\ Can't disassemble using miasm /!\\"
        dis_len = 2  # GV: seems fischy !

    # Fill the RAsmOp structure
    rasmop.size = dis_len
    rasmop.buf_asm = dis_str
    rasmop.buf_hex = opcode[0:rasmop.size].encode("hex")
Exemplo n.º 3
0
def miasm_asm(r2_buffer, r2_op):
    """Assemble an instruction using miasm."""

    # Cast radare2 variables
    rasmop = ffi.cast("RAsmOp_r2m2*", r2_op)
    mn_str = ffi.string(r2_buffer)

    # miasm2 only parses upper case mnemonics
    mn_str = mn_str.upper()
    mn_str = mn_str.replace("X", "x")  # hexadecimal

    # Get the miasm2 machine
    machine = miasm_machine()
    if machine is None:
        return

    # Get the miasm2 mnemonic object
    mn = machine.mn()

    # Assemble and return all possible candidates
    mode = machine.dis_engine().attrib
    instr = mn.fromstring(mn_str, mode)
    asm_instr = [i for i in mn.asm(instr)][0]

    # Fill the RAsmOp structure
    rasmop.size = len(asm_instr)
    rasmop.buf = asm_instr
    rasmop.buf_hex = asm_instr.encode("hex")
Exemplo n.º 4
0
def miasm_asm(r2_op, r2_address, r2_buffer):
    """Assemble an instruction using miasm."""

    # Cast radare2 variables
    rasmop = ffi.cast("RAsmOp_r2m2*", r2_op)
    mn_str = ffi.string(r2_buffer)

    # miasm2 only parses upper case mnemonics
    mn_str = mn_str.upper()
    mn_str = mn_str.replace("X", "x")  # hexadecimal

    # Get the miasm2 machine
    machine = miasm_machine()
    if machine is None:
        return

    # Get the miasm2 mnemonic object
    mn = machine.mn()

    # Assemble and return all possible candidates
    loc_db = LocationDB()
    mode = machine.dis_engine().attrib
    instr = mn.fromstring(mn_str, loc_db, mode)
    instr.mode = mode
    instr.offset = r2_address
    if instr.offset and instr.dstflow():
        # Adjust arguments values using the instruction offset
        instr.fixDstOffset()
    asm_instr = [i for i in mn.asm(instr)][0]

    # Assembled instructions in hexadecimal
    buf_hex = asm_instr.encode("hex")

    # Check buffer sizes
    if len(asm_instr)-1 > 256:
        print >> sys.stderr, "/!\ Assembled instruction is too long /!\\"
        return
    if len(buf_hex)-1 > 256:
        buf_hex = buf_hex[:255]

    # Fill the RAsmOp structure
    rasmop.size = len(asm_instr)
    set_rbuf(rasmop.buf, asm_instr)
    set_rbuf(rasmop.buf_hex, buf_hex)
Exemplo n.º 5
0
def miasm_dis(r2_op, r2_address, r2_buffer, r2_length):
    """Disassemble an instruction using miasm."""

    # Cast radare2 variables
    rasmop = ffi.cast("RAsmOp_r2m2*", r2_op)
    opcode = ffi.cast("char*", r2_buffer)

    # Prepare the opcode
    opcode = ffi.unpack(opcode, r2_length)

    # Get the miasm2 machine
    machine = miasm_machine()
    if machine is None:
        return

    # Disassemble the opcode
    try:
        mode = machine.dis_engine().attrib
        instr = machine.mn().dis(opcode, mode)
        instr.offset = r2_address
        if instr.dstflow():

            # Remember ExprInt arguments sizes
            args_size = list()
            for i in range(len(instr.args)):
                if isinstance(instr.args[i], ExprInt):
                    args_size.append(instr.args[i].size)
                else:
                    args_size.append(None)

            # Adjust arguments values using the instruction offset
            instr.dstflow2label(AsmSymbolPool())

            # Convert label back to ExprInt
            for i in range(len(instr.args)):
                if args_size[i] is None:
                    continue
                if isinstance(instr.args[i], ExprId) and \
                   isinstance(instr.args[i].name, AsmLabel):
                    addr = str(instr.args[i].name)
                    addr = int(addr.split(":")[1], 16)
                    instr.args[i] = ExprInt(addr, args_size[i])


        dis_str = str(instr)
        dis_len = instr.l
    except:
        dis_str = "/!\ Can't disassemble using miasm /!\\"
        dis_len = 2  # GV: seems fischy !

    # Remaining bytes
    buf_hex = opcode[0:dis_len].encode("hex")

    # Check buffer sizes
    if len(dis_str)-1 > 256:
        dis_str = "/!\ Disassembled instruction is too long /!\\"
    if len(buf_hex)-1 > 256:
        buf_hex = buf_hex[:255]

    # Fill the RAsmOp structure
    rasmop.size = dis_len
    rasmop.buf_asm = dis_str
    rasmop.buf_hex = buf_hex