2026-02-05 15:18:26 +01:00
|
|
|
use std::any::Any;
|
2026-02-06 15:08:38 +01:00
|
|
|
use crate::{InputTarget, AppContext, formats::Format};
|
2026-02-05 15:18:26 +01:00
|
|
|
pub fn format() -> Format {
|
2026-02-05 18:53:35 +01:00
|
|
|
Format { name: "pfl_upg", detector_func: is_pfl_upg_file, extractor_func: extract_pfl_upg }
|
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};
|
2025-09-27 00:31:18 +02:00
|
|
|
|
|
|
|
|
use aes::Aes256;
|
2025-09-27 17:19:55 +02:00
|
|
|
use ecb::{Decryptor, cipher::{BlockDecryptMut, KeyInit, generic_array::GenericArray}};
|
2025-10-10 19:52:36 +02:00
|
|
|
use binrw::{BinRead, 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;
|
2025-09-27 00:31:18 +02:00
|
|
|
|
2025-10-10 22:04:51 +02:00
|
|
|
#[derive(BinRead)]
|
2025-10-10 19:52:36 +02:00
|
|
|
struct Header {
|
|
|
|
|
#[br(count = 8)] _magic_bytes: Vec<u8>,
|
|
|
|
|
header_size: u32,
|
|
|
|
|
data_size: u32,
|
|
|
|
|
#[br(count = 4)] _crc32: Vec<u8>,
|
|
|
|
|
mask: u32,
|
|
|
|
|
_data_size_decompressed: u32,
|
|
|
|
|
_padding2: u32,
|
|
|
|
|
#[br(count = 512)] description_bytes: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
impl Header {
|
|
|
|
|
fn description(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.description_bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-10 22:04:51 +02:00
|
|
|
#[derive(BinRead)]
|
2025-10-10 19:52:36 +02:00
|
|
|
struct FileHeader {
|
|
|
|
|
#[br(count = 60)] file_name_bytes: Vec<u8>,
|
|
|
|
|
real_size: u32,
|
|
|
|
|
stored_size: u32,
|
2026-01-25 00:54:46 +01:00
|
|
|
header_size: u32,
|
2025-12-04 19:17:34 +01:00
|
|
|
#[br(count = 4)] attributes: Vec<u8>,
|
2025-10-10 19:52:36 +02:00
|
|
|
}
|
|
|
|
|
impl FileHeader {
|
|
|
|
|
fn file_name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.file_name_bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 15:08:38 +01:00
|
|
|
let file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Ok(None)};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 17:19:55 +02:00
|
|
|
static AUTO_FWS: &[(&str, &str)] = &[
|
|
|
|
|
("Q5551", "q5551"),
|
|
|
|
|
("Q5553", "q5551"),
|
|
|
|
|
("Q554E", "q5551"),
|
|
|
|
|
("Q554M", "q5551"),
|
|
|
|
|
("QF1EU", "qf1eu"),
|
|
|
|
|
("QF2EU", "qf1eu"),
|
|
|
|
|
("Q591E", "q591e"),
|
|
|
|
|
("Q522E", "q522e"),
|
|
|
|
|
("Q582E", "q522e"),
|
|
|
|
|
("Q5481", "q5481"),
|
|
|
|
|
("Q5431", "q5431"),
|
2025-10-31 22:15:51 +01:00
|
|
|
("Q5492", "q5492"),
|
2025-10-10 19:52:36 +02:00
|
|
|
("S5551", "q5551"), //Sharp
|
2025-09-27 17:19:55 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
type Aes256EcbDec = Decryptor<Aes256>;
|
|
|
|
|
|
|
|
|
|
fn decrypt_aes256_ecb(key: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
|
|
|
|
|
let key_array: [u8; 32] = key.try_into()?;
|
|
|
|
|
|
|
|
|
|
let mut decryptor = Aes256EcbDec::new(&key_array.into());
|
|
|
|
|
let mut buffer = ciphertext.to_vec();
|
|
|
|
|
|
|
|
|
|
for chunk in buffer.chunks_exact_mut(16) {
|
|
|
|
|
let block: &mut [u8; 16] = chunk.try_into()?;
|
|
|
|
|
decryptor.decrypt_block_mut(GenericArray::from_mut_slice(block));
|
2025-09-27 00:31:18 +02:00
|
|
|
}
|
2025-09-27 17:19:55 +02:00
|
|
|
|
|
|
|
|
Ok(buffer)
|
2025-09-27 00:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn extract_pfl_upg(app_ctx: &AppContext, _ctx: Option<Box<dyn Any>>) -> Result<(), Box<dyn std::error::Error>> {
|
2026-02-06 15:08:38 +01:00
|
|
|
let mut file = match &app_ctx.input {InputTarget::File(f) => f, InputTarget::Directory(_) => return Err("Extractor expected file, not directory".into())};
|
|
|
|
|
|
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() {
|
|
|
|
|
println!("Sorry, this firmware is not supported!");
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//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
|
|
|
}
|
2025-10-06 22:44:30 +02:00
|
|
|
|
2025-10-07 16:00:32 +02:00
|
|
|
println!("\nExtraction finished!");
|
2025-09-27 00:31:18 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|