def filter_intents_by_folder(rule_file,intent_filters_folder):
    files = get_all_in_dir(intent_filters_folder,"*")
    lines_to_remove = []
    filtered_file = rule_file+".filtered"
    for file in files:
        with open(file,'r') as r:
            lines_to_remove.extend([line[:-1] for line in r.readlines()])
    with open(filtered_file,'w') as f, open(rule_file,'r') as rulef:
        rule_lines = rulef.readlines()
        final_lines = [rule for rule in rule_lines if len([line for line in lines_to_remove if line in rule])==0]
        f.writelines(final_lines)
    return filtered_file
def filter_intents_by_folder(rule_file, intent_filters_folder):
    files = get_all_in_dir(intent_filters_folder, "*")
    lines_to_remove = []
    filtered_file = rule_file + ".filtered"
    for file in files:
        with open(file, 'r') as r:
            lines_to_remove.extend([line[:-1] for line in r.readlines()])
    with open(filtered_file, 'w') as f, open(rule_file, 'r') as rulef:
        rule_lines = rulef.readlines()
        final_lines = [
            rule for rule in rule_lines
            if len([line for line in lines_to_remove if line in rule]) == 0
        ]
        f.writelines(final_lines)
    return filtered_file
Esempio n. 3
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"