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, env_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, debug_mode=opts.debug_mode, keep_app=opts.keep_app, keep_env=opts.keep_env, with_droidbox=opts.with_droidbox, use_hierarchy_viewer=opts.use_hierarchy_viewer, profiling_method=opts.profiling_method, grant_perm=opts.grant_perm) 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 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, use_hierarchy_viewer=opts.use_hierarchy_viewer, enable_method_profiling=opts.enable_method_profiling) 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)." 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) 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 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
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="/Users/maomao/Desktop/p/AppOffer_2.4.9_Demo.apk", device_serial="08fd8e5ba2439881", output_dir="/Users/maomao/Desktop/AppOffer_2.4.9_Demo", env_policy="static", event_policy="utg_dynamic", with_droidbox=None, event_interval=None, event_duration=None, event_count=None, quiet=False, use_hierarchy_viewer=True) 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() return
def main(): """ the main function it starts a droidbot according to the arguments given in cmd line """ ''' apknames = os.listdir(app_path) print apknames for apkname in apknames: print len(apkname) if apkname == '.DS_Store': continue else: name = apkname[0: len(apkname) - 4] print name try: droidbot = DroidBot(app_path + apkname, device_serial, output_dir + name, env_policy="static", event_policy="utg_dynamic", with_droidbox=False, event_interval=None, event_duration=3, event_count=20, quiet=False, use_hierarchy_viewer=True) # droidbot.setRedis(r2) droidbot.start() except: droidbot.stop() continue return ''' pool1 = redis.ConnectionPool(host='192.168.3.70', port=6379, db=5) pool2 = redis.ConnectionPool(host='192.168.3.70', port=6379, db=6) r1 = redis.Redis(connection_pool=pool1) r2 = redis.Redis(connection_pool=pool2) res_db0 = r1.lrange('apk_name', 0, -1) res_db1 = r2.lrange('apk_name', 0, -1) for apkname in res_db1: if apkname in res_db0: res_db0.remove(apkname) for apkname in res_db0: if not os.path.exists(app_path + apkname + ".apk"): print app_path + apkname + ".apk not exists" continue try: print apkname droidbot = DroidBot(app_path + apkname + ".apk", device_serial, output_dir + apkname, env_policy="static", event_policy="utg_dynamic", with_droidbox=None, event_interval=None, event_duration=3, event_count=15, quiet=False, use_hierarchy_viewer=True) droidbot.start() if r2 is not None: r2.lpush('apk_name', apkname) except: droidbot.stop() if r2 is not None: r2.lpush('apk_name', apkname) print apkname + " can not use! Continue!" import traceback traceback.print_exc() 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()