This commit is contained in:
Jean-Marie Mineau 2024-02-05 17:31:28 +01:00
parent 2cf3963532
commit 84eacfb7d4
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
9 changed files with 860 additions and 49 deletions

View file

@ -32,6 +32,10 @@ pub struct DexAnnotationItem {
#[pymethods] #[pymethods]
impl DexAnnotationItem { impl DexAnnotationItem {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[new] #[new]
pub fn new(annotation: DexAnnotation) -> Self { pub fn new(annotation: DexAnnotation) -> Self {
Self { Self {
@ -124,6 +128,10 @@ pub struct DexAnnotation {
#[pymethods] #[pymethods]
impl DexAnnotation { impl DexAnnotation {
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[new] #[new]
pub fn new(type_: IdType, elements: HashMap<DexString, DexValue>) -> Self { pub fn new(type_: IdType, elements: HashMap<DexString, DexValue>) -> Self {
Self { type_, elements } Self { type_, elements }

View file

@ -2364,6 +2364,10 @@ impl Apk {
Ok(()) Ok(())
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
#[pyo3(name = "gen_raw_dex")] //Sad GIL noise #[pyo3(name = "gen_raw_dex")] //Sad GIL noise
pub fn py_gen_raw_dex(&self, py: Python<'_>) -> Result<Vec<PyObject>> { pub fn py_gen_raw_dex(&self, py: Python<'_>) -> Result<Vec<PyObject>> {
Ok(self Ok(self

View file

@ -368,4 +368,8 @@ impl Class {
} }
flags flags
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }

View file

@ -38,6 +38,8 @@ pub struct Code {
pub insns: Vec<Instruction>, pub insns: Vec<Instruction>,
} }
// TODO reimplement PartialEq: label should become address independant
#[pymethods] #[pymethods]
impl Code { impl Code {
pub fn to_json(&self) -> Result<String> { pub fn to_json(&self) -> Result<String> {
@ -128,4 +130,8 @@ impl Code {
} }
handles handles
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }

View file

@ -448,7 +448,6 @@ impl IdType {
types.insert(self.clone()); types.insert(self.clone());
types types
} }
// TODO: TESTS // TODO: TESTS
} }
@ -679,6 +678,7 @@ impl PartialOrd for IdMethod {
#[pyclass] #[pyclass]
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct IdEnum(pub IdField); pub struct IdEnum(pub IdField);
#[pymethods] #[pymethods]
impl IdEnum { impl IdEnum {
pub fn to_json(&self) -> Result<String> { pub fn to_json(&self) -> Result<String> {
@ -721,4 +721,8 @@ impl IdEnum {
pub fn get_all_field_ids(&self) -> HashSet<IdField> { pub fn get_all_field_ids(&self) -> HashSet<IdField> {
self.0.get_all_field_ids() self.0.get_all_field_ids()
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }

View file

@ -248,4 +248,8 @@ impl Field {
pub fn has_annotations(&self) -> bool { pub fn has_annotations(&self) -> bool {
!self.annotations.is_empty() !self.annotations.is_empty()
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }

File diff suppressed because it is too large Load diff

View file

@ -289,4 +289,8 @@ impl Method {
.iter() .iter()
.any(|list| !list.is_empty()) .any(|list| !list.is_empty())
} }
pub fn __eq__(&self, other: &Self) -> bool {
self == other
}
} }

91
test.py
View file

@ -5,20 +5,21 @@ logging.basicConfig(format=FORMAT)
logging.getLogger().setLevel(logging.DEBUG) logging.getLogger().setLevel(logging.DEBUG)
import json import json
import androscalpel as asc
import zipfile as z import zipfile as z
from androscalpel import *
from pathlib import Path from pathlib import Path
from androscalpel import Apk, IdType, IdMethodType, ins, DexString, IdMethod, Code, utils # type: ignore
# APK_NAME = Path(__file__).parent / "test.apk" # APK_NAME = Path(__file__).parent / "test.apk"
APK_NAME = Path(__file__).parent / "app-release.apk" APK_NAME = Path(__file__).parent / "app.apk"
DEX_NAME = "classes.dex" DEX_NAME = "classes.dex"
print(f"[+] Load bytecode ")
with z.ZipFile(APK_NAME) as zipf: with z.ZipFile(APK_NAME) as zipf:
with zipf.open(DEX_NAME, "r") as dex: with zipf.open(DEX_NAME, "r") as dex_f:
dex = dex.read() dex = dex_f.read()
apk = asc.Apk() apk = Apk()
apk.add_dex_file(dex) apk.add_dex_file(dex)
clazz_id = IdType("Lcom/example/testapplication/ui/home/HomeViewModel;") clazz_id = IdType("Lcom/example/testapplication/ui/home/HomeViewModel;")
@ -35,17 +36,18 @@ print(f"[+] Code of {method_id} ")
for i in code.insns: for i in code.insns:
print(f" {i}") print(f" {i}")
print("[+] Modify code") print("[+] Modify code")
new_insns = [] new_insns = []
for i in code.insns: for i in code.insns:
if isinstance(i, asc.ins.ConstString): if isinstance(i, ins.ConstString):
if i.lit == "Hello": if i.lit == "Hello":
i = asc.ins.ConstString(i.reg, DexString("Degemer Mat")) i = ins.ConstString(i.reg, DexString("Degemer Mat"))
elif i.lit == "Bye": elif i.lit == "Bye":
i = asc.ins.ConstString(i.reg, DexString("Kenavo")) i = ins.ConstString(i.reg, DexString("Kenavo"))
new_insns.append(i) new_insns.append(i)
# This need improving! # This need improving!
code = asc.Code(code.registers_size, code.ins_size, code.outs_size, new_insns) 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_id, code)
# apk.set_method_code(method.descriptor, code) # apk.set_method_code(method.descriptor, code)
@ -58,49 +60,44 @@ for i in code.insns:
print(f" {i}") print(f" {i}")
# Strip class for debugging # Strip class for debugging
# classes = list( classes = list(
# filter( filter(
# lambda x: x lambda x: x
# not in [ not in [
# IdType("Lcom/example/testapplication/ui/home/HomeViewModel;"), IdType("Lcom/example/testapplication/ui/home/HomeViewModel;"),
# IdType("Landroidx/navigation/NavDeepLink$Builder;"), IdType("Landroidx/navigation/NavDeepLink$Builder;"),
# IdType("Landroidx/constraintlayout/core/widgets/ConstraintWidget$1;"), IdType("Landroidx/constraintlayout/core/widgets/ConstraintWidget$1;"),
# ], ],
# apk.classes.keys(), apk.classes.keys(),
# ) )
# ) )
# for cls in classes: for cls in classes:
# apk.remove_class(cls) apk.remove_class(cls)
print("[+] Recompile") print("[+] Recompile")
dex_raw = apk.gen_raw_dex() dex_raw = apk.gen_raw_dex()
utils.replace_dex( new_apk = Apk()
APK_NAME,
APK_NAME.parent / "app-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",
)
# assert len(dex_raw) == 1
# with open(DEX_NAME, "wb") as file:
# file.write(dex_raw[0])
#
# with open(DEX_NAME, "rb") as file:
# dex = file.read()
print("[+] Load new dex")
new_apk = asc.Apk()
for dex in dex_raw: for dex in dex_raw:
new_apk.add_dex_file(dex) new_apk.add_dex_file(dex)
clazz = new_apk.classes[clazz_id] print(f"{new_apk == apk=}")
method = clazz.virtual_methods[method_id]
code = method.code
print(f"[+] Code of {method_id} in new apk")
for i in code.insns: # print("[+] Repackage")
print(f" {i}") #
# 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",
# )
def cmp(a, b):
for f in dir(a):
if getattr(getattr(a, f), "__call__", None) is None:
print(f"{f}: {getattr(a, f) == getattr(b, f)}")