poc repackaging

This commit is contained in:
Jean-Marie Mineau 2024-01-22 16:50:26 +01:00
parent 0f5764c340
commit d3e005fd68
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
3 changed files with 121 additions and 74 deletions

View file

@ -1,76 +1,78 @@
use apk_frauder::external_file_attributes;
use apk_frauder::file_header::FileHeader;
use apk_frauder::ZipFileReader;
use apk_frauder::ZipFileWriter;
use std::fs::File;
use std::io::Cursor;
use std::process::Command;
fn main() {
//let file = File::open("app-release.apk").expect("failed to open file");
//let file = File::open("tst_64.zip").expect("failed to open file");
//let zip_file = ZipFileReader::new(file);
/*
//println!("{}", zip_file.get_file_names().join("\n"));
for file in &zip_file.files {
println!("{}", file.get_name());
println!("local: {:?}", file.local_header.malformed_extra_field);
println!("central dir: {:?}", file.header.malformed_extra_field);
println!();
// Remove previous generation
println!("Remove files from previous run");
Command::new("rm").arg("app-striped.apk").status().unwrap();
Command::new("rm")
.arg("app-instrumented.apk")
.status()
.unwrap();
Command::new("rm")
.arg("app-instrumented-signed.apk")
.status()
.unwrap();
Command::new("rm")
.arg("app-instrumented-aligned.apk")
.status()
.unwrap();
println!("Read the headers of the existing dex file and strip the apk signature and dex files");
let file = File::open("app-release.apk").expect("failed to open file");
let mut apk = ZipFileReader::new(file);
let mut file_info_ref = (*apk.get_classes_file_info().first().unwrap()).clone();
apk.unlink_signature_files();
apk.unlink_bytecode_files();
let out_file = File::create("app-striped.apk").expect("failed to create file");
let mut apk_out = ZipFileWriter::new(out_file, apk.zip64_end_of_central_directory.clone());
for f in apk.files.clone() {
apk_out.insert_file_from_zip(f, &mut apk);
}
apk_out.write_central_directory();
println!(
"uncompressed size: {}",
zip_file.files[0].get_uncompressed_size()
);
println!(
"{}",
zip_file
.get_jar_sig_files()
.iter()
.map(|f| f.get_name())
.collect::<Vec<_>>()
.join("\n")
);
if zip_file.is_signed_v2() {
println!("Signed >= v2");
} else {
println!("Not signed whith scheme >= v2");
println!("Insert the dex file to the stripped apk with the header of the original dex file");
let file = File::open("app-striped.apk").expect("failed to open file");
let mut bytecodes = File::open("classes.dex").expect("failed to open file");
let mut apk = ZipFileReader::new(file);
let out_file = File::create("app-instrumented.apk").expect("failed to create file");
let mut apk_out = ZipFileWriter::new(out_file, apk.zip64_end_of_central_directory.clone());
for f in apk.files.clone() {
apk_out.insert_file_from_zip(f, &mut apk);
}
zip_file.check_holes();
*/
//println!("{:#?}", zip_file.get_file_info("classes.dex"));
/*
let file = File::open("tst_64.zip").expect("failed to open file");
let out_file = File::create("tst_64.out.zip").expect("failed to create file");
let mut zip_file = ZipFileReader::new(file);
let mut out_file =
ZipFileWriter::new(out_file, zip_file.zip64_end_of_central_directory.clone());
for f in zip_file.files.clone() {
out_file.insert_file_from_zip(f, &mut zip_file);
}
out_file.write_central_directory();
*/
//println!("{:#?}", zip_file.zip64_end_of_central_directory);
let out_file = File::create("test_write.zip").expect("failed to create file");
let mut out_file = ZipFileWriter::new(out_file, None);
out_file.insert_file(
&mut Cursor::new(b"plop\n"),
FileHeader::new_default("plop.txt"),
None,
);
out_file.insert_file(
&mut Cursor::new(b"Hello World !!!\n"),
FileHeader::new_default("plip.txt"),
None,
file_info_ref.set_name("classes.dex");
apk_out.insert_file(
&mut bytecodes,
file_info_ref.header,
Some(file_info_ref.local_header),
);
apk_out.write_central_directory();
//let mut header_dir = FileHeader::new_default("dir");
//header_dir.set_file_type(external_file_attributes::DIR_FILE);
//out_file.insert_file(&mut Cursor::new(b""), header_dir, None);
let mut header_link = FileHeader::new_default("dir/link");
header_link.set_file_type(external_file_attributes::SYMBOLIC_LINK_FILE);
out_file.insert_file(&mut Cursor::new(b"../plop.txt"), header_link, None);
println!("Align the apk");
// This could probably be performed by ourself
Command::new("/home/histausse/Android/Sdk/build-tools/34.0.0/zipalign")
.arg("-v")
.arg("-p")
.arg("4")
.arg("app-instrumented.apk")
.arg("app-instrumented-aligned.apk")
.status()
.unwrap();
out_file.write_central_directory();
println!("Sign the apk");
Command::new("/home/histausse/Android/Sdk/build-tools/34.0.0/apksigner")
.arg("sign")
.arg("--ks")
.arg("/home/histausse/AndroidStudioProjects/TestApplication/my-release-key.jks")
.arg("--out")
.arg("app-instrumented-signed.apk")
.arg("app-instrumented-aligned.apk")
.status()
.unwrap();
}