Example #1
0
    def ev_ana_insn(self, insn):
        b1 = idaapi.get_byte(insn.ea)
        if b1 >= 0x70 and b1 <= 0x7F:
            d1 = idaapi.get_byte(insn.ea + 1)
            b2 = idaapi.get_byte(insn.ea + 2)
            d2 = idaapi.get_byte(insn.ea + 3)
            if b2 == b1 ^ 0x01 and d1 - 2 == d2:
                idaapi.put_byte(insn.ea, 0xEB)
                idaapi.put_word(insn.ea + 2, 0x9090)

        elif b1 == 0x0F:
            b1_1 = idaapi.get_byte(insn.ea + 1)
            d1 = idaapi.get_long(insn.ea + 2)
            b2 = idaapi.get_byte(insn.ea + 6)
            b2_1 = idaapi.get_byte(insn.ea + 7)
            d2 = idaapi.get_long(insn.ea + 8)
            if b2 == 0x0F and b1_1 ^ 0x01 == b2_1 and d1 - 6 == d2:
                idaapi.put_byte(insn.ea, 0xE9)
                idaapi.put_long(insn.ea + 1, d1 + 1)
                idaapi.put_byte(insn.ea + 5, 0x90)
                idaapi.put_word(insn.ea + 6, 0x9090)
                idaapi.put_long(insn.ea + 8, 0x90909090)

        return False
Example #2
0
    def load_file(li, neflags, format):
        idaapi.set_processor_type("arm",
                                  idaapi.SETPROC_ALL | idaapi.SETPROC_FATAL)
        f = load_nxo(li)
        if f.armv7:
            SetShortPrm(INF_LFLAGS, GetShortPrm(INF_LFLAGS) | LFLG_PC_FLAT)
        else:
            SetShortPrm(INF_LFLAGS, GetShortPrm(INF_LFLAGS) | LFLG_64BIT)

        SetCharPrm(INF_DEMNAMES, idaapi.DEMNAM_GCC3)
        idaapi.set_compiler_id(idaapi.COMP_GNU)
        idaapi.add_til2('gnulnx_arm' if f.armv7 else 'gnulnx_arm64', 1)

        loadbase = 0x60000000 if f.armv7 else 0x7100000000

        f.binfile.seek(0)
        as_string = f.binfile.read(f.bssoff)
        idaapi.mem2base(as_string, loadbase)
        if f.text[1] != None:
            li.file2base(f.text[1], loadbase + f.text[2],
                         loadbase + f.text[2] + f.text[3], True)
        if f.ro[1] != None:
            li.file2base(f.ro[1], loadbase + f.ro[2],
                         loadbase + f.ro[2] + f.ro[3], True)
        if f.data[1] != None:
            li.file2base(f.data[1], loadbase + f.data[2],
                         loadbase + f.data[2] + f.data[3], True)

        for start, end, name, kind in f.sections:
            if name.startswith('.got'):
                kind = 'CONST'
            idaapi.add_segm(0, loadbase + start, loadbase + end, name, kind)
            segm = idaapi.get_segm_by_name(name)
            if kind == 'CONST':
                segm.perm = idaapi.SEGPERM_READ
            elif kind == 'CODE':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_EXEC
            elif kind == 'DATA':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_WRITE
            elif kind == 'BSS':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_WRITE
            idaapi.update_segm(segm)
            idaapi.set_segm_addressing(segm, 1 if f.armv7 else 2)

        # do imports
        # TODO: can we make imports show up in "Imports" window?
        undef_count = 0
        for s in f.symbols:
            if not s.shndx and s.name:
                undef_count += 1
        last_ea = max(loadbase + end for start, end, name, kind in f.sections)
        undef_entry_size = 8
        undef_ea = (
            (last_ea + 0xFFF) & ~0xFFF
        ) + undef_entry_size  # plus 8 so we don't end up on the "end" symbol
        idaapi.add_segm(0, undef_ea, undef_ea + undef_count * undef_entry_size,
                        "UNDEF", "XTRN")
        segm = idaapi.get_segm_by_name("UNDEF")
        segm.type = idaapi.SEG_XTRN
        idaapi.update_segm(segm)
        for i, s in enumerate(f.symbols):
            if not s.shndx and s.name:
                MakeQword(undef_ea)
                idaapi.do_name_anyway(undef_ea, s.name)
                s.resolved = undef_ea
                undef_ea += undef_entry_size
            elif i != 0:
                assert s.shndx
                s.resolved = loadbase + s.value
                if s.name:
                    if s.type == STT_FUNC:
                        print hex(s.resolved), s.name
                        idaapi.add_entry(s.resolved, s.resolved, s.name, 0)
                    else:
                        idaapi.do_name_anyway(s.resolved, s.name)

            else:
                # NULL symbol
                s.resolved = 0

        funcs = set()
        for s in f.symbols:
            if s.name and s.shndx and s.value:
                if s.type == STT_FUNC:
                    funcs.add(loadbase + s.value)

        got_name_lookup = {}
        for offset, r_type, sym, addend in f.relocations:
            target = offset + loadbase
            if r_type in (R_ARM_GLOB_DAT, R_ARM_JUMP_SLOT, R_ARM_ABS32):
                if not sym:
                    print 'error: relocation at %X failed' % target
                else:
                    idaapi.put_long(target, sym.resolved)
            elif r_type == R_ARM_RELATIVE:
                idaapi.put_long(target, idaapi.get_long(target) + loadbase)
            elif r_type in (R_AARCH64_GLOB_DAT, R_AARCH64_JUMP_SLOT,
                            R_AARCH64_ABS64):
                idaapi.put_qword(target, sym.resolved + addend)
                if addend == 0:
                    got_name_lookup[offset] = sym.name
            elif r_type == R_AARCH64_RELATIVE:
                idaapi.put_qword(target, loadbase + addend)
                if addend < f.textsize:
                    funcs.add(loadbase + addend)
            else:
                print 'TODO r_type %d' % (r_type, )
            ida_make_offset(f, target)

        for func, target in f.plt_entries:
            if target in got_name_lookup:
                addr = loadbase + func
                funcs.add(addr)
                idaapi.do_name_anyway(addr, got_name_lookup[target])

        funcs |= find_bl_targets(loadbase, loadbase + f.textsize)

        for addr in sorted(funcs, reverse=True):
            AutoMark(addr, AU_CODE)
            AutoMark(addr, AU_PROC)

        return 1
Example #3
0
    def load_one_file(li, options, idx, basename=None):
        bypass_plt = OPT_BYPASS_PLT in options

        f = load_nxo(li)

        if idx == 0:
            if f.armv7:
                idc.SetShortPrm(idc.INF_LFLAGS, idc.GetShortPrm(idc.INF_LFLAGS) | idc.LFLG_PC_FLAT)
            else:
                idc.SetShortPrm(idc.INF_LFLAGS, idc.GetShortPrm(idc.INF_LFLAGS) | idc.LFLG_64BIT)

            idc.SetCharPrm(idc.INF_DEMNAMES, idaapi.DEMNAM_GCC3)
            idaapi.set_compiler_id(idaapi.COMP_GNU)
            idaapi.add_til2('gnulnx_arm' if f.armv7 else 'gnulnx_arm64', 1)
            # don't create tails
            idc.set_inf_attr(idc.INF_AF, idc.get_inf_attr(idc.INF_AF) & ~idc.AF_FTAIL)

        if OPT_LOAD_31_BIT in options:
            loadbase = 0x8000000
            step = 0x1000000
        elif f.armv7:
            loadbase = 0x60000000
            step = 0x10000000
        else:
            loadbase = 0x7100000000
            step = 0x100000000
        loadbase += idx * step

        f.binfile.seek(0)
        as_string = f.binfile.read(f.bssoff)
        idaapi.mem2base(as_string, loadbase)

        seg_prefix = basename if basename is not None else ''
        for start, end, name, kind in f.sections:
            if name.startswith('.got'):
                kind = 'CONST'
            idaapi.add_segm(0, loadbase+start, loadbase+end, seg_prefix+name, kind)
            segm = idaapi.get_segm_by_name(seg_prefix+name)
            if kind == 'CONST':
                segm.perm = idaapi.SEGPERM_READ
            elif kind == 'CODE':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_EXEC
            elif kind == 'DATA':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_WRITE
            elif kind == 'BSS':
                segm.perm = idaapi.SEGPERM_READ | idaapi.SEGPERM_WRITE
            idaapi.update_segm(segm)
            idaapi.set_segm_addressing(segm, 1 if f.armv7 else 2)

        # do imports
        # TODO: can we make imports show up in "Imports" window?
        undef_count = 0
        for s in f.symbols:
            if not s.shndx and s.name:
                undef_count += 1
        last_ea = max(loadbase + end for start, end, name, kind in f.sections)
        undef_entry_size = 8
        undef_ea = ((last_ea + 0xFFF) & ~0xFFF) + undef_entry_size # plus 8 so we don't end up on the "end" symbol

        undef_seg = basename + '.UNDEF' if basename is not None else 'UNDEF'
        idaapi.add_segm(0, undef_ea, undef_ea+undef_count*undef_entry_size, undef_seg, 'XTRN')
        segm = idaapi.get_segm_by_name(undef_seg)
        segm.type = idaapi.SEG_XTRN
        idaapi.update_segm(segm)
        for i,s in enumerate(f.symbols):
            if not s.shndx and s.name:
                idc.MakeQword(undef_ea)
                idaapi.do_name_anyway(undef_ea, s.name)
                s.resolved = undef_ea
                undef_ea += undef_entry_size
            elif i != 0:
                assert s.shndx
                s.resolved = loadbase + s.value
                if s.name:
                    if s.type == STT_FUNC:
                        idaapi.add_entry(s.resolved, s.resolved, s.name, 0)
                    else:
                        idaapi.do_name_anyway(s.resolved, s.name)

            else:
                # NULL symbol
                s.resolved = 0

        funcs = set()
        for s in f.symbols:
            if s.name and s.shndx and s.value:
                if s.type == STT_FUNC:
                    funcs.add(loadbase+s.value)
                    symend = loadbase+s.value+s.size
                    if Dword(symend) != 0:
                        funcs.add(symend)

        got_name_lookup = {}
        for offset, r_type, sym, addend in f.relocations:
            target = offset + loadbase
            if r_type in (R_ARM_GLOB_DAT, R_ARM_JUMP_SLOT, R_ARM_ABS32):
                if not sym:
                    print 'error: relocation at %X failed' % target
                else:
                    idaapi.put_long(target, sym.resolved)
            elif r_type == R_ARM_RELATIVE:
                idaapi.put_long(target, idaapi.get_long(target) + loadbase)
            elif r_type in (R_AARCH64_GLOB_DAT, R_AARCH64_JUMP_SLOT, R_AARCH64_ABS64):
                idaapi.put_qword(target, sym.resolved + addend)
                if addend == 0:
                    got_name_lookup[offset] = sym.name
            elif r_type == R_AARCH64_RELATIVE:
                idaapi.put_qword(target, loadbase + addend)
                if addend < f.textsize:
                    funcs.add(loadbase + addend)
            else:
                print 'TODO r_type %d' % (r_type,)
            ida_make_offset(f, target)

        for func, target in f.plt_entries:
            if target in got_name_lookup:
                addr = loadbase + func
                funcs.add(addr)
                idaapi.do_name_anyway(addr, got_name_lookup[target])

        if not f.armv7:
            funcs |= find_bl_targets(loadbase, loadbase+f.textsize)

            if bypass_plt:
                plt_lookup = f.plt_lookup
                for pco in xrange(0, f.textsize, 4):
                    pc = loadbase + pco
                    d = Dword(pc)
                    if (d & 0x7c000000) == (0x94000000 & 0x7c000000):
                        imm = d & 0x3ffffff
                        if imm & 0x2000000:
                            imm |= ~0x1ffffff
                        if 0 <= imm <= 2:
                            continue
                        target = (pc + imm * 4) - loadbase
                        if target in plt_lookup:
                            new_target = plt_lookup[target] + loadbase
                            new_instr = (d & ~0x3ffffff) | (((new_target - pc) / 4) & 0x3ffffff)
                            idaapi.put_long(pc, new_instr)

            for pco in xrange(0, f.textsize, 4):
                pc = loadbase + pco
                d = Dword(pc)
                if d == 0x14000001:
                    funcs.add(pc + 4)

        for pc, _ in f.eh_table:
            funcs.add(loadbase + pc)

        for addr in sorted(funcs, reverse=True):
            idaapi.auto_make_proc(addr)

        return 1
Example #4
0
def load_file(li, neflags, format):

    # Read in the bFLT header fields
    li.seek(0)
    (magic, version, entry, data_start, data_end, bss_end, stack_size,
     reloc_start, reloc_count, flags) = struct.unpack(">IIIIIIIIII",
                                                      li.read(4 * 10))

    # Check for the GZIP flag.
    # The loader doesn't de-compress GZIP'd files, as these can be easily decompressed with external tools prior to loading the file into IDA
    if (flags & FLAGS_GZIP) == FLAGS_GZIP:
        Warning(
            "Code/data is GZIP compressed. You probably want to decompress the bFLT file with the flthdr or gunzip_bflt utilities before loading it into IDA."
        )

    # Load the file data into IDA
    li.file2base(BFLT_HEADER_SIZE, BFLT_HEADER_SIZE, data_end, True)

    # Add the .text .data and .bss segments
    idaapi.add_segm(0, BFLT_HEADER_SIZE, data_start, ".text", "CODE")
    idaapi.add_segm(0, data_start, data_end, ".data", "DATA")
    idaapi.add_segm(0, data_end, bss_end, ".bss", "BSS")

    if DEBUG:
        print "Created File Segments: "
        print "\t.text   0x%.8X - 0x%.8X" % (BFLT_HEADER_SIZE, data_start)
        print "\t.data   0x%.8X - 0x%.8X" % (data_start, data_end)
        print "\t.bss    0x%.8X - 0x%.8X" % (data_end, bss_end)

    # Entry point is at the beginning of the .text section
    idaapi.add_entry(entry, entry, "_start", 1)

    # Set default processor
    idaapi.set_processor_type(DEFAULT_CPU, SETPROC_ALL)

    # Explicitly set 32 bit addressing on .text segment
    idaapi.set_segm_addressing(idaapi.getseg(entry), 1)

    # prepare structure for set_fixup()
    fd = idaapi.fixup_data_t()
    fd.type = idaapi.FIXUP_OFF32

    # Is there a global offset table?
    if (flags & FLAGS_GOTPIC) == FLAGS_GOTPIC:

        # Add a reptable comment and name the offset so that all references to GOT are obvious
        MakeRptCmt(data_start, "GLOBAL_OFFSET_TABLE")
        MakeName(data_start, "GOT")

        if DEBUG:
            print "Global Offset Table detected, patching..."

        # GOT starts at the beginning of the data section; loop through the data section, patching up valid GOT entries.
        i = data_start
        while i < data_end:

            # Get the next GOT entry
            li.seek(i)
            got_entry = struct.unpack("<I", li.read(4))[0]

            # The last GOT entry is -1
            if got_entry == 0xFFFFFFFF:
                if DEBUG:
                    print "Finished processing Global Offset Table."
                break

            # All other non-zero entries are valid GOT entries
            elif got_entry > 0:

                # The actual data is located at <original GOT entry> + <BFLT_HEADER_SIZE>
                new_entry = got_entry + BFLT_HEADER_SIZE

                if DEBUG:
                    print "Replacing GOT entry value 0x%.8X with 0x%.8X at offset 0x%.8X" % (
                        got_entry, new_entry, i)

                # Replace the GOT entry with the correct pointer
                idaapi.put_long(i, new_entry)
                # add info about relocation to help analyzer
                fd.off = new_entry
                idaapi.set_fixup(i, fd)

            # Make each GOT entry a DWORD
            MakeDword(i)

            # Point i at the next GOT entry address
            i = i + 4

    # Patch relocation addresses
    for i in range(0, reloc_count):
        try:
            # Get the next relocation entry.
            # Relocation entry = <address of bytes to be patched> - <BFLT_HEADER_SIZE>
            li.seek(reloc_start + (i * 4))
            reloc_offset = struct.unpack(">I",
                                         li.read(4))[0] + BFLT_HEADER_SIZE

            # Sanity check, make sure the relocation offset is in a defined segment
            if reloc_offset < bss_end:
                try:
                    # reloc_offset + base_offset == <pointer to actual data> - <BFLT_HEADER_SIZE>
                    li.seek(reloc_offset)
                    reloc_val = struct.unpack(">I", li.read(4))[0]
                    if reloc_val == 0:
                        # skip zero relocs
                        # see fs/binfmt_flat.c
                        if DEBUG:
                            print "Skipping zero reloc at (0x%.8X)" % reloc_offset
                        continue

                    reloc_data_offset = reloc_val + BFLT_HEADER_SIZE

                    if DEBUG:
                        print "Patching reloc: (0x%.8X) == 0x%.8X" % (
                            reloc_offset, reloc_data_offset)

                    # Replace pointer at reloc_offset with the address of the actual data
                    idaapi.put_long(reloc_offset, reloc_data_offset)
                    # add info about relocation to help analyzer
                    fd.off = reloc_data_offset
                    idaapi.set_fixup(reloc_offset, fd)
                except Exception, e:
                    print "Error patching relocation entry #%d: %s" % (i,
                                                                       str(e))
            elif DEBUG:
                print "Relocation entry #%d outside of defined file sections, skipping..." % i