diff --git a/Cargo.lock b/Cargo.lock index de5cfa3..ca6db0f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,17 +30,6 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" -[[package]] -name = "atomic_enum" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a57588a20357c74ee9036656b7fad5c54328ee8657c6ff29ba31fd5ea503a1c" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "atty" version = "0.2.14" @@ -76,12 +65,24 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2e8c087f005730276d1096a652e92a8bacee2e2472bcc9715a74d2bec38b5820" +[[package]] +name = "cc" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" + [[package]] name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "2.33.1" @@ -109,7 +110,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "69323bff1fb41c635347b8ead484a5ca6c3f11914d784170b158d8449ab07f8e" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-channel", "crossbeam-deque", "crossbeam-epoch", @@ -123,7 +124,7 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ee0cc8804d5393478d743b035099520087a5186f3b93fa58cec08fa62407b6" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", ] @@ -145,7 +146,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "lazy_static", "maybe-uninit", @@ -159,7 +160,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "maybe-uninit", ] @@ -171,7 +172,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] @@ -214,6 +215,21 @@ dependencies = [ "synstructure", ] +[[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + [[package]] name = "futures" version = "0.3.5" @@ -405,7 +421,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce4ce1ba28d3293b8cb8c3d33f50e6da2e5cfeefa59a0d10d922ab8015791609" dependencies = [ "bitflags", - "cfg-if", + "cfg-if 0.1.10", "futures-channel", "futures-core", "futures-util", @@ -725,7 +741,7 @@ checksum = "db65c6da02e61f55dae90a0ae427b2a5f6b3e8db09f58d10efab23af92592616" dependencies = [ "arrayvec", "bitflags", - "cfg-if", + "cfg-if 0.1.10", "ryu", "static_assertions", ] @@ -742,7 +758,7 @@ version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -788,7 +804,6 @@ checksum = "0419348c027fa7be448d2ae7ea0e4e04c2334c31dc4e74ab29f00a2a7ca69204" name = "neolink" version = "0.3.0" dependencies = [ - "atomic_enum", "cookie-factory", "crossbeam", "env_logger", @@ -805,6 +820,7 @@ dependencies = [ "log", "md5", "nom", + "openssl", "regex", "serde", "socket2", @@ -861,9 +877,36 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.4.0" +version = "1.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" + +[[package]] +name = "openssl" +version = "0.10.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a61075b62a23fef5a29815de7536d940aa35ce96d18ce0cc5076272db678a577" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "foreign-types", + "libc", + "once_cell", + "openssl-sys", +] + +[[package]] +name = "openssl-sys" +version = "0.9.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "313752393519e876837e09e1fa183ddef0be7735868dced3196f4472d536277f" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] [[package]] name = "paste" @@ -1112,7 +1155,7 @@ version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03088793f677dce356f3ccc2edb1b314ad191ab702a5de3faf49304f7e104918" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "redox_syscall", "winapi", @@ -1321,7 +1364,7 @@ version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a51cadc5b1eec673a685ff7c33192ff7b7603d0b75446fb354939ee615acb15" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", "standback", "stdweb", @@ -1451,6 +1494,12 @@ dependencies = [ "validator", ] +[[package]] +name = "vcpkg" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d" + [[package]] name = "vec_map" version = "0.8.2" @@ -1475,7 +1524,7 @@ version = "0.2.67" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0563a9a4b071746dd5aedbc3a28c6fe9be4586fb3fbadb67c400d4f53c6b16c" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "wasm-bindgen-macro", ] diff --git a/Cargo.toml b/Cargo.toml index 6c80ef6..9bc29ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,4 +35,4 @@ yaserde_derive = "0.3.16" xml-rs = "0.8" validator = "0.10" validator_derive = "0.10" -atomic_enum = "0.1.1" +openssl = "0.10.33" diff --git a/src/bc/de.rs b/src/bc/de.rs index a6e5240..0a4c4b9 100644 --- a/src/bc/de.rs +++ b/src/bc/de.rs @@ -127,8 +127,11 @@ fn bc_modern_msg<'a, 'b>( let encryption_protocol_byte = (header.response_code & 0xff) as usize; match encryption_protocol_byte { 0x00 => context.set_encrypted(EncryptionProtocol::Unencrypted), - 0x01 => context.set_encrypted(EncryptionProtocol::VersionOne), - 0x03 => context.set_encrypted(EncryptionProtocol::VersionThree), + 0x01 => context.set_encrypted(EncryptionProtocol::BCEncrypt), + 0x03 => context.set_encrypted(EncryptionProtocol::Aes(AesKey { + nonce: None, + passwd: None, + })), _ => context.set_encrypted(EncryptionProtocol::Unknown), } } diff --git a/src/bc/model.rs b/src/bc/model.rs index 3906586..1d766e7 100644 --- a/src/bc/model.rs +++ b/src/bc/model.rs @@ -1,7 +1,6 @@ pub use super::xml::{BcPayloads, BcXml, Extension}; -use atomic_enum::atomic_enum; use std::collections::HashSet; -use std::sync::{atomic::Ordering, Arc}; +use std::sync::{Arc, Mutex}; pub(super) const MAGIC_HEADER: u32 = 0xabcdef0; @@ -72,12 +71,32 @@ pub(super) struct BcSendInfo { pub payload_offset: Option, } -#[atomic_enum] -#[derive(PartialEq)] +#[derive(Debug, Clone)] +pub struct AesKey { + pub nonce: Option, + pub passwd: Option, +} + +impl AesKey { + pub fn get_key(&self) -> Option> { + if let (Some(nonce), Some(passwd)) = (&self.nonce, &self.passwd) { + let key_phrase = format!("{}-{}", nonce, passwd); + let key_phrase_hash = format!("{:X}\0", md5::compute(&key_phrase)) + .to_uppercase() + .into_bytes(); + let key = &key_phrase_hash[0..16]; + Some(key.to_vec()) + } else { + None + } + } +} + +#[derive(Debug, Clone)] pub enum EncryptionProtocol { - Unencrypted = 0, - VersionOne, - VersionThree, + Unencrypted, + BCEncrypt, + Aes(AesKey), Unknown, } @@ -86,7 +105,7 @@ pub struct BcContext { pub(super) in_bin_mode: HashSet, // Arc because it is shared between context // and connection for deserialisation and serialistion respectivly - pub(super) encryption_protocol: Arc, + pub(super) encryption_protocol: Arc>, } impl Bc { @@ -122,7 +141,7 @@ impl Bc { } } - pub fn new_from_ext_ext(meta: BcMeta, ext: Extension, xml: BcXml) -> Bc { + pub fn new_from_ext_xml(meta: BcMeta, ext: Extension, xml: BcXml) -> Bc { Bc { meta, body: BcBody::ModernMsg(ModernMsg { @@ -143,14 +162,12 @@ impl BcContext { pub fn new() -> BcContext { BcContext { in_bin_mode: HashSet::new(), - encryption_protocol: Arc::new(AtomicEncryptionProtocol::new( - EncryptionProtocol::Unencrypted, - )), + encryption_protocol: Arc::new(Mutex::new(EncryptionProtocol::Unencrypted)), } } pub fn new_with_encryption_protocol( - encryption_protocol: Arc, + encryption_protocol: Arc>, ) -> BcContext { BcContext { in_bin_mode: HashSet::new(), @@ -159,12 +176,11 @@ impl BcContext { } pub fn set_encrypted(&mut self, encryption_protocol: EncryptionProtocol) { - self.encryption_protocol - .store(encryption_protocol, Ordering::Relaxed); + *(self.encryption_protocol.lock().unwrap()) = encryption_protocol; } pub fn get_encrypted(&self) -> EncryptionProtocol { - self.encryption_protocol.load(Ordering::Relaxed) + (*(self.encryption_protocol.lock().unwrap())).clone() } } diff --git a/src/bc/samples/EncryptionProtocol02_login.pcapng b/src/bc/samples/EncryptionProtocol02_login.pcapng new file mode 100644 index 0000000..11a3af1 Binary files /dev/null and b/src/bc/samples/EncryptionProtocol02_login.pcapng differ diff --git a/src/bc/ser.rs b/src/bc/ser.rs index 7bc13a9..f87bb45 100644 --- a/src/bc/ser.rs +++ b/src/bc/ser.rs @@ -26,7 +26,11 @@ impl Bc { // First serialize ext let (temp_buf, ext_len) = gen( opt_ref(&modern.extension, |ext| { - bc_ext(self.meta.channel_id as u32, ext, encryption_protocol) + bc_ext( + self.meta.channel_id as u32, + ext, + encryption_protocol.clone(), + ) }), vec![], )?; @@ -49,7 +53,7 @@ impl Bc { bc_payload( self.meta.channel_id as u32, payload_offset, - encryption_protocol, + encryption_protocol.clone(), ) }), temp_buf, diff --git a/src/bc/xml_crypto.rs b/src/bc/xml_crypto.rs index efc0c69..e4f5e57 100644 --- a/src/bc/xml_crypto.rs +++ b/src/bc/xml_crypto.rs @@ -1,21 +1,31 @@ use super::model::EncryptionProtocol; use log::error; +use openssl::symm::*; const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF]; +const IV: &[u8] = b"0123456789abcdef"; pub fn crypt(offset: u32, buf: &[u8], encryption_protocol: EncryptionProtocol) -> Vec { match encryption_protocol { EncryptionProtocol::Unencrypted => buf.to_vec(), - EncryptionProtocol::VersionOne => { + EncryptionProtocol::BCEncrypt => { 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() } - EncryptionProtocol::VersionThree => { + EncryptionProtocol::Aes(key) => { // New protocol here - unimplemented!(); + let aeskey = key.get_key(); + if let Some(aeskey) = &aeskey { + let t = Cipher::aes_128_cfb128(); + decrypt(t, aeskey, Some(IV), &buf).unwrap() + } else { + // Not yet ready to decrypt (still in login phase) + // Use BCEncrypt + crypt(offset, buf, EncryptionProtocol::BCEncrypt) + } } _ => { error!("Unknown encryption protocol"); diff --git a/src/bc_protocol.rs b/src/bc_protocol.rs index 58c01b4..fda0692 100644 --- a/src/bc_protocol.rs +++ b/src/bc_protocol.rs @@ -158,7 +158,7 @@ impl BcCamera { channel_id: self.channel_id, msg_num: self.new_message_num(), stream_type: 0, - response_code: 0x01dc, + response_code: 0x03dc, class: 0x6514, }, body: BcBody::LegacyMsg(LegacyMsg::LoginMsg { @@ -254,6 +254,15 @@ impl BcCamera { } } + if let EncryptionProtocol::Aes(_) = connection.get_encrypted() { + // We setup the data for the AES key now + // as all subsequent communications will use it + let nonce = Some(nonce); + let passwd = Some(password.unwrap_or("").to_string()); + let full_key = AesKey { nonce, passwd }; + connection.set_encrypted(EncryptionProtocol::Aes(full_key)); + } + Ok(device_info) } diff --git a/src/bc_protocol/connection.rs b/src/bc_protocol/connection.rs index 2899b9a..3949c8f 100644 --- a/src/bc_protocol/connection.rs +++ b/src/bc_protocol/connection.rs @@ -8,7 +8,7 @@ use std::collections::BTreeMap; use std::error::Error as StdErr; // Just need the traits use std::net::{Shutdown, SocketAddr, TcpStream}; use std::sync::mpsc::{channel, Receiver, Sender}; -use std::sync::{atomic::Ordering, Arc, Mutex}; +use std::sync::{Arc, Mutex}; use std::thread::JoinHandle; use std::time::Duration; @@ -23,7 +23,7 @@ pub struct BcConnection { rx_thread: Option>, // Arc because it is shared between context // and connection for deserialisation and serialistion respectivly - encryption_protocol: Arc, + encryption_protocol: Arc>, } pub struct BcSubscription<'a> { @@ -57,9 +57,7 @@ impl BcConnection { let mut subs = subscribers.clone(); let conn = tcp_conn.try_clone()?; - let encryption_protocol = Arc::new(AtomicEncryptionProtocol::new( - EncryptionProtocol::Unencrypted, - )); + let encryption_protocol = Arc::new(Mutex::new(EncryptionProtocol::Unencrypted)); let connections_encryption_protocol = encryption_protocol.clone(); let rx_thread = std::thread::spawn(move || { let mut context = @@ -133,11 +131,11 @@ impl BcConnection { } pub fn set_encrypted(&self, value: EncryptionProtocol) { - self.encryption_protocol.store(value, Ordering::Relaxed); + *(self.encryption_protocol.lock().unwrap()) = value; } pub fn get_encrypted(&self) -> EncryptionProtocol { - self.encryption_protocol.load(Ordering::Relaxed) + (*self.encryption_protocol.lock().unwrap()).clone() } }