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 diff --git a/crates/core/src/bc_protocol/connection/bcconn.rs b/crates/core/src/bc_protocol/connection/bcconn.rs index 08270cb..6cff8d4 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>, @@ -190,16 +193,35 @@ 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 { 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/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(()) } } 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 diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 6111e96..7ff1941 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -147,11 +147,14 @@ local message_types = { [106]="", [107]=" (write)", [108]=" (log)", + [109]="", [114]="", [115]="", + [116]="", [120]="", [122]="", [123]="", + [124]="", [132]="", -- [133]="", [141]=" (test)", @@ -161,6 +164,7 @@ local message_types = { [151]="", [190]="PTZ Preset", [194]=" (test)", + [195]="", [199]="", [208]="", [209]=" (write)", @@ -175,9 +179,13 @@ local message_types = { [228]="", [229]=" (write)", [230]="", + [232]="", [234]="UDP Keep Alive", [252]="", [253]="", + [268]="", + [282]="", + [287]="", [272]="", [273]="", [274]="", @@ -189,6 +197,7 @@ local message_classes = { [0x6514]="legacy", [0x6614]="modern", [0x6414]="modern", + [0x6482]="modern (file download)", [0x0000]="modern", } @@ -196,9 +205,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 +253,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 +285,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 +313,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 +349,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() == "