-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathview.py
More file actions
70 lines (56 loc) · 2.8 KB
/
Copy pathview.py
File metadata and controls
70 lines (56 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
This script provides a function to dump the live View hierarchy of a focused Android application using Frida.
It leverages the `view.js` script to inspect the application's memory and retrieve detailed view information.
"""
from typing import List, Dict, Optional
from android_util_impls.manager import android_util_manager
from script_base.frida_utils import FridaScriptExecutor
from script_base.utils import run_command, ensure_directory_exists
from script_base.log import logger
import os
FRIDA_SCRIPT_FILE = "view.js"
def dump_views_with_frida(package_name: str, device_id: Optional[str]=None) -> bool:
"""
Attaches to the foreground application and dumps its live View hierarchy using a Frida script.
This function identifies the foreground application, injects a Frida script (`view.js`)
to traverse the UI tree, and returns detailed information for each View.
Args:
app_name (Optional[str]): The package name of the target application.
If not provided, it will be automatically detected.
device_id (Optional[str]): The ID of the target Android device.
Returns:
Optional[List[Dict]]: A list of dictionaries, where each dictionary represents a View,
or None if an error occurs.
"""
script_dir = os.path.dirname(os.path.abspath(__file__))
frida_script_path = os.path.join(script_dir, FRIDA_SCRIPT_FILE)
try:
executor = FridaScriptExecutor(frida_script_path, device_id=device_id)
android_util = android_util_manager.select()
pid = android_util.get_pid_of_app(package_name, executor.device)
if pid is None or pid == -1:
logger.warning(f"Process '{package_name}' not found.")
return False
# Define the RPC calls to be executed
# Format: (function_name, (arg1, arg2, ...), wait_for_result)
rpc_calls = [
#("dumpviewsbytype", ("android.widget.TextView",), True)
#("dumpviewhierarchy", (), True)
("callviewmethod", (0xc01c26e, "setText", ""), True)
]
# Run the script and execute all defined calls
executor.run_script(pid, rpc_calls)
if executor.error_occurred:
logger.error("Failed to dump views. Please check the Frida script and device logs.")
return False
else:
logger.info("View hierarchy dumped successfully.")
return True
except Exception as e:
logger.error(f"An unexpected error occurred during execution: {e}", exc_info=True)
return False
if __name__ == "__main__":
# Simple test case: Dump views from the foreground application.
logger.info("Running test: Dumping views from the current foreground app...")
dump_views_with_frida("com.android.settings")