From cfc8e4743ebfe29670fc791f09e538d87d4dc9c6 Mon Sep 17 00:00:00 2001 From: Jean-Marie 'Histausse' Mineau Date: Thu, 31 Aug 2023 18:03:55 +0200 Subject: [PATCH] first python binding --- androscalpel/src/apk.rs | 5 +--- androscalpel/src/class.rs | 32 +++++++++++++++++++--- androscalpel/src/lib.rs | 24 ++++++++++++++++ androscalpel_serializer/src/file_reader.rs | 1 - 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/androscalpel/src/apk.rs b/androscalpel/src/apk.rs index c41bf15..615f15d 100644 --- a/androscalpel/src/apk.rs +++ b/androscalpel/src/apk.rs @@ -15,10 +15,7 @@ pub struct Apk { impl Apk { /// Add the content of a dex file to the apk. pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> { - println!("Start parsing file"); let dex = DexFileReader::new(data)?; - println!("Header and lists parsed"); - println!("Start parsing classes"); for class in dex.get_class_defs() { self.add_class_from_dex_file(class, &dex)?; } @@ -92,10 +89,10 @@ impl Apk { impl Apk { #[new] fn new() -> Self { - println!("Create APK"); Self { classes: vec![] } } + #[pyo3(name = "add_dex_file")] fn py_add_dex_file(&mut self, data: &[u8]) -> Result<()> { self.add_dex_file(data) } diff --git a/androscalpel/src/class.rs b/androscalpel/src/class.rs index b7d750c..da48304 100644 --- a/androscalpel/src/class.rs +++ b/androscalpel/src/class.rs @@ -35,11 +35,35 @@ impl Class { } } - pub fn __str__(&self) -> DexString { - self.name.clone() + pub fn __str__(&self) -> String { + let name: String = (&self.name).into(); + let file = if let Some(file) = &self.source_file { + let file: String = file.into(); + format!(" defined in {file}\n") + } else { + "".into() + }; + let superclass = if let Some(spcl) = &self.superclass { + let spcl: String = spcl.into(); + format!(" extends: {spcl}\n") + } else { + "".into() + }; + let interfaces = if self.interfaces.is_empty() { + "".into() + } else { + let mut interfaces: String = " implements:\n".into(); + for it in &self.interfaces { + let it: String = it.into(); + interfaces += &format!(" {it}\n"); + } + interfaces + }; + + format!("{name}\n{file}{superclass}{interfaces}") } - pub fn __repr__(&self) -> DexString { - self.name.clone() + pub fn __repr__(&self) -> String { + (&self.name).into() } } diff --git a/androscalpel/src/lib.rs b/androscalpel/src/lib.rs index f47f1a2..2fb22d8 100644 --- a/androscalpel/src/lib.rs +++ b/androscalpel/src/lib.rs @@ -79,6 +79,30 @@ impl From for DexString { } } +impl From<&DexString> for String { + fn from(DexString(string): &DexString) -> Self { + string + .try_into() + .unwrap_or(format!("InvalidEncoding:{:x?}", string.data)) + } +} + +impl From for String { + fn from(string: DexString) -> Self { + (&string).into() + } +} + +#[pymethods] +impl DexString { + pub fn __str__(&self) -> String { + self.into() + } + pub fn __repr__(&self) -> String { + self.into() + } +} + /// Androscalpel. #[pymodule] fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { diff --git a/androscalpel_serializer/src/file_reader.rs b/androscalpel_serializer/src/file_reader.rs index 50f3812..436d45e 100644 --- a/androscalpel_serializer/src/file_reader.rs +++ b/androscalpel_serializer/src/file_reader.rs @@ -25,7 +25,6 @@ impl<'a> DexFileReader<'a> { pub fn new(data: &'a [u8]) -> Result { let mut buffer = Cursor::new(data); let header = HeaderItem::deserialize(&mut buffer)?; - println!("{header:x?}"); let mut tmp_file = Self { data, header,