From 97703bdb3811e533c2af6ed908e9761686b0b92c Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 14 Aug 2023 17:12:07 +0700 Subject: [PATCH 1/5] Fix for Snap on older cameras --- .../core/src/bc_protocol/connection/bcconn.rs | 19 ++++++++++++++++--- crates/core/src/bc_protocol/snap.rs | 1 + 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 08270cb..63cdf93 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -19,6 +19,9 @@ type MsgHandler = dyn 'static + Send + Sync + for<'a> Fn(&'a Bc) -> BoxFuture<'a #[derive(Default)] struct Subscriber { /// Subscribers based on their ID and their num + /// First filtered by ID then number + /// If num is None it will be upgraded to a Some based on the number the + /// camera assigns num: BTreeMap, Sender>>>, /// Subscribers based on their ID id: BTreeMap>, @@ -194,12 +197,22 @@ impl Poller { PollCommand::Bc(boxed_response) => { match *boxed_response { Ok(response) => { - let msg_num = response.meta.msg_num; let msg_id = response.meta.msg_id; - + let msg_num = response.meta.msg_num; + log::trace!( + "Looking for ID: {} with num: {}, in {:?} and {:?}", + msg_id, + msg_num, + self.subscribers.id.keys().to_owned(), + self.subscribers + .num + .iter() + .map(|(k, v)| (k, v.keys())) + .collect::>(), + ); match ( self.subscribers.id.get(&msg_id), - self.subscribers.num.get_mut(&msg_id), + self.subscribers.num.get_mut(&msg_id), // Both filter first on ID ) { (Some(occ), _) => { if let Some(reply) = occ(&response).await { diff --git a/crates/core/src/bc_protocol/snap.rs b/crates/core/src/bc_protocol/snap.rs index a1d23e1..ab7c09c 100644 --- a/crates/core/src/bc_protocol/snap.rs +++ b/crates/core/src/bc_protocol/snap.rs @@ -57,6 +57,7 @@ impl BcCamera { .. }) = msg.body { + drop(sub_get); // Ensure that we are NOT listening on that msgnum as the reply can come on ANY msgnum log::trace!("Got snap XML {} with size {}", filename, expected_size); // Messages are now sent on ID 109 but not with the same message ID // preumably because the camera considers it to be a new message rather From 1cde5ec3e5452976338824155b77dea35cec3c91 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Mon, 14 Aug 2023 18:42:15 +0700 Subject: [PATCH 2/5] Cleanup old subscribers --- crates/core/src/bc_protocol/connection/bcconn.rs | 9 +++++++++ crates/core/src/bc_protocol/ping.rs | 1 + 2 files changed, 10 insertions(+) diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 63cdf93..6cff8d4 100644 --- a/crates/core/src/bc_protocol/connection/bcconn.rs +++ b/crates/core/src/bc_protocol/connection/bcconn.rs @@ -193,6 +193,15 @@ impl Poller { async fn run(&mut self) -> Result<()> { while let Some(command) = self.reciever.next().await { yield_now().await; + // Clean Up subscribers + self.subscribers + .num + .iter_mut() + .for_each(|(_, channels)| channels.retain(|_, channel| !channel.is_closed())); + self.subscribers + .num + .retain(|_, channels| !channels.is_empty()); + // Handle the command match command { PollCommand::Bc(boxed_response) => { match *boxed_response { diff --git a/crates/core/src/bc_protocol/ping.rs b/crates/core/src/bc_protocol/ping.rs index 6426536..5f76f1f 100644 --- a/crates/core/src/bc_protocol/ping.rs +++ b/crates/core/src/bc_protocol/ping.rs @@ -27,6 +27,7 @@ impl BcCamera { sub_ping.recv().await?; + log::trace!("Ping complete"); Ok(()) } } From d084a13eb002d36524d30aa9ef3dbfbe1c42d086 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 15 Aug 2023 18:01:42 +0700 Subject: [PATCH 3/5] Add AES decryption to dissector. REQUIRES luagcrypt --- dissector/baichuan.lua | 70 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 6111e96..042f295 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -88,7 +88,6 @@ local message_types = { [15]="", [16]="", [18]="", - [19]="", [23]="Reboot", [25]=" (write)", [26]="", -- @@ -147,11 +146,14 @@ local message_types = { [106]="", [107]=" (write)", [108]=" (log)", + [109]="", [114]="", [115]="", + [116]="", [120]="", [122]="", [123]="", + [124]="", [132]="", -- [133]="", [141]=" (test)", @@ -161,6 +163,7 @@ local message_types = { [151]="", [190]="PTZ Preset", [194]=" (test)", + [195]="", [199]="", [208]="", [209]=" (write)", @@ -175,9 +178,13 @@ local message_types = { [228]="", [229]=" (write)", [230]="", + [232]="", [234]="UDP Keep Alive", [252]="", [253]="", + [268]="", + [282]="", + [287]="", [272]="", [273]="", [274]="", @@ -189,6 +196,7 @@ local message_classes = { [0x6514]="legacy", [0x6614]="modern", [0x6414]="modern", + [0x6482]="modern (file download)", [0x0000]="modern", } @@ -196,9 +204,37 @@ local header_lengths = { [0x6514]=20, [0x6614]=20, [0x6414]=24, + [0x6482]=24, [0x0000]=24, } +----- +-- Decryption routine. +----- +-- For other locations, use: LUA_CPATH=.../luagcrypt/?.so +bc_protocol.prefs.key = Pref.string("Decryption key", "", + "Passphrase used for the camera. Required to decrypt the AES packets") +bc_protocol.prefs.nonce = Pref.string("Nonce key", "", + "Nonce negotiate during login. You can find this in the msg_id == 1 packets.") +local function hexencode(str) + return (str:gsub(".", function(char) return string.format("%02X", char:byte()) end)) +end +local gcrypt = require("luagcrypt") +local function aes_decrypt(data) + local raw_key = bc_protocol.prefs.nonce .. "-" .. bc_protocol.prefs.key + local iv = "0123456789abcdef" + local hasher = gcrypt.Hash(gcrypt.MD_MD5) + hasher:write(raw_key) + local key = string.sub(hexencode(hasher:read()),0,16) + local ciphertext = data + local cipher = gcrypt.Cipher(gcrypt.CIPHER_AES256, gcrypt.CIPHER_MODE_CFB) + cipher:setkey(key) + cipher:setiv(iv) + local decrypted = cipher:decrypt(ciphertext) + local result = ByteArray.new(decrypted, true) + return result +end + local function xml_decrypt(ba, offset) local key = "\031\045\060\075\090\105\120\255" -- 1f, 2d, 3c, 4b, 5a, 69, 78 ,ff local e = ByteArray.new() @@ -216,12 +252,14 @@ local function get_header_len(buffer) return -1 -- No header found end local header_len = header_lengths[buffer(18, 2):le_uint()] + if (not header_len) then + return 20 + end return header_len end local function get_header(buffer) -- bin_offset is either nil (no binary data) or nonzero - -- TODO: bin_offset is actually stateful! local bin_offset = nil local return_code = nil local encr_xml = nil @@ -246,12 +284,11 @@ local function get_header(buffer) channel_id = buffer(12, 1):le_uint(), enc_offset = buffer(12, 1):le_uint(), stream_type = stream_text, - unknown = buffer(14, 1):le_uint(), - msg_handle = buffer(15, 1):le_uint(), + msg_handle = buffer(14, 2):le_uint(), msg_cls = buffer(18, 2):le_uint(), status_code = return_code, - class = message_classes[buffer(18, 2):le_uint()], - header_len = header_lengths[buffer(18, 2):le_uint()], + class = message_classes[buffer(18, 2):le_uint()] or "unknown", + header_len = get_header_len(buffer(0, nil)), bin_offset = bin_offset, } end @@ -275,8 +312,7 @@ local function process_header(buffer, headers_tree) header:add_le(channel_id, buffer(12, 1)) header:add_le(stream_id, buffer(13, 1)) :append_text(stream_text) - header:add_le(unknown, buffer(14, 1)) - header:add_le(msg_handle, buffer(15, 1)) + header:add_le(msg_handle, buffer(14, 2)) header:add_le(message_class, buffer(18, 2)):append_text(" (" .. header_data.class .. ")") @@ -312,7 +348,14 @@ local function process_body(header, body_buffer, bc_subtree, pinfo) local body_tvb = xml_buffer:tvb("Meta Payload") body:add(body_tvb(), "Meta Payload") if xml_len >= 4 then - if xml_decrypt(xml_buffer(0,5):bytes(), header.enc_offset):raw() == " 4 then - if xml_decrypt(binary_buffer(0,5):bytes(), header.enc_offset):raw() == " Date: Tue, 15 Aug 2023 18:14:19 +0700 Subject: [PATCH 4/5] Name wireshark reassembly --- dissector/baichuan.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 042f295..7ff1941 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -88,6 +88,7 @@ local message_types = { [15]="", [16]="", [18]="", + [19]="", [23]="Reboot", [25]=" (write)", [26]="", -- @@ -693,7 +694,7 @@ local function udp_reassemple(udp_header, subbuffer, more, pinfo, tree) local needed = start_fragment.result if needed == "DONE" then if start_fragment.message_id == udp_header.packet_count then - process_bc_message(start_fragment.buffer:tvb(), pinfo, tree) + process_bc_message(start_fragment.buffer:tvb("UDP Reassemble"), pinfo, tree) end elseif needed == "+1" then -- Cannot handle in UDP... From d08a5fe4aab5e1e856ab2719730706a01a9bcac9 Mon Sep 17 00:00:00 2001 From: QuantumEntangledAndy Date: Tue, 15 Aug 2023 18:50:29 +0700 Subject: [PATCH 5/5] Add Pref to luacheck globals --- .github/workflows/style_checks.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/style_checks.yml b/.github/workflows/style_checks.yml index d56488c..83c2d02 100644 --- a/.github/workflows/style_checks.yml +++ b/.github/workflows/style_checks.yml @@ -62,4 +62,4 @@ jobs: uses: nebularg/actions-luacheck@v1 with: files: 'dissector/baichuan.lua' - args: --globals Dissector Proto ProtoField base ByteArray DESEGMENT_ONE_MORE_SEGMENT DissectorTable + args: --globals Dissector Proto ProtoField base ByteArray DESEGMENT_ONE_MORE_SEGMENT DissectorTable Pref