diff --git a/dissector/baichuan.lua b/dissector/baichuan.lua index 14f38ce..aa543f6 100644 --- a/dissector/baichuan.lua +++ b/dissector/baichuan.lua @@ -20,6 +20,18 @@ 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) +-- 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_size = ProtoField.int32("baichuan.udp_size", "udp_size", base.DEC) + bc_protocol.fields = { magic_bytes, message_id, @@ -36,6 +48,16 @@ bc_protocol.fields = { f_bin_offset, username, password, + udp_magic, + udp_type, + udp_message_id, + udp_connection_id, + udp_unknown, + udp_tid, + udp_checksum, + udp_packet_count, + udp_last_ack_packet, + udp_size, } message_types = { @@ -314,9 +336,278 @@ function process_body(header, body_buffer, bc_subtree, pinfo) end end -function bc_protocol.dissector(buffer, pinfo, tree) + +-- UDP CONTENT +udp_fragments = {} + +function rshift(x, by) + return math.floor(x / 2 ^ by) +end + +function udp_decrypt(data, tid) + local result = ByteArray.new() + result:set_size(data:len()) + local key = { + 0x1f2d3c4b, 0x5a6c7f8d, + 0x38172e4b, 0x8271635a, + 0x863f1a2b, 0xa5c6f7d8, + 0x8371e1b4, 0x17f2d3a5 + } + + for i=1, 8 do + key[i] = key[i] + tid + end + + local i = data:len() + 3 + if i < 0 then + i = data:len() + 6 + end + + for x=0, rshift(i, 2) do + local index = bit32.band(x, 7) + local xor_key_word = key[index + 1] + for b=0, 3 do + byte_index = x * 4 + b + local val = data:get_index(byte_index) + local key_byte = bit32.extract(xor_key_word, b*8, 8) + val = bit32.bxor(key_byte, val) + result:set_index(byte_index, val) + if byte_index >= data:len() - 1 then + return result + end + end + end + return result +end + +function xml_encrypt(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()) + for i=0,ba:len() - 1 do + e:set_index(i, bit32.bxor(bit32.band(offset, 0xFF), bit32.bxor(ba:get_index(i), key:byte(((i + offset) % 8) + 1)))) + end + return e +end + +function get_udp_header_len(buffer) + local udpmagic = buffer(1, 3):le_uint() + if not udpmagic == 0x2a87cf then + return 0 + else + udptype = buffer(0, 1):le_uint() + if udptype == 0x3a then + return 20 + elseif udptype == 0x20 then + return 28 + elseif udptype == 0x10 then + return 20 + else + print("Unknown udp type") + return -1 + end + end +end + +function get_udp_header(buffer) + local udp_class = buffer(0, 1):le_uint() + local udp_magic = buffer(1, 3):le_uint() + local length = get_udp_header_len(buffer) + local udp_size = nil + local udp_unknown1 = nil + local udp_tid = nil + local udp_checksum = nil + local udp_unknown2 = nil + local udp_unknown3 = nil + local udp_unknown4 = nil + local udp_last_ack_packet = nil + local udp_connection_id = nil + local udp_packet_count = nil + if udp_class == 0x3a then + udp_size = buffer(4, 4):le_uint() + udp_unknown1 = buffer(8, 4):le_uint() + udp_tid = buffer(12, 4):le_uint() + udp_checksum = buffer(16, 4):le_uint() + elseif udp_class == 0x20 then + udp_connection_id = buffer(4, 4):le_uint() + udp_unknown1 = buffer(8, 4):le_uint() + udp_unknown2 = buffer(12, 4):le_uint() + udp_last_ack_packet = buffer(16, 4):le_uint() + udp_unknown3 = buffer(20, 4):le_uint() + udp_unknown4 = buffer(24, 4):le_uint() + elseif udp_class == 0x10 then + udp_connection_id = buffer(4, 4):le_uint() + udp_unknown1 = buffer(8, 4):le_uint() + udp_packet_count = buffer(12, 4):le_uint() + udp_size = buffer(16, 4):le_uint() + end + return { + length = length, + class = udp_class, + magic = udp_magic, + payload_size = udp_size, + unknown1 = unknown1, + unknown2 = unknown2, + unknown3 = unknown3, + unknown4 = unknown4, + tid = udp_tid, + checksum = udp_checksum, + connection_id = udp_connection_id, + packet_count = udp_packet_count, + last_ack_packet = udp_last_ack_packet, + } +end + +function process_udp_header(buffer, headers_tree) + local header_data = get_udp_header(buffer) + local header = headers_tree:add(bc_protocol, buffer(0, header_data.length), + "Baichuan UDP Header, length: " .. header_data.length .. ", type " .. header_data.class) + header:add_le(udp_magic, buffer(0,4)) + header:add_le(udp_type, buffer(0,1)) + if header_data.class == 0x3a then + header:add_le(udp_size, buffer(4, 4)) + header:add_le(udp_unknown,buffer(8, 4)) + header:add_le(udp_tid, buffer(12, 4)) + header:add_le(udp_checksum, buffer(16, 4)) + elseif header_data.class == 0x20 then + header:add_le(udp_connection_id, buffer(4, 4)) + header:add_le(udp_unknown, buffer(8, 4)) + header:add_le(udp_unknown, buffer(12, 4)) + header:add_le(udp_last_ack_packet, buffer(16, 4)) + header:add_le(udp_unknown, buffer(20, 4)) + header:add_le(udp_unknown, buffer(24, 4)) + elseif header_data.class == 0x10 then + header:add_le(udp_connection_id, buffer(4, 4)) + header:add_le(udp_unknown, buffer(8, 4)) + header:add_le(udp_packet_count, buffer(12, 4)) + header:add_le(udp_size, buffer(16, 4)) + end + return header_data.length +end + +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 + 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 +--- 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() - if length == 0 then return end pinfo.cols.protocol = bc_protocol.name @@ -327,44 +618,16 @@ function bc_protocol.dissector(buffer, pinfo, tree) 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 - pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT - pinfo.desegment_offset = 0 - pinfo.cols['info'] = "Need more header" - return - end - header_len = get_header_len(sub_buffer(0, nil)) + if header_len >= 0 then - pinfo.cols['info'] = "Valid header" -- Valid magic and header found - - -- Ensure min bytes for full header - if sub_buffer:len() < header_len then - pinfo.cols['info'] = "Need even more header" - -- Need more bytes - pinfo.desegment_len = header_len - sub_buffer:len() - pinfo.desegment_offset = 0 - return buffer:len() - end - - local header = get_header(sub_buffer(0, nil)) table.insert(table_msg_type_str, header.msg_type_str) table.insert(table_msg_type, header.msg_type) -- Get full header and body local full_body_len = header.msg_len + header.header_len - if sub_buffer:len() < full_body_len then - -- need more bytes, - -- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly - pinfo.cols['info'] = "Need more body: " .. sub_buffer:len() .. " expected " .. full_body_len - pinfo.desegment_len = full_body_len - sub_buffer:len() - pinfo.desegment_offset = 0 - return buffer:len() - end local remaining = sub_buffer:len() - header.header_len @@ -382,19 +645,83 @@ function bc_protocol.dissector(buffer, pinfo, tree) if remaining == 0 then continue_loop = false else - pinfo.cols['info'] = "Another packet" sub_buffer = sub_buffer(full_body_len, nil) end else - pinfo.cols['info'] = "Invalid magic" - return -- Not a valid header + return end end local msg_type_strs = table.concat(table_msg_type_str, ",") local msg_types = table.concat(table_msg_type, ",") pinfo.cols['info'] = msg_type_strs .. ", type " .. msg_types + return +end + +function bc_protocol.init () + -- print ("(re-)initialise") + fragments = {} +end + +function bc_protocol.dissector(buffer, pinfo, tree) + local subbuffer = nil + local udp_header = nil + if pinfo.can_desegment == 0 then -- UDP + local udp_header_len = get_udp_header_len(buffer) + if udp_header_len > 0 then + 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 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" + elseif udp_header.class == 0x20 then + pinfo.cols.protocol = bc_protocol.name .. " UDP ACK" + else + subbuffer = buffer(udp_header_len, nil) + end + else + subbuffer = buffer(udp_header_len, nil) + end + else + subbuffer = buffer + 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) + return + elseif more == "+1" then + pinfo.desegment_len = DESEGMENT_ONE_MORE_SEGMENT + pinfo.desegment_offset = 0 + return subbuffer:len() + elseif more == "NOMAGIC" then + return + else + pinfo.desegment_len = more + pinfo.desegment_offset = 0 + return subbuffer:len() + end + else -- UDP can not use the desegment method, must reassemble manually + if udp_header ~= nil then + if udp_header.class == 0x10 then -- Continuable udp class + udp_reassemple(udp_header, subbuffer, more, pinfo, tree) + end + end + end + end end DissectorTable.get("tcp.port"):add(9000, bc_protocol) DissectorTable.get("tcp.port"):add(52941, bc_protocol) -- change to your own custom port + +DissectorTable.get("udp.port"):add(23534, bc_protocol) -- change to your own custom port +DissectorTable.get("udp.port"):add(29237, bc_protocol) +DissectorTable.get("udp.port"):add(39654, bc_protocol) +DissectorTable.get("udp.port"):add(28100, bc_protocol) +DissectorTable.get("udp.port"):add(13154, bc_protocol) +DissectorTable.get("udp.port"):add(2015, bc_protocol)