diff --git a/androscalpel/src/apk.rs b/androscalpel/src/apk.rs index f3327a9..bf97a4c 100644 --- a/androscalpel/src/apk.rs +++ b/androscalpel/src/apk.rs @@ -2342,12 +2342,26 @@ impl Apk { } #[pyo3(name = "add_dex_file")] - fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> { + pub fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> { self.add_dex_file(data) } + pub fn set_method_code(&mut self, method_id: IdMethod, code: Option) -> Result<()> { + let class = self + .classes + .get_mut(&method_id.class_) + .ok_or_else(|| anyhow!("Class {} not found", method_id.class_.__repr__()))?; + let method = class + .direct_methods + .get_mut(&method_id) + .or_else(|| class.virtual_methods.get_mut(&method_id)) + .ok_or_else(|| anyhow!("Method {} not found", method_id.__repr__()))?; + method.code = code; + Ok(()) + } + #[pyo3(name = "gen_raw_dex")] //Sad GIL noise - fn py_gen_raw_dex(&self, py: Python<'_>) -> Result> { + pub fn py_gen_raw_dex(&self, py: Python<'_>) -> Result> { Ok(self .gen_raw_dex()? .into_iter() diff --git a/androscalpel/src/dex_writer.rs b/androscalpel/src/dex_writer.rs index 97757af..c8e7581 100644 --- a/androscalpel/src/dex_writer.rs +++ b/androscalpel/src/dex_writer.rs @@ -2753,7 +2753,7 @@ enum Section { } impl Section { - const VARIANT_LIST: &[Self] = &[ + const VARIANT_LIST: &'static [Self] = &[ Self::HeaderItem, Self::StringIdItem, Self::TypeIdItem, diff --git a/test.py b/test.py index 0a18628..0f14568 100644 --- a/test.py +++ b/test.py @@ -9,7 +9,7 @@ import zipfile as z from androscalpel import * # APK_NAME = "test.apk" -APK_NAME = "/home/histausse/workspace/androscalpel/apk_frauder/app-release.apk" +APK_NAME = __file__.removesuffix("test.py") + "/apk_frauder/app-release.apk" DEX_NAME = "classes.dex" with z.ZipFile(APK_NAME) as zipf: @@ -25,13 +25,16 @@ method_id = IdMethod("text_gen", proto_id, clazz_id) clazz = apk.classes[clazz_id] method = clazz.virtual_methods[method_id] +code = method.code -print("Code of {method_id}") -for i in method.code.insns: +logging.getLogger().setLevel(logging.ERROR) + +print(f"Code of {method_id}") +for i in code.insns: print(i) new_insns = [] -for i in method.code.insns: +for i in code.insns: if isinstance(i, asc.ins.ConstString): if i.lit == "Hello": i.lit = DexString("Degemer Mat") @@ -40,11 +43,19 @@ for i in method.code.insns: new_insns.append(i) # This need improving! -new_code = method.code -new_code.insns = new_insns -method.code = new_code -clazz.virtual_methods[method_id] = method -apk.classes[clazz_id] = clazz +code.insns = 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"Code of {method_id}") +for i in code.insns: + print(i) dex_raw = apk.gen_raw_dex() +assert len(dex_raw) == 1 +with open(DEX_NAME + ".out", "wb") as file: + file.write(dex_raw[0])