Exemplo n.º 1
0
 def get_app_path(self):
     """
     get app file path of current app
     :return:
     """
     if self.app_path is not None:
         return self.app_path
     if self.package_name is None:
         self.logger.warning("Trying to get app path without package name")
         return None
     # if we only have package name, use `adb pull` to get the package from device
     try:
         from droidbot import DroidBot
         app_path_in_device = DroidBot.get_instance().device.get_adb().getPackagePath(self.package_name)
         out_dir = DroidBot.get_instance().output_dir
         app_path = os.path.join(out_dir, 'temp', "%s.apk" % self.package_name)
         subprocess.check_call(["adb", "pull", app_path_in_device, app_path])
         self.app_path = app_path
         return self.app_path
     except Exception:
         return None
Exemplo n.º 2
0
def main():
    """
    the main function
    it starts a droidbot according to the arguments given in cmd line
    """
    opts = parse_args()
    import os
    if not os.path.exists(opts.apk_path):
        print "apk not exist"
        return

    droidbot = DroidBot(app_path=opts.apk_path,
                        device_serial=opts.device_serial,
                        output_dir=opts.output_dir,
                        env_policy=opts.env_policy,
                        event_policy=opts.event_policy,
                        with_droidbox=opts.with_droidbox,
                        event_interval=opts.event_interval,
                        event_duration=opts.event_duration,
                        event_count=opts.event_count,
                        quiet=opts.quiet)
    droidbot.start()
    return
Exemplo n.º 3
0
def main():
    """
    the main function
    it starts a droidbot according to the arguments given in cmd line
    """
    opts = parse_args()
    import os
    if not os.path.exists(opts.apk_path):
        print "APK does not exist."
        return
    if not opts.output_dir and opts.cv_mode:
        print "To run in CV mode, you need to specify an output dir (using -o option)."

    if opts.distributed:
        if opts.distributed == "master":
            start_mode = "master"
        else:
            start_mode = "worker"
    else:
        start_mode = "normal"

    if start_mode == "master":
        droidmaster = DroidMaster(app_path=opts.apk_path,
                                  device_serial=opts.device_serial,
                                  is_emulator=opts.is_emulator,
                                  output_dir=opts.output_dir,
                                  # env_policy=opts.env_policy,
                                  env_policy=env_manager.POLICY_NONE,
                                  policy_name=opts.input_policy,
                                  random_input=opts.random_input,
                                  script_path=opts.script_path,
                                  event_interval=opts.interval,
                                  timeout=opts.timeout,
                                  event_count=opts.count,
                                  cv_mode=opts.cv_mode,
                                  debug_mode=opts.debug_mode,
                                  keep_app=opts.keep_app,
                                  keep_env=opts.keep_env,
                                  profiling_method=opts.profiling_method,
                                  grant_perm=opts.grant_perm,
                                  enable_accessibility_hard=opts.enable_accessibility_hard,
                                  qemu_hda=opts.qemu_hda,
                                  qemu_no_graphic=opts.qemu_no_graphic)
        droidmaster.start()
    else:
        droidbot = DroidBot(app_path=opts.apk_path,
                            device_serial=opts.device_serial,
                            is_emulator=opts.is_emulator,
                            output_dir=opts.output_dir,
                            # env_policy=opts.env_policy,
                            env_policy=env_manager.POLICY_NONE,
                            policy_name=opts.input_policy,
                            random_input=opts.random_input,
                            script_path=opts.script_path,
                            event_interval=opts.interval,
                            timeout=opts.timeout,
                            event_count=opts.count,
                            cv_mode=opts.cv_mode,
                            debug_mode=opts.debug_mode,
                            keep_app=opts.keep_app,
                            keep_env=opts.keep_env,
                            profiling_method=opts.profiling_method,
                            grant_perm=opts.grant_perm,
                            enable_accessibility_hard=opts.enable_accessibility_hard,
                            master=opts.master)
        droidbot.start()
    return
def main():
    """
    the main function
    it starts a droidbot according to the arguments given in cmd line
    """

    opts = parse_args()
    import os
    if not os.path.exists(opts.apk_path):
        print("APK does not exist.")
        return
    if not opts.output_dir and opts.cv_mode:
        print("To run in CV mode, you need to specify an output dir (using -o option).")

    if opts.distributed:
        if opts.distributed == "master":
            start_mode = "master"
        else:
            start_mode = "worker"
    else:
        start_mode = "normal"

    if start_mode == "master":
        droidmaster = DroidMaster(
            app_path=opts.apk_path,
            is_emulator=opts.is_emulator,
            output_dir=opts.output_dir,
            # env_policy=opts.env_policy,
            env_policy=env_manager.POLICY_NONE,
            policy_name=opts.input_policy,
            random_input=opts.random_input,
            script_path=opts.script_path,
            event_interval=opts.interval,
            timeout=opts.timeout,
            event_count=opts.count,
            cv_mode=opts.cv_mode,
            debug_mode=opts.debug_mode,
            keep_app=opts.keep_app,
            keep_env=opts.keep_env,
            profiling_method=opts.profiling_method,
            grant_perm=opts.grant_perm,
            enable_accessibility_hard=opts.enable_accessibility_hard,
            qemu_hda=opts.qemu_hda,
            qemu_no_graphic=opts.qemu_no_graphic,
            humanoid=opts.humanoid,
            ignore_ad=opts.ignore_ad,
            replay_output=opts.replay_output)
        droidmaster.start()
    else:
        droidbot = DroidBot(
            app_path=opts.apk_path,
            device_serial=opts.device_serial,
            is_emulator=opts.is_emulator,
            output_dir=opts.output_dir,
            # env_policy=opts.env_policy,
            env_policy=env_manager.POLICY_NONE,
            policy_name=opts.input_policy,
            random_input=opts.random_input,
            script_path=opts.script_path,
            event_interval=opts.interval,
            timeout=opts.timeout,
            event_count=opts.count,
            cv_mode=opts.cv_mode,
            debug_mode=opts.debug_mode,
            keep_app=opts.keep_app,
            keep_env=opts.keep_env,
            profiling_method=opts.profiling_method,
            grant_perm=opts.grant_perm,
            enable_accessibility_hard=opts.enable_accessibility_hard,
            master=opts.master,
            humanoid=opts.humanoid,
            ignore_ad=opts.ignore_ad,
            replay_output=opts.replay_output)

        droidbot.start()

    env = DummyVecEnv([lambda: droidbot_env.DroidBotEnv(droidbot)])
    start_time = time.time()
    env.reset()

    def events_so_state(env):
        events = env.envs[0].possible_events
        state_now = env.envs[0].device.get_current_state()
        event_ids = []
        probs = []

        for i, event in enumerate(events):
            event_str = str(type(event)) + '_' + event.get_event_str(state_now)
            if event_str in event_ids:
                1/0
            if event:
                event_ids.append(event_str)
                probs.append(env.envs[0].events_probs[i])
        state = state_now.state_str
        probs = np.array(probs)
        return state, probs, event_ids

    state_function = {}
    num_iterations = 100000
    EPSILON = 0.1
    Q_TABLE = []
    transitions_matrix = None
    number_of_trans = []
    event_to_id = []
    max_number_of_actions = 50

    def check_state(state_id):
        nonlocal Q_TABLE
        nonlocal transitions_matrix
        nonlocal number_of_trans
        nonlocal event_to_id
        nonlocal state_function
        #print(state_id)
        if state_function.get(state_id) is None:
            if Q_TABLE == []:
                Q_TABLE = np.zeros((1, max_number_of_actions))
                transitions_matrix = np.zeros((1, max_number_of_actions, 1))
            else:
                Q_TABLE = np.concatenate([Q_TABLE, np.zeros((1, max_number_of_actions))], axis=0)
                transition_matrix_new = np.zeros((Q_TABLE.shape[0], max_number_of_actions, Q_TABLE.shape[0]))
                transition_matrix_new[:-1, :, :-1] = transitions_matrix
                transitions_matrix = transition_matrix_new
            event_to_id.append({})
            state_function[state_id] = Q_TABLE.shape[0] - 1
            Q_TABLE[-1][-1] = 1.0
            number_of_trans.append(np.zeros(max_number_of_actions))
        #print(state_function)
#    if True:
#        Q_TABLE = np.load('q_function.npy')
#        transitions_matrix = np.load('transition_function.npy')
#        with open('states.json', 'r') as data_file:
#            state_function = json.load(data_file)

    state_pre, probs, event_ids = events_so_state(env)
    check_state(state_pre)
    state = state_function[state_pre]

    def make_decision(state_i, events):
        nonlocal Q_TABLE, event_to_id
        id_to_action = np.zeros((max_number_of_actions), dtype=np.int32) + 1000
        q_values = np.zeros(max_number_of_actions)
        probs_now = np.zeros(max_number_of_actions)

        for i, event in enumerate(events):
            if i == len(events) - 1:
                q_values[-1] = Q_TABLE[state_i][-1]
                id_to_action[-1] = min(len(events), max_number_of_actions) - 1
                continue
            if event_to_id[state_i].get(event) is None:
                if len(event_to_id[state_i]) >= max_number_of_actions - 1:
                    continue
                event_to_id[state_i][event] = int(len(list(event_to_id[state_i].keys())))
                Q_TABLE[state_i][event_to_id[state_i][event]] = 1.0
            q_values[event_to_id[state_i][event]] = Q_TABLE[state_i][event_to_id[state_i][event]]

            id_to_action[event_to_id[state_i][event]] = int(i)


        if np.random.rand() < EPSILON:
            action = max_number_of_actions - 1
            make_action = id_to_action[action]
        else:
            max_q = np.max(q_values)
            actions_argmax = np.arange(max_number_of_actions)[q_values >= max_q - 0.0001]
            probs_unnormed = 1/(np.arange(actions_argmax.shape[0]) + 1.)
            probs_unnormed /= np.sum(probs_unnormed)
            action = np.random.choice(actions_argmax)
            make_action = id_to_action[action]
        return action, make_action

    for i_step in np.arange(num_iterations):
        action, make_action = make_decision(state, event_ids)
        print(state, action, make_action)
        env.step([make_action])
        new_state_pre, probs, event_ids = events_so_state(env)

        check_state(new_state_pre)
        new_state = state_function[new_state_pre]

        number_of_trans[state][action] += 1
        transitions_matrix[state, action] *= (number_of_trans[state][action] - 1)
        transitions_matrix[state, action, new_state] += 1
        transitions_matrix[state, action] /= number_of_trans[state][action]
        for _ in np.arange(10):
            for i in np.arange(max_number_of_actions):
                transitions = transitions_matrix[:, i, :]
                q_target = np.array([[np.max(Q_TABLE[i])] for i in np.arange(Q_TABLE.shape[0])])
                new_q_values = np.matmul(transitions, q_target) * 0.99
                good_states = np.sum(transitions, axis=1) > 0.5
                if True in good_states:
                    Q_TABLE[good_states, i] = new_q_values[good_states, 0]
                else:
                    continue
        for i in np.arange(Q_TABLE.shape[0]):
            print(Q_TABLE[i])
        if i_step%10==0:
            np.save('q_function', Q_TABLE)
            np.save('transition_function', transitions_matrix)
            with open('states.json', 'w') as f:
                json.dump(state_function, f)
        state = new_state
    1/0
    droidbot.stop()