def test_static_intent_receives():
    a, d, dx = AnalyzeAPK("../tests/apks/Rec_TaskManager_StaticIntent.apk")
    x = get_static_receivers(apk=a)
    actions = ["stop", "gettasks", "start", "android.intent.action.MAIN"]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
def test_static_intent_receives():
    a,d, dx = AnalyzeAPK("../tests/apks/Rec_TaskManager_StaticIntent.apk")
    x = get_static_receivers(apk=a)
    actions = ["stop","gettasks","start","android.intent.action.MAIN"]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
def test_fwd_intent_receives():
    a, d, dx = AnalyzeAPK("../tests/apks/FWD_Gaming_Intent.apk")
    x = get_static_receivers(apk=a)
    x.extend(get_dynamic_receivers(a, d, dx))
    actions = ['android.intent.action.MAIN', "action.SEND.WHATEVER"]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
def test_fwd_intent_receives():
    a,d, dx = AnalyzeAPK("../tests/apks/FWD_Gaming_Intent.apk")
    x = get_static_receivers(apk=a)
    x.extend(get_dynamic_receivers(a,d,dx))
    actions = ['android.intent.action.MAIN',"action.SEND.WHATEVER"]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
def test_weatherapp_intent_receives():
    a,d, dx = AnalyzeAPK("../tests/apks/Send_WeatherApp_StaticIntent.apk")
    x = get_static_receivers(apk=a)
    x.extend(get_dynamic_receivers(a,d,dx))
    actions = ['android.intent.action.MAIN',"gettasks_response","readcontacts_response"]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
def test_weatherapp_intent_receives():
    a, d, dx = AnalyzeAPK("../tests/apks/Send_WeatherApp_StaticIntent.apk")
    x = get_static_receivers(apk=a)
    x.extend(get_dynamic_receivers(a, d, dx))
    actions = [
        'android.intent.action.MAIN', "gettasks_response",
        "readcontacts_response"
    ]
    for i in actions:
        assert i in [bi.get_action() for bi in x]
    for i in x:
        print i.get_action()
    assert len(x) == len(actions)
Esempio n. 7
0
def generate_facts(app_folder,result_prefix,rules,storage=None):
    files = get_all_in_dir(app_folder,"*")
    send_intent_actions_stats = Counter()
    recv_intent_actions_stats = Counter()
    len_files = 0
    is_apk = None
    for file in files:
        logging.info("Analyzing file %s",file)
        try:
            a,d, dx = AnalyzeAPK(file)
            is_apk = True
            # Create package to file relations
        except:
            is_apk = None
            print "Not valid APK file:  "+file
        try:
            if is_apk:
                with open(result_prefix+"_packages.txt", 'a') as f:
                    f.write("package('"+a.get_package()+"','"+ntpath.basename(file)+"').\n")
                # Permissions
                permissions = []
                permissions.extend([(str(a.get_package()), permission) for permission in a.get_permissions()])
                with open(result_prefix+"_uses_aux.txt", 'a') as f:
                    for permission in permissions:
                        f.write("uses('"+permission[0]+"','"+permission[1]+"').\n")
                # Intents
                logging.info("Looking for Intent Sends")
                sends = Set()
                sends.update([(str(a.get_package()),"i_"+intent.action) for intent in get_implicit_intents(a,d,dx)])
                send_intent_actions_stats.update([send[1] for send in sends])
                # Shared Prefs
                logging.info("Looking for Shared Prefs Sends")
                sends.update([(str(a.get_package()),"sp_"+shared.package+"_"+shared.preference_file) for shared in get_shared_preferences_writes(a,d,dx)])
                with open(result_prefix+"_trans_aux.txt", 'a') as f:
                    for send in sends:
                        f.write("trans('"+send[0]+"','"+escape_quotes(send[1])+"').\n")
                # Receivers
                logging.info("Looking for Dynamic Receivers")
                receives = Set()
                receives.update([(str(a.get_package()),"i_"+receiver.get_action()) for receiver in get_dynamic_receivers(a,d,dx)])
                logging.info("Looking for Static Receivers")
                receives.update([(str(a.get_package()),"i_"+receiver.get_action()) for receiver in get_static_receivers(a)])
                recv_intent_actions_stats.update([receive[1] for receive in receives])
                # Shared Prefs
                logging.info("Looking for Shared Prefs Receives")
                receives.update([(str(a.get_package()),"sp_"+shared.package+"_"+shared.preference_file) for shared in get_shared_preferences_reads(a,d,dx)])
                with open(result_prefix+"_recv_aux.txt", 'a') as f:
                     for receive in receives:
                        f.write("recv('"+receive[0]+"','"+escape_quotes(receive[1])+"').\n")
                len_files += 1
                utils.remove_duplicate_lines(result_prefix+"_uses_aux.txt",result_prefix+"_uses.txt",True)
                utils.remove_duplicate_lines(result_prefix+"_trans_aux.txt",result_prefix+"_trans.txt",True)
                utils.remove_duplicate_lines(result_prefix+"_recv_aux.txt",result_prefix+"_recv.txt",True)
        except:
            print "Error during analysis:  "+file
            traceback.print_exc()
    if rules != "":
        with open(os.path.splitext(rules)[0]+"_program.pl", 'w') as f:
            #write packages
            with open(result_prefix+"_packages.txt", 'r') as to_read:
                f.writelines(to_read.readlines())
            #write uses
            with open(result_prefix+"_uses.txt", 'r') as to_read:
                f.writelines(to_read.readlines())
            #write trans
            with open(result_prefix+"_trans.txt", 'r') as to_read:
                f.writelines(to_read.readlines())
                if storage:
                    f.write("trans(A,'external_storage'):- uses(A,'android.permission.WRITE_EXTERNAL_STORAGE').\n")
            #write receives
            with open(result_prefix+"_recv.txt", 'r') as to_read:
                f.writelines(to_read.readlines())
                if storage:
                    f.write("recv(A,'external_storage'):- uses(A,'android.permission.WRITE_EXTERNAL_STORAGE').\n")
                    f.write("recv(A,'external_storage'):- uses(A,'android.permission.READ_EXTERNAL_STORAGE').\n")
            with open(rules, 'r') as to_read:
                f.writelines(to_read.readlines())
    with open(result_prefix+"_intent_send_stats",'w') as send_stats_file:
        send_stats_file.write("**** Results for send intent analysis ****\n")
        send_stats_file.write("Files analized: ")
        send_stats_file.write(str(len_files))
        send_stats_file.write("\n")
        for send_stat in send_intent_actions_stats.most_common():
            freq = send_stat[1]/len_files
            send_stats_file.write(send_stat[0]+", "+"{0:.2f}".format(round(freq,2))+", "+str(send_stat[1])+"\n")
    with open(result_prefix+"_intent_recv_stats",'w') as recv_stats_file:
        recv_stats_file.write("**** Results for send intent analysis ****\n")
        recv_stats_file.write("Files analized: ")
        recv_stats_file.write(str(len_files))
        recv_stats_file.write("\n")
        for recv_stat in recv_intent_actions_stats.most_common():
            freq = recv_stat[1]/len_files
            recv_stats_file.write(recv_stat[0]+", "+"{0:.2f}".format(round(freq,2))+", "+str(recv_stat[1])+"\n")
    logging.info("Results saved in %s files",result_prefix)
    return os.path.splitext(rules)[0]+"_program.pl"
Esempio n. 8
0
def analyse_apk_file(apk_filename):
    logging.info("Analyzing file %s", apk_filename)

    try:
        a, d, dx = androguard.misc.AnalyzeAPK(apk_filename)
    except:
        logging.warning(apk_filename + " is not a valid APK. Skipping")
        return None

    try:
        # Perform analysis
        app_facts_dict = {}

        # Package
        package_name = a.get_package()
        app_facts_dict['package_name'] = package_name

        app_base_file_name = ntpath.basename(apk_filename)
        app_facts_dict['app_base_file_name'] = app_base_file_name

        # Permissions
        logging.info("Looking for permissions")
        permission_facts = set()
        for permission in a.get_permissions():
            permission_facts.add(permission)
        app_facts_dict['permissions'] = permission_facts

        # Intent sends
        logging.info("Looking for intent sends")
        send_intent_facts = set()
        for intent in get_implicit_intents(a, d, dx):
            send_intent_facts.add(escape_quotes("i_" + intent.action))
        app_facts_dict['send_intents'] = send_intent_facts

        # Shared Prefs sends
        logging.info("Looking for shared preferences sends")
        send_shared_prefs_facts = set()
        for shared_pref in get_shared_preferences_writes(a, d, dx):
            send_shared_prefs_facts.add("sp_" + shared_pref.package + "_" +
                                        shared_pref.preference_file)
        app_facts_dict['send_shared_prefs'] = send_shared_prefs_facts

        # Receivers
        logging.info("Looking for dynamic receivers")
        recv_intents_facts = set()
        for receiver in get_dynamic_receivers(a, d, dx):
            recv_intents_facts.add("i_" + receiver.get_action())
        for receiver in get_static_receivers(a):
            recv_intents_facts.add("i_" + receiver.get_action())
        app_facts_dict['recv_intents'] = recv_intents_facts

        # Shared Prefs Recv
        logging.info("Looking for shared preferences receives")
        recv_shared_prefs_facts = set()
        for shared_pref in get_shared_preferences_reads(a, d, dx):
            recv_shared_prefs_facts.add("sp_" + shared_pref.package + "_" +
                                        shared_pref.preference_file)
        app_facts_dict['recv_shared_prefs'] = recv_shared_prefs_facts

        return app_facts_dict
    except Exception as err:
        logging.critical(err)
        logging.critical("Error during analysis of " + apk_filename +
                         ". Skpping")
        return None