创建 command.py
parent
21aa528f08
commit
aec778ff41
|
|
@ -0,0 +1,98 @@
|
|||
import subprocess
|
||||
import logging
|
||||
import time
|
||||
import json
|
||||
from modules.files_utils import get_path
|
||||
|
||||
|
||||
def run_adb_command(command_list):
|
||||
process = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout, stderr = process.communicate()
|
||||
output = stdout.decode().strip()
|
||||
status_code = process.returncode
|
||||
error = stderr.decode().strip()
|
||||
return output, status_code, error
|
||||
def start_frida():
|
||||
if is_frida_running():
|
||||
return
|
||||
else:
|
||||
output, status_code, error = run_adb_command(['adb', 'shell', 'su', '-c', '/data/local/tmp/frica'])
|
||||
if status_code == 0:
|
||||
print(output)
|
||||
logging.info(f"start frida output: {output}")
|
||||
return True
|
||||
else:
|
||||
print(error)
|
||||
logging.error(f"start error,error:{error}")
|
||||
return False
|
||||
|
||||
def is_frida_running():
|
||||
output, status_code, error = run_adb_command(['adb', 'shell', 'ps', '|', 'grep', 'frica'])
|
||||
print(output)
|
||||
return 'frica' in output
|
||||
|
||||
|
||||
def get_main_activity_for_package(package_name):
|
||||
output, status_code, error = run_adb_command(['adb', 'shell', 'dumpsys', 'package', package_name])
|
||||
if status_code != 0:
|
||||
print(f"Error getting main activity: {error}")
|
||||
return None
|
||||
else:
|
||||
# print(f"output: {output},output type: {type(output)}")
|
||||
return find_mainActivity(output,package_name)
|
||||
|
||||
|
||||
def find_mainActivity(output,package_name):
|
||||
activity = []
|
||||
start_append = False
|
||||
lines = [line.strip() for line in output.split("\n")]
|
||||
for line in lines:
|
||||
if "Activity Resolver Table:" in line:
|
||||
activity.append(line)
|
||||
if "Non-Data Actions:" in line:
|
||||
activity.append(line)
|
||||
start_append = True
|
||||
elif "android.intent.category.LAUNCHER" in line:
|
||||
activity.append(line)
|
||||
break
|
||||
elif start_append == True:
|
||||
activity.append(line)
|
||||
for main_ac in activity:
|
||||
if package_name in main_ac:
|
||||
tmp = main_ac.split()
|
||||
print(tmp[1])
|
||||
return tmp[1]
|
||||
# lines = output.split('\n')
|
||||
# print(f"lines: \n{lines},lines type: {type(lines)}")
|
||||
# last_line_indent = 0
|
||||
# for line in lines:
|
||||
# stripped = line.lstrip()
|
||||
# indent = len(line) - len(stripped)
|
||||
# print(indent)
|
||||
|
||||
|
||||
|
||||
def clearCache(package_name):
|
||||
if stopApp(package_name):
|
||||
output, status_code, error = run_adb_command(['adb', 'shell', 'pm', 'clear', package_name])
|
||||
if status_code == 0:
|
||||
print(f"clear cache status_code: {status_code}\n output: {output}")
|
||||
return True
|
||||
else:
|
||||
print(error)
|
||||
print(f"clear cache error: {error}")
|
||||
return False
|
||||
else:
|
||||
print("stopApp error")
|
||||
|
||||
def stopApp(package_name):
|
||||
print(f"强行停止{package_name}")
|
||||
output, status_code, error = run_adb_command(['adb', 'shell', 'am', 'force-stop', package_name])
|
||||
if status_code == 0:
|
||||
print(f"status_code: {status_code}\n output: {output}")
|
||||
return True
|
||||
else:
|
||||
print(error)
|
||||
logging.error(f"stop APP error: {error}")
|
||||
return False
|
||||
|
||||
Reference in New Issue