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() == "