androscalpel/androscalpel/examples/count_ins_and_genapk.rs

56 lines
1.4 KiB
Rust

use androscalpel::{Apk, Instruction, Result, VisitableMut, VisitorMut};
use apk_frauder::replace_dex_unsigned;
use std::collections::HashMap;
use std::env;
use std::io::Cursor;
#[derive(Default)]
struct InsCounter {
pub n: u64,
}
impl VisitorMut for InsCounter {
fn visit_instruction(&mut self, ins: Instruction) -> Result<Instruction> {
if !ins.is_pseudo_ins() {
self.n += 1;
}
ins.default_visit_mut(self)
}
}
fn main() {
let args: Vec<_> = env::args().collect();
assert!(args.len() > 2, "usage: {} input.apk output.apk", args[0]);
let mut cnt = InsCounter::default();
let apk = cnt
.visit_apk(Apk::load_apk_path((&args[1]).into(), false, false).unwrap())
.unwrap();
println!("Nb INS: {}", cnt.n);
// This should be streamlined
let mut dex_files = vec![];
let mut files = apk.gen_raw_dex().unwrap();
let mut i = 0;
loop {
let name = if i == 0 {
"classes.dex".into()
} else {
format!("classes{}.dex", i + 1)
};
if let Some(file) = files.remove(&name) {
dex_files.push(Cursor::new(file))
} else {
break;
}
i += 1;
}
replace_dex_unsigned(
&args[1],
&args[2],
&mut dex_files,
None::<HashMap<_, Option<Cursor<&[u8]>>>>,
)
.unwrap();
}