frida-utils/apk-injector/patch-main.py

45 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-03-04 16:50:14 +01:00
#!/usr/bin/python3
import os
import xml.etree.ElementTree as ET
def get_main_activity() -> str:
root = ET.parse("app/AndroidManifest.xml").getroot()
for activity in root.iter("activity"):
for action in activity.iter("action"):
if action.attrib.get("{http://schemas.android.com/apk/res/android}name") == "android.intent.action.MAIN":
return str(activity.attrib.get("{http://schemas.android.com/apk/res/android}name"))
raise Exception("No main activity found")
def find_main_activity(classname: str) -> str:
classname = classname.replace(".", "/") + ".smali"
if classname.startswith("/"):
raise Exception("Invalid classname")
for path, _, files in os.walk("."):
for file in files:
result = os.path.join(path,file)
if result.endswith(classname):
return result
raise Exception("No file with classname found")
def patch_class(path: str) -> None:
with open(path, "r") as file:
lines = file.readlines()
patched = False
with open(path, "w") as file:
matched = False
for line in lines:
2024-03-09 18:58:59 +01:00
if ".method public constructor <init>()V" in line:
2024-03-04 16:50:14 +01:00
matched = True
if matched and "return-void" in line:
file.write("const-string v0, \"frida-gadget\"\n")
file.write("invoke-static {v0}, Ljava/lang/System;->loadLibrary(Ljava/lang/String;)V\n")
matched = False
patched = True
file.write(line)
if not patched:
raise Exception("Failed to patch class")
main_activity = get_main_activity()
path = find_main_activity(main_activity)
patch_class(path)