Exemplo n.º 1
0
def load_script(db, script, path):

    if script.section.s_id in done_scripts:
        return

    done_scripts.add(script.section.s_id)

    f = io.StringIO()

    def print_into(*args, **kwargs):
        return print(*args, **kwargs, file=f)

    t = decompiler.decompile(db, script, print_into, path)
    t.info = f.getvalue()
    types.append(t)
    if t.parent is not None:
        tyxrefs.setdefault(t.parent.section.s_id, []).append(
            ("derived class", script))
        tyxrefs.setdefault(t.own_id, []).append(("parent class", t.parent))
    for offset, name, entry in t.fields:
        zs = []
        if entry.objty:
            zs = [entry.objty]
        elif entry.init.decl.tyext:
            zs = [entry.init.decl.tyext]
        if entry.init and entry.init.decl.tyarg:
            zs.append(entry.init.decl.tyarg.tyext)

        for z in zs:
            if not z:
                continue  # why
            if entry.init:
                link = "initialized field in"
                if entry.init.decl.ty & 15 == 12:
                    print("found something in", t.classname)

            else:
                link = "field in"
            tyxrefs.setdefault(z.section.s_id, []).append((link, script))

            if entry.init:
                process_entry_init(script, entry.init)

    for s in t.signatures:
        for d in s.args:
            if d.ty & 15 == 12:
                print("found something in", t.classname)
            if d.tyext is not None:
                r = d.tyext.section.s_id
                tyxrefs.setdefault(r, []).append(
                    ("argument to method on", script))
        for sig in s.calling:
            if isinstance(sig, tuple):  # ScriptDynArrayImpl or ScriptMapImpl
                continue
            #cs = decompiler.Signature(sig, symbols, 0x9999)
            method_xrefs.setdefault(sig, []).append(s)
Exemplo n.º 2
0
def load_single_pyc(GLOBALS, original, basedir, path):
    dirname, filename = os.path.split(path)
    module_name, ext  = os.path.splitext(filename)
    f = open(path, "rb")
    magic, moddate = f.read(4), f.read(4)
    assert magic == imp.get_magic()
    modtime = time.asctime(time.localtime(struct.unpack('=L', moddate)[0]))
    code = marshal.load(f)

    pyc_info = defaultdict(dict)
    pyc_info['magic_no']          = magic.encode('hex')
    pyc_info['mod_ts']['date']    = moddate.encode('hex')
    pyc_info['mod_ts']['time']    = modtime
    pyc_info['code_object']       = code
    pyc_info['ext_bytecode']      = decompiler.decompile(code)
    pyc_info['code']['argcount']  = code.co_argcount
    pyc_info['code']['nlocals']   = code.co_nlocals
    pyc_info['code']['stacksize'] = code.co_stacksize
    pyc_info['code']['flags']     = code.co_flags
    pyc_info['code']['name']      = code.co_name
    pyc_info['code']['names']     = code.co_names
    pyc_info['code']['varnames']  = code.co_varnames
    pyc_info['code']['freevars']  = code.co_freevars
    pyc_info['code']['cellvars']  = code.co_cellvars
    pyc_info['code']['filename']  = code.co_filename
    pyc_info['code']['firstlineno']  = code.co_firstlineno
    pyc_info['code']['consts']    = code.co_consts
    pyc_info['code']['lnotab']    = code.co_lnotab

    if False:
        import mypkg.instrumentor
        instrumentor = mypkg.instrumentor.Instrumentor(path.replace('_instrumented.pyc','.pyc'))
        exit_code = instrumentor.run()
        if module_name in sys.modules:
            del sys.modules[module_name]
        new_path = os.path.join(dirname, module_name+'_instrumented.pyc')
        module = imp.load_compiled(module_name, new_path)

    module = imp.load_compiled(module_name, path)
    submodule_key = basedir[len(original)+1:]+'/'+module_name
    GLOBALS['pyc_info'][submodule_key] = pyc_info
    for name, predicate in inspect_types.iteritems():
        the_list = inspect.getmembers(module,
            lambda m: inspect.getmodule(m) == module and apply(predicate,[m]))
        if the_list:
            GLOBALS['modules'][submodule_key][name] = the_list
    return True
Exemplo n.º 3
0
    file_name = file_list[i]
    src_path = constant.APK_SOURCES_DIR + file_name
    file_md5 = utils.get_file_md5(src_path)

    file_size = os.path.getsize(src_path)
    file_size /= float(1024 * 1024)
    file_size = round(file_size, 2)
    print('1. file_md5: ' + file_md5)
    print('2. file_name: ' + file_name)
    print('3. file_size: ' + str(file_size) + 'MB')
    dst_path = constant.OUTCOMES_DIR + file_md5 + '/'

    try:
        utils.make_dir(dst_path)
    except OSError:
        print('3. The Num.' + str(i) + ' apk workspace has existed')
    else:
        print('>> The Num.' + str(i) +
              ' workspace is been created in outcomes successfully')
        decompiler.decompile(src_path)
        utils.remove_file_or_dir('apktool_unzip', dst_path)
        utils.remove_file_or_dir('apk_enjarify.jar', dst_path)
        utils.remove_file_or_dir('procyon_decompile_java/', dst_path)
        print('7. Scanner: Begin static analysis...')

        src_path = constant.APK_SOURCES_DIR + file_name
        dst_path = constant.OUTCOMES_DIR + file_md5 + '/'
        # scanner.do_static_scan(src_path, dst_path)
        # scanner.do_dynamic_scan(src_path, dst_path)
        # print('------The Num.' + str(i) + ' apk scanning finishes------\n')
Exemplo n.º 4
0
    main_dbb, main_decrypt = stripped_roots.pop(), decrypt_procs.pop()

    vmis = matcher.identify_handlers(stripped_roots)
    
    assert(len(handlers_addrs) == len(stripped_roots) == len(decrypt_procs) == len(vmis))

    handlers = []
    for addr, dbb, decrypt, vmi in zip(handlers_addrs, stripped_roots, decrypt_procs, vmis):
        h = Handler(addr, dbb, decrypt, vmi)
        handlers.append(h)
    
    main = Handler(magic_lodsb, main_dbb, main_decrypt, VM_Instruction())
    main.set_main()

    #print main.dbb.dump()

    vm_codes = get_vm_codes(delta_call) 

    for i,vm_code in enumerate(vm_codes):
        print "vm:", i
        vmis = decompiler.decompile(main, handlers, vm_code)
        print "-"*5
        #x86.recover(vmis)
    
    delta =  time.time() - start_time
    print "done. total time:", delta

    if die:
        idc.Exit(0)
    
Exemplo n.º 5
0
    vmis = matcher.identify_handlers(stripped_roots)

    assert (len(handlers_addrs) == len(stripped_roots) == len(decrypt_procs) ==
            len(vmis))

    handlers = []
    for addr, dbb, decrypt, vmi in zip(handlers_addrs, stripped_roots,
                                       decrypt_procs, vmis):
        h = Handler(addr, dbb, decrypt, vmi)
        handlers.append(h)

    main = Handler(magic_lodsb, main_dbb, main_decrypt, VM_Instruction())
    main.set_main()

    #print main.dbb.dump()

    vm_codes = get_vm_codes(delta_call)

    for i, vm_code in enumerate(vm_codes):
        print "vm:", i
        vmis = decompiler.decompile(main, handlers, vm_code)
        print "-" * 5
        #x86.recover(vmis)

    delta = time.time() - start_time
    print "done. total time:", delta

    if die:
        idc.Exit(0)