2026-02-17 17:28:59 +01:00
|
|
|
mod include;
|
2026-02-05 15:18:26 +01:00
|
|
|
use std::any::Any;
|
2026-02-17 17:28:59 +01:00
|
|
|
use crate::AppContext;
|
2026-02-05 15:18:26 +01:00
|
|
|
|
2025-09-27 00:31:18 +02:00
|
|
|
use rsa::{RsaPublicKey, BigUint};
|
|
|
|
|
use hex::decode;
|
2025-09-27 17:19:55 +02:00
|
|
|
use std::path::Path;
|
2025-12-04 19:17:34 +01:00
|
|
|
use std::io::{Read, Cursor, Write};
|
2025-09-27 17:19:55 +02:00
|
|
|
use std::fs::{self, OpenOptions};
|
2026-02-17 17:28:59 +01:00
|
|
|
use binrw::BinReaderExt;
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2025-11-26 17:29:25 +01:00
|
|
|
use crate::utils::common;
|
2025-10-08 15:14:53 +02:00
|
|
|
use crate::keys;
|
2026-02-17 17:28:59 +01:00
|
|
|
use include::*;
|
2025-10-10 19:52:36 +02:00
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn is_pfl_upg_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
|
2026-02-06 16:27:13 +01:00
|
|
|
let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)};
|
2026-02-06 15:08:38 +01:00
|
|
|
|
|
|
|
|
let header = common::read_file(&file, 0, 8)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
if header == b"2SWU3TXV" {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(Some(Box::new(())))
|
2025-09-27 00:31:18 +02:00
|
|
|
} else {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(None)
|
2025-09-27 00:31:18 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 16:27:13 +01:00
|
|
|
pub fn extract_pfl_upg(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
|
2026-02-06 15:08:38 +01:00
|
|
|
|
2025-10-10 19:52:36 +02:00
|
|
|
let header: Header = file.read_le()?;
|
2025-09-27 00:31:18 +02:00
|
|
|
let signature = common::read_exact(&mut file, 128)?;
|
2025-10-07 00:04:47 +02:00
|
|
|
let _ = common::read_exact(&mut file, 32)?; //unknown
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2025-10-10 19:52:36 +02:00
|
|
|
let version_bytes = common::read_exact(&mut file, header.header_size as usize - 704)?;
|
2025-09-27 00:31:18 +02:00
|
|
|
let version = common::string_from_bytes(&version_bytes);
|
|
|
|
|
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("\nVersion: {}", version);
|
2025-10-10 19:52:36 +02:00
|
|
|
println!("Description: \n{}", header.description());
|
|
|
|
|
println!("Data size: {}", header.data_size);
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2025-12-04 19:17:34 +01:00
|
|
|
let mut decrypted_data;
|
2025-10-10 19:52:36 +02:00
|
|
|
if (header.mask & 0x2000_0000) != 0 {
|
2025-09-27 17:19:55 +02:00
|
|
|
println!("File is encrypted.");
|
|
|
|
|
let mut key = None;
|
|
|
|
|
let mut n_hex = None;
|
|
|
|
|
|
|
|
|
|
//find key
|
|
|
|
|
for (firmware, value) in AUTO_FWS {
|
|
|
|
|
if version.starts_with(firmware) {
|
|
|
|
|
key = Some(value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if key.is_none() {
|
2026-02-17 18:34:46 +01:00
|
|
|
return Err("This firmware is not supported!".into());
|
2025-09-27 17:19:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//get key
|
2025-10-08 15:14:53 +02:00
|
|
|
for (prefix, value) in keys::PFLUPG {
|
2025-09-27 17:19:55 +02:00
|
|
|
if key == Some(prefix) {
|
|
|
|
|
n_hex = Some(value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let e_hex = "010001";
|
|
|
|
|
|
|
|
|
|
let n = BigUint::from_bytes_be(&decode(n_hex.unwrap())?);
|
|
|
|
|
let e = BigUint::from_bytes_be(&decode(e_hex)?);
|
|
|
|
|
let pubkey = RsaPublicKey::new(n, e)?;
|
|
|
|
|
|
|
|
|
|
let signature_int = BigUint::from_bytes_le(&signature);
|
|
|
|
|
|
|
|
|
|
let decrypted_int = rsa::hazmat::rsa_encrypt(&pubkey, &signature_int)?;
|
|
|
|
|
let decrypted = decrypted_int.to_bytes_le();
|
|
|
|
|
|
|
|
|
|
let aes_key = &decrypted[20..52];
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("AES key: {}\n", hex::encode(aes_key));
|
2025-09-27 17:19:55 +02:00
|
|
|
|
2025-12-04 19:17:34 +01:00
|
|
|
//for encrypted data we need to read file to end
|
|
|
|
|
let mut encrypted_data = Vec::new();
|
|
|
|
|
file.read_to_end(&mut encrypted_data)?;
|
|
|
|
|
|
2025-09-27 17:19:55 +02:00
|
|
|
println!("Decrypting data...");
|
|
|
|
|
decrypted_data = decrypt_aes256_ecb(aes_key, &encrypted_data)?;
|
2025-12-04 19:17:34 +01:00
|
|
|
decrypted_data.truncate(header.data_size as usize);
|
|
|
|
|
|
2025-09-27 17:19:55 +02:00
|
|
|
} else {
|
|
|
|
|
println!("File is not encrypted.");
|
2025-10-10 19:52:36 +02:00
|
|
|
decrypted_data = common::read_exact(&mut file, header.data_size as usize)?;
|
2025-09-27 17:19:55 +02:00
|
|
|
}
|
2025-09-27 00:31:18 +02:00
|
|
|
|
|
|
|
|
let mut data_reader = Cursor::new(decrypted_data);
|
|
|
|
|
|
|
|
|
|
while (data_reader.position() as usize) < data_reader.get_ref().len() {
|
2025-10-10 19:52:36 +02:00
|
|
|
let file_header: FileHeader = data_reader.read_le()?;
|
2025-09-27 17:19:55 +02:00
|
|
|
|
2025-12-04 19:17:34 +01:00
|
|
|
//its a folder not a file
|
|
|
|
|
if (file_header.attributes[3] & (1 << 1)) != 0 {
|
2026-01-25 00:54:46 +01:00
|
|
|
println!("\nFolder - {}", file_header.file_name());
|
2026-02-06 15:08:38 +01:00
|
|
|
let output_path = Path::new(&app_ctx.output_dir).join(file_header.file_name().trim_start_matches('/'));
|
2025-12-04 19:17:34 +01:00
|
|
|
fs::create_dir_all(output_path)?;
|
|
|
|
|
continue
|
|
|
|
|
}
|
2025-09-27 17:19:55 +02:00
|
|
|
|
2026-01-25 00:54:46 +01:00
|
|
|
//extended name is used
|
|
|
|
|
let file_name = if (file_header.attributes[2] & (1 << 7)) != 0 {
|
|
|
|
|
let ex_name_size = file_header.header_size - 76; //76 is base file header size
|
|
|
|
|
//println!("extended name {}, org name: {}", ex_name_size, file_header.file_name());
|
|
|
|
|
let ex_name_bytes = common::read_exact(&mut data_reader, ex_name_size as usize)?;
|
|
|
|
|
common::string_from_bytes(&ex_name_bytes)
|
|
|
|
|
} else {
|
|
|
|
|
file_header.file_name()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
println!("\nFile - {}, Size: {}", file_name, file_header.real_size);
|
2025-10-10 19:52:36 +02:00
|
|
|
let data = common::read_exact(&mut data_reader, file_header.stored_size as usize)?;
|
2025-09-27 17:19:55 +02:00
|
|
|
|
2026-02-06 15:08:38 +01:00
|
|
|
let output_path = Path::new(&app_ctx.output_dir).join(file_name.trim_start_matches('/'));
|
2025-11-28 00:05:23 +01:00
|
|
|
let output_path_parent = output_path.parent().expect("Failed to get parent of the output path!");
|
|
|
|
|
|
2025-12-04 19:17:34 +01:00
|
|
|
//prevent collisions
|
2025-11-28 00:05:23 +01:00
|
|
|
if output_path_parent.exists() && output_path_parent.is_file() {
|
2025-12-04 19:17:34 +01:00
|
|
|
println!("[!] Warning: File collision detected, Skipping this file!");
|
|
|
|
|
continue
|
2025-11-28 00:05:23 +01:00
|
|
|
}
|
2025-09-27 17:19:55 +02:00
|
|
|
|
|
|
|
|
if let Some(parent) = output_path.parent() {
|
|
|
|
|
fs::create_dir_all(parent)?;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-06 15:08:38 +01:00
|
|
|
fs::create_dir_all(&app_ctx.output_dir)?;
|
|
|
|
|
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
|
2025-09-27 17:19:55 +02:00
|
|
|
|
2025-10-10 19:52:36 +02:00
|
|
|
out_file.write_all(&data[..file_header.real_size as usize])?;
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2026-01-25 00:54:46 +01:00
|
|
|
//if it contains a PFL upg in itself to extract
|
|
|
|
|
//if (file_header.attributes[3] & (1 << 2)) != 0 {
|
|
|
|
|
// println!("Container file");
|
|
|
|
|
//}
|
|
|
|
|
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("- Saved file!");
|
2025-09-27 00:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|