mirror of
https://github.com/Ap0dexMe0/unixtract.git
synced 2026-07-15 21:00:03 +02:00
main: Add auto folder creation, mtk_pkg: add custom key support + some fixes., general: message costistency
This commit is contained in:
+45
-19
@@ -5,6 +5,7 @@ use binrw::{BinRead, BinReaderExt};
|
||||
|
||||
use crate::utils::common;
|
||||
use crate::utils::aes::{decrypt_aes128_cbc_nopad};
|
||||
use crate::keys;
|
||||
|
||||
#[derive(BinRead)]
|
||||
struct Header {
|
||||
@@ -26,6 +27,7 @@ impl Header {
|
||||
fn product_name(&self) -> String {
|
||||
common::string_from_bytes(&self.product_name_bytes)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(BinRead)]
|
||||
@@ -38,6 +40,12 @@ impl PartEntry {
|
||||
fn name(&self) -> String {
|
||||
common::string_from_bytes(&self.name_bytes)
|
||||
}
|
||||
fn is_valid(&self) -> bool {
|
||||
self.name().is_ascii()
|
||||
}
|
||||
fn is_encrypted(&self) -> bool {
|
||||
(self.flags & 1 << 0) == 1 << 0
|
||||
}
|
||||
}
|
||||
|
||||
static HEADER_KEY: [u8; 16] = [
|
||||
@@ -66,6 +74,7 @@ pub fn is_mtk_pkg_file(file: &File) -> bool {
|
||||
}
|
||||
|
||||
pub fn extract_mtk_pkg(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let file_size = file.metadata()?.len();
|
||||
let encrypted_header = common::read_exact(&mut file, 144)?;
|
||||
let header = decrypt_aes128_cbc_nopad(&encrypted_header, &HEADER_KEY, &HEADER_IV)?;
|
||||
let mut hdr_reader = Cursor::new(header);
|
||||
@@ -75,32 +84,56 @@ pub fn extract_mtk_pkg(mut file: &File, output_folder: &str) -> Result<(), Box<d
|
||||
hdr.file_size, hdr.vendor_magic(), hdr.version(), hdr.product_name());
|
||||
|
||||
let mut part_n = 0;
|
||||
while file.stream_position()? < hdr.file_size as u64 {
|
||||
while file.stream_position()? < file_size as u64 {
|
||||
part_n += 1;
|
||||
let part_entry: PartEntry = file.read_le()?;
|
||||
let is_encrypted = if (part_entry.flags & 1 << 0) == 1 << 0 {true} else {false};
|
||||
if !part_entry.is_valid() {
|
||||
break
|
||||
}
|
||||
|
||||
println!("\n{} - {}, Size: {} {}", part_n, part_entry.name(), part_entry.size, if is_encrypted {"[ENCRYPTED]"} else {""} );
|
||||
println!("\n#{} - {}, Size: {} {}", part_n, part_entry.name(), part_entry.size, if part_entry.is_encrypted() {"[ENCRYPTED]"} else {""} );
|
||||
|
||||
let data = common::read_exact(&mut file, part_entry.size as usize + 48)?;
|
||||
|
||||
if part_entry.size == 0 {
|
||||
println!("- Empty entry, skipping!");
|
||||
continue
|
||||
}
|
||||
|
||||
let out_data;
|
||||
if is_encrypted {
|
||||
// try decrypting with vendor magic repeated 4 times (works for sony and hisense)
|
||||
let mut out_data = Vec::new();
|
||||
if part_entry.is_encrypted() {
|
||||
let crypted_header = &data[..48];
|
||||
|
||||
// try decrypting with vendor magic repeated 4 times (works for most)
|
||||
let mut key = [0u8; 16];
|
||||
for i in 0..4 {
|
||||
key[i * 4..(i + 1) * 4].copy_from_slice(&hdr.vendor_magic_bytes);
|
||||
}
|
||||
|
||||
let crypted_header = &data[..48];
|
||||
let try_decrypt = decrypt_aes128_cbc_nopad(&crypted_header, &key, &HEADER_IV)?;
|
||||
|
||||
if try_decrypt.starts_with(b"reserved mtk inc") {
|
||||
println!("- Decrypting with 4xVendor magic...");
|
||||
out_data = decrypt_aes128_cbc_nopad(&data[..data.len() & !15], &key, &HEADER_IV)?;
|
||||
} else {
|
||||
println!("- Failed to decrypt data!");
|
||||
continue
|
||||
} else {
|
||||
//try decrypting with one of custom keys
|
||||
let mut decrypted = false;
|
||||
for (key_hex, iv_hex, name) in keys::MTK_PKG_CUST {
|
||||
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 = decrypt_aes128_cbc_nopad(&crypted_header, &key_array, &iv_array)?;
|
||||
|
||||
if try_decrypt.starts_with(b"reserved mtk inc") {
|
||||
println!("- Decrypting with key {}...", name);
|
||||
out_data = decrypt_aes128_cbc_nopad(&data[..data.len() & !15], &key_array, &iv_array)?;
|
||||
decrypted = true;
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !decrypted {
|
||||
println!("- Failed to decrypt data!");
|
||||
continue
|
||||
}
|
||||
};
|
||||
} else {
|
||||
out_data = data;
|
||||
@@ -113,17 +146,10 @@ pub fn extract_mtk_pkg(mut file: &File, output_folder: &str) -> Result<(), Box<d
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
//println!("Extra header size: {}", extra_header_len);
|
||||
|
||||
let output_path = Path::new(&output_folder).join(part_entry.name() + ".bin");
|
||||
|
||||
fs::create_dir_all(&output_folder)?;
|
||||
let mut out_file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.open(output_path)?;
|
||||
|
||||
let mut out_file = OpenOptions::new().write(true).create(true).open(output_path)?;
|
||||
out_file.write_all(&out_data[48 + extra_header_len as usize..])?;
|
||||
|
||||
println!("-- Saved file!");
|
||||
|
||||
@@ -37,7 +37,7 @@ pub fn extract_mtk_upgrade_loader(mut file: &File, output_folder: &str) -> Resul
|
||||
let part_entry: PartEntry = file.read_le()?;
|
||||
let is_encrypted = if (part_entry.flags & 1 << 0) == 1 << 0 {true} else {false};
|
||||
|
||||
println!("\n{} - {}, Size: {} {}", part_n, part_entry.name(), part_entry.size, if is_encrypted {"[ENCRYPTED]"} else {""} );
|
||||
println!("\n#{} - {}, Size: {} {}", part_n, part_entry.name(), part_entry.size, if is_encrypted {"[ENCRYPTED]"} else {""} );
|
||||
|
||||
let data = common::read_exact(&mut file, part_entry.size as usize)?;
|
||||
|
||||
|
||||
@@ -12,11 +12,18 @@ struct Header {
|
||||
#[br(count = 4)] _magic_bytes: Vec<u8>,
|
||||
#[br(count = 4)] _flags: Vec<u8>,
|
||||
_header_size: u32,
|
||||
#[br(count = 40)] _unknown1: Vec<u8>,
|
||||
_unused: u32,
|
||||
#[br(count = 16)] firmware_name_bytes: Vec<u8>,
|
||||
#[br(count = 20)] _unknown1: Vec<u8>,
|
||||
part_count: u32,
|
||||
_first_part_offset: u32,
|
||||
#[br(count = 116)] _unknown2: Vec<u8>,
|
||||
}
|
||||
impl Header {
|
||||
fn firmware_name(&self) -> String {
|
||||
common::string_from_bytes(&self.firmware_name_bytes)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(BinRead)]
|
||||
struct PartEntry {
|
||||
@@ -38,7 +45,7 @@ pub fn is_novatek_file(file: &File) -> bool {
|
||||
pub fn extract_novatek(mut file: &File, output_folder: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let header: Header = file.read_le()?;
|
||||
|
||||
println!("Part count: {}", header.part_count);
|
||||
println!("File info:\nFirmware name: {}\nPart count: {}", header.firmware_name(), header.part_count);
|
||||
let mut entries: Vec<PartEntry> = Vec::new();
|
||||
|
||||
for _i in 0..header.part_count {
|
||||
|
||||
@@ -51,7 +51,7 @@ pub fn extract_nvt_timg(mut file: &File, output_folder: &str) -> Result<(), Box<
|
||||
};
|
||||
let data = common::read_exact(&mut file, pimg.size as usize)?;
|
||||
|
||||
println!("\n({}) - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
|
||||
println!("\n#{} - {}, Size: {}, Dest: {}, Compression: {}", pimg_i, pimg.name(), pimg.size, pimg.dest_dev(), pimg.comp_type());
|
||||
|
||||
let out_data;
|
||||
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ pub fn extract_rvp(mut file: &File, output_folder: &str) -> Result<(), Box<dyn s
|
||||
i += 1;
|
||||
let header_size_bytes = common::read_exact(&mut data_reader, 4)?;
|
||||
let header_size = u32::from_be_bytes(header_size_bytes.try_into().unwrap());
|
||||
println!("\n({}) - Offset: {}, Header size: {}", i, data_reader.position() - 4, header_size);
|
||||
println!("\n#{} - Offset: {}, Header size: {}", i, data_reader.position() - 4, header_size);
|
||||
let hdr = common::read_exact(&mut data_reader, header_size as usize)?;
|
||||
|
||||
let size;
|
||||
|
||||
@@ -88,7 +88,7 @@ pub fn extract_sony_bdp(mut file: &File, output_folder: &str) -> Result<(), Box<
|
||||
continue
|
||||
}
|
||||
|
||||
println!("\n({}) - Offset: {}, Size: {}", i, entry.offset, entry.size);
|
||||
println!("\n#{} - Offset: {}, Size: {}", i, entry.offset, entry.size);
|
||||
if i == 1 {
|
||||
first_entry_offset = entry.offset as u64;
|
||||
}
|
||||
|
||||
+10
@@ -87,4 +87,14 @@ pub static PANA_DVD_KEYONLY: &[&str] = &[
|
||||
//pana dvd keys (PANAEUSB pair (AES + cust))
|
||||
pub static PANA_DVD_AESPAIR: &[(&str, &str)] = &[
|
||||
("62A39E1C5594AE09244EB326EF7938FA", "06C943F3B997F7E0"), //# 9 firmwares, ~2018
|
||||
];
|
||||
|
||||
//mtk pkg custom keys
|
||||
//github.com/openlgtv/epk2extract
|
||||
//key, iv, desc
|
||||
pub static MTK_PKG_CUST: &[(&str, &str, &str)] = &[
|
||||
("D378EAF81D378A801B556985789A7C31", "73079FD19183715E130858588479C652", "Philips 2012"),
|
||||
("47FBF8CAD62BB95AF3AD9509E5C2175D", "63120FB321B0410F216D6DC2D8641A11", "Philips 2013"),
|
||||
("1B569AA7D2E4CCE66584A7A3D8A45679", "A0E88D5D52A813260D3A34A14AA89416", "TPV_MTK2K17PLF_EU0"),
|
||||
("135AFB6DE91CD56496244BC7C0E08D63", "C6A38C89F0AF5637EB6E19D35E12E257", "TPV_MTK2K14PLF_EU1"),
|
||||
];
|
||||
+10
-5
@@ -10,7 +10,7 @@ use std::fs::{self, File};
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
input_target: String,
|
||||
output_folder: String,
|
||||
output_folder: Option<String>,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -19,8 +19,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let target_path = args.input_target;
|
||||
println!("Input target: {}", target_path);
|
||||
let path = PathBuf::from(target_path);
|
||||
|
||||
let output_path = args.output_folder;
|
||||
|
||||
let output_path = if args.output_folder.is_some() {
|
||||
args.output_folder.unwrap()
|
||||
} else {
|
||||
format!("_{}", path.file_name().and_then(|s| s.to_str()).unwrap())
|
||||
};
|
||||
println!("Output folder: {}\n", output_path);
|
||||
|
||||
let output_folder_path = PathBuf::from(&output_path);
|
||||
@@ -28,14 +34,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if output_folder_path.is_dir() {
|
||||
let is_empty = fs::read_dir(&output_folder_path)?.next().is_none();
|
||||
if !is_empty {
|
||||
println!("Warning: Output folder exists and is NOT empty! Files may be overwritten!");
|
||||
println!("Warning: Output folder already exists and is NOT empty! Files may be overwritten!");
|
||||
println!("Press Enter if you want to continue...");
|
||||
io::stdin().read_line(&mut String::new())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let path = PathBuf::from(target_path);
|
||||
|
||||
if path.is_dir() {
|
||||
if formats::samsung_old::is_samsung_old_dir(&path) {
|
||||
println!("Samsung old firmware dir detected!\n");
|
||||
|
||||
Reference in New Issue
Block a user