-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatcher.py
More file actions
executable file
·147 lines (138 loc) · 6.46 KB
/
patcher.py
File metadata and controls
executable file
·147 lines (138 loc) · 6.46 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python3
# Copyright (C) 2025 IsHacker
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os
import time
import xml.etree.ElementTree as ET
# Util functions here
def get_targetsdk_version(apk_path):
try:
tree = ET.parse(apk_path)
root = tree.getroot()
android_namespace = 'http://schemas.android.com/apk/res/android'
ET.register_namespace('android', android_namespace)
uses_sdk_element = root.find('uses-sdk')
target_sdk = uses_sdk_element.get(f'{{{android_namespace}}}targetSdkVersion')
return target_sdk
except ET.ParseError as e:
print("Error parsing APK: "+str(e))
return None
def get_minsdk_version(apk_path):
try:
tree = ET.parse(apk_path)
root = tree.getroot()
android_namespace = 'http://schemas.android.com/apk/res/android'
ET.register_namespace('android', android_namespace)
uses_sdk_element = root.find('uses-sdk')
min_sdk = uses_sdk_element.get(f'{{{android_namespace}}}minSdkVersion')
return min_sdk
except ET.ParseError as e:
print("Error parsing APK: "+str(e))
return None
def add_receiver_to_manifest(package_name):
receiver_string='<receiver android:name="'+package_name+'.CmdReceiver"'+"""
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</receiver>
</application>
"""
with open('tmp/app_decompile_xml/AndroidManifest.xml', 'r') as file:
filedata = file.read()
filedata = filedata.replace('</application>', receiver_string)
with open('tmp/app_decompile_xml/AndroidManifest.xml', 'w') as file:
file.write(filedata)
def exec_cleanup():
print("Cleaning up....")
print("Execute: utils/cleanup.sh")
os.system("utils/cleanup.sh")
def check_classes_dir(pkg_path):
if os.path.isdir("tmp/app_decompile_xml/smali/classes/"+pkg_path) == True:
target_dir = "tmp/app_decompile_xml/smali/classes/"+pkg_path
return target_dir
else:
for i in range(2, 101):
base_dir = f"tmp/app_decompile_xml/smali/classes{i}"
target_dir = base_dir+"/"+pkg_path
if os.path.isdir(target_dir) == True:
return target_dir
else:
return None
# Main code
try:
if os.path.exists("apk/app.apk"):
pkgName=input("Enter package name (leave empty for com.android.settings): ")
if pkgName == "":
pkgName= "com.android.settings"
pkgPath=pkgName.replace(".", "/")
time.sleep(1)
print("Decompiling APK....")
os.system("cp apk/app.apk tmp")
os.system("java -jar bin/apkedit.jar d -i tmp/app.apk > /dev/null 2> error_log_decompile.txt")
time.sleep(1)
targetsdk_ver = get_targetsdk_version("tmp/app_decompile_xml/AndroidManifest.xml")
minsdk_ver = get_minsdk_version("tmp/app_decompile_xml/AndroidManifest.xml")
if targetsdk_ver is not None:
print("Detected SDK: "+str(targetsdk_ver))
else:
print("Could not determine SDK version.")
quit()
if targetsdk_ver != minsdk_ver:
print("WARNING: TargetSDK version and MinSDK versions don't match. App may not be a system app.")
print("Removing existing signatures....")
os.system("rm tmp/app_decompile_xml/root/META-INF/MANIFEST.MF")
os.system("rm tmp/app_decompile_xml/root/META-INF/*.RSA")
os.system("rm tmp/app_decompile_xml/root/META-INF/*.SF")
os.system("rm tmp/app_decompile_xml/.cache/*")
time.sleep(1)
classes_dir = check_classes_dir(pkgPath)
if classes_dir == None:
print("Error reading smali code from APK. Either you have entered the wrong package name, or the APK is odexed. Please ensure you entered the correct package name, and that the APK contains at least one classes.dex file.")
exec_cleanup()
quit()
if pkgName == "com.android.settings":
os.system("utils/copy.sh")
print("Execute: utils/copy.sh")
else:
print("Execute: utils/copy.sh")
os.system("utils/copy.sh")
time.sleep(1)
with open('tmp/CmdReceiver.java', 'r') as file:
filedata = file.read()
filedata = filedata.replace('com.android.settings', pkgName)
with open('tmp/CmdReceiver.java', 'w') as file:
file.write(filedata)
time.sleep(1)
print("Compiling patch from source....")
os.system("export CLASSPATH=classpath/android-"+str(targetsdk_ver)+"/android.jar && javac tmp/CmdReceiver.java")
os.system("cd tmp && jar -cvf CmdReceiver.jar CmdReceiver.class")
os.system("cp tmp/CmdReceiver.jar bin/dex-tools")
os.system("cd bin/dex-tools && ./d2j-jar2dex.sh CmdReceiver.jar")
os.system("mv bin/dex-tools/CmdReceiver-jar2dex.dex tmp/CmdReceiver.dex")
os.system("cd tmp && java -jar ../bin/baksmali.jar d CmdReceiver.dex")
print("Patching app source code....")
os.system("mv tmp/out/"+pkgPath+"/CmdReceiver.smali "+classes_dir)
add_receiver_to_manifest(pkgName)
time.sleep(1)
print("Recompiling APK....")
os.system("java -jar bin/apkedit.jar b -i tmp/app_decompile_xml > /dev/null 2> error_log_compile.txt")
print("Signing compiled APK with testkey....")
os.system("java -jar bin/apksigner.jar sign --key sigs/testkey.pk8 --cert sigs/testkey.x509.pem --out "+pkgName+".apk tmp/app_decompile_xml_out.apk")
exec_cleanup()
print("Done! Now just normally install "+pkgName+".apk on your device to be able to run commands.")
else:
print("APK not found. Please rename the APK of the app to be patched to app.apk and place it inside the apk/ directory.")
quit()
except KeyboardInterrupt:
exec_cleanup()
quit()