This commit is contained in:
Jean-Marie Mineau 2024-02-26 14:27:53 +01:00
parent 78b6bba5fb
commit 4e1c36ad3c
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
4 changed files with 115 additions and 61 deletions

144
test.py
View file

@ -22,44 +22,44 @@ with z.ZipFile(APK_NAME) as zipf:
apk = Apk()
apk.add_dex_file(dex)
clazz_id = IdType("Lcom/example/testapplication/ui/home/HomeViewModel;")
proto_id = IdMethodType(IdType("Ljava/lang/String;"), [])
method_id = IdMethod("text_gen", proto_id, clazz_id)
clazz = apk.classes[clazz_id]
method = clazz.virtual_methods[method_id]
code = method.code
# clazz_id = IdType("Lcom/example/testapplication/ui/home/HomeViewModel;")
# proto_id = IdMethodType(IdType("Ljava/lang/String;"), [])
# method_id = IdMethod("text_gen", proto_id, clazz_id)
#
# clazz = apk.classes[clazz_id]
# method = clazz.virtual_methods[method_id]
# code = method.code
logging.getLogger().setLevel(logging.WARNING)
print(f"[+] Code of {method_id} ")
for i in code.insns:
print(f" {i}")
print("[+] Modify code")
new_insns = []
for i in code.insns:
if isinstance(i, ins.ConstString):
if i.lit == "Hello":
i = ins.ConstString(i.reg, DexString("Degemer Mat"))
elif i.lit == "Bye":
i = ins.ConstString(i.reg, DexString("Kenavo"))
new_insns.append(i)
# print(f"[+] Code of {method_id} ")
# for i in code.insns:
# print(f" {i}")
# print("[+] Modify code")
#
# new_insns = []
# for i in code.insns:
# if isinstance(i, ins.ConstString):
# if i.lit == "Hello":
# i = ins.ConstString(i.reg, DexString("Degemer Mat"))
# elif i.lit == "Bye":
# i = ins.ConstString(i.reg, DexString("Kenavo"))
# new_insns.append(i)
# This need improving!
code = Code(code.registers_size, code.ins_size, code.outs_size, new_insns)
apk.set_method_code(method_id, code)
# code = Code(code.registers_size, code.ins_size, code.outs_size, new_insns)
# apk.set_method_code(method_id, code)
# apk.set_method_code(method.descriptor, code)
clazz = apk.classes[clazz_id]
method = clazz.virtual_methods[method_id]
code = method.code
print(f"[+] New code of {method_id} ")
for i in code.insns:
print(f" {i}")
# clazz = apk.classes[clazz_id]
# method = clazz.virtual_methods[method_id]
# code = method.code
# print(f"[+] New code of {method_id} ")
# for i in code.insns:
# print(f" {i}")
# # Strip class for debugging
# Strip class for debugging
# classes = list(
# filter(
# lambda x: x
@ -77,29 +77,29 @@ for i in code.insns:
# for cls in classes:
# apk.remove_class(cls)
#
print("[+] Recompile")
dex_raw = apk.gen_raw_dex()
new_apk = Apk()
for dex in dex_raw:
new_apk.add_dex_file(dex)
# print("[+] Recompile")
#
# dex_raw = apk.gen_raw_dex()
#
# new_apk = Apk()
# for dex in dex_raw:
# new_apk.add_dex_file(dex)
print("[+] Repackage")
utils.replace_dex(
APK_NAME,
APK_NAME.parent / (APK_NAME.name.removesuffix(".apk") + "-instrumented.apk"),
dex_raw,
Path().parent / "my-release-key.jks",
zipalign=Path.home() / "Android" / "Sdk" / "build-tools" / "34.0.0" / "zipalign",
apksigner=Path.home() / "Android" / "Sdk" / "build-tools" / "34.0.0" / "apksigner",
)
# print("[+] Repackage")
#
# utils.replace_dex(
# APK_NAME,
# APK_NAME.parent / (APK_NAME.name.removesuffix(".apk") + "-instrumented.apk"),
# dex_raw,
# Path().parent / "my-release-key.jks",
# zipalign=Path.home() / "Android" / "Sdk" / "build-tools" / "34.0.0" / "zipalign",
# apksigner=Path.home() / "Android" / "Sdk" / "build-tools" / "34.0.0" / "apksigner",
# )
#
last_id = None
MAX_REQ = 1
MAX_REQ = 5
def cmp(a, b, req=0):
@ -168,11 +168,45 @@ def cmp_list(a, b, req=0):
cmp(a[i], b[i], req + 1)
c1_id = IdType("Lcom/example/testapplication/ui/home/HomeViewModel;")
c2_id = IdType("Landroidx/navigation/NavDeepLink$Builder;")
c1 = apk.classes[c1_id]
c2 = apk.classes[c2_id]
apk_1 = Apk()
apk_2 = Apk()
apk_1_2 = Apk()
apk_1_then_2 = Apk()
apk_2_then_1 = Apk()
apk_then_1_2 = Apk()
apk_1.add_class(c1)
apk_2.add_class(c2)
apk_1_2.add_class(c1)
apk_1_2.add_class(c2)
dex_1 = apk_1.gen_raw_dex()[0]
dex_2 = apk_2.gen_raw_dex()[0]
dex_1_2 = apk_1_2.gen_raw_dex()[0]
apk_1_then_2.add_dex_file(dex_1)
apk_1_then_2.add_dex_file(dex_2)
apk_2_then_1.add_dex_file(dex_2)
apk_2_then_1.add_dex_file(dex_1)
apk_then_1_2.add_dex_file(dex_1_2)
cmp(c1, apk_1_then_2.classes[c1_id])
cmp(c1, apk_2_then_1.classes[c1_id])
cmp(c1, apk_then_1_2.classes[c1_id])
cmp(c2, apk_1_then_2.classes[c2_id])
cmp(c2, apk_2_then_1.classes[c2_id])
cmp(c2, apk_then_1_2.classes[c2_id])
# apk_eq = new_apk == apk
# print(f"[+] apk are equals: {nice_bool(apk_eq)}")
# if not apk_eq:
# cmp(new_apk, apk)
#
# Landroidx/constraintlayout/core/widgets/ConstraintWidget$1;.<clinit>()V
# mid = IdMethod(
# "<clinit>",
@ -184,18 +218,18 @@ def cmp_list(a, b, req=0):
# )
# m = apk.classes[mid.class_].direct_methods[mid]
# nm = new_apk.classes[mid.class_].direct_methods[mid]
#
#
# mid = IdMethod(
# "setValue",
# IdMethodType(
# IdType("Z"),
# [
# IdType("Ljava/lang/String;"),
# IdType("Landroidx/constraintlayout/core/parser/CLElement;"),
# ],
# ),
# IdType("Landroidx/constraintlayout/core/state/WidgetFrame;"),
# IdType("Landroidx/constraintlayout/core/parser/CLElement;"),
# ],
# ),
# IdType("Landroidx/constraintlayout/core/state/WidgetFrame;"),
# )
#
# m = apk.classes[mid.class_].virtual_methods[mid]