diff --git a/.github/workflows/style_checks.yml b/.github/workflows/style_checks.yml index 27757f6..2d02681 100644 --- a/.github/workflows/style_checks.yml +++ b/.github/workflows/style_checks.yml @@ -41,3 +41,13 @@ jobs: - name: rustfmt run: | cargo +nightly fmt --all -- --check + + check_lua: + name: Luacheck + runs-on: ubuntu-latest + steps: + - name: Run luacheck + uses: nebularg/actions-luacheck@v1 + with: + files: 'dissector/baichuan.lua' + args: --globals Dissector,Proto,ProtoField,base,ByteArray,DESEGMENT_ONE_MORE_SEGMENT,DissectorTable diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index e345695..06cd55d 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -2,35 +2,35 @@ -- Copy/symlink it into ~/.local/lib/wireshark/plugins/ and restart Wireshark; it should -- automatically attempt to decode TCP connections on port 9000. -bc_protocol = Proto("Baichuan", "Baichuan/Reolink IP Camera Protocol") +local bc_protocol = Proto("Baichuan", "Baichuan/Reolink IP Camera Protocol") -magic_bytes = ProtoField.int32("baichuan.magic", "magic", base.DEC) -message_id = ProtoField.int32("baichuan.msg_id", "messageId", base.DEC) -message_len = ProtoField.int32("baichuan.msg_len", "messageLen", base.DEC) -xml_enc_offset = ProtoField.int8("baichuan.xml_encryption_offset", "xmlEncryptionOffset", base.DEC) -encrypt_xml = ProtoField.bool("baichuan.encrypt_xml", "encrypt_xml", base.NONE) -channel_id = ProtoField.int8("baichuan.channel_id", "channel_id", base.DEC) -stream_id = ProtoField.int8("baichuan.stream_id", "streamID", base.DEC) -unknown = ProtoField.int8("baichuan.unknown", "unknown", base.DEC) -msg_handle = ProtoField.int8("baichuan.message_handle", "messageHandle", base.DEC) -status_code = ProtoField.int16("baichuan.status_code", "status_code", base.DEC) -message_class = ProtoField.int32("baichuan.msg_class", "messageClass", base.DEC) -f_bin_offset = ProtoField.int32("baichuan.bin_offset", "binOffset", base.DEC) -username = ProtoField.string("baichuan.username", "username", base.ASCII) -password = ProtoField.string("baichuan.password", "password", base.ASCII) +local magic_bytes = ProtoField.int32("baichuan.magic", "magic", base.DEC) +local message_id = ProtoField.int32("baichuan.msg_id", "messageId", base.DEC) +local message_len = ProtoField.int32("baichuan.msg_len", "messageLen", base.DEC) +local xml_enc_offset = ProtoField.int8("baichuan.xml_encryption_offset", "xmlEncryptionOffset", base.DEC) +local encrypt_xml = ProtoField.bool("baichuan.encrypt_xml", "encrypt_xml", base.NONE) +local channel_id = ProtoField.int8("baichuan.channel_id", "channel_id", base.DEC) +local stream_id = ProtoField.int8("baichuan.stream_id", "streamID", base.DEC) +local unknown = ProtoField.int8("baichuan.unknown", "unknown", base.DEC) +local msg_handle = ProtoField.int8("baichuan.message_handle", "messageHandle", base.DEC) +local status_code = ProtoField.int16("baichuan.status_code", "status_code", base.DEC) +local message_class = ProtoField.int32("baichuan.msg_class", "messageClass", base.DEC) +local f_bin_offset = ProtoField.int32("baichuan.bin_offset", "binOffset", base.DEC) +local username = ProtoField.string("baichuan.username", "username", base.ASCII) +local password = ProtoField.string("baichuan.password", "password", base.ASCII) -- UDP Related content -udp_magic = ProtoField.int32("baichuan.udp_magic", "udp_magic", base.DEC) -udp_type = ProtoField.int8("baichuan.udp_type", "udp_type", base.DEC) -udp_message_id = ProtoField.int8("baichuan.udp_message_id", "udp_message_id", base.DEC) -udp_connection_id = ProtoField.int32("baichuan.udp_connection_id", "udp_connection_id", base.DEC) -udp_unknown = ProtoField.int32("baichuan.udp_unknown", "udp_unknown", base.DEC) -udp_tid = ProtoField.int32("baichuan.udp_tid", "udp_tid", base.DEC) -udp_checksum = ProtoField.int32("baichuan.udp_checksum", "udp_checksum", base.DEC) -udp_packet_count = ProtoField.int32("baichuan.udp_packet_count", "udp_packet_count", base.DEC) -udp_last_ack_packet = ProtoField.int32("baichuan.udp_last_ack_packet", "udp_last_ack_packet", base.DEC) -udp_ack_payload_size = ProtoField.int32("baichuan.udp_ack_payload_size", "ack_payload_size", base.DEC) -udp_size = ProtoField.int32("baichuan.udp_size", "udp_size", base.DEC) +local udp_magic = ProtoField.int32("baichuan.udp_magic", "udp_magic", base.DEC) +local udp_type = ProtoField.int8("baichuan.udp_type", "udp_type", base.DEC) +local udp_message_id = ProtoField.int8("baichuan.udp_message_id", "udp_message_id", base.DEC) +local udp_connection_id = ProtoField.int32("baichuan.udp_connection_id", "udp_connection_id", base.DEC) +local udp_unknown = ProtoField.int32("baichuan.udp_unknown", "udp_unknown", base.DEC) +local udp_tid = ProtoField.int32("baichuan.udp_tid", "udp_tid", base.DEC) +local udp_checksum = ProtoField.int32("baichuan.udp_checksum", "udp_checksum", base.DEC) +local udp_packet_count = ProtoField.int32("baichuan.udp_packet_count", "udp_packet_count", base.DEC) +local udp_last_ack_packet = ProtoField.int32("baichuan.udp_last_ack_packet", "udp_last_ack_packet", base.DEC) +local udp_ack_payload_size = ProtoField.int32("baichuan.udp_ack_payload_size", "ack_payload_size", base.DEC) +local udp_size = ProtoField.int32("baichuan.udp_size", "udp_size", base.DEC) bc_protocol.fields = { magic_bytes, @@ -60,7 +60,7 @@ bc_protocol.fields = { udp_size, } -message_types = { +local message_types = { [1]="login", -- / / [2]="logout", [3]=" (video)", @@ -163,21 +163,21 @@ message_types = { [274]="", } -message_classes = { +local message_classes = { [0x6514]="legacy", [0x6614]="modern", [0x6414]="modern", [0x0000]="modern", } -header_lengths = { +local header_lengths = { [0x6514]=20, [0x6614]=20, [0x6414]=24, [0x0000]=24, } -function xml_encrypt(ba, offset) +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() e:set_size(ba:len()) @@ -187,13 +187,9 @@ function xml_encrypt(ba, offset) return e end -function get_header_len(buffer) +local function get_header_len(buffer) local magic = buffer(0, 4):le_uint() - if magic == 0x0abcdef0 then - -- Client <-> BC - elseif magic == 0x0fedcba0 then - -- BC <-> BC - else + if magic ~= 0x0abcdef0 and magic ~= 0x0fedcba0 then -- Unknown magic return -1 -- No header found end @@ -201,19 +197,18 @@ function get_header_len(buffer) return header_len end -function get_header(buffer) +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 status_code = nil - local encrypt_xml = nil + local return_code = nil + local encr_xml = nil local header_len = header_lengths[buffer(18, 2):le_uint()] - local msg_type = buffer(4, 4):le_uint() if header_len == 24 then bin_offset = buffer(20, 4):le_uint() -- if NHD-805/806 legacy protocol 30 30 30 30 aka "0000" - status_code = buffer(16, 2):le_uint() + return_code = buffer(16, 2):le_uint() else - encrypt_xml = buffer(16, 1):le_uint() + encr_xml = buffer(16, 1):le_uint() end local msg_type = buffer(4, 4):le_uint() local stream_text = "HD (Clear)" @@ -225,23 +220,23 @@ function get_header(buffer) msg_type = buffer(4, 4):le_uint(), msg_type_str = message_types[msg_type] or "unknown", msg_len = buffer(8, 4):le_uint(), - encrypt_xml = encrypt_xml, + encrypt_xml = encr_xml, 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_cls = buffer(18, 2):le_uint(), - status_code = status_code, + status_code = return_code, class = message_classes[buffer(18, 2):le_uint()], header_len = header_lengths[buffer(18, 2):le_uint()], bin_offset = bin_offset, } end -function process_header(buffer, headers_tree) +local function process_header(buffer, headers_tree) local header_data = get_header(buffer) - local header = headers_tree:add(bc_protocol, buffer(0, header_len), + local header = headers_tree:add(bc_protocol, buffer(0, header_data.header_len), "Baichuan Message Header, length: " .. header_data.header_len .. ", type " .. header_data.msg_type) local stream_text = " HD (Clear)" if buffer(13, 1):le_uint() == 1 then @@ -269,10 +264,10 @@ function process_header(buffer, headers_tree) else header:add_le(encrypt_xml, buffer(16, 1)) end - return header_len + return header_data.header_len end -function process_body(header, body_buffer, bc_subtree, pinfo) +local function process_body(header, body_buffer, bc_subtree, pinfo) if header.msg_len == 0 then return end @@ -295,9 +290,9 @@ 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_encrypt(xml_buffer(0,5):bytes(), header.enc_offset):raw() == " 0 then local binary_buffer = body_buffer(header.bin_offset, bin_len) -- Don't extend beyond msg size - body_tvb = binary_buffer:tvb("Main Payload"); + local body_tvb = binary_buffer:tvb("Main Payload"); body:add(body_tvb(), "Main Payload") if bin_len > 4 then - if xml_encrypt(binary_buffer(0,5):bytes(), header.enc_offset):raw() == "= target_len then - start_fragment.result = "DONE" - process_bc_message(reassembled:tvb("Reassembled UDP"), pinfo, tree) - end - end - end -end ---- END UDP CONTENT - -function is_complete_bc_message(buffer) - length = buffer:len() - if length == 0 then - return "DONE" - end - - local sub_buffer = buffer - local table_msg_type_str = {} - local table_msg_type = {} - - local continue_loop = true - while ( continue_loop ) - do - - -- Get min bytes for a magic and header len - if sub_buffer:len() < 20 then - -- Need more bytes but we don't have a header to learn how many bytes - return "+1" - end - header_len = get_header_len(sub_buffer(0, nil)) - - if header_len >= 0 then - -- Valid magic and header found - - -- Ensure min bytes for full header - if sub_buffer:len() < header_len then - -- Need more bytes - return header_len - sub_buffer:len() - end - - - local header = get_header(sub_buffer(0, nil)) - - -- Get full header and body - local full_body_len = header.msg_len + header.header_len - if sub_buffer:len() < full_body_len then - return full_body_len - sub_buffer:len() - end - - local remaining = sub_buffer:len() - header.header_len - if header.header_len < sub_buffer:len() then - local body_buffer = sub_buffer(header.header_len, nil) - remaining = body_buffer:len() - header.msg_len - end - - -- Remaning bytes? - if remaining == 0 then - continue_loop = false - else - sub_buffer = sub_buffer(full_body_len, sub_buffer:len() - full_body_len) - end - else - return "NOMAGIC" - end - end - return "DONE" -end - -function process_bc_message(buffer, pinfo, tree) - length = buffer:len() - +local function process_bc_message(buffer, pinfo, tree) pinfo.cols.protocol = bc_protocol.name local sub_buffer = buffer @@ -631,7 +494,7 @@ function process_bc_message(buffer, pinfo, tree) local continue_loop = true while ( continue_loop ) do - header_len = get_header_len(sub_buffer(0, nil)) + local header_len = get_header_len(sub_buffer(0, nil)) if header_len >= 0 then -- Valid magic and header found @@ -671,8 +534,127 @@ function process_bc_message(buffer, pinfo, tree) return end +local function is_complete_bc_message(buffer) + local length = buffer:len() + if length == 0 then + return "DONE" + end + + local sub_buffer = buffer + + local continue_loop = true + while ( continue_loop ) + do + + -- Get min bytes for a magic and header len + if sub_buffer:len() < 20 then + -- Need more bytes but we don't have a header to learn how many bytes + return "+1" + end + local header_len = get_header_len(sub_buffer(0, nil)) + + if header_len >= 0 then + -- Valid magic and header found + + -- Ensure min bytes for full header + if sub_buffer:len() < header_len then + -- Need more bytes + return header_len - sub_buffer:len() + end + + + local header = get_header(sub_buffer(0, nil)) + + -- Get full header and body + local full_body_len = header.msg_len + header.header_len + if sub_buffer:len() < full_body_len then + return full_body_len - sub_buffer:len() + end + + local remaining = sub_buffer:len() - header.header_len + if header.header_len < sub_buffer:len() then + local body_buffer = sub_buffer(header.header_len, nil) + remaining = body_buffer:len() - header.msg_len + end + + -- Remaning bytes? + if remaining == 0 then + continue_loop = false + else + sub_buffer = sub_buffer(full_body_len, sub_buffer:len() - full_body_len) + end + else + return "NOMAGIC" + end + end + return "DONE" +end + +local function udp_reassemple(udp_header, subbuffer, more, pinfo, tree) + -- Cache udp message for later lookup + local con_id = udp_header.connection_id + if udp_fragments[con_id] == nil then + udp_fragments[con_id] = {} + end + local mess_id = udp_header.packet_count + if udp_fragments[con_id][mess_id] == nil then + udp_fragments[con_id][mess_id] = {} + end + udp_fragments[con_id][mess_id]['result'] = more + udp_fragments[con_id][mess_id]['message_id'] = mess_id + udp_fragments[con_id][mess_id]['buffer'] = subbuffer:bytes() + + -- Go backwards from current ID until: + -- I hit a result that is not NOMAGIC + -- Can be myself + local start_idx = mess_id + local start_fragment = udp_fragments[con_id][start_idx] + while start_fragment.result == "NOMAGIC" do + start_idx = start_idx -1 + start_fragment = udp_fragments[con_id][start_idx] + if start_fragment == nil then + break + end + end + + if start_fragment ~= nil then -- Found a starting fragment + 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) + end + elseif needed == "+1" then + -- Cannot handle in UDP... + -- Only happens in off chance not enough data for + -- even the header + -- Never observed to date + return + else + -- pinfo.cols['info'] = "SEARCHING" + local next_id = start_fragment.message_id + 1 + local reassembled = ByteArray.new() + local total_packet = 1 + reassembled = reassembled .. start_fragment.buffer + local target_len = reassembled:len() + start_fragment.result + while reassembled:len() < target_len do + local next_fragment = udp_fragments[con_id][next_id] + if next_fragment ~= nil then + reassembled = reassembled .. next_fragment.buffer + total_packet = total_packet + 1 + else + break + end + end + if reassembled:len() >= target_len then + start_fragment.result = "DONE" + process_bc_message(reassembled:tvb("Reassembled UDP"), pinfo, tree) + end + end + end +end + function bc_protocol.init () - fragments = {} + udp_fragments = {} end function bc_protocol.dissector(buffer, pinfo, tree) @@ -684,8 +666,8 @@ function bc_protocol.dissector(buffer, pinfo, tree) udp_header = get_udp_header(buffer(0, udp_header_len)) process_udp_header(buffer(0, udp_header_len), tree) if udp_header.class == 0x3a then - decrypted_bytes = udp_decrypt(buffer(udp_header_len, nil):bytes(), udp_header.tid) - decryped_tvb = decrypted_bytes:tvb("UDP Decrypted Message") + local decrypted_bytes = udp_decrypt(buffer(udp_header_len, nil):bytes(), udp_header.tid) + local decryped_tvb = decrypted_bytes:tvb("UDP Decrypted Message") local subtree = tree:add(bc_protocol, decryped_tvb, "UDP Message Data") Dissector.get("xml"):call(decryped_tvb, pinfo, subtree) pinfo.cols.protocol = bc_protocol.name .. " UDP Heartbeat" @@ -708,7 +690,6 @@ function bc_protocol.dissector(buffer, pinfo, tree) end if subbuffer ~= nil then local more = is_complete_bc_message(subbuffer) - if pinfo.can_desegment == 1 then -- TCP can use the desegment method if more == "DONE" then process_bc_message(subbuffer, pinfo, tree) @@ -733,10 +714,11 @@ function bc_protocol.dissector(buffer, pinfo, tree) end end end +--- END UDP CONTENT local function heuristic_checker_udp(buffer, pinfo, tree) -- guard for length - length = buffer:len() + local length = buffer:len() if length < 4 then return false end local potential_magic = buffer(0,4):le_uint() @@ -752,25 +734,33 @@ local function heuristic_checker_udp(buffer, pinfo, tree) return true end +local added_tcp_ports = {} + local function heuristic_checker_tcp(buffer, pinfo, tree) -- guard for length - length = buffer:len() + local length = buffer:len() if length < 4 then return false end - local potential_magic = buffer(0,4):le_uint() - if potential_magic ~= 0xabcdef0 and + if potential_magic ~= 0x0abcdef0 and potential_magic ~= 0x0fedcba0 then return false end + if added_tcp_ports[pinfo.dst_port] == nil then + table.insert(added_tcp_ports, pinfo.dst_port) + DissectorTable.get("tcp.port"):add(pinfo.dst_port, bc_protocol) + end + if added_tcp_ports[pinfo.src_port] == nil then + table.insert(added_tcp_ports, pinfo.src_port) + DissectorTable.get("tcp.port"):add(pinfo.src_port, bc_protocol) + end + bc_protocol.dissector(buffer, pinfo, tree) return true end bc_protocol:register_heuristic("udp", heuristic_checker_udp) bc_protocol:register_heuristic("tcp", heuristic_checker_tcp) - --- DissectorTable.get("tcp.port"):add(9000, bc_protocol) -- DissectorTable.get("tcp.port"):add(53959, bc_protocol) -- change to your own custom port -- DissectorTable.get("udp.port"):add(2000, bc_protocol)