Ejemplo n.º 1
0
    def anterior(self, comment):
        index = 0

        for index, line in enumerate(comment.splitlines()):
            idc.ExtLinA(self._ea, index, line)

        idc.DelExtLnA(self._ea, index + 1)
Ejemplo n.º 2
0
def load_struc(struc, p, path):
    '''
    Load the given structure into the current IDA Pro at the given offset.

    Example::

        load_struc(sections[0].data, 0x0, 'foo-section')

    Args:
      struc (wasm.Structure): the structure to load.
      p (int): the effective address at which to load.
      path (str): the namespaced name of the given structure.

    Returns:
      int: the next offset following the loaded structure.
    '''
    for field in idawasm.common.get_fields(struc):
        # build names like: `sections:2:payload:entries:0:module_str`
        name = path + ':' + field.name

        # recurse into nested structures
        if idawasm.common.is_struc(field.value):
            p = load_struc(field.value, p, name)

        # recurse into lists of structures
        elif (isinstance(field.value, list) and len(field.value) > 0
              and idawasm.common.is_struc(field.value[0])):

            for i, v in enumerate(field.value):
                p = load_struc(v, p, name + ':' + str(i))

        # emit primitive types
        else:
            # add annotations like follows:
            #
            #     imports:002D sections:2:payload:entries:0:module_len         <--- Add line prior to element.
            #     imports:002D                 db 3                    ;; 0x3  <--- Render element for human.
            #     imports:002E sections:2:payload:entries:0:module_str
            #     imports:002E                 db 0x65 ;; e            ;; env  <--- Pull out strings and lists nicely.
            #     imports:002F                 db 0x6E ;; n
            #     imports:0030                 db 0x76 ;; v

            # add line prior to element
            idc.ExtLinA(p, 0, name)

            # if primitive integer element, set it as such
            if isinstance(field.value, int):
                MakeN(p, field.size)

            # add comment containing human-readable representation
            idc.MakeComm(p, format_value(name, field.value).encode('utf-8'))

            p += field.size

    return p
Ejemplo n.º 3
0
def header_info(li, addr):
    idaapi.add_long_cmt(addr, True, "-------------------------------")
    li.seek(0x0)
    idc.ExtLinA(addr, 1, "; ROM HEADER")
    idc.ExtLinA(addr, 2, "; Signature : %s" % li.read(4))
    idc.ExtLinA(
        addr, 3, "; Number of 16K PRG-ROM Pages : %02X" %
        struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        addr, 4, "; Number of 8K CHR-ROM Pages : %02X" %
        struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        addr, 5,
        "; Cartridge Type LSB : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        addr, 6,
        "; Cartridge Type MSB : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        addr, 7,
        "; Number of 8K RAM : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(addr, 8, "-------------------------------")
Ejemplo n.º 4
0
def load_globals_section(section, p):
    '''
    Specialized handler for the GLOBALS section to mark the initializer as code.
    '''
    ppayload = p + idawasm.common.offset_of(section.data, 'payload')
    pglobals = ppayload + idawasm.common.offset_of(section.data.payload,
                                                   'globals')
    pcur = pglobals
    for i, body in enumerate(section.data.payload.globals):
        gname = 'global_%X' % (i)
        # we need a target that people can rename.
        # so lets map `global_N` to the init expr field.
        # this will look like:
        #
        #     global_0        <---- named address we can reference
        #     global_0_init:  <---- fake label line
        #        i32.const    <---- init expression insns
        #        ret
        pinit = pcur + idawasm.common.offset_of(body, 'init')
        idc.MakeName(pinit, gname)
        idc.ExtLinA(pinit, 0, gname + '_init:')
        idc.MakeCode(pinit)

        pcur += idawasm.common.size_of(body)
Ejemplo n.º 5
0
def load_file(li, neflags, format):
    if format != ROM_FORMAT_NAME:
        Warning("Unknown format name: '%s'" % format)
        return 0
    jump = dwordAt(li, 0)
    # Test ARM branch
    if jump & 0xFF000000 != 0xEA000000:
        Warning("Unknown format name: '%s'" % format)
        return 0
    idaapi.set_processor_type("arm", SETPROC_ALL | SETPROC_FATAL)
    li.seek(0, idaapi.SEEK_END)
    size = li.tell()

    # Adding Header Section
    idc.AddSeg(ROM_START, ROM_START + SIZE_HEADER, 0, 1, idaapi.saRelPara,
               idaapi.scPub)
    idc.RenameSeg(ROM_START, "HEADER")
    idc.SetSegmentType(ROM_START, idc.SEG_CODE)
    li.seek(0)
    li.file2base(0, ROM_START, ROM_START + SIZE_HEADER, 0)

    # Adding OEP
    idaapi.add_entry(ROM_START, ROM_START, "start", 1)
    idaapi.cvar.inf.startIP = ROM_START
    idaapi.cvar.inf.beginEA = ROM_START

    # Adding ROM Section
    idc.AddSeg(ROM_START + SIZE_HEADER, ROM_START + (ROM_SIZE - SIZE_HEADER),
               0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.RenameSeg(ROM_START + SIZE_HEADER, "ROM")
    idc.SetSegmentType(ROM_START + SIZE_HEADER, idc.SEG_CODE)
    li.seek(SIZE_HEADER)
    li.file2base(0, ROM_START + SIZE_HEADER, ROM_START + size, 0)

    # Adding EWRAM
    idc.AddSeg(0x02000000, 0x02040000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.RenameSeg(0x02000000, "EWRAM")
    memset_seg(0x02000000, 0x40000)

    # Adding IWRAM
    idc.AddSeg(0x03000000, 0x03008000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.RenameSeg(0x03000000, "IWRAM")
    memset_seg(0x03000000, 0x8000)

    # Adding IO / Registers
    idc.AddSeg(0x04000000, 0x04000400, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.RenameSeg(0x04000000, "IOregisters")
    memset_seg(0x04000000, 0x400)

    # Adding BIOS System ROM
    idc.AddSeg(0x00000000, 0x00004000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.RenameSeg(0x00000000, "BIOS")
    memset_seg(0x00000000, 0x4000)
    idc.SetSegmentType(0x0000000, idc.SEG_CODE)

    idaapi.add_long_cmt(ROM_START, True, "ROM HEADER")
    li.seek(0xA0)
    idc.ExtLinA(ROM_START, 1, "; Game Title : %s" % li.read(12))
    idc.ExtLinA(ROM_START, 2, "; Game Code : %s" % li.read(4))
    idc.ExtLinA(ROM_START, 3, "; Marker Code : %s" % li.read(2))
    idc.ExtLinA(ROM_START, 4,
                "; Fixed value : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(ROM_START, 5,
                "; Main unit code : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(ROM_START, 6,
                "; Device type : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(ROM_START, 7, "; Reserved Area : db 7 dup(0)")
    li.read(7)
    idc.ExtLinA(ROM_START, 8,
                "; Software version %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(ROM_START, 9,
                "; Complement Check %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(ROM_START, 10, "; Reserved Area : db 2 dup(0)")

    io_naming()
    print("[+] Load OK")
    return 1
Ejemplo n.º 6
0
def load_file(li, neflags, format):
    li.seek(0)
    ndsRom = NintendoDSRom(li.read(li.size()))
    retval = 1

    useArm9 = ask_yn(
        1,
        "This ROM potentially contains both ARM9 and ARM7 code\nDo you want to load the ARM9 binary?"
    )
    if (useArm9 == -1):
        useArm9 = 0

    useArm9 = bool(useArm9)

    proc = ""
    startEA = 0
    endEA = 0
    offset = 0
    entryAddr = 0
    size = 0
    name = ""
    rom = ""
    if (useArm9):
        name = "ARM9 ROM"
        proc = "ARM"
        entryAddr = ndsRom.arm9EntryAddress
        startEA = ndsRom.arm9RamAddress
        endEA = ndsRom.arm9RamAddress + ndsRom.arm9Len
        offset = ndsRom.arm9Offset
        size = ndsRom.arm9Len
        rom = ndsRom.arm9
    else:
        name = "ARM7 ROM"
        proc = "ARM710A"
        entryAddr = ndsRom.arm7EntryAddress
        startEA = ndsRom.arm7RamAddress
        endEA = ndsRom.arm7RamAddress + ndsRom.arm7Len
        offset = ndsRom.arm7Offset
        size = ndsRom.arm7Len
        rom = ndsRom.arm7

    idaapi.set_processor_type(
        proc, idaapi.SETPROC_LOADER_NON_FATAL | idaapi.SETPROC_LOADER)

    memory =  \
    [
        [ startEA, endEA, "RAM" ],
        [ 0x04000000, 0x04001056, "General_Regs" ],
        [ 0x05000000, 0x05000600, "VMEM_Regs" ],
    ]

    if ((startEA < memory[0][0] or endEA > memory[0][1])
            and (startEA < memory[1][0] or endEA > memory[1][1])
            and (startEA < memory[2][0] or endEA > memory[2][1])):
        raise Exception("ROM not mapped into valid mem!")

    for segment in memory:
        idc.AddSeg(segment[0], segment[1], 0, 1, idaapi.saRelPara,
                   idaapi.scPub)
        idc.RenameSeg(segment[0], segment[2])

        if "RAM" not in segment[2]:
            for i in xrange(segment[0], segment[1]):
                idc.PatchByte(i, 0)

    idaapi.add_entry(entryAddr, entryAddr, "start", 1)
    idc.MakeNameEx(entryAddr, "start", idc.SN_NOCHECK | idc.SN_NOWARN)
    idaapi.cvar.inf.startIP = entryAddr
    idaapi.cvar.inf.beginEA = entryAddr
    ida_segment.set_selector(1, 0)
    idaapi.cvar.inf.startCS = 1

    li.seek(0)
    li.file2base(offset, startEA, endEA, 1)

    idaapi.cvar.inf.startCS = 0
    idaapi.cvar.inf.startIP = entryAddr

    idc.ExtLinA(startEA, 1, "; Title : " + str(ndsRom.name))
    idc.ExtLinA(startEA, 1, "; Software Version: " + str(ndsRom.version))

    # Add TwlHdr
    MakeVideoRegs()
    MakeVMemRegs()
    MakeJoypadRegs()
    MakeSystemRegs()
    if name == "ARM7 ROM":
        MakeARM7Regs()
    else:
        MakeARM9Regs()

    print("Done! Entry point @ " + hex(entryAddr))
    return 1
Ejemplo n.º 7
0
def header_info(li):
    idaapi.add_long_cmt(0, True, "-------------------------------")
    li.seek(0x100)
    idc.ExtLinA(0, 1, "; ROM HEADER")
    idc.ExtLinA(
        0, 2,
        "; Entry Point : %04X" % (struct.unpack("<I", li.read(4))[0] >> 0x10))
    li.read(0x30)
    idc.ExtLinA(0, 3, "; TITLE : %s" % li.read(0xF))
    idc.ExtLinA(0, 4, "; Manufacturer Code : %s" % li.read(4))
    idc.ExtLinA(0, 5, "; CGB Flag : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 6,
        "; New Licensee Code : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(0, 7, "; SGB Flag : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(0, 8,
                "; Cartridge Type : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(0, 9, "; ROM Size : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(0, 10,
                "; RAM Size : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 11,
        "; Destination Code : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 12,
        "; Old license Code : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 13, "; Mask ROM Version number : %02X" %
        struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 14, "; Header Checksum : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(
        0, 15, "; Global Checksum : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.ExtLinA(0, 16, "-------------------------------")