mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-16 05:04:22 +02:00
sony_bdp: add support for aes encryption, add keys for MSB19-29
This commit is contained in:
+58
-17
@@ -5,29 +5,70 @@ use crate::{InputTarget, AppContext};
|
||||
use std::fs::File;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{Cursor, Write};
|
||||
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
|
||||
use binrw::BinReaderExt;
|
||||
|
||||
use crate::utils::common;
|
||||
use crate::formats;
|
||||
use crate::keys;
|
||||
use include::*;
|
||||
|
||||
struct SonyBdpCtx {
|
||||
encryption_type: EncryptionType,
|
||||
}
|
||||
|
||||
pub fn is_sony_bdp_file(app_ctx: &AppContext) -> Result<Option<Box<dyn Any>>, Box<dyn std::error::Error>> {
|
||||
let file = match app_ctx.file() {Some(f) => f, None => return Ok(None)};
|
||||
let header_magic = common::read_file(&file, 0, 16)?;
|
||||
|
||||
let header = common::read_file(&file, 0, 4)?;
|
||||
if header == b"\x01\x73\xEC\xC9" || header == b"\x01\x73\xEC\x1F" || header == b"\xEC\x7D\xB0\xB0" { //MSB1x, MSB0x, BDPPxx
|
||||
Ok(Some(Box::new(())))
|
||||
} else {
|
||||
Ok(None)
|
||||
//try old encryption (hex subst)
|
||||
if is_valid_header_magic(&hex_substitute(&header_magic)) {
|
||||
return Ok(Some(Box::new(
|
||||
SonyBdpCtx {encryption_type:
|
||||
EncryptionType::HexSubst
|
||||
}
|
||||
)));
|
||||
}
|
||||
|
||||
//try new encryption (aes)
|
||||
for (key_hex, iv_hex, name) in keys::SONY_BDP_AES {
|
||||
let key_array: [u8; 16] = hex::decode(key_hex)?.as_slice().try_into()?;
|
||||
let iv_array: [u8; 16] = hex::decode(iv_hex)?.as_slice().try_into()?;
|
||||
let try_decrypt = ver_up_decrypt_aes128ofb(&key_array, &iv_array, &header_magic);
|
||||
|
||||
if is_valid_header_magic(&try_decrypt) {
|
||||
return Ok(Some(Box::new(
|
||||
SonyBdpCtx {encryption_type:
|
||||
EncryptionType::AesOfb((key_array, iv_array, name.to_string()))
|
||||
}
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
pub fn extract_sony_bdp(app_ctx: &AppContext, ctx: Box<dyn Any>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut file = app_ctx.file().ok_or("Extractor expected file")?;
|
||||
let ctx = ctx.downcast::<SonyBdpCtx>().expect("Missing context");
|
||||
|
||||
let obf_header = common::read_exact(&mut file, 300)?;
|
||||
let header = hex_substitute(&obf_header);
|
||||
//need to decrypt entire file of new aes enc
|
||||
let mut enc_data = Vec::new();
|
||||
file.read_to_end(&mut enc_data)?;
|
||||
|
||||
let dec_data = match ctx.encryption_type {
|
||||
EncryptionType::HexSubst => {
|
||||
println!("Decrypting with hex substitution...");
|
||||
hex_substitute(&enc_data)
|
||||
},
|
||||
EncryptionType::AesOfb((key, iv, key_name)) => {
|
||||
println!("Decrypting with AES key: {}...", key_name);
|
||||
ver_up_decrypt_aes128ofb(&key, &iv, &enc_data)
|
||||
}
|
||||
};
|
||||
let mut data_reader = Cursor::new(dec_data);
|
||||
|
||||
let header = common::read_exact(&mut data_reader, 300)?;
|
||||
let mut hdr_reader = Cursor::new(header);
|
||||
let hdr: Header = hdr_reader.read_le()?;
|
||||
|
||||
@@ -53,8 +94,8 @@ pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(),
|
||||
first_entry_offset = entry.offset as u64;
|
||||
}
|
||||
|
||||
let obf_data = common::read_file(&file, entry.offset as u64, entry.size as usize)?;
|
||||
let data = hex_substitute(&obf_data);
|
||||
data_reader.seek(SeekFrom::Start(entry.offset as u64))?;
|
||||
let data = common::read_exact(&mut data_reader, entry.size as usize)?;
|
||||
|
||||
let output_path = Path::new(&app_ctx.output_dir).join(format!("{}.bin", i+1));
|
||||
last_file_path = Some(output_path.clone());
|
||||
@@ -67,19 +108,19 @@ pub fn extract_sony_bdp(app_ctx: &AppContext, _ctx: Box<dyn Any>) -> Result<(),
|
||||
i += 1;
|
||||
}
|
||||
|
||||
//The last file is often a Mtk BDP file so we can extract that here.
|
||||
//The last file is the host MTK BDP file so we can extract that here (wont work for pre-linux which have old mtk bdp though.)
|
||||
if last_file_path.is_some() {
|
||||
println!("\nChecking if it's also MTK BDP...");
|
||||
|
||||
let last_file = File::open(last_file_path.unwrap())?;
|
||||
let mtk_extraction_path = app_ctx.output_dir.join(format!("{}", i));
|
||||
|
||||
//this is getting stupid...
|
||||
let ctx: AppContext = AppContext { input: InputTarget::File(last_file), output_dir: mtk_extraction_path, options: app_ctx.options.clone() };
|
||||
let ctx: AppContext = AppContext {
|
||||
input: InputTarget::File(last_file),
|
||||
output_dir: mtk_extraction_path,
|
||||
options: app_ctx.options.clone()
|
||||
};
|
||||
|
||||
if let Some(result) = formats::mtk_bdp::is_mtk_bdp_file(&ctx)? {
|
||||
println!("- MTK BDP file detected!\n");
|
||||
|
||||
formats::mtk_bdp::extract_mtk_bdp(&ctx, result)?;
|
||||
} else {
|
||||
println!("- Not an MTK BDP file.");
|
||||
|
||||
Reference in New Issue
Block a user