def aapt(cmd, apk): if cmd in ["label", "package", "launch"]: stdout, _, code = try_call_std( [BUILD_TOOLS + "/aapt", "dump", "badging", apk], noexception=True, output=False, print_cmd=False) if code == 0: for l in stdout.splitlines(): if l.startswith("application-label:"): label = extract_in_quotes(l) if l.startswith("package: name="): package = extract_in_quotes(l) if l.startswith("launchable-activity: name="): launchable = extract_in_quotes(l) if cmd == "label": print(label) if cmd == "package": print(package) if cmd == "launch": try_call_std([ "adb", "shell", "am", "start", "-n", package + "/" + launchable ]) if cmd == "permissions": permissions = [] stdout, _, code = try_call_std( [BUILD_TOOLS + "/aapt", "dump", "permissions", apk], noexception=True) if code == 0: for l in stdout.splitlines(): if l.startswith("uses-permission: name="): permissions.append(extract_in_quotes(l)) print(permissions)
def run_markii(apk: str, facts_dir: str): """ Depends on Scala SBT by default """ os.system("mkdir -p " + facts_dir) # Run markii try_call_std(["bash", MARKII_DIR + "/build-run-markii.sh", apk, facts_dir], output=False, timeout_s=1200)
def run_markii(apk: str, facts_dir: str, vasco_mode: str = "context-sensitive,flow-sensitive"): os.system("mkdir -p " + facts_dir) # Run markii try_call_std(["bash", "markii/build-run-markii.sh", apk, facts_dir], output=False, timeout_s=MARKII_TIMEOUT_SECONDS, env={"VASCO_MODE": vasco_mode})
def run_with_config(self, config): for step in self.steps: start_seconds = time.time() output = try_call_std(step.command, cwd=step.cwd, env=dict(step.env, **config), noexception=True) _, _, return_code = output seconds_spent = time.time() - start_seconds stat = { "step_name": step.name, "seconds": seconds_spent, "output": output, "command": step.command } stat = dict(config, **stat) self.log(stat) if return_code != 0: return
def run_souffle(): souffle_duration_seconds = None if os.path.isdir(facts_dir): if os.getenv("REPORT"): produce_report(os.getenv("REPORT"), facts_dir, spec_path, output_dir, apk_name) # type: ignore else: souffle_start_time = time.time() # Run Souffle stdout, stderr, code = try_call_std([ gtime, '-f', 'gtime_memory: %M\ngtime_seconds: %E\ngtime_user_seconds: %U', "souffle", "-w", "-F", facts_dir, spec_path, "-D", output_dir ], output=False) assert "Error" not in stdout, stdout assert "Error" not in stderr, stderr souffle_duration_seconds = time.time() - souffle_start_time for line in stderr.split("\n"): if "gtime_memory: " in line: souffle_stats["souffle_gtime_memory_KB"] = float( line.split("gtime_memory: ")[1].strip()) if "gtime_seconds: " in line: seconds = line.split("gtime_seconds: ")[1].strip() souffle_stats[ "souffle_gtime_duration_seconds"] = parse_seconds( seconds) if "gtime_user_seconds: " in line: seconds = line.split("gtime_user_seconds: ")[1].strip() souffle_stats[ "souffle_gtime_user_duration_seconds"] = parse_seconds( seconds) print("Souffle results written to " + os.path.realpath(output_dir)) violated = set() for f in glob.glob(output_dir + "/*.csv"): # type: ignore if file_size_mb(f) > 1.0: # minimize the output if too big tmp = tempfile.NamedTemporaryFile(delete=False).name assert os.system("shuf -n 10 %s > %s" % (f, tmp)) == 0 shutil.move(tmp, f) # type: ignore decl_name = os.path.basename(f).replace(".csv", "") # type: ignore if len(open(f, "r").read().strip()) > 0: # type: ignore violated.add(decl_name) if len(violated): cprint("======== Violated ========", "red") for rule in sorted(list(violated)): cprint('- ' + rule, "red") total_duration_seconds = time.time() - uicheck_start_time print("Spent %.2f seconds" % total_duration_seconds) stat_results = { "total_duration_seconds": total_duration_seconds, "souffle_duration_seconds": souffle_duration_seconds, "markii_duration_seconds": markii_duration_seconds, "gator_duration_seconds": gator_duration_seconds, } if souffle_stats: for k, v in souffle_stats.items(): stat_results[k] = v if os.path.exists(output_dir + "/uicheck-results.json"): old_stat_results = load_json(output_dir + "/uicheck-results.json") for k, v in stat_results.items(): # type: ignore if v is None and k in old_stat_results: stat_results[k] = old_stat_results[k] write_pretty_json(stat_results, output_dir + "/uicheck-results.json")
def run_jadx(apk: str, jadx: str): try_call_std(['jadx', apk, '-d', jadx])
def sha256sum(apk_path): stdout, _, _ = try_call_std(["sha256sum", apk_path], print_cmd=False, output=False) return stdout.split()[0].strip()
from msbase.subprocess_ import try_call_std ret = try_call_std(["ping", "-c", "3", "google.com"]) print() print(ret)
def run_gator(apk: str, facts_dir: str): os.system("mkdir -p " + facts_dir) # Run gator try_call_std(["bash", "gator/build-and-run-gator.sh", apk, facts_dir], output=False, timeout_s=GATOR_TIMEOUT_SECONDS)