2026-04-08 16:51:45 +02:00
|
|
|
//base: sddl_dec 7.0 https://github.com/theubusu/sddl_dec
|
|
|
|
|
pub mod include;
|
2026-02-18 18:39:44 +01:00
|
|
|
mod util;
|
2026-02-05 15:18:26 +01:00
|
|
|
use std::any::Any;
|
2026-02-17 17:28:59 +01:00
|
|
|
use crate::AppContext;
|
2025-12-04 19:33:23 +01:00
|
|
|
|
2025-10-07 16:00:32 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
use std::fs::{self, File, OpenOptions};
|
2025-12-04 19:33:23 +01:00
|
|
|
use std::io::{Cursor, Seek, SeekFrom, Write};
|
2026-02-17 17:28:59 +01:00
|
|
|
use binrw::BinReaderExt;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2025-11-26 17:29:25 +01:00
|
|
|
use crate::utils::common;
|
2026-04-08 16:51:45 +02:00
|
|
|
use crate::utils::common::{string_from_bytes, read_exact};
|
|
|
|
|
use crate::utils::aes::{decrypt_aes128_cbc_nopad, decrypt_aes128_cbc_pcks7};
|
2025-11-26 17:29:25 +01:00
|
|
|
use crate::utils::compression::{decompress_zlib};
|
2026-02-17 17:28:59 +01:00
|
|
|
use include::*;
|
2026-02-18 18:39:44 +01:00
|
|
|
use util::split_peaks_file;
|
2026-06-05 21:02:40 +08:00
|
|
|
use log::info;
|
2025-12-04 19:33:23 +01:00
|
|
|
|
2026-02-05 18:53:35 +01:00
|
|
|
pub fn is_sddl_sec_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-06-05 21:02:40 +08:00
|
|
|
let header = common::read_file(&file, 0, 32)?;
|
2025-12-04 19:33:23 +01:00
|
|
|
let deciph_header = decipher(&header);
|
|
|
|
|
if deciph_header.starts_with(b"\x11\x22\x33\x44") {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(Some(Box::new(())))
|
2025-10-07 16:00:32 +02:00
|
|
|
} else {
|
2026-02-05 15:18:26 +01:00
|
|
|
Ok(None)
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 16:51:45 +02:00
|
|
|
fn get_sec_file(mut file: &File, key_entry: &KeyEntry) -> Result<(FileHeader, Vec<u8>), Box<dyn std::error::Error>> {
|
|
|
|
|
//new type check because only new is Pcks7.. i know
|
|
|
|
|
let new_type = match key_entry {
|
|
|
|
|
KeyEntry::AESPcks7(_) => true,
|
|
|
|
|
_ => false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let dec_header = KeyEntry::decrypt(key_entry, &read_exact(&mut file, 32)?)?;
|
|
|
|
|
let mut hdr_reader = Cursor::new(dec_header);
|
2026-02-16 22:41:06 +01:00
|
|
|
let file_header: FileHeader = hdr_reader.read_be()?;
|
2026-04-08 16:51:45 +02:00
|
|
|
|
|
|
|
|
let enc_size= if new_type {
|
|
|
|
|
file_header.size() as usize
|
|
|
|
|
} else {
|
|
|
|
|
//extra ciphered data before encrypted data, prefixed by size, like "0021XXXXXX PEAKS.T00/12900002"
|
|
|
|
|
//this counts into file size but not decrypt size
|
2026-06-05 21:02:40 +08:00
|
|
|
let extra_size: usize = string_from_bytes(&read_exact(&mut file, 4)?).parse::<usize>().map_err(|e| format!("Parse error: {}", e))?;
|
2026-04-08 16:51:45 +02:00
|
|
|
let _extra_data = read_exact(&mut file, extra_size)?;
|
|
|
|
|
|
|
|
|
|
file_header.size() as usize - (extra_size + 4)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let dec_data = KeyEntry::decrypt(key_entry, &read_exact(&mut file, enc_size)?)?;
|
|
|
|
|
let file_data = if new_type {
|
|
|
|
|
dec_data
|
|
|
|
|
} else {
|
|
|
|
|
let mut data_rdr = Cursor::new(dec_data);
|
|
|
|
|
|
|
|
|
|
//extra info in enc data, like "0021XXXXXX PEAKS.T00/12900002000000571800"
|
|
|
|
|
//part before size looks to be a duplicate of previous extra data, probably for signing purpose, size used for unpad
|
2026-06-05 21:02:40 +08:00
|
|
|
let extra_size: usize = string_from_bytes(&read_exact(&mut data_rdr, 4)?).parse::<usize>().map_err(|e| format!("Parse error: {}", e))?;
|
2026-04-08 16:51:45 +02:00
|
|
|
let _extra_data = read_exact(&mut data_rdr, extra_size + 4)?;
|
|
|
|
|
|
2026-06-05 21:02:40 +08:00
|
|
|
let data_size: usize = string_from_bytes(&read_exact(&mut data_rdr, 12)?).parse::<usize>().map_err(|e| format!("Parse error: {}", e))?;
|
2026-04-08 16:51:45 +02:00
|
|
|
read_exact(&mut data_rdr, data_size)?
|
|
|
|
|
};
|
2026-02-16 22:41:06 +01:00
|
|
|
|
|
|
|
|
Ok((file_header, file_data))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_tdi_to_modules(tdi_data: Vec<u8>) -> Result<Vec<TdiTgtInf>, Box<dyn std::error::Error>> {
|
|
|
|
|
let mut tdi_reader = Cursor::new(tdi_data);
|
|
|
|
|
let tdi_header: TdiHead = tdi_reader.read_be()?;
|
|
|
|
|
if tdi_header.download_id != DOWNLOAD_ID {
|
|
|
|
|
return Err("Invalid TDI header!".into());
|
|
|
|
|
}
|
|
|
|
|
if tdi_header.format_version != SUPPORTED_TDI_VERSION {
|
|
|
|
|
return Err(format!("Unsupported TDI format version {}! (The supported version is {})", tdi_header.format_version, SUPPORTED_TDI_VERSION).into());
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("[TDI] Group count: {}", tdi_header.num_of_group);
|
2026-02-16 22:41:06 +01:00
|
|
|
let mut modules: Vec<TdiTgtInf> = Vec::new();
|
|
|
|
|
|
|
|
|
|
for _i in 0..tdi_header.num_of_group {
|
|
|
|
|
let group_head: TdiGroupHead = tdi_reader.read_be()?;
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("[TDI] Group ID: {}, Target count: {}", group_head.group_id, group_head.num_of_target);
|
2026-02-16 22:41:06 +01:00
|
|
|
|
|
|
|
|
for _i in 0..group_head.num_of_target {
|
|
|
|
|
let tgt_inf: TdiTgtInf = tdi_reader.read_be()?;
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("[TDI] - {}, Target ID: {}, Segment count: {}, Version: {}",
|
2026-02-16 22:41:06 +01:00
|
|
|
tgt_inf.module_name(), tgt_inf.target_id, tgt_inf.num_of_txx, tgt_inf.version_string());
|
|
|
|
|
|
|
|
|
|
//push unique modules
|
|
|
|
|
if !modules.iter().any(|m| m.module_name() == tgt_inf.module_name()) {
|
|
|
|
|
modules.push(tgt_inf);
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-02-16 22:41:06 +01:00
|
|
|
|
|
|
|
|
Ok(modules)
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-06 16:27:13 +01:00
|
|
|
pub fn extract_sddl_sec(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-22 20:14:04 +01:00
|
|
|
let save_extra = app_ctx.has_option("sddl_sec:save_extra");
|
2026-02-17 21:28:43 +01:00
|
|
|
|
2026-04-08 16:51:45 +02:00
|
|
|
let mut secfile_hdr_reader = Cursor::new(decipher(&read_exact(&mut file, 32)?));
|
2026-02-16 22:41:06 +01:00
|
|
|
let secfile_header: SecHeader = secfile_hdr_reader.read_be()?;
|
2026-02-06 15:08:38 +01:00
|
|
|
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("File info -\nKey ID: {}\nGroup count: {}\nModule file count: {}", secfile_header.key_id(), secfile_header.grp_num(), secfile_header.prg_num());
|
2026-04-08 16:51:45 +02:00
|
|
|
|
|
|
|
|
//by knowing that the first file is always SDIT.FDI, find key(and mode)
|
|
|
|
|
let try_hdr = read_exact(&mut file, 0x20)?;
|
|
|
|
|
|
|
|
|
|
let mut key: Option<KeyEntry> = None;
|
|
|
|
|
|
|
|
|
|
//for new, key will always be the same
|
|
|
|
|
if let Ok(dec) = decrypt_aes128_cbc_pcks7(&try_hdr, &NEW_KEY.key, &NEW_KEY.iv) {
|
|
|
|
|
if dec.starts_with(TDI_FILENAME.as_bytes()) {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("- New type detected\n");
|
2026-04-08 16:51:45 +02:00
|
|
|
key = Some(KeyEntry::AESPcks7(NEW_KEY));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//new did not match, try all old AES keys
|
|
|
|
|
if key.is_none() {
|
|
|
|
|
for key_entry in OLD_KEYS_AES {
|
|
|
|
|
let dec = decrypt_aes128_cbc_nopad(&try_hdr, &key_entry.key, &key_entry.iv)?;
|
|
|
|
|
if dec.starts_with(TDI_FILENAME.as_bytes()) {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("- Old type detected with AES key={}, iv={}\n", hex::encode(key_entry.key) ,hex::encode(key_entry.iv));
|
2026-04-08 16:51:45 +02:00
|
|
|
key = Some(KeyEntry::AES(key_entry));
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//...old DES keys
|
|
|
|
|
if key.is_none() {
|
|
|
|
|
for key_entry in OLD_KEYS_DES {
|
|
|
|
|
let dec = decrypt_3des(&try_hdr, &key_entry)?;
|
|
|
|
|
if dec.starts_with(TDI_FILENAME.as_bytes()) {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("- Old type detected with DES key={}, iv={}\n", hex::encode(key_entry.key) ,hex::encode(key_entry.iv));
|
2026-04-08 16:51:45 +02:00
|
|
|
key = Some(KeyEntry::DES(key_entry));
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//nothing matched, quit
|
|
|
|
|
if key.is_none() {
|
|
|
|
|
return Err("No matching key found!".into());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// -- key search end
|
|
|
|
|
|
|
|
|
|
let key = key.unwrap();
|
2026-02-16 23:42:23 +01:00
|
|
|
fs::create_dir_all(&app_ctx.output_dir)?;
|
2026-04-08 16:51:45 +02:00
|
|
|
file.seek(SeekFrom::Start(0x20))?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-04-08 16:51:45 +02:00
|
|
|
let (tdi_file, tdi_data) = get_sec_file(&file, &key)?;
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("[TDI] Name: {}, Size: {}", tdi_file.name(), tdi_file.size());
|
2026-02-17 21:28:43 +01:00
|
|
|
if save_extra { //Save SDIT
|
|
|
|
|
let mut out_file = OpenOptions::new().write(true).create(true).open(Path::new(&app_ctx.output_dir).join(tdi_file.name()))?;
|
|
|
|
|
out_file.write_all(&tdi_data)?;
|
|
|
|
|
}
|
2026-02-16 22:41:06 +01:00
|
|
|
if tdi_file.name() != TDI_FILENAME {
|
|
|
|
|
return Err(format!("Invalid TDI filename {}!, expected: {}", tdi_file.name(), TDI_FILENAME).into());
|
|
|
|
|
}
|
|
|
|
|
//parse TDI
|
|
|
|
|
let modules = parse_tdi_to_modules(tdi_data)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-16 22:41:06 +01:00
|
|
|
//get info files, each info file belongs to its respecitve group in the TDI
|
|
|
|
|
for i in 0..secfile_header.grp_num() {
|
2026-04-08 16:51:45 +02:00
|
|
|
let (info_file, info_data) = get_sec_file(&file, &key)?;
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("\n[INFO] ID: {}, Name: {}, Size: {}", i, info_file.name(), info_file.size());
|
2026-02-16 22:41:06 +01:00
|
|
|
if !info_file.name().ends_with(INFO_FILE_EXTENSION) {
|
|
|
|
|
return Err(format!("Info file {} does not have the expected extension {}!", info_file.name(), INFO_FILE_EXTENSION).into());
|
|
|
|
|
}
|
2026-02-17 21:28:43 +01:00
|
|
|
if save_extra { //Save info file
|
|
|
|
|
let mut out_file = OpenOptions::new().write(true).create(true).open(Path::new(&app_ctx.output_dir).join(info_file.name()))?;
|
|
|
|
|
out_file.write_all(&info_data)?;
|
|
|
|
|
}
|
2026-02-16 22:41:06 +01:00
|
|
|
//print info file
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("{}", String::from_utf8_lossy(&info_data));
|
2026-02-16 22:41:06 +01:00
|
|
|
}
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-16 22:41:06 +01:00
|
|
|
//parse module data
|
|
|
|
|
for (i, module) in modules.iter().enumerate(){
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("\nModule #{}/{} - {}, Target ID: {}, Segment count: {}, Version: {}",
|
2026-02-16 22:41:06 +01:00
|
|
|
i+1, &modules.len(), module.module_name(), module.target_id, module.num_of_txx, module.version_string());
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-18 18:39:44 +01:00
|
|
|
let mut final_out_path: Option<PathBuf> = None;
|
|
|
|
|
|
2026-02-16 22:41:06 +01:00
|
|
|
for i in 0..module.num_of_txx {
|
2026-04-08 16:51:45 +02:00
|
|
|
let (module_file, module_data) = get_sec_file(&file, &key)?;
|
2026-02-16 22:41:06 +01:00
|
|
|
if !module_file.name().starts_with(&module.module_name()) {
|
|
|
|
|
return Err(format!("Module file {} does not start with the module's name: {}!", module_file.name(), module.module_name()).into());
|
|
|
|
|
}
|
2026-06-05 21:02:40 +08:00
|
|
|
info!(" Segment #{}/{} - Name: {}, Size: {}", i+1, module.num_of_txx, module_file.name(), module_file.size());
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-16 22:41:06 +01:00
|
|
|
let mut module_reader = Cursor::new(module_data);
|
2026-04-08 16:51:45 +02:00
|
|
|
let _com_header: ModuleComHeader = module_reader.read_be()?;
|
|
|
|
|
//if com_header.download_id != DOWNLOAD_ID { it seems this can differ in some files
|
|
|
|
|
// return Err("Invalid module com_header!".into());
|
|
|
|
|
//}
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2025-12-04 19:33:23 +01:00
|
|
|
let module_header: ModuleHeader = module_reader.read_be()?;
|
2026-04-08 16:51:45 +02:00
|
|
|
let mut module_data = read_exact(&mut module_reader, module_header.cmp_size as usize)?;
|
2026-02-16 22:41:06 +01:00
|
|
|
if module_header.is_ciphered() {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!(" - Deciphering...");
|
2026-02-16 22:41:06 +01:00
|
|
|
module_data = decipher(&module_data);
|
|
|
|
|
}
|
2025-12-04 19:33:23 +01:00
|
|
|
if module_header.is_compressed() {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!(" - Decompressing...");
|
2026-02-16 22:41:06 +01:00
|
|
|
module_data = decompress_zlib(&module_data)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2026-02-16 22:41:06 +01:00
|
|
|
let mut content_reader = Cursor::new(module_data);
|
2025-12-04 19:33:23 +01:00
|
|
|
let content_header: ContentHeader = content_reader.read_be()?;
|
2026-06-05 21:02:40 +08:00
|
|
|
info!(" --> 0x{:X} @ 0x{:X}", content_header.size, content_header.dest_offset());
|
2026-02-16 22:41:06 +01:00
|
|
|
|
|
|
|
|
let output_path: PathBuf;
|
|
|
|
|
if content_header.has_subfile() {
|
2026-04-08 16:51:45 +02:00
|
|
|
let sub_filename_bytes = read_exact(&mut content_reader, 0x100)?;
|
2026-02-16 22:41:06 +01:00
|
|
|
let sub_filename = common::string_from_bytes(&sub_filename_bytes);
|
2026-06-05 21:02:40 +08:00
|
|
|
info!(" --> {}", sub_filename);
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-02-16 23:42:23 +01:00
|
|
|
let sub_folder_path = Path::new(&app_ctx.output_dir).join(module.module_name());
|
2026-02-16 22:41:06 +01:00
|
|
|
fs::create_dir_all(&sub_folder_path)?;
|
|
|
|
|
output_path = Path::new(&sub_folder_path).join(sub_filename);
|
2025-10-07 16:00:32 +02:00
|
|
|
|
|
|
|
|
} else {
|
2026-02-16 23:42:23 +01:00
|
|
|
output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", module.module_name()));
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
2026-02-18 18:39:44 +01:00
|
|
|
final_out_path = Some(output_path.clone());
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2026-04-08 16:51:45 +02:00
|
|
|
let data = read_exact(&mut content_reader, content_header.size as usize)?;
|
|
|
|
|
let mut out_file = OpenOptions::new().read(true).write(true).create(true).open(output_path)?;
|
2025-12-04 19:33:23 +01:00
|
|
|
out_file.seek(SeekFrom::Start(content_header.dest_offset() as u64))?;
|
|
|
|
|
out_file.write_all(&data)?;
|
2025-10-07 16:00:32 +02:00
|
|
|
|
2025-12-04 19:33:23 +01:00
|
|
|
}
|
2026-02-18 18:39:44 +01:00
|
|
|
|
2026-02-22 20:14:04 +01:00
|
|
|
if app_ctx.has_option("sddl_sec:split_peaks") && module.module_name() == "PEAKS" {
|
2026-06-05 21:02:40 +08:00
|
|
|
info!("\n- Splitting PEAKS");
|
2026-02-18 18:39:44 +01:00
|
|
|
if let Some(ref path) = final_out_path {
|
2026-02-22 20:14:04 +01:00
|
|
|
split_peaks_file(path, &app_ctx.output_dir, !app_ctx.has_option("sddl_sec:no_decomp_peaks"))?;
|
2026-02-18 18:39:44 +01:00
|
|
|
}
|
|
|
|
|
}
|
2025-10-07 16:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|