Implement stateful packet parsing based on binaryData
This commit is contained in:
1 parent
de0e5e6814
commit
68d1c5b70f
8 files changed
+174
-51
No files matched your search
+4
-3
@@ -8,11 +8,12 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
cookie-factory = "0.3.1"
|
||||
env_logger = "*"
|
||||
err-derive = "0.2.4"
|
||||
log = "0.4.8"
|
||||
log = { version = "0.4.8", features = ["release_max_level_warn"] }
|
||||
indoc = "0.3.5"
|
||||
md5 = "0.7.0"
|
||||
nom = "5.1.1"
|
||||
yaserde = "0.3.15"
|
||||
yaserde_derive = "0.3.15"
|
||||
yaserde = { git = "https://github.com/media-io/yaserde.git" }
|
||||
yaserde_derive = { git = "https://github.com/media-io/yaserde.git" }
|
||||
xml-rs = "0.8.2"
|
||||
+93
-36
@@ -1,9 +1,10 @@
|
||||
use err_derive::Error;
|
||||
use log::error;
|
||||
use nom::IResult;
|
||||
use nom::{bytes::streaming::take, number::streaming::*, combinator::*, sequence::*};
|
||||
use std::io::Read;
|
||||
use super::model::*;
|
||||
use super::xml::BcXml;
|
||||
use super::xml::{Extension, AllTopXmls};
|
||||
use super::xml_crypto;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -28,16 +29,16 @@ impl<'a> From<nom::Err<NomErrorTuple<'a>>> for Error {
|
||||
}
|
||||
|
||||
impl Bc {
|
||||
pub fn deserialize<R: Read>(r: R) -> Result<Bc, Error> {
|
||||
pub fn deserialize<R: Read>(context: &mut BcContext, r: R) -> Result<Bc, Error> {
|
||||
// Throw away the nom-specific return types
|
||||
read_from_reader(bc_msg, r)
|
||||
read_from_reader(|reader| bc_msg(context, reader), r)
|
||||
}
|
||||
}
|
||||
|
||||
fn read_from_reader<P, O, E, R>(parser: P, mut rdr: R) -> Result<O, E>
|
||||
fn read_from_reader<P, O, E, R>(mut parser: P, mut rdr: R) -> Result<O, E>
|
||||
where R: Read,
|
||||
E: for<'a> From<nom::Err<NomErrorTuple<'a>>> + From<std::io::Error>,
|
||||
P: Fn(&[u8]) -> nom::IResult<&[u8], O>,
|
||||
P: FnMut(&[u8]) -> nom::IResult<&[u8], O>,
|
||||
{
|
||||
let mut input: Vec<u8> = Vec::new();
|
||||
loop {
|
||||
@@ -56,9 +57,9 @@ fn read_from_reader<P, O, E, R>(parser: P, mut rdr: R) -> Result<O, E>
|
||||
}
|
||||
}
|
||||
|
||||
fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> {
|
||||
fn bc_msg<'a, 'b>(context: &'a mut BcContext, buf: &'b [u8]) -> IResult<&'b [u8], Bc> {
|
||||
let (buf, header) = bc_header(buf)?;
|
||||
let (buf, body) = bc_body(&header, buf)?;
|
||||
let (buf, body) = bc_body(context, &header, buf)?;
|
||||
|
||||
let bc = Bc {
|
||||
meta: header.to_meta(),
|
||||
@@ -68,11 +69,11 @@ fn bc_msg(buf: &[u8]) -> IResult<&[u8], Bc> {
|
||||
Ok((buf, bc))
|
||||
}
|
||||
|
||||
fn bc_body<'a, 'b>(header: &'a BcHeader, buf: &'b [u8])
|
||||
fn bc_body<'a, 'b, 'c>(context: &'c mut BcContext, header: &'a BcHeader, buf: &'b [u8])
|
||||
-> IResult<&'b [u8], BcBody>
|
||||
{
|
||||
if header.is_modern() {
|
||||
let (buf, body) = bc_modern_msg(header, buf)?;
|
||||
let (buf, body) = bc_modern_msg(context, header, buf)?;
|
||||
Ok((buf, BcBody::ModernMsg(body)))
|
||||
} else {
|
||||
let (buf, body) = match header.msg_id {
|
||||
@@ -99,12 +100,20 @@ fn bc_legacy_login_msg<'a, 'b>(buf: &'b [u8])
|
||||
}))
|
||||
}
|
||||
|
||||
fn bc_modern_msg<'a, 'b>(header: &'a BcHeader, buf: &'b [u8])
|
||||
fn bc_modern_msg<'a, 'b, 'c>(context: &mut BcContext, header: &'a BcHeader, buf: &'b [u8])
|
||||
-> IResult<&'b [u8], ModernMsg>
|
||||
{
|
||||
let end_of_xml = match header.bin_offset {
|
||||
Some(off) if off > 0 => off,
|
||||
_ => header.body_len
|
||||
use nom::{Err, error::{make_error, ErrorKind}};
|
||||
|
||||
let mut in_bin_mode = context.in_bin_mode.contains(&header.msg_id);
|
||||
|
||||
// We'd like to know where the XML stops, but we haven't parsed the XML yet to see if the
|
||||
// binaryData offset in the header is valid
|
||||
let end_of_xml = if in_bin_mode { 0 } else {
|
||||
match header.bin_offset {
|
||||
Some(off) if off > 0 => off,
|
||||
_ => header.body_len
|
||||
}
|
||||
};
|
||||
|
||||
let (mut buf, body_buf) = take(end_of_xml)(buf)?;
|
||||
@@ -115,31 +124,48 @@ fn bc_modern_msg<'a, 'b>(header: &'a BcHeader, buf: &'b [u8])
|
||||
&decrypted
|
||||
};
|
||||
|
||||
// Extract remainder of message as binary, if it exists
|
||||
// Now we'll take the buffer that Nom gave a ref to and parse it.
|
||||
let mut xml = None;
|
||||
let mut binary = None;
|
||||
if let Some(bin_offset) = header.bin_offset {
|
||||
if bin_offset > 0 {
|
||||
let (buf_after, payload) = map(take(header.body_len - bin_offset),
|
||||
|x: &[u8]| x.to_vec())(buf)?;
|
||||
binary = Some(payload);
|
||||
buf = buf_after;
|
||||
if end_of_xml > 0 {
|
||||
// Apply the XML parse 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.
|
||||
let parsed = AllTopXmls::try_parse(processed_body_buf)
|
||||
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
|
||||
|
||||
// If this is the first message containing binary, the Extension message puts the message
|
||||
// ID into binary mode, then the first binary is sent after the XML.
|
||||
match parsed {
|
||||
AllTopXmls::BcXml(x) => { xml = Some(x); }
|
||||
AllTopXmls::Extension(Extension { binary_data: _ }) => {
|
||||
in_bin_mode = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now we'll take the buffer that Nom gave a ref to and parse it. Do this last, so we don't
|
||||
// parse over and over if we get Incompletes on the binary part. Apply the XML parse 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 xml = if end_of_xml > 0 {
|
||||
let parsed = BcXml::try_parse(processed_body_buf)
|
||||
.map_err(|_| Err::Error(make_error(buf, ErrorKind::MapRes)))?;
|
||||
Some(parsed)
|
||||
} else { None };
|
||||
// If we are in binary mode, extract it
|
||||
if in_bin_mode {
|
||||
if let Some(bin_offset) = header.bin_offset {
|
||||
// Extract remainder of message as binary, if it exists
|
||||
let (buf_after, payload) = map(take(header.body_len - bin_offset),
|
||||
|x: &[u8]| x.to_vec())(buf)?;
|
||||
|
||||
let msg = ModernMsg { xml, binary, };
|
||||
// Since the parser operates in streaming mode, must wait until after we successfully
|
||||
// receive enough bytes before modifying the context (otherwise we'll alter the
|
||||
// behavior of future passes of this function even if we didn't yet consume the
|
||||
// message).
|
||||
context.in_bin_mode.insert(header.msg_id);
|
||||
|
||||
Ok((buf, msg))
|
||||
binary = Some(payload);
|
||||
buf = buf_after;
|
||||
} else {
|
||||
// Seriously, Nom, what even is this
|
||||
error!("Expected header to contain a binary offset, but there was none");
|
||||
return Err(Err::Error(make_error(buf, ErrorKind::Verify)));
|
||||
}
|
||||
}
|
||||
|
||||
Ok((buf, ModernMsg { xml, binary }))
|
||||
}
|
||||
|
||||
fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
|
||||
@@ -170,8 +196,10 @@ fn bc_header(buf: &[u8]) -> IResult<&[u8], BcHeader> {
|
||||
fn test_bc_modern_login() {
|
||||
let sample = include_bytes!("samples/model_sample_modern_login.bin");
|
||||
|
||||
let mut context = BcContext::new();
|
||||
|
||||
let (buf, header) = bc_header(&sample[..]).unwrap();
|
||||
let (_, body) = bc_body(&header, buf).unwrap();
|
||||
let (_, body) = bc_body(&mut context, &header, buf).unwrap();
|
||||
assert_eq!(header.msg_id, 1);
|
||||
assert_eq!(header.body_len, 145);
|
||||
assert_eq!(header.enc_offset, 0x1000000);
|
||||
@@ -189,8 +217,10 @@ fn test_bc_modern_login() {
|
||||
fn test_bc_legacy_login() {
|
||||
let sample = include_bytes!("samples/model_sample_legacy_login.bin");
|
||||
|
||||
let mut context = BcContext::new();
|
||||
|
||||
let (buf, header) = bc_header(&sample[..]).unwrap();
|
||||
let (_, body) = bc_body(&header, buf).unwrap();
|
||||
let (_, body) = bc_body(&mut context, &header, buf).unwrap();
|
||||
assert_eq!(header.msg_id, 1);
|
||||
assert_eq!(header.body_len, 1836);
|
||||
assert_eq!(header.enc_offset, 0x1000000);
|
||||
@@ -211,8 +241,10 @@ fn test_bc_legacy_login() {
|
||||
fn test_bc_modern_login_failed() {
|
||||
let sample = include_bytes!("samples/modern_login_failed.bin");
|
||||
|
||||
let mut context = BcContext::new();
|
||||
|
||||
let (buf, header) = bc_header(&sample[..]).unwrap();
|
||||
let (_, body) = bc_body(&header, buf).unwrap();
|
||||
let (_, body) = bc_body(&mut context, &header, buf).unwrap();
|
||||
assert_eq!(header.msg_id, 1);
|
||||
assert_eq!(header.body_len, 0);
|
||||
assert_eq!(header.enc_offset, 0x0);
|
||||
@@ -230,8 +262,10 @@ fn test_bc_modern_login_failed() {
|
||||
fn test_bc_modern_login_success() {
|
||||
let sample = include_bytes!("samples/modern_login_success.bin");
|
||||
|
||||
let mut context = BcContext::new();
|
||||
|
||||
let (buf, header) = bc_header(&sample[..]).unwrap();
|
||||
let (_, body) = bc_body(&header, buf).unwrap();
|
||||
let (_, body) = bc_body(&mut context, &header, buf).unwrap();
|
||||
assert_eq!(header.msg_id, 1);
|
||||
assert_eq!(header.body_len, 2949);
|
||||
assert_eq!(header.enc_offset, 0x0);
|
||||
@@ -245,3 +279,26 @@ fn test_bc_modern_login_success() {
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bc_binary_mode() {
|
||||
let sample1 = include_bytes!("samples/modern_video_start1.bin");
|
||||
let sample2 = include_bytes!("samples/modern_video_start2.bin");
|
||||
|
||||
let mut context = BcContext::new();
|
||||
|
||||
let msg1 = Bc::deserialize(&mut context, &sample1[..]).unwrap();
|
||||
let msg2 = Bc::deserialize(&mut context, &sample2[..]).unwrap();
|
||||
match msg1.body {
|
||||
BcBody::ModernMsg(ModernMsg{ xml: None, binary: Some(bin) }) => {
|
||||
assert_eq!(bin.len(), 32);
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
match msg2.body {
|
||||
BcBody::ModernMsg(ModernMsg{ xml: None, binary: Some(bin) }) => {
|
||||
assert_eq!(bin.len(), 30344);
|
||||
}
|
||||
_ => assert!(false),
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::collections::HashSet;
|
||||
use super::xml::BcXml;
|
||||
|
||||
pub(super) const MAGIC_HEADER: u32 = 0xabcdef0;
|
||||
@@ -62,6 +63,19 @@ pub(super) struct BcSendInfo {
|
||||
pub bin_offset: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BcContext {
|
||||
pub(super) in_bin_mode: HashSet<u32>,
|
||||
}
|
||||
|
||||
impl BcContext {
|
||||
pub fn new() -> BcContext {
|
||||
BcContext {
|
||||
in_bin_mode: HashSet::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BcHeader {
|
||||
pub fn is_modern(&self) -> bool {
|
||||
// Most modern messages have an extra word at the end of the header; this
|
||||
|
||||
Binary file not shown.
Binary file not shown.
+8
-4
@@ -133,24 +133,28 @@ fn do_nothing<W>() -> impl SerializeFn<W> {
|
||||
|
||||
#[test]
|
||||
fn test_legacy_login_roundtrip() {
|
||||
let mut context = BcContext::new();
|
||||
|
||||
// I don't want to make up a sample message; just load it
|
||||
let sample = include_bytes!("samples/model_sample_legacy_login.bin");
|
||||
let msg = Bc::deserialize::<&[u8]>(&sample[..]).unwrap();
|
||||
let msg = Bc::deserialize::<&[u8]>(&mut context, &sample[..]).unwrap();
|
||||
|
||||
let ser_buf = msg.serialize(vec!()).unwrap();
|
||||
let msg2 = Bc::deserialize::<&[u8]>(ser_buf.as_ref()).unwrap();
|
||||
let msg2 = Bc::deserialize::<&[u8]>(&mut context, ser_buf.as_ref()).unwrap();
|
||||
assert_eq!(msg, msg2);
|
||||
assert_eq!(&sample[..], ser_buf.as_slice());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_modern_login_roundtrip() {
|
||||
let mut context = BcContext::new();
|
||||
|
||||
// I don't want to make up a sample message; just load it
|
||||
let sample = include_bytes!("samples/model_sample_modern_login.bin");
|
||||
|
||||
let msg = Bc::deserialize::<&[u8]>(&sample[..]).unwrap();
|
||||
let msg = Bc::deserialize::<&[u8]>(&mut context, &sample[..]).unwrap();
|
||||
|
||||
let ser_buf = msg.serialize(vec!()).unwrap();
|
||||
let msg2 = Bc::deserialize::<&[u8]>(ser_buf.as_ref()).unwrap();
|
||||
let msg2 = Bc::deserialize::<&[u8]>(&mut context, ser_buf.as_ref()).unwrap();
|
||||
assert_eq!(msg, msg2);
|
||||
}
|
||||
+51
-6
@@ -5,15 +5,27 @@ use std::io::{Read, Write};
|
||||
// YaSerde is currently naming the traits and the derive macros identically
|
||||
use yaserde_derive::{YaDeserialize, YaSerialize};
|
||||
use yaserde::{ser::Config, YaDeserialize, YaSerialize};
|
||||
// YaSerde currently needs this imported
|
||||
#[allow(pub_use_of_private_extern_crate)]
|
||||
use yaserde::log;
|
||||
|
||||
#[cfg(test)]
|
||||
use indoc::indoc;
|
||||
|
||||
#[derive(PartialEq, Eq, Debug, YaDeserialize)]
|
||||
#[yaserde(flatten)]
|
||||
pub(super) enum AllTopXmls {
|
||||
#[yaserde(rename="body")]
|
||||
BcXml(BcXml),
|
||||
Extension(Extension),
|
||||
}
|
||||
|
||||
// Required for YaDeserialize
|
||||
impl Default for AllTopXmls {
|
||||
fn default() -> Self {
|
||||
AllTopXmls::BcXml(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
#[yaserde(rename="body")]
|
||||
#[yaserde(root="body")]
|
||||
pub struct BcXml {
|
||||
#[yaserde(rename="Encryption")]
|
||||
pub encryption: Option<Encryption>,
|
||||
@@ -25,6 +37,12 @@ pub struct BcXml {
|
||||
pub device_info: Option<DeviceInfo>,
|
||||
}
|
||||
|
||||
impl AllTopXmls {
|
||||
pub fn try_parse(s: impl Read) -> Result<Self, String> {
|
||||
yaserde::de::from_reader(s)
|
||||
}
|
||||
}
|
||||
|
||||
impl BcXml {
|
||||
pub fn try_parse(s: impl Read) -> Result<Self, String> {
|
||||
yaserde::de::from_reader(s)
|
||||
@@ -87,6 +105,11 @@ pub struct Resolution {
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Default, Debug, YaDeserialize, YaSerialize)]
|
||||
pub struct Extension {
|
||||
#[yaserde(rename="binaryData")]
|
||||
pub binary_data: u32,
|
||||
}
|
||||
|
||||
pub fn xml_ver() -> String {
|
||||
"1.1".to_string()
|
||||
@@ -103,11 +126,17 @@ fn test_encryption_deser() {
|
||||
</Encryption>
|
||||
</body>"#);
|
||||
let b: BcXml = yaserde::de::from_str(sample).unwrap();
|
||||
let enc = b.encryption.unwrap();
|
||||
let enc = b.encryption.as_ref().unwrap();
|
||||
|
||||
assert_eq!(enc.version, "1.1");
|
||||
assert_eq!(enc.nonce, "9E6D1FCB9E69846D");
|
||||
assert_eq!(enc.type_, "md5");
|
||||
|
||||
let t = AllTopXmls::try_parse(sample.as_bytes()).unwrap();
|
||||
match t {
|
||||
AllTopXmls::BcXml(top_b) if top_b == b => assert!(true),
|
||||
_ => assert!(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -204,7 +233,6 @@ fn test_deviceinfo_partial_deser() {
|
||||
|
||||
// Needs to ignore all the other crap that we don't care about
|
||||
let b = BcXml::try_parse(sample.as_bytes()).unwrap();
|
||||
println!("{:?}", b);
|
||||
match b {
|
||||
BcXml {
|
||||
device_info: Some(DeviceInfo {
|
||||
@@ -218,3 +246,20 @@ fn test_deviceinfo_partial_deser() {
|
||||
_ => assert!(false)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_binary_deser() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
|
||||
let sample = indoc!(r#"
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<binaryData>1</binaryData>
|
||||
</Extension>
|
||||
"#);
|
||||
let b = AllTopXmls::try_parse(sample.as_bytes()).unwrap();
|
||||
match b {
|
||||
AllTopXmls::Extension(Extension { binary_data: 1 }) => assert!(true),
|
||||
_ => assert!(false)
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -9,6 +9,7 @@ use Md5Trunc::*;
|
||||
pub struct BcCamera {
|
||||
address: SocketAddr,
|
||||
|
||||
bc_context: BcContext,
|
||||
connection: Option<TcpStream>,
|
||||
logged_in: bool,
|
||||
}
|
||||
@@ -54,6 +55,7 @@ impl BcCamera {
|
||||
address,
|
||||
connection: None,
|
||||
logged_in: false,
|
||||
bc_context: BcContext::new(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -99,7 +101,7 @@ impl BcCamera {
|
||||
|
||||
legacy_login.serialize(connection)?;
|
||||
|
||||
let legacy_reply = Bc::deserialize(connection)?;
|
||||
let legacy_reply = Bc::deserialize(&mut self.bc_context, connection)?;
|
||||
let nonce;
|
||||
match legacy_reply.body {
|
||||
BcBody::ModernMsg(ModernMsg {
|
||||
@@ -151,7 +153,7 @@ impl BcCamera {
|
||||
|
||||
modern_login.serialize(connection)?;
|
||||
|
||||
let modern_reply = Bc::deserialize(connection)?;
|
||||
let modern_reply = Bc::deserialize(&mut self.bc_context, connection)?;
|
||||
let device_info;
|
||||
match modern_reply.body {
|
||||
BcBody::ModernMsg(ModernMsg {
|
||||
|
||||
Reference in new issue
Block a user