fix: ncm decryption
This commit is contained in:
parent
493ed8bdb5
commit
abd4549da3
@ -1,20 +1,24 @@
|
|||||||
use crate::{content_key, NetEaseCryptoError as Error};
|
use crate::{content_key, metadata, NetEaseCryptoError as Error};
|
||||||
use byteorder::{ByteOrder, LE};
|
use byteorder::{ByteOrder, LE};
|
||||||
|
|
||||||
const CRC32: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
|
const CRC32: crc::Crc<u32> = crc::Crc::<u32>::new(&crc::CRC_32_ISO_HDLC);
|
||||||
|
|
||||||
struct NCMFile {
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
|
pub struct NCMFile {
|
||||||
|
audio_rc4_key_stream: [u8; 256],
|
||||||
|
|
||||||
|
/// NCM File format version, 0x01
|
||||||
pub ncm_version: u8,
|
pub ncm_version: u8,
|
||||||
|
/// CloudMusic client version (guess)
|
||||||
pub client_version: u8,
|
pub client_version: u8,
|
||||||
/// Encrypted content key
|
/// Encrypted content key.
|
||||||
pub content_key: Vec<u8>,
|
pub content_key: Vec<u8>,
|
||||||
/// Encrypted metadata.
|
/// Encrypted metadata.
|
||||||
pub metadata: Vec<u8>,
|
pub metadata: Vec<u8>,
|
||||||
/// Cover image 1
|
/// Cover image 1, usually jpg.
|
||||||
pub image1: Option<Vec<u8>>,
|
pub image1: Option<Vec<u8>>,
|
||||||
/// Cover image 2, format unknown
|
/// Cover image 2, format unknown
|
||||||
pub image2: Option<Vec<u8>>,
|
pub image2: Option<Vec<u8>>,
|
||||||
audio_rc4_key_stream: [u8; 256],
|
|
||||||
pub audio_data_offset: usize,
|
pub audio_data_offset: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,6 +38,7 @@ fn build_audio_rc4_key_stream(enciphered_content_key: &[u8]) -> Result<[u8; 256]
|
|||||||
|
|
||||||
let mut key = [0u8; 256];
|
let mut key = [0u8; 256];
|
||||||
for (i, k) in key.iter_mut().enumerate() {
|
for (i, k) in key.iter_mut().enumerate() {
|
||||||
|
let i = (i + 1) & 0xff;
|
||||||
let j = s[i].wrapping_add(i as u8);
|
let j = s[i].wrapping_add(i as u8);
|
||||||
let idx = s[i].wrapping_add(s[j as usize]);
|
let idx = s[i].wrapping_add(s[j as usize]);
|
||||||
*k = s[idx as usize];
|
*k = s[idx as usize];
|
||||||
@ -129,35 +134,66 @@ impl NCMFile {
|
|||||||
audio_data_offset: offset,
|
audio_data_offset: offset,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_metadata(&self) -> Result<Vec<u8>, Error> {
|
||||||
|
metadata::decrypt(&self.metadata)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Decrypt audio data.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `data`: The data to decrypt
|
||||||
|
/// * `offset`: Offset of the data in file, subtract `self.audio_data_offset`
|
||||||
|
///
|
||||||
|
/// returns: Result<(), NetEaseCryptoError>
|
||||||
|
pub fn decrypt<T>(&self, data: &mut T, offset: usize) -> Result<(), Error>
|
||||||
|
where
|
||||||
|
T: AsMut<[u8]>,
|
||||||
|
{
|
||||||
|
let key_stream = self.audio_rc4_key_stream.iter().cycle().skip(offset & 0xff);
|
||||||
|
for (datum, &key) in data.as_mut().iter_mut().zip(key_stream) {
|
||||||
|
*datum ^= key;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_load_ncm() -> Result<(), Error> {
|
fn test_load_ncm() -> Result<(), Error> {
|
||||||
let ncm_header = include_bytes!("__fixture__/ncm_test1.bin");
|
let ncm_header = include_bytes!("__fixture__/ncm_test1.bin");
|
||||||
let ncm = NCMFile::new(ncm_header)?;
|
let ncm = NCMFile::new(ncm_header)?;
|
||||||
let sbox = [
|
let key_stream = [
|
||||||
0x08, 0x67, 0x20, 0xF0, 0x5C, 0xAC, 0xAF, 0x1B, 0x74, 0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B1, 0xE1,
|
||||||
0xE1, 0xA0, 0xA5, 0xA0, 0xDD, 0x15, 0x66, 0xBB, 0x4F, 0x7E, 0xBC, 0x99, 0x76, 0x1A, 0xD2,
|
0xA0, 0xA5, 0xA0, 0xDD, 0x15, 0x66, 0xBB, 0x4F, 0x7E, 0xBC, 0x99, 0x76, 0x1A, 0xD2, 0xEF,
|
||||||
0xEF, 0xBE, 0xD9, 0x79, 0xA0, 0x33, 0xD5, 0x96, 0x6C, 0xD9, 0xEF, 0x42, 0x7E, 0x11, 0x45,
|
0xBE, 0xD9, 0x79, 0xA0, 0x33, 0xD5, 0x96, 0x6C, 0xD9, 0xEF, 0x42, 0x7E, 0x11, 0x45, 0x1F,
|
||||||
0x1F, 0x99, 0x96, 0x1A, 0x90, 0x0C, 0x75, 0xBC, 0x01, 0xE2, 0xC8, 0xEF, 0x16, 0x98, 0x9A,
|
0x99, 0x96, 0x1A, 0x90, 0x0C, 0x75, 0xBC, 0x01, 0xE2, 0xC8, 0xEF, 0x16, 0x98, 0x9A, 0x09,
|
||||||
0x09, 0xEB, 0xD8, 0xA1, 0xEA, 0x62, 0xE7, 0x92, 0x20, 0x8F, 0x6F, 0x72, 0xDE, 0x14, 0xFE,
|
0xEB, 0xD8, 0xA1, 0xEA, 0x62, 0xE7, 0x92, 0x20, 0x8F, 0x6F, 0x72, 0xDE, 0x14, 0xFE, 0xBF,
|
||||||
0xBF, 0xCD, 0xBA, 0x97, 0xDE, 0xA3, 0x3B, 0x67, 0xD4, 0x0B, 0xF4, 0xD3, 0x97, 0x25, 0xBA,
|
0xCD, 0xBA, 0x97, 0xDE, 0xA3, 0x3B, 0x67, 0xD4, 0x0B, 0xF4, 0xD3, 0x97, 0x25, 0xBA, 0xCA,
|
||||||
0xCA,
|
0x08,
|
||||||
];
|
];
|
||||||
assert_eq!(ncm.audio_rc4_key_stream, sbox);
|
assert_eq!(ncm.audio_rc4_key_stream, key_stream);
|
||||||
assert_eq!(ncm.image1, Some(b"img#1".to_vec()));
|
assert_eq!(ncm.image1, Some(b"img#1".to_vec()));
|
||||||
assert_eq!(ncm.image2, Some(b"IMAGE#2".to_vec()));
|
assert_eq!(ncm.image2, Some(b"IMAGE#2".to_vec()));
|
||||||
|
|
||||||
|
let mut audio_data = ncm_header[ncm.audio_data_offset..].to_vec();
|
||||||
|
ncm.decrypt(&mut audio_data, 0)?;
|
||||||
|
let actual = vec![
|
||||||
|
0x49, 0x44, 0x33, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x73, 0x54, 0x50, 0x45, 0x31, 0x00,
|
||||||
|
];
|
||||||
|
assert_eq!(audio_data, actual);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -34,4 +34,6 @@ pub enum NetEaseCryptoError {
|
|||||||
MetadataDecryptError(UnpadError),
|
MetadataDecryptError(UnpadError),
|
||||||
#[error("Metadata: Decode metadata failed: {0}")]
|
#[error("Metadata: Decode metadata failed: {0}")]
|
||||||
MetadataDecodeError(base64::DecodeError),
|
MetadataDecodeError(base64::DecodeError),
|
||||||
|
#[error("Metadata: Invalid prefix on final json: {0}")]
|
||||||
|
MetadataInvalidJsonPrefix(String),
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,12 @@ where
|
|||||||
let metadata = aes
|
let metadata = aes
|
||||||
.decrypt_padded::<Pkcs7>(&mut data[..])
|
.decrypt_padded::<Pkcs7>(&mut data[..])
|
||||||
.map_err(NetEaseCryptoError::MetadataDecryptError)?;
|
.map_err(NetEaseCryptoError::MetadataDecryptError)?;
|
||||||
|
let metadata = match metadata.strip_prefix(b"music:") {
|
||||||
|
None => Err(NetEaseCryptoError::MetadataInvalidJsonPrefix(
|
||||||
|
String::from_utf8_lossy(metadata).to_string(),
|
||||||
|
))?,
|
||||||
|
Some(stripped) => stripped,
|
||||||
|
};
|
||||||
Ok(Vec::from(metadata))
|
Ok(Vec::from(metadata))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,6 +106,6 @@ fn test_decrypt_metadata() -> Result<(), NetEaseCryptoError> {
|
|||||||
0x25, 0x1B, 0x37, 0x48, 0x57, 0x53, 0x2A, 0x22, 0x3A, 0x00, 0x34, 0x53, 0x24, 0x12,
|
0x25, 0x1B, 0x37, 0x48, 0x57, 0x53, 0x2A, 0x22, 0x3A, 0x00, 0x34, 0x53, 0x24, 0x12,
|
||||||
];
|
];
|
||||||
let key = decrypt(enciphered_metadata)?;
|
let key = decrypt(enciphered_metadata)?;
|
||||||
assert_eq!(&key[..6], b"music:");
|
assert_eq!(key[0], b'{');
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user