예제 #1
0
def analyze_apk(eandro_apk,
                scripts,
                min_script_needs,
                propagate_error=False,
                reset_scripts=True):
    ''' Analyze the `eandro_apk` with the given `scripts` assuming each `AndroScript`
    neads at least `min_script_needs`.

    Be sure that you reseted the `scripts`!

    Parameters
    ----------
    eandro_apk : EAndroApk
        The apk.
    scripts : iterable<AndroScript>
        The scripts to use for the analysis.
    min_script_needs : tuple<bool>
        See :py:meth:ScriptUtil.get_maximal_script_options`
    propagate_error : bool, optional (default is False)
        If true propagate errors.
    reset_scripts : bool, optional (default is True)
        If given, reset the `AndroScript` before analyzing.

    Returns
    -------
    list<FastApk, list<AndroScript>>
        Uses `FastApk` to only store the meta information, not the apk data!
    None
        If error happened.
    '''
    from androlyze.analyze.exception import AndroScriptError

    try:
        # reset scripts
        if reset_scripts:
            for s in scripts:
                s.reset()

        if eandro_apk is not None:
            fastapk = None
            # analyze classes.dex with script requirements and get time
            args = [eandro_apk.get_dex()] + list(min_script_needs)

            time_s, analysis_objs = Util.timeit(analyze_dex, *args, raw=True)

            script_results = []
            for s in scripts:
                try:
                    result_obj = s.analyze(eandro_apk, *analysis_objs)

                    # we only need the meta infos of the apk
                    if eandro_apk is not None:
                        fastapk = FastApk.load_from_eandroapk(eandro_apk)

                    # set androguard analysis time if script wants stats
                    s.add_apk_androguard_analyze_time(time_s)

                    # link to apk
                    if isinstance(result_obj, ResultObject):
                        result_obj.set_apk(fastapk)

                    script_results.append(s)
                except Exception as e:
                    if propagate_error:
                        raise
                    else:
                        log.exception(AndroScriptError(s, e))

            if fastapk is not None:
                # use fastapk to only store the meta information, not the apk data!
                return [fastapk, script_results]

    # interrupt analysis if analysis objects could not be created!
    except DexError as e:
        log.exception(e)
예제 #2
0
        disable_std_loggers()
    else:
        log_set_level(LOG_LEVEL)
        clilog_set_level(logging.INFO)

    # write to file with specified log level
    redirect_to_file_handler(logger_filename, LOG_LEVEL)


if __name__ == "__main__":
    if PROFILE:
        import cProfile
        import pstats
        profile_filename = 'androlyze.Main_profile.txt'
        cProfile.run('main()', profile_filename)
        statsfile = open("profile_stats.txt", "wb")
        p = pstats.Stats(profile_filename, stream=statsfile)
        stats = p.strip_dirs().sort_stats('cumulative')
        stats.print_stats()
        statsfile.close()
        sys.exit(0)
    time, ret_code = Util.timeit(main)
    log.warn('Took %s (h/m/s)\n' % datetime.timedelta(seconds=round(time)))

    if DEBUG:
        with open("time.txt", "a") as f:
            f.write('%s : %s\n' % (datetime.datetime.now(),
                                   datetime.timedelta(seconds=round(time))))

    sys.exit()
예제 #3
0
def analyze_apk(eandro_apk, scripts, min_script_needs, propagate_error = False, reset_scripts = True):
    ''' Analyze the `eandro_apk` with the given `scripts` assuming each `AndroScript`
    neads at least `min_script_needs`.

    Be sure that you reseted the `scripts`!

    Parameters
    ----------
    eandro_apk : EAndroApk
        The apk.
    scripts : iterable<AndroScript>
        The scripts to use for the analysis.
    min_script_needs : tuple<bool>
        See :py:meth:ScriptUtil.get_maximal_script_options`
    propagate_error : bool, optional (default is False)
        If true propagate errors.
    reset_scripts : bool, optional (default is True)
        If given, reset the `AndroScript` before analyzing.

    Returns
    -------
    list<FastApk, list<AndroScript>>
        Uses `FastApk` to only store the meta information, not the apk data!
    None
        If error happened.
    '''
    from androlyze.analyze.exception import AndroScriptError

    try:
        # reset scripts
        if reset_scripts:
            for s in scripts:
                s.reset()

        if eandro_apk is not None:
            fastapk = None
            # analyze classes.dex with script requirements and get time
            args = [eandro_apk.get_dex()] + list(min_script_needs)

            time_s, analysis_objs = Util.timeit(analyze_dex, *args, raw = True)

            script_results = []
            for s in scripts:
                try:
                    result_obj = s.analyze(eandro_apk, *analysis_objs)

                    # we only need the meta infos of the apk
                    if eandro_apk is not None:
                        fastapk = FastApk.load_from_eandroapk(eandro_apk)

                    # set androguard analysis time if script wants stats
                    s.add_apk_androguard_analyze_time(time_s)

                    # link to apk
                    if isinstance(result_obj, ResultObject):
                        result_obj.set_apk(fastapk)

                    script_results.append(s)
                except Exception as e:
                    if propagate_error:
                        raise
                    else:
                        log.exception(AndroScriptError(s, e))

            if fastapk is not None:
                # use fastapk to only store the meta information, not the apk data!
                return [fastapk, script_results]

    # interrupt analysis if analysis objects could not be created!
    except DexError as e:
        log.exception(e)
예제 #4
0
    if quiet:
        disable_std_loggers()
    else:
        log_set_level(LOG_LEVEL)
        clilog_set_level(logging.INFO)

    # write to file with specified log level
    redirect_to_file_handler(logger_filename, LOG_LEVEL)

if __name__ == "__main__":
    if PROFILE:
        import cProfile
        import pstats
        profile_filename = 'androlyze.Main_profile.txt'
        cProfile.run('main()', profile_filename)
        statsfile = open("profile_stats.txt", "wb")
        p = pstats.Stats(profile_filename, stream=statsfile)
        stats = p.strip_dirs().sort_stats('cumulative')
        stats.print_stats()
        statsfile.close()
        sys.exit(0)
    time, ret_code = Util.timeit(main)
    log.warn('Took %s (h/m/s)\n' % datetime.timedelta(seconds=round(time)))

    if DEBUG:
        with open("time.txt", "a") as f:
            f.write('%s : %s\n' % (datetime.datetime.now(), datetime.timedelta(seconds=round(time))))

    sys.exit()