Implement nom-based Bc message parser
This commit is contained in:
1 parent
a166f72b92
commit
b4462fd5f6
8 files changed
+178
-15
No files matched your search
+16
-7
@@ -9,19 +9,28 @@ use yaserde::{YaDeserialize, YaSerialize};
|
||||
#[allow(pub_use_of_private_extern_crate)]
|
||||
use yaserde::log;
|
||||
|
||||
#[cfg(test)]
|
||||
use indoc::indoc;
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename="body")]
|
||||
struct Body {
|
||||
pub struct Body {
|
||||
#[yaserde(rename="Encryption")]
|
||||
encryption: Option<Encryption>,
|
||||
pub encryption: Option<Encryption>,
|
||||
#[yaserde(rename="LoginUser")]
|
||||
login_user: Option<LoginUser>,
|
||||
pub login_user: Option<LoginUser>,
|
||||
#[yaserde(rename="LoginNet")]
|
||||
login_net: Option<LoginNet>,
|
||||
pub login_net: Option<LoginNet>,
|
||||
}
|
||||
|
||||
impl Body {
|
||||
pub fn try_parse(s: impl Read) -> Result<Self, String> {
|
||||
yaserde::de::from_reader(s)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
struct Encryption {
|
||||
pub struct Encryption {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
#[yaserde(rename="type")]
|
||||
@@ -31,7 +40,7 @@ struct Encryption {
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename_all="camelCase")]
|
||||
struct LoginUser {
|
||||
pub struct LoginUser {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
#[yaserde(rename="userName")]
|
||||
@@ -43,7 +52,7 @@ struct LoginUser {
|
||||
|
||||
#[derive(PartialEq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename_all="camelCase")]
|
||||
struct LoginNet {
|
||||
pub struct LoginNet {
|
||||
#[yaserde(attribute)]
|
||||
pub version: String,
|
||||
#[yaserde(rename="type")]
|
||||
|
||||
+1
-2
@@ -1,6 +1,5 @@
|
||||
#[allow(dead_code)] // while I'm still fleshing this out
|
||||
|
||||
mod model;
|
||||
mod bc_xml;
|
||||
mod xml_crypto;
|
||||
|
||||
pub use model::Bc;
|
||||
+138
-4
@@ -1,11 +1,145 @@
|
||||
pub enum Bc {
|
||||
use nom::{IResult, error::ParseError};
|
||||
use nom::{bytes::complete::take, number::complete::*, combinator::*, sequence::*};
|
||||
use super::bc_xml::Body;
|
||||
use super::xml_crypto;
|
||||
|
||||
const MAGIC_HEADER: u32 = 0xabcdef0;
|
||||
|
||||
const MSG_ID_LOGIN: u32 = 1;
|
||||
const MSG_ID_VIDEO: u32 = 3;
|
||||
|
||||
pub struct Bc {
|
||||
header: BcHeader,
|
||||
body: BcBody,
|
||||
}
|
||||
|
||||
pub enum BcBody {
|
||||
LegacyMsg(LegacyMsg),
|
||||
ModernMsg(ModernMsg),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
pub struct ModernMsg {
|
||||
|
||||
xml: Option<Body>,
|
||||
binary: Option<Vec<u8>>,
|
||||
}
|
||||
|
||||
pub enum LegacyMsg {}
|
||||
pub enum LegacyMsg {
|
||||
LoginMsg {
|
||||
username: String,
|
||||
password: String,
|
||||
}
|
||||
}
|
||||
|
||||
struct BcHeader {
|
||||
body_len: u32,
|
||||
msg_id: u32,
|
||||
enc_offset: u32,
|
||||
encrypted: bool,
|
||||
class: u16,
|
||||
bin_offset: Option<u32>,
|
||||
}
|
||||
|
||||
impl BcHeader {
|
||||
pub fn is_modern(&self) -> bool {
|
||||
// Most modern messages have an extra word at the end of the header; this
|
||||
// serves as the start offset of the appended binary data, if any.
|
||||
// A notable exception is the encrypted reply to the login message;
|
||||
// in this case the message is modern (with XML encryption etc), but there is
|
||||
// no extra word.
|
||||
// Here are the message classes:
|
||||
// 0x6514: legacy, no bin offset (initial login message, encrypted or not)
|
||||
// 0x6614: modern, no bin offset (reply to encrypted 0x6514 login)
|
||||
// 0x6414: modern, has bin offset, always encrypted (re-sent login message)
|
||||
// 0x0000, modern, has bin offset (most modern messages)
|
||||
self.class != 0x6514
|
||||
}
|
||||
|
||||
pub fn is_encrypted(&self) -> bool {
|
||||
self.encrypted || self.class == 0x6414
|
||||
}
|
||||
}
|
||||
|
||||
fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> {
|
||||
let (buf, header) = bc_header(buf)?;
|
||||
let (buf, msg) = bc_modern_msg(&header, buf)?;
|
||||
|
||||
let bc = Bc {
|
||||
header,
|
||||
body: BcBody::ModernMsg(msg),
|
||||
};
|
||||
|
||||
Ok((buf, bc))
|
||||
}
|
||||
|
||||
fn bc_modern_msg<'a, 'b, E: ParseError<&'b [u8]>>(header: &'a BcHeader, buf: &'b [u8]) -> IResult<&'b [u8], ModernMsg, E> {
|
||||
let end_of_xml = header.bin_offset.unwrap_or(buf.len() as u32);
|
||||
|
||||
let (mut buf, body_buf) = take(end_of_xml)(buf)?;
|
||||
|
||||
let decrypted;
|
||||
let processed_body_buf = if !header.is_encrypted() { buf } else {
|
||||
decrypted = xml_crypto::crypt(header.enc_offset, body_buf);
|
||||
&decrypted
|
||||
};
|
||||
|
||||
// Apply the function, but throw away the reference to decrypted in the Ok and Err case
|
||||
// This error-error-error thing is the same idiom Nom uses internally.
|
||||
use nom::{Err, error::{make_error, ErrorKind}};
|
||||
let body = Body::try_parse(processed_body_buf)
|
||||
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
|
||||
|
||||
// Extract remainder of message as binary, if it exists
|
||||
let mut binary = None;
|
||||
if let Some(bin_offset) = header.bin_offset {
|
||||
let (buf_after, payload) = map(take(header.body_len - bin_offset), |x: &[u8]| x.to_vec())(buf)?;
|
||||
binary = Some(payload);
|
||||
buf = buf_after;
|
||||
}
|
||||
|
||||
let msg = ModernMsg {
|
||||
xml: Some(body),
|
||||
binary,
|
||||
};
|
||||
|
||||
Ok((buf, msg))
|
||||
}
|
||||
|
||||
fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
|
||||
let (buf, _magic) = verify(le_u32, |x| *x == MAGIC_HEADER)(buf)?;
|
||||
let (buf, msg_id) = le_u32(buf)?;
|
||||
let (buf, body_len) = le_u32(buf)?;
|
||||
let (buf, enc_offset) = le_u32(buf)?;
|
||||
let (buf, (encrypted, _ignored, class)) = tuple((le_u8, le_u8, le_u16))(buf)?;
|
||||
let encrypted = encrypted != 0;
|
||||
|
||||
// See BcHeader::is_modern() for a description of which packets have the bin offset
|
||||
let has_bin_offset = class == 0x6414 || class == 0x0000;
|
||||
let (buf, bin_offset) = cond(has_bin_offset, le_u32)(buf)?;
|
||||
|
||||
Ok((buf, BcHeader {
|
||||
msg_id,
|
||||
body_len,
|
||||
enc_offset,
|
||||
encrypted,
|
||||
class,
|
||||
bin_offset,
|
||||
}))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bc_modern_login() {
|
||||
let sample = include_bytes!("samples/model_sample_modern_login.bin");
|
||||
|
||||
let (_, msg) = bc_msg(&sample[..]).unwrap();
|
||||
assert_eq!(msg.header.msg_id, 1);
|
||||
assert_eq!(msg.header.body_len, 145);
|
||||
assert_eq!(msg.header.enc_offset, 0x1000000);
|
||||
assert_eq!(msg.header.encrypted, true);
|
||||
assert_eq!(msg.header.class, 0x6614);
|
||||
match msg.body {
|
||||
BcBody::ModernMsg(ModernMsg{ xml: Some(xml), binary: None }) => {
|
||||
assert_eq!(xml.encryption.unwrap().nonce, "9E6D1FCB9E69846D")
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<Encryption version="1.1">
|
||||
<type>md5</type>
|
||||
<nonce>9E6D1FCB9E69846D</nonce>
|
||||
</Encryption>
|
||||
</body>
|
||||
@@ -0,0 +1,15 @@
|
||||
const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF];
|
||||
|
||||
pub fn crypt(offset: u32, buf: &[u8]) -> Vec<u8> {
|
||||
let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8);
|
||||
key_iter.zip(buf).map(|(key, i)| *i ^ key ^ (offset as u8)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_xml_crypto() {
|
||||
let sample = include_bytes!("samples/xml_crypto_sample1.bin");
|
||||
let should_be = include_bytes!("samples/xml_crypto_sample1_plaintext.bin");
|
||||
|
||||
let decrypted = crypt(0, &sample[..]);
|
||||
assert_eq!(decrypted, &should_be[..]);
|
||||
}
|
||||
+1
-2
@@ -1,5 +1,4 @@
|
||||
#[cfg(test)] #[macro_use]
|
||||
extern crate indoc;
|
||||
#![allow(dead_code)] // while I'm still fleshing this out
|
||||
|
||||
mod bc;
|
||||
|
||||
|
||||
Reference in new issue
Block a user