(source_directory, vt_api_key, vt_analysis_output_folder=None, output_samples_folder=None,
                       with_color=True)
Analyses a set of APK files with the VirusTotal service

    Parameters
    ----------
    :param source_directory: Folder containing apk files
    :param vt_analysis_output_folder: Folder where VirusTotal reports are saved
    :param output_samples_folder:  Folder where apk files are saved after analysed with VirusTotal
    :return:
Analyses a set of APK files with the VirusTotal service
Analyses a set of APK files with the VirusTotal service
def analyse_virustotal(source_directory, vt_api_key, vt_analysis_output_folder=None, output_samples_folder=None,
                       with_color=True):
    """
    Analyses a set of APK files with the VirusTotal service

    Parameters
    ----------
    :param source_directory: Folder containing apk files
    :param vt_analysis_output_folder: Folder where VirusTotal reports are saved
    :param output_samples_folder:  Folder where apk files are saved after analysed with VirusTotal
    :return:
    """
    if len(vt_api_key) != 64:
        print 'ERROR! - invalid vt_key file. Please, provide a virustotal key!'
        sys.exit(0)

    global VT_KEY
    VT_KEY = vt_api_key

    if vt_analysis_output_folder is None:
        vt_analysis_output_folder = join_dir(source_directory, VT_ANALYSIS_DIRECTORY_NAME)

    reports_not_received = 0

    # TODO It is necessary to control when the directory could not be created (for instance if the folder is going to be
    # TODO created in a non existing directory

    if not os.path.exists(vt_analysis_output_folder):
        os.makedirs(vt_analysis_output_folder)

    if output_samples_folder is not None:
        if not os.path.exists(output_samples_folder):
            os.makedirs(output_samples_folder)

    apks_found = [f for f in listdir(source_directory) if isfile(join(source_directory, f))
                  and f.endswith(".apk")]

    count_positives = 0

    for apk in tqdm(apks_found):

        if isfile(join(source_directory, apk.replace(".apk", ".json"))):
            print_message("APK WITH JSON. CONTINUE...", with_color, 'green')
            continue
        apk_path = source_directory + apk
        hash_sha = sha256(apk_path)
        report = ""
        while report == "":
            report = get_report_hash(hash_sha)
            if report == "":
                print_message("No report received. Waiting...", with_color, 'red')
                time.sleep(1)

        response_dict = simplejson.loads(report)

        response_code = response_dict.get("response_code")
        if response_code == 1:  # Report generated
            positives = response_dict.get("positives")
            file_json = open(apk_path.replace(".apk", "") + ".json", "w")
            file_json.write(report)
            if positives > 0:
                count_positives += 1

            shutil.move(apk_path.replace(".apk", "") + ".json",
                        join_dir(vt_analysis_output_folder, apk.replace(".apk", "") + ".json"))
            if output_samples_folder is not None:
                shutil.move(join_dir(source_directory, apk), join_dir(output_samples_folder, apk))

        if response_code == 0:
            reports_not_received += 1

            params = {'apikey': VT_KEY}
            files = {'file': ("apk", open(apk_path, 'rb'))}
            print "Uploading APK: " + apk
            print "File not analysed yet. Uploading file..."
            try:
                response = requests.post('https://www.virustotal.com/vtapi/v2/file/scan', files=files, params=params)

            except requests.exceptions.ConnectionError:
                print_message("Connection error", with_color, 'red')
                continue
            print str(response)
            try:
                response.json()
            except JSONDecodeError:
                print_message("JSONDecodeError", with_color, 'red')
                continue
            print_message("SENT TO VIRUS-TOTAL", with_color, 'blue')

    if reports_not_received > 0:
        print "WARNING! " + str(reports_not_received) + " apks does not have yet a VT analysis. Please" \
                                                         ", execute again this script after a while"
    else:
        print_message("SUCCESS!!", with_color, 'green')
        print " All reports have been saved in the VT_ANALYSIS folder. APKS are in SAMPLES folder."
def analyse_virustotal ( source_directory , vt_api_key , vt_analysis_output_folder = None , output_samples_folder = None , with_color = True ) : if len ( vt_api_key ) != 64 : print 'ERROR! - invalid vt_key file. Please, provide a virustotal key!' sys . exit ( 0 ) global VT_KEY VT_KEY = vt_api_key if vt_analysis_output_folder is None : vt_analysis_output_folder = join_dir ( source_directory , VT_ANALYSIS_DIRECTORY_NAME ) reports_not_received = 0 # TODO It is necessary to control when the directory could not be created (for instance if the folder is going to be # TODO created in a non existing directory if not os . path . exists ( vt_analysis_output_folder ) : os . makedirs ( vt_analysis_output_folder ) if output_samples_folder is not None : if not os . path . exists ( output_samples_folder ) : os . makedirs ( output_samples_folder ) apks_found = [ f for f in listdir ( source_directory ) if isfile ( join ( source_directory , f ) ) and f . endswith ( ".apk" ) ] count_positives = 0 for apk in tqdm ( apks_found ) : if isfile ( join ( source_directory , apk . replace ( ".apk" , ".json" ) ) ) : print_message ( "APK WITH JSON. CONTINUE..." , with_color , 'green' ) continue apk_path = source_directory + apk hash_sha = sha256 ( apk_path ) report = "" while report == "" : report = get_report_hash ( hash_sha ) if report == "" : print_message ( "No report received. Waiting..." , with_color , 'red' ) time . sleep ( 1 ) response_dict = simplejson . loads ( report ) response_code = response_dict . get ( "response_code" ) if response_code == 1 : # Report generated positives = response_dict . get ( "positives" ) file_json = open ( apk_path . replace ( ".apk" , "" ) + ".json" , "w" ) file_json . write ( report ) if positives > 0 : count_positives += 1 shutil . move ( apk_path . replace ( ".apk" , "" ) + ".json" , join_dir ( vt_analysis_output_folder , apk . replace ( ".apk" , "" ) + ".json" ) ) if output_samples_folder is not None : shutil . move ( join_dir ( source_directory , apk ) , join_dir ( output_samples_folder , apk ) ) if response_code == 0 : reports_not_received += 1 params = { 'apikey' : VT_KEY } files = { 'file' : ( "apk" , open ( apk_path , 'rb' ) ) } print "Uploading APK: " + apk print "File not analysed yet. Uploading file..." try : response = requests . post ( 'https://www.virustotal.com/vtapi/v2/file/scan' , files = files , params = params ) except requests . exceptions . ConnectionError : print_message ( "Connection error" , with_color , 'red' ) continue print str ( response ) try : response . json ( ) except JSONDecodeError : print_message ( "JSONDecodeError" , with_color , 'red' ) continue print_message ( "SENT TO VIRUS-TOTAL" , with_color , 'blue' ) if reports_not_received > 0 : print "WARNING! " + str ( reports_not_received ) + " apks does not have yet a VT analysis. Please" ", execute again this script after a while" else : print_message ( "SUCCESS!!" , with_color , 'green' ) print " All reports have been saved in the VT_ANALYSIS folder. APKS are in SAMPLES folder."