예제 #1
0
def generate_all_apks_method_graph(apk_directory_path, apk_type):
    try:
        file = Path(apk_directory_path)
        my_abs_path = file.resolve()
    except FileNotFoundError:
        # 不存在
        print("file or dir not exist")
        return
    else:
        typeApkFiles = os.listdir(apk_directory_path)
        for typeApkFile in typeApkFiles:
            print(typeApkFile)
            smali_loc = apk_directory_path + '\\' + typeApkFile
            apk_name = typeApkFile[:-4]
            print(apk_name)
            apk = Apk(apk_name)
            # 生成FCG图
            fcg_start_time = datetime.datetime.now()
            graph = generate_apk_method_graph(smali_loc, apk)
            print("generate apk fcg successfully")
            fcg_end_time = datetime.datetime.now()
            apk.fcg_graph_time = (fcg_end_time - fcg_start_time).seconds
            # test_api(apk)

            # 先创建一个存储apk信息的目录
            os.makedirs('..\data\{}\{}'.format(apk_type, apk_name))
            pattern_start_time = datetime.datetime.now()

            # 寻找三点子图的模式
            find_three_point_pattern(apk, graph, apk_type)
            # 寻找四点子图的模式
            find_four_point_pattern(apk, graph, apk_type)
            # 寻找五点子图的模式
            find_five_point_pattern(apk, graph, apk_type)
            pattern_end_time = datetime.datetime.now()
            apk.pattern_find_time = (pattern_end_time -
                                     pattern_start_time).seconds

            print("find apk pattern successfully")

            # 统计信息,输出到文件
            message_path = '..\data\{}\{}\message.txt'.format(
                apk_type, apk_name)
            write_apk_message(apk, message_path)
            # 展示一下生成的子图
            print_opcode_graph(apk)
예제 #2
0
def main():
    import config_reader as config
    from apk import Apk
    from adb_settings import AdbSettings
    import emulator_manager
    from adb_logcat import Logcat, TestType
    # print("resetting emulator")
    import os
    import api_commands

    import os
    dir = os.path.dirname(__file__)
    activity = os.path.join(dir, 'test/activity')
    activity_list = os.path.join(dir, 'test/activity_list')
    activities = []
    apk = Apk(config.APK_FULL_PATH)
    emulator = emulator_manager.get_adb_instance_from_emulators(
        config.EMULATOR_NAME)
    # file = open("test/activity", "w")
    file = open(activity, 'w')
    file.write(api_commands.adb_get_activity_list(emulator, apk))
    file.close()

    # file = open('test/activity', 'r')
    file = open(activity, 'r')
    # file2 = open('test/activity_list', 'w')
    file2 = open(activity_list, 'w')

    for l in file.readlines():
        if 'A: android:name' in l and 'Activity' in l:
            arr = l.split('"')
            activities.append(arr[1])
            file2.write(arr[1] + "\n")
            print(arr[1])
    file.close()
    file2.close()
    os.remove('test/activity')

    # log = Logcat(emulator, apk, TestType.MobileMonkey)
    # log.start_logcat()
    AdbMonkey(emulator, apk, config.SEED, config.DURATION * 10).start_monkey()
예제 #3
0
def find_sen_api(apk_directory_path, api_dict):
    try:
        file = Path(apk_directory_path)
        my_abs_path = file.resolve()
    except FileNotFoundError:
        # 不存在
        print("file or dir not exist")
        return
    else:
        typeApkFiles = os.listdir(apk_directory_path)
        for typeApkFile in typeApkFiles:
            print(typeApkFile)
            smali_loc = apk_directory_path + '\\' + typeApkFile
            apk_name = typeApkFile[:-4]
            print(apk_name)
            apk = Apk(apk_name)
            # 生成FCG图
            start_time = datetime.datetime.now()
            api_set = find_apk_smali(smali_loc, apk, api_dict)
            for sen_api in api_set:
                api_dict[sen_api] = api_dict[sen_api] + 1
            end_time = datetime.datetime.now()
            print('{}:time:{}'.format(typeApkFile,
                                      (start_time - end_time).seconds))
예제 #4
0
        # log_thread.join()
    # thread_monkey.start()
    # thread_fuzz_rotation.start()
    # thread_fuzz_delay.start()
    # thread_fuzz_profile.start()
    # thread_fuzz_speed.start()
    # thread_fuzz_airplane.start()
    # thread_test.start()
    # thread_test.join()
    # thread_fuzz_delay.join()
    # thread_fuzz_profile.join()
    # thread_fuzz_speed.join()
    # thread_fuzz_rotation.join()
    # thread_fuzz_airplane.join()
    # thread_monkey.join()
    # telnet_connector.kill_avd()
    api_commands.adb_stop_activity_of_apk(emulator, apk)
    log.stop_logcat()
    api_commands.adb_uninstall_apk(emulator, apk)
    util.debug_print(
        'Finished testing and uninstalling app at {}'.format(time.ctime()), flag=TIME_PRINT_FLAG)
    print(Analyzer(log.file_address))
    if wipe_after_finish:
        print("successfully completed testing app. Closing emulator")
        telnet_connector.kill_avd()
        emulator_manager.emulator_wipe_data(emulator)


if __name__ == '__main__':
    run(Apk(config.APK_FULL_PATH), config.EMULATOR_NAME, config.EMULATOR_PORT)
예제 #5
0
def start_test():

    apk = Apk(config.APK_FULL_PATH)

    emulator = emulator_manager.get_adb_instance_from_emulators(
        config.EMULATOR_NAME)
    adb_settings = AdbSettings("emulator-" + str(emulator.port))
    activities = []

    log = Logcat(emulator, apk, TestType.MobileMonkey)

    if config.GUIDED_APPROACH == 3:

        monkey.run_contexts_only(emulator, config.EMULATOR_NAME,
                                 config.EMULATOR_PORT, config.SEED, log)
        return

    log.start_logcat()
    activity = os.path.join(dir, 'test/activity')
    activity_list = os.path.join(dir, 'test/activity_list')

    if config.GUIDED_APPROACH == 2:
        # file = open("test/activity", "w")
        file = open(activity, "w")
        file.write(api_commands.adb_get_activity_list(emulator, apk))
        file.close()

        # file = open('test/activity', 'r')
        file = open(activity, 'r')

        # file2 = open('test/activity_list', 'w')
        file2 = open(activity_list, 'w')

        for l in file.readlines():
            if 'A: android:name' in l and 'Activity' in l:
                arr = l.split('"')
                activities.append(arr[1])
                file2.write(arr[1] + "\n")
                print(arr[1])
        file.close()
        file2.close()
        # os.remove('test/activity')
        os.remove(activity)

        print(len(activities))
        seed = config.SEED
        for activity in activities:

            try:
                api_commands.adb_start_activity(emulator, apk, activity)
            except Exception:
                print(Exception)

            threads = []

            threads.append(
                Thread(target=monkey.run,
                       args=(emulator, apk, config.EMULATOR_NAME,
                             config.EMULATOR_PORT, seed, log)))

            # monkey.run(emulator, apk, config.EMULATOR_NAME,
            # config.EMULATOR_PORT, seed, log)

            # threads.append(Thread(target=test_ui, args=(
            #     activity, emulator, adb_settings, display_height)))
            from adb_monkey import AdbMonkey
            ad_monkey = AdbMonkey(emulator, apk, seed, config.DURATION)
            # ad_monkey.start_monkey()
            threads.append(Thread(target=ad_monkey.start_monkey))

            [thread.start() for thread in threads]

            [thread.join() for thread in threads]

            seed = seed + 1

        log.stop_logcat()
        eventlog.close()
        return

    if config.GUIDED_APPROACH == 1:
        # file = open('test/activity_list', 'r')
        file = open(activity_list, 'r')
        for l in file.readlines():
            activities.append(l.strip())
            print(l.strip())
        file.close()

    else:
        # file = open("test/activity", "w")
        file = open(activity, "w")
        file.write(api_commands.adb_get_activity_list(emulator, apk))
        file.close()

        # file = open('test/activity', 'r')
        # file2 = open('test/activity_list', 'w')
        file = open(activity, 'r')
        file2 = open(activity_list, 'w')
        for l in file.readlines():
            if 'A: android:name' in l and 'Activity' in l:
                arr = l.split('"')
                activities.append(arr[1])
                file2.write(arr[1] + "\n")
                print(arr[1])
        file.close()
        file2.close()
        # os.remove('test/activity')
        os.remove(activity)

    print(len(activities))

    display_properties = api_commands.adb_display_properties().decode()
    if 'DisplayDeviceInfo' in display_properties:
        arr = display_properties.split('height=')[1]
        display_height = arr.split(',')[0]

    seed = config.SEED

    for activity in activities:

        try:
            api_commands.adb_start_activity(emulator, apk, activity)
        except Exception:
            print(Exception)

        threads = []

        threads.append(
            Thread(target=monkey.run,
                   args=(emulator, apk, config.EMULATOR_NAME,
                         config.EMULATOR_PORT, seed, log)))

        # monkey.run(emulator, apk, config.EMULATOR_NAME,
        # config.EMULATOR_PORT, seed, log)

        threads.append(
            Thread(target=test_ui,
                   args=(activity, emulator, adb_settings, display_height)))

        [thread.start() for thread in threads]

        [thread.join() for thread in threads]

        seed = seed + 1

    log.stop_logcat()
    eventlog.close()
예제 #6
0
 def __init__(self, apk_id, apk, package_name, apk_conf_files,
              apk_conf_gzip, apk_launch_activity, av_start_scan_activity):
     Apk.__init__(self, apk_id, apk, package_name, apk_conf_files,
                  apk_conf_gzip, apk_launch_activity)
     self.av_start_scan_activity = av_start_scan_activity
예제 #7
0
import os
import api_commands
import emulator_manager
import config_reader as config
from apk import Apk
import api_commands
from adb_logcat import Logcat, TestType

activities = []
apk = Apk(config.APK_FULL_PATH)
emulator = emulator_manager.get_adb_instance_from_emulators(
    config.EMULATOR_NAME)
# file = open("test/activity", "w")
# file.write(api_commands.adb_get_activity_list(emulator, apk))
# file.close()

# file = open('test/activity', 'r')
# file2 = open('test/activity_list', 'w')

# for l in file.readlines():
#     if 'A: android:name' in l and 'Activity' in l:
#         arr = l.split('"')
#         activities.append(arr[1])
#         file2.write(arr[1] + "\n")
#         print(arr[1])
# file.close()
# file2.close()
# os.remove('test/activity')
from telnet_connector import TelnetAdb
import adb_monkey
from adb_logcat import Logcat
예제 #8
0
def get_apk(apk_id):
    return Apk(apk_id, apksConf[apk_id]['apk_path'],
               apksConf[apk_id]['package_name'], apksConf[apk_id]['conf_file'],
               apksConf[apk_id]['conf_gzip'],
               apksConf[apk_id]['launch_activity'])
예제 #9
0
 def __init__(self, apk_id, apk, package_name, apk_conf_files, apk_conf_gzip, apk_launch_activity, av_start_scan_activity):
     Apk.__init__(self, apk_id, apk, package_name, apk_conf_files, apk_conf_gzip, apk_launch_activity)
     self.av_start_scan_activity = av_start_scan_activity
예제 #10
0
# coding=utf-8

import telebot
from telebot import types
import os
from flask import Flask, request

from apk import Apk
import messages

bot = telebot.TeleBot(os.environ['telegram_token'])

server = Flask(__name__)

apk = Apk()

@bot.message_handler(commands=['start'])
def start(message):
    # bot.send_message(message.chat.id, messages.hello, reply_markup=types.ReplyKeyboardHide())
    bot.send_message(message.chat.id, messages.hello)

@bot.message_handler(commands=['help'])
def help(message):
    bot.send_message(message.chat.id, messages.help)
    # bot.send_message(message.chat.id, messages.help, reply_markup=types.ReplyKeyboardHide())

@bot.message_handler(commands=['about'])
def about(message):
    bot.send_message(message.chat.id, messages.about)

@bot.message_handler(content_types=['text'])