From b7c7282f1bb58cbf12c60bb0c575515135b332de Mon Sep 17 00:00:00 2001 From: Jean-Marie Mineau Date: Wed, 26 Mar 2025 17:08:31 +0100 Subject: [PATCH] rename duplicate classes --- patcher/Cargo.lock | 8 ++++---- patcher/Cargo.toml | 4 ++-- patcher/src/code_loading_patcher.rs | 28 +++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/patcher/Cargo.lock b/patcher/Cargo.lock index 864999d..5d6da14 100644 --- a/patcher/Cargo.lock +++ b/patcher/Cargo.lock @@ -35,7 +35,7 @@ dependencies = [ [[package]] name = "androscalpel" version = "0.1.0" -source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=5379c29#5379c29eb881bb2fbed9703206f01f4a9ec73ca3" +source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=c0152e7#c0152e76089b7a51ce28e19c1828cdbdd435271a" dependencies = [ "adler", "androscalpel_serializer", @@ -51,7 +51,7 @@ dependencies = [ [[package]] name = "androscalpel_serializer" version = "0.1.0" -source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=5379c29#5379c29eb881bb2fbed9703206f01f4a9ec73ca3" +source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=c0152e7#c0152e76089b7a51ce28e19c1828cdbdd435271a" dependencies = [ "androscalpel_serializer_derive", "log", @@ -60,7 +60,7 @@ dependencies = [ [[package]] name = "androscalpel_serializer_derive" version = "0.1.0" -source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=5379c29#5379c29eb881bb2fbed9703206f01f4a9ec73ca3" +source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=c0152e7#c0152e76089b7a51ce28e19c1828cdbdd435271a" dependencies = [ "proc-macro2", "quote", @@ -129,7 +129,7 @@ dependencies = [ [[package]] name = "apk_frauder" version = "0.1.0" -source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=5379c29#5379c29eb881bb2fbed9703206f01f4a9ec73ca3" +source = "git+ssh://git@git.mineau.eu/histausse/androscalpel.git?rev=c0152e7#c0152e76089b7a51ce28e19c1828cdbdd435271a" dependencies = [ "androscalpel_serializer", "flate2", diff --git a/patcher/Cargo.toml b/patcher/Cargo.toml index 2d767c7..f8b8afb 100644 --- a/patcher/Cargo.toml +++ b/patcher/Cargo.toml @@ -6,8 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -androscalpel = { git = "ssh://git@git.mineau.eu/histausse/androscalpel.git", rev = "5379c29", features = ["code-analysis"] } -apk_frauder = { git = "ssh://git@git.mineau.eu/histausse/androscalpel.git", rev = "5379c29"} +androscalpel = { git = "ssh://git@git.mineau.eu/histausse/androscalpel.git", rev = "c0152e7", features = ["code-analysis"] } +apk_frauder = { git = "ssh://git@git.mineau.eu/histausse/androscalpel.git", rev = "c0152e7"} anyhow = { version = "1.0.95", features = ["backtrace"] } clap = { version = "4.5.27", features = ["derive"] } env_logger = "0.11.6" diff --git a/patcher/src/code_loading_patcher.rs b/patcher/src/code_loading_patcher.rs index 5877b76..0b941e8 100644 --- a/patcher/src/code_loading_patcher.rs +++ b/patcher/src/code_loading_patcher.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fs::File; -use androscalpel::{Apk, DexString, IdType, VisitableMut, VisitorMut}; +use androscalpel::{Apk, DexString, IdType, VisitorMut}; use anyhow::{Context, Result}; use clap::ValueEnum; @@ -70,17 +70,30 @@ fn insert_code_model_class_loaders(apk: &mut Apk, data: &RuntimeData) -> Result< }; let collisions = class_defined.intersection(&classes); for cls in collisions { - class_loader.rename_classdef(cls); + class_loader.rename_classdef(cls)?; } class_defined.extend(classes); class_loaders.insert(dyn_data.classloader.clone(), class_loader); } - // TODO: rename colliding classes according to class laoder // TODO: get the ClassLoader::parent values... // TODO: model the delegation behavior and rename ref to class accordingly // TODO: update Runtime Data to reflect the name change - todo!() + + let apk = if let ApkOrRef::Ref(apk) = class_loaders.remove("MAIN").unwrap().apk { + apk + } else { + panic!("Main APK is not stored as ref?") + }; + for (_, ClassLoader { apk: other, .. }) in class_loaders.into_iter() { + if let ApkOrRef::Owned(other) = other { + apk.merge(other); + } else { + panic!("Secondary APK is not stored as owned?") + } + } + //todo!() + Ok(()) } /// Structure modelizing a class loader. @@ -129,13 +142,18 @@ impl ClassLoader<'_> { new_name.try_to_smali().unwrap(), ); - let class = self.apk().remove_class(cls, None).with_context(|| { + let class = self.apk().remove_class(cls, None)?.with_context(|| { format!( "Try to rename classdef of {} in class loader {}, but classdef not found", cls.__str__(), &id ) })?; + let class = RenameTypeVisitor { + new_names: [(cls.clone(), new_name.clone())].into(), + } + .visit_class(class)?; + self.apk().add_class("classes.dex", class)?; self.renamed_classes.insert(cls.clone(), new_name); Ok(())