2025-09-27 00:31:18 +02:00
|
|
|
use std::fs::{File};
|
|
|
|
|
use rsa::{RsaPublicKey, BigUint};
|
|
|
|
|
use hex::decode;
|
2025-09-27 17:19:55 +02:00
|
|
|
use std::path::Path;
|
|
|
|
|
use std::io::{Cursor, Write};
|
|
|
|
|
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,
|
|
|
|
|
_header_size: u32,
|
|
|
|
|
_attributes: u32,
|
|
|
|
|
}
|
|
|
|
|
impl FileHeader {
|
|
|
|
|
fn file_name(&self) -> String {
|
|
|
|
|
common::string_from_bytes(&self.file_name_bytes)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-27 00:31:18 +02:00
|
|
|
pub fn is_pfl_upg_file(file: &File) -> bool {
|
|
|
|
|
let header = common::read_file(&file, 0, 8).expect("Failed to read from file.");
|
2025-10-07 16:00:32 +02:00
|
|
|
if header == b"2SWU3TXV" {
|
2025-09-27 00:31:18 +02:00
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn extract_pfl_upg(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
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-09-27 17:19:55 +02:00
|
|
|
let 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-10-10 19:52:36 +02:00
|
|
|
let encrypted_data = common::read_exact(&mut file, header.data_size as usize)?;
|
2025-09-27 17:19:55 +02:00
|
|
|
println!("Decrypting data...");
|
|
|
|
|
decrypted_data = decrypt_aes256_ecb(aes_key, &encrypted_data)?;
|
|
|
|
|
} 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-10-10 19:52:36 +02:00
|
|
|
println!("\nFile: {}, Size: {}", file_header.file_name(), file_header.real_size);
|
2025-09-27 17:19:55 +02:00
|
|
|
|
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
|
|
|
|
2025-11-28 00:05:23 +01:00
|
|
|
let mut output_path = Path::new(&output_folder).join(file_header.file_name().trim_start_matches('/'));
|
|
|
|
|
let output_path_parent = output_path.parent().expect("Failed to get parent of the output path!");
|
|
|
|
|
|
|
|
|
|
//prevent collisions because philips is dumb
|
|
|
|
|
if output_path_parent.exists() && output_path_parent.is_file() {
|
|
|
|
|
let fixed_file_name = if let Some((a, b)) = file_header.file_name().rsplit_once('/') {
|
|
|
|
|
format!("{}_{}", a, b)
|
|
|
|
|
} else {
|
|
|
|
|
file_header.file_name()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
output_path = Path::new(&output_folder).join(file_header.file_name().trim_start_matches('/'));
|
|
|
|
|
println!("[!] Warning: File collision detected, this file will be saved to: {}", fixed_file_name);
|
|
|
|
|
}
|
2025-09-27 17:19:55 +02:00
|
|
|
|
|
|
|
|
if let Some(parent) = output_path.parent() {
|
|
|
|
|
fs::create_dir_all(parent)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs::create_dir_all(&output_folder)?;
|
|
|
|
|
let mut out_file = OpenOptions::new()
|
2025-10-06 22:44:30 +02:00
|
|
|
.write(true)
|
2025-09-27 17:19:55 +02:00
|
|
|
.create(true)
|
|
|
|
|
.open(output_path)?;
|
|
|
|
|
|
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
|
|
|
|
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(())
|
|
|
|
|
}
|