Beispiel #1
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")
Beispiel #2
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)