Dissector update and some notes about BC message (#80)
Co-authored-by: twistedddx <bits@live.com>
This commit is contained in:
1 parent
affc0d9b94
commit
b5312edb90
4 files changed
+2729
-79
No files matched your search
+300
-79
@@ -7,8 +7,13 @@ 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.int32("baichuan.xml_encryption_offset", "xmlEncryptionOffset", base.DEC)
|
||||
xml_enc_used = ProtoField.bool("baichuan.xml_encryption_used", "encrypted", base.NONE)
|
||||
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)
|
||||
@@ -19,7 +24,12 @@ bc_protocol.fields = {
|
||||
message_id,
|
||||
message_len,
|
||||
xml_enc_offset,
|
||||
xml_enc_used,
|
||||
channel_id,
|
||||
stream_id,
|
||||
unknown,
|
||||
msg_handle,
|
||||
encrypt_xml,
|
||||
status_code,
|
||||
message_class,
|
||||
f_bin_offset,
|
||||
username,
|
||||
@@ -27,16 +37,99 @@ bc_protocol.fields = {
|
||||
}
|
||||
|
||||
message_types = {
|
||||
[1]="login",
|
||||
[1]="login", -- <Encryption> <LoginUser>/<LoginNet> <DeviceInfo>/<StreamInfoList>
|
||||
[2]="logout",
|
||||
[3]="<Preview> (video)",
|
||||
[58]="<AbilitySupport>",
|
||||
[78]="<VideoInput>",
|
||||
[79]="<Serial>",
|
||||
[4]="<Preview> (stop)",
|
||||
[5]="<FileInfoList> (replay)",
|
||||
[7]="<FileInfoList> (stop)",
|
||||
[8]="<FileInfoList> (DL Video)",
|
||||
[10]="<TalkAbility>",
|
||||
[13]="<FileInfoList> (download)",
|
||||
[14]="<FileInfoList>",
|
||||
[15]="<FileInfoList>",
|
||||
[16]="<FileInfoList>",
|
||||
[18]="<PtzControl>",
|
||||
[25]="<VideoInput> (write)",
|
||||
[26]="<VideoInput>", -- <InputAdvanceCfg>
|
||||
[31]="Start Motion Alarm",
|
||||
[33]="<AlarmEventList>",
|
||||
[36]="<ServerPort> (write)",
|
||||
[37]="<ServerPort>", -- <HttpPort>/<RtspPort>/<OnvifPort>/<HttpsPort>/<RtmpPort>
|
||||
[38]="<Ntp>",
|
||||
[39]="<Ntp> (write)",
|
||||
[40]="<Ddns>",
|
||||
[41]="<Ddns> (write)",
|
||||
[42]="<Email>",
|
||||
[43]="<Email> (write)",
|
||||
[44]="<OsdChannelName>", -- <OsdDatetime>
|
||||
[45]="<OsdChannelName> (write)",
|
||||
[46]="<MD>",
|
||||
[47]="<MD> (write)",
|
||||
[50]="<VideoLoss>",
|
||||
[51]="<VideoLoss> (write)",
|
||||
[52]="<Shelter> (priv mask)",
|
||||
[53]="<Shelter> (write)",
|
||||
[54]="<RecordCfg>",
|
||||
[55]="<RecordCfg> (write)",
|
||||
[56]="<Compression>",
|
||||
[57]="<Compression> (write)",
|
||||
[58]="<AbilitySupport>", -- <UserList>
|
||||
[59]="<UserList> (write)",
|
||||
[65]="<ConfigFileInfo> (Export)",
|
||||
[66]="<ConfigFileInfo> (Import)",
|
||||
[68]="<Ftp>",
|
||||
[69]="<Ftp> (write)",
|
||||
[70]="<FtpTask>",
|
||||
[71]="<FtpTask> (write)",
|
||||
[76]="<Ip>", -- <Dhcp>/<AutoDNS>/<Dns>
|
||||
[77]="<Ip> (write)",
|
||||
[78]="<VideoInput> (IPC desc)",
|
||||
[79]="<Serial> (ptz)",
|
||||
[80]="<VersionInfo>",
|
||||
[81]="<Record> (schedule)",
|
||||
[82]="<Record> (write)",
|
||||
[83]="<HandleException>",
|
||||
[84]="<HandleException> (write)",
|
||||
[91]="<DisplayOutput>",
|
||||
[92]="<DisplayOutput> (write)",
|
||||
[93]="<LinkType>",
|
||||
[97]="<Upnp>",
|
||||
[98]="<Upnp> (write)",
|
||||
[100]="<AutoReboot> (write)",
|
||||
[101]="<AutoReboot>",
|
||||
[102]="<HDDInfoList>",
|
||||
[104]="<SystemGeneral>",
|
||||
[105]="<SystemGeneral> (write)",
|
||||
[106]="<Dst>",
|
||||
[107]="<Dst> (write)",
|
||||
[108]="<ConfigFileInfo> (log)",
|
||||
[114]="<Uid>",
|
||||
[115]="<WifiSignal>",
|
||||
[120]="<OnlineUserList>",
|
||||
[122]="<PerformanceInfo>",
|
||||
[123]="<ReplaySeek>",
|
||||
[132]="<VideoInput>", -- <InputAdvanceCfg>
|
||||
[133]="<RfAlarm>",
|
||||
[141]="<Email> (test)",
|
||||
[142]="<DayRecords>",
|
||||
[145]="<ChannelInfoList>",
|
||||
[146]="<StreamInfoList>",
|
||||
[151]="<AbilityInfo>",
|
||||
[190]="PTZ Preset",
|
||||
[194]="<Ftp> (test)",
|
||||
[199]="<Support>",
|
||||
[208]="<LedState>",
|
||||
[209]="<LedState> (write)",
|
||||
[210]="<PTOP>",
|
||||
[211]="<PTOP> (write)",
|
||||
[219]="<PushTask>",
|
||||
[228]="<Crop>",
|
||||
[229]="<Crop> (write)",
|
||||
[230]="<cropSnap>",
|
||||
[272]="<findAlarmVideo>",
|
||||
[273]="<alarmVideoInfo>",
|
||||
[274]="<findAlarmVideo>",
|
||||
}
|
||||
|
||||
message_classes = {
|
||||
@@ -54,7 +147,7 @@ header_lengths = {
|
||||
}
|
||||
|
||||
function xml_encrypt(ba, offset)
|
||||
local key = "\031\045\060\075\090\105\120\255"
|
||||
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
|
||||
@@ -63,103 +156,231 @@ function xml_encrypt(ba, offset)
|
||||
return e
|
||||
end
|
||||
|
||||
function bc_protocol.dissector(buffer, pinfo, tree)
|
||||
length = buffer:len()
|
||||
if length == 0 then return end
|
||||
|
||||
if 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
|
||||
return
|
||||
end
|
||||
|
||||
pinfo.cols.protocol = bc_protocol.name
|
||||
|
||||
function get_header_len(buffer)
|
||||
local magic = buffer(0, 4):le_uint()
|
||||
if magic ~= 0x0abcdef0 then
|
||||
-- Case 3, capture started in middle of packet,
|
||||
-- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly
|
||||
-- The camera always seems to emit a new TCP packet for a new message
|
||||
return 0
|
||||
return -1 -- No header found
|
||||
end
|
||||
|
||||
local msg_type = buffer(4, 4):le_uint()
|
||||
local msg_type_str = message_types[msg_type] or "unknown"
|
||||
local msg_len = buffer(8, 4):le_uint()
|
||||
local enc_offset = buffer(12, 4):le_uint()
|
||||
local msg_cls = buffer(18, 2):le_uint()
|
||||
local encrypted = (msg_cls == 0x6414 or buffer(16, 1):le_uint() ~= 0)
|
||||
local class = message_classes[buffer(18, 2):le_uint()]
|
||||
local header_len = header_lengths[buffer(18, 2):le_uint()]
|
||||
return header_len
|
||||
end
|
||||
|
||||
if buffer:len() ~= msg_len + header_len then
|
||||
-- Case 1, need more bytes,
|
||||
-- from https://wiki.wireshark.org/Lua/Dissectors#TCP_reassembly
|
||||
pinfo.desegment_len = msg_len + header_len - buffer:len()
|
||||
pinfo.desegment_offset = 0
|
||||
return buffer:len()
|
||||
end
|
||||
|
||||
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 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 bin_offset == 0 then bin_offset = nil end
|
||||
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()
|
||||
else
|
||||
encrypt_xml = buffer(16, 1):le_uint()
|
||||
end
|
||||
local msg_type = buffer(4, 4):le_uint()
|
||||
local stream_text = "HD (Clear)"
|
||||
if buffer(13, 1):le_uint() == 1 then
|
||||
stream_text = "SD (Fluent)"
|
||||
end
|
||||
return {
|
||||
magic = buffer(0, 4):le_uint(),
|
||||
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,
|
||||
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,
|
||||
class = message_classes[buffer(18, 2):le_uint()],
|
||||
header_len = header_lengths[buffer(18, 2):le_uint()],
|
||||
bin_offset = bin_offset,
|
||||
}
|
||||
end
|
||||
|
||||
local bc_subtree = tree:add(bc_protocol, buffer(),
|
||||
"Baichuan IP Camera Protocol, " .. msg_type_str .. " message")
|
||||
local header = bc_subtree:add(bc_protocol, buffer(0, header_len),
|
||||
"Baichuan Message Header, length: " .. header_len)
|
||||
|
||||
pinfo.cols['info'] = msg_type_str .. ", type " .. msg_type .. ", " .. msg_len .. " bytes"
|
||||
|
||||
function process_header(buffer, headers_tree)
|
||||
local header_data = get_header(buffer)
|
||||
local header = headers_tree:add(bc_protocol, buffer(0, 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
|
||||
stream_text = " SD (Fluent)"
|
||||
end
|
||||
header:add_le(magic_bytes, buffer(0, 4))
|
||||
header:add_le(message_id, buffer(4, 4))
|
||||
:append_text(" (" .. msg_type_str .. ")")
|
||||
:append_text(" (" .. header_data.msg_type_str .. ")")
|
||||
header:add_le(message_len, buffer(8, 4))
|
||||
header:add_le(xml_enc_offset, buffer(12, 4))
|
||||
:append_text(" (& 0xF == " .. bit32.band(enc_offset, 0xF) .. ")")
|
||||
header:add_le(xml_enc_used, buffer(16, 1))
|
||||
header:add_le(message_class, buffer(18, 2))
|
||||
:append_text(" (" .. class .. ")")
|
||||
header:add_le(f_bin_offset, buffer(20, 4))
|
||||
|
||||
if msg_len == 0 then
|
||||
header:add_le(xml_enc_offset, buffer(12, 1))
|
||||
:append_text(" (& 0xF == " .. bit32.band(header_data.enc_offset, 0xF) .. ")")
|
||||
|
||||
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(message_class, buffer(18, 2)):append_text(" (" .. header_data.class .. ")")
|
||||
|
||||
if header_data.header_len == 24 then
|
||||
header:add_le(status_code, buffer(16, 2))
|
||||
header:add_le(f_bin_offset, buffer(20, 4))
|
||||
else
|
||||
header:add_le(encrypt_xml, buffer(16, 1))
|
||||
end
|
||||
return header_len
|
||||
end
|
||||
|
||||
function process_body(header, body_buffer, bc_subtree, pinfo)
|
||||
if header.msg_len == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local body = bc_subtree:add(bc_protocol, buffer(header_len, nil),
|
||||
"Baichuan Message Body, " .. class .. ", length: " .. msg_len .. ", encrypted: " .. tostring(encrypted))
|
||||
local body = bc_subtree:add(bc_protocol, body_buffer(0,header.msg_len),
|
||||
"Baichuan Message Body, " .. header.class .. ", length: " .. header.msg_len .. ", type " .. header.msg_type)
|
||||
|
||||
if class == "legacy" then
|
||||
if msg_type == 1 then
|
||||
body:add_le(username, buffer(header_len, 32))
|
||||
body:add_le(password, buffer(header_len + 32, 32))
|
||||
if header.class == "legacy" then
|
||||
if header.msg_type == 1 then
|
||||
body:add_le(username, body_buffer(0, 32))
|
||||
body:add_le(password, body_buffer(0 + 32, 32))
|
||||
end
|
||||
else
|
||||
local body_tvb = buffer(header_len, bin_offset):tvb()
|
||||
body:add(body_tvb(), "XML Payload")
|
||||
|
||||
if encrypted then
|
||||
local ba = buffer(header_len, bin_offset):bytes()
|
||||
local decrypted = xml_encrypt(ba, enc_offset)
|
||||
body_tvb = decrypted:tvb("Decrypted XML")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML")
|
||||
local xml_len = header.bin_offset
|
||||
if xml_len == nil then
|
||||
xml_len = header.msg_len
|
||||
end
|
||||
local xml_buffer = body_buffer(0, xml_len)
|
||||
if xml_len > 0 then
|
||||
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() == "<?xml" then -- Encrypted xml found
|
||||
local ba = xml_buffer:bytes()
|
||||
local decrypted = xml_encrypt(ba, header.enc_offset)
|
||||
body_tvb = decrypted:tvb("Decrypted XML (in Meta Payload)")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML (in Meta Payload)")
|
||||
Dissector.get("xml"):call(body_tvb, pinfo, body)
|
||||
elseif xml_buffer(0,5):string() == "<?xml" then -- Unencrypted xml
|
||||
body:add(body_tvb(), "XML (in Meta Payload)")
|
||||
Dissector.get("xml"):call(body_tvb, pinfo, body)
|
||||
else
|
||||
body:add(body_tvb(), "Binary (in Meta Payload)")
|
||||
end
|
||||
end
|
||||
end
|
||||
Dissector.get("xml"):call(body_tvb, pinfo, body)
|
||||
|
||||
if bin_offset ~= nil then
|
||||
local binary_tvb = buffer(header_len + bin_offset, nil):tvb()
|
||||
body:add(binary_tvb(), "Binary Payload")
|
||||
if msg_type == 0x03 then -- video
|
||||
Dissector.get("h265"):call(binary_tvb, pinfo, tree)
|
||||
if header.bin_offset ~= nil then
|
||||
local bin_len = header.msg_len - header.bin_offset
|
||||
if bin_len > 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");
|
||||
body:add(body_tvb(), "Main Payload")
|
||||
if bin_len > 4 then
|
||||
if xml_encrypt(binary_buffer(0,5):bytes(), header.enc_offset):raw() == "<?xml" then -- Encrypted xml found
|
||||
local decrypted = xml_encrypt(binary_buffer:bytes(), header.enc_offset)
|
||||
body_tvb = decrypted:tvb("Decrypted XML (in Main Payload)")
|
||||
-- Create a tree item that, when clicked, automatically shows the tab we just created
|
||||
body:add(body_tvb(), "Decrypted XML (in Main Payload)")
|
||||
Dissector.get("xml"):call(body_tvb, pinfo, body)
|
||||
elseif binary_buffer(0,5):string() == "<?xml" then -- Unencrypted xml
|
||||
body:add(body_tvb(), "XML (in Main Payload)")
|
||||
Dissector.get("xml"):call(body_tvb, pinfo, body)
|
||||
else
|
||||
body:add(body_tvb(), "Binary (in Main Payload)")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function bc_protocol.dissector(buffer, pinfo, tree)
|
||||
length = buffer:len()
|
||||
if length == 0 then return end
|
||||
|
||||
pinfo.cols.protocol = bc_protocol.name
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
local bc_subtree = tree:add(bc_protocol, sub_buffer(0, header.msg_len),
|
||||
"Baichuan IP Camera Protocol, " .. header.msg_type_str .. ":" .. header.msg_type .. " message")
|
||||
process_header(sub_buffer, bc_subtree)
|
||||
if header.header_len < sub_buffer:len() then
|
||||
local body_buffer = sub_buffer(header.header_len,nil)
|
||||
process_body(header, body_buffer, bc_subtree, pinfo)
|
||||
|
||||
remaining = body_buffer:len() - header.msg_len
|
||||
end
|
||||
|
||||
-- Remaning bytes?
|
||||
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
|
||||
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
|
||||
end
|
||||
|
||||
DissectorTable.get("tcp.port"):add(9000, bc_protocol)
|
||||
DissectorTable.get("tcp.port"):add(52941, bc_protocol) -- change to your own custom port
|
||||
@@ -0,0 +1,111 @@
|
||||
# Media Packets
|
||||
|
||||
This file attempts to document the encapsulated stream that makes up the binary
|
||||
data of message ID 3. This stream represents the video and audio data.
|
||||
|
||||
The are six types of media packets
|
||||
|
||||
- Info V1
|
||||
|
||||
- Info V2
|
||||
|
||||
- I Frame
|
||||
|
||||
- P Frame
|
||||
|
||||
- AAC
|
||||
|
||||
- ADPCM
|
||||
|
||||
The I and P frames come in two formats H264 and H265.
|
||||
|
||||
The ADPCM is DVI-4 encoded with an extra header that represents the block size.
|
||||
|
||||
## Magic
|
||||
|
||||
Each packet can be distinguished with the following magic bytes
|
||||
|
||||
- Info V1: 0x31, 0x30, 0x30, 0x31
|
||||
- Info V2: 0x31, 0x30, 0x30, 0x32
|
||||
- I Frame: 0x30, 0x30, 0x64, 0x63
|
||||
- P Frame: 0x30, 0x31, 0x64, 0x63
|
||||
- AAC: 0x30, 0x35, 0x77, 0x62
|
||||
- ADPCM: 0x30, 0x31, 0x77, 0x62
|
||||
|
||||
|
||||
## Headers
|
||||
|
||||
The full headers for each of these are as follows:
|
||||
|
||||
Most of this information comes from the work of @twisteddx at his
|
||||
[site](https://www.wasteofcash.com/BCConvert/BC_fileformat.txt)
|
||||
|
||||
- Info V1/V2:
|
||||
|
||||
- 4 bytes magic
|
||||
- 4 bytes data size of header itself
|
||||
- 4 bytes video width
|
||||
- 4 bytes video height
|
||||
- 1 byte unknown. Known values 00/01
|
||||
- 1 byte Frames per second (Reolink=FPS, Swann=Appears to be the index value of the FPS setting)
|
||||
- 1 byte Start UTC year since 1900
|
||||
- 1 byte Start UTC month
|
||||
- 1 byte Start UTC date
|
||||
- 1 byte Start UTC hour
|
||||
- 1 byte Start UTC minute
|
||||
- 1 byte Start UTC seconds
|
||||
- 1 byte End UTC year since 1900
|
||||
- 1 byte End UTC month
|
||||
- 1 byte End UTC date
|
||||
- 1 byte End UTC hour
|
||||
- 1 byte End UTC minute
|
||||
- 1 byte End UTC seconds
|
||||
- 2 bytes reserved
|
||||
|
||||
- I Frame
|
||||
|
||||
- 4 bytes magic
|
||||
- 4 bytes video type (ASCII text of either H264 or H265)
|
||||
- 4 bytes data size of payload after header
|
||||
- 4 bytes unknown. NVR channel count? Known values 1-00/08 2-00 3-00 4-00
|
||||
- 4 bytes Microseconds
|
||||
- 4 bytes unknown. Known values 1-00/23/5A 2-00 3-00 4-00
|
||||
- 4 bytes POSIX time_t 32bit UTC time (seconds since 00:00:00 Jan 1 1970)
|
||||
- 4 bytes unknown. Known values 1-00/06/29 2-00/01 3-00/C3 4-00
|
||||
|
||||
- P Frame
|
||||
- 4 bytes magic
|
||||
- 4 bytes video type (eg H264 or H265)
|
||||
- 4 bytes data size of payload after header
|
||||
- 4 bytes unknown. Known values 1-00 2-00 3-00 4-00
|
||||
- 4 bytes Microseconds
|
||||
- 4 bytes unknown. Known values 1-00/5A 2-00 3-00 4-00
|
||||
|
||||
- AAC
|
||||
|
||||
- 4 bytes magic
|
||||
- 2 bytes data size of payload after header
|
||||
- 2 bytes data size of payload after header (Same as previous)
|
||||
|
||||
- ADPCM
|
||||
|
||||
- 4 bytes magic
|
||||
- 2 bytes data size of payload after header
|
||||
- 2 bytes data size of payload after header
|
||||
|
||||
### ADPCM
|
||||
|
||||
After the ADPCM header is another header of the form
|
||||
|
||||
- 2 bytes Magic either 0x00 0x01 or 0x00 0x7a
|
||||
- 2 Bytes DVI-4 Block size
|
||||
|
||||
After this header the adpcm DVI-4 data follows.
|
||||
|
||||
## Processing
|
||||
|
||||
The data in ID 3 messages represents an encapsulated stream. BC messages may
|
||||
terminate mid media packet messages. Clients should create a buffer of the
|
||||
BC ID 3 messages then read the media packet magic and expected length. Once
|
||||
length is known they should wait for more BC message ID 3 packets until a
|
||||
complete media packet is received before processing it.
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,235 @@
|
||||
# BC Protocol
|
||||
|
||||
This is an attempt to document the BC protocol. It is far from complete
|
||||
but should serve as a good basis for those wishing to develop apps for
|
||||
BC cameras.
|
||||
|
||||
## Messages
|
||||
|
||||
Each message has the general format:
|
||||
|
||||
- Header: 20-24 bytes
|
||||
|
||||
- Message Body
|
||||
|
||||
### Header
|
||||
|
||||
The header has the format:
|
||||
|
||||
| magic | message id | message length | encryption offset | Encryption flag | Unknown | message class |
|
||||
|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|
|
||||
| f0 de bc 0a | 01 00 00 00 | 2c 07 00 00 | 00 00 00 01 | 01 | dc | 14 65 |
|
||||
|
||||
Or
|
||||
|
||||
| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset |
|
||||
|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------|
|
||||
| f0 de bc 0a | 01 00 00 00 | 28 01 00 00 | 00 00 00 01 | 00 | 00 | 14 64 | 00 00 00 00 |
|
||||
|
||||
|
||||
- Magic 4 bytes
|
||||
- ID 4 bytes
|
||||
- Message length 4 bytes
|
||||
- Encryption offset 4 bytes
|
||||
- Encryption flag (not always honoured by clients) 1 byte
|
||||
- Unknown 1 byte (always zero)
|
||||
- Binary offset 4 bytes (Presence depend on message class)
|
||||
|
||||
#### Magic
|
||||
|
||||
The magic bytes for BC messages is always `f0 de bc 0a`. When receiving packet
|
||||
this should be used to quickly discard invalid packets.
|
||||
|
||||
#### Message ID
|
||||
|
||||
Each function in BC has its own message ID. For example login is 1, video data
|
||||
is 3, motion detection is 33.
|
||||
|
||||
For a more complete list please see the [messages doc](dissector/messages.md)
|
||||
|
||||
#### Message length
|
||||
|
||||
The message length contains the full length of the data to follow the header
|
||||
this includes both the XML and binary parts.
|
||||
|
||||
#### Encryption offset
|
||||
|
||||
The encryption offset is used as part of the decoding process. It is combined
|
||||
with the key to decrypt the data.
|
||||
|
||||
Here is an example decrypter in rust
|
||||
|
||||
```rust
|
||||
const XML_KEY: [u8; 8] = [0x1F, 0x2D, 0x3C, 0x4B, 0x5A, 0x69, 0x78, 0xFF];
|
||||
|
||||
pub fn crypt(offset: u32, buf: &[u8]) -> Vec<u8> {
|
||||
let key_iter = XML_KEY.iter().cycle().skip(offset as usize % 8);
|
||||
key_iter
|
||||
.zip(buf)
|
||||
.map(|(key, i)| *i ^ key ^ (offset as u8))
|
||||
.collect()
|
||||
}
|
||||
```
|
||||
|
||||
In short the key is offset by the encryption offset in the header. Then each
|
||||
encrypted byte is paired with the offseted keys bytes (looping the offseted
|
||||
key as necessary). Then each byte is XORed with the paired key byte and the
|
||||
offset.
|
||||
|
||||
The key is the same for all cameras.
|
||||
|
||||
Older cameras do not use encryption and all messages are sent as plain text.
|
||||
|
||||
#### Encryption Flag
|
||||
|
||||
This flag is 0 for unencrypted or non zero for encrypted. This flag seems
|
||||
only to be valid during the login step, and seems to be arbitrary otherwise.
|
||||
|
||||
Clients should decide to use encryption or not based on this byte that the
|
||||
camera send during login. Then should use that same setting for all further
|
||||
messages with the camera.
|
||||
|
||||
#### Unknown
|
||||
|
||||
The purpose of this byte is unknown but is always zero. If you figure out
|
||||
what it does please submit a PR.
|
||||
|
||||
#### Message class
|
||||
|
||||
The message class determines the length of the header. The following classes
|
||||
and header lengths are known.
|
||||
|
||||
- 0x6514: "legacy" 20 bytes
|
||||
|
||||
- 0x6614: "modern" 20 bytes
|
||||
|
||||
- 0x6414: "modern" 24 bytes
|
||||
|
||||
- 0x0000: "modern" 24 bytes
|
||||
|
||||
#### Binary offset
|
||||
|
||||
For messages that contain the binary offset field this represents where to start
|
||||
the binary part of the message. The total length of XML + binary is equal
|
||||
to the message length in the header so Binary Offset is
|
||||
total_length - this_offset. Where as this field also represents the end of the
|
||||
XML part of the message.
|
||||
|
||||
Some messages seems to not set this field correctly. For example message ID 1
|
||||
sets this to zero meaning that all data in the message is binary, however
|
||||
it contains XML. This best way to tell is this is binary or XML is to check the
|
||||
message ID and determine from there. Alternatively you could look for `<xml?` at
|
||||
the beginning of the message (although may need to decrypt first for this to
|
||||
work)
|
||||
|
||||
# Login
|
||||
|
||||
For message details see the [docs](dissector/messages.md)
|
||||
|
||||
Clients should login by
|
||||
|
||||
- Send legacy login message
|
||||
- User and pass MD5'ed
|
||||
- Capped at 32 bytes with a null terminator
|
||||
- Bytes 32 is always zero so only first 31 bytes are compared
|
||||
|
||||
- Receive modern upgrade message with nonce in XML
|
||||
|
||||
- Send modern login:
|
||||
- User and pass concatenated with the nonce
|
||||
- Send MD5'ed user and password
|
||||
|
||||
- Receive reply with device info
|
||||
|
||||
# Starting Video
|
||||
|
||||
Video is requested and received with message ID 3.
|
||||
|
||||
Video is requested with an XML of the following format:
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<Preview version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<handle>0</handle>
|
||||
<streamType>mainStream</streamType>
|
||||
</Preview>
|
||||
</body>
|
||||
```
|
||||
|
||||
streamType can be either
|
||||
|
||||
- `mainStream` in which case it will be HD
|
||||
- `subStream` in which case it will be SD
|
||||
|
||||
|
||||
channelId is part of the NVR when multiple cameras use the same IP. In this
|
||||
case each camera has its own channelId.
|
||||
|
||||
Not sure what handle does.
|
||||
|
||||
|
||||
The reply is first a message with the following xml
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<binaryData>1</binaryData>
|
||||
</Extension>
|
||||
```
|
||||
|
||||
After which all message bodies of type id 3 are binary.
|
||||
|
||||
The binary represents a stream of data. It can be interrupted by packet
|
||||
boundaries. Clients should create a buffer and pop bytes for processing when
|
||||
complete media packets are received. Media packets descriptions can be found in
|
||||
the [docs](dissector/mediapacket.md)
|
||||
|
||||
# Other Function
|
||||
|
||||
Other data can be received from the camera by sending the appropriate header to
|
||||
the camera. For example sending the header for ID 78
|
||||
|
||||
| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset |
|
||||
|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------|
|
||||
| f0 de bc 0a | 4e 00 00 00 | d3 00 00 00 | 08 db 9c 00 | c8 | 00 | 00 00 | 00 00 00 00 |
|
||||
|
||||
The camera will reply with an xml with brightness and contrast
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<body>
|
||||
<VideoInput version="1.1">
|
||||
<channelId>0</channelId>
|
||||
<bright>128</bright>
|
||||
<contrast>128</contrast>
|
||||
<saturation>128</saturation>
|
||||
<hue>128</hue>
|
||||
</VideoInput>
|
||||
</body>
|
||||
```
|
||||
|
||||
Some message IDs also require input along with the request header. For example
|
||||
|
||||
ID 151 which is the users ability info requires the header
|
||||
|
||||
| Magic | Message ID | Message Length | Encryption Offset | Encryption Flag | Unknown | Message Class | Binary Offset |
|
||||
|--------------|--------------|----------------|-------------------|-----------------|---------|---------------|---------------|
|
||||
| f0 de bc 0a | 97 00 00 00 | a7 00 00 00 | 00 00 00 02 | 00 | 00 | 14 64 | a7 00 00 00 |
|
||||
|
||||
and the body of
|
||||
|
||||
```xml
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<Extension version="1.1">
|
||||
<userName>...</userName> <!-- Plain text username -->
|
||||
<token>system, network, alarm, record, video, image</token>
|
||||
</Extension>
|
||||
```
|
||||
|
||||
Which contains the plain text of the username of interest and the tokens for
|
||||
abilities you want to know about.
|
||||
|
||||
Details of expected formats should be found from the
|
||||
[docs](dissector/messages.md)
|
||||
Reference in new issue
Block a user