-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbmw_bcl.lua
executable file
·458 lines (390 loc) · 16 KB
/
bmw_bcl.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
print("hello world!")
local debug_level = {
DISABLED = 0,
LEVEL_1 = 1,
LEVEL_2 = 2
}
local DEBUG = debug_level.LEVEL_1
local dprint = function() end
local dprint2 = function() end
local function resetDebugLevel()
current_debug_level = 2
if current_debug_level > debug_level.DISABLED then
dprint = function(...)
print("INFO: " .. table.concat({"BMW BCL: ", ...}," "))
end
if current_debug_level > debug_level.LEVEL_1 then
dprint2 = dprint
end
else
dprint = function() end
dprint2 = dprint
end
end
-- call it now
resetDebugLevel()
local bmw_proto = Proto("bmw", "BMW BCL")
local COMMAND_NAMES = {}
COMMAND_NAMES[1] = "OPEN"
COMMAND_NAMES[2] = "DATA"
COMMAND_NAMES[3] = "CLOSE"
COMMAND_NAMES[4] = "HANDSHAKE"
COMMAND_NAMES[5] = "SELECTPROTO"
COMMAND_NAMES[6] = "DATAACK"
COMMAND_NAMES[7] = "ACK"
COMMAND_NAMES[8] = "KNOCK"
COMMAND_NAMES[9] = "LAUNCH"
COMMAND_NAMES[10] = "HANGUP"
COMMAND_NAMES[11] = "BROADCAST"
COMMAND_NAMES[12] = "REGISTER"
local APPTYPE_NAMES = {}
APPTYPE_NAMES[0] = "A4A"
APPTYPE_NAMES[1] = "TouchCommand"
APPTYPE_NAMES[2] = "Carplay"
APPTYPE_NAMES[3] = "AndroidAuto"
local DST_NAMES = {}
DST_NAMES[0x0FA4] = "Etch"
DST_NAMES[0x1389] = "Watchdog"
local hdr_fields =
{
command = ProtoField.uint16 ("bmw.command", "Command", base.DEC, COMMAND_NAMES),
src = ProtoField.uint16 ("bmw.src", "Source", base.DEC),
dst = ProtoField.uint16 ("bmw.dst", "Dest", base.DEC, DST_NAMES),
len = ProtoField.uint16 ("bmw.len", "Length", base.DEC),
-- startup
magic = ProtoField.bytes("bmw.magic", "Magic", base.COLON),
-- knock fields
serialNumber = ProtoField.string ("bmw.serial", "Serial"),
btAddr = ProtoField.bytes ("bmw.btaddr", "BT Addr", base.COLON),
macAddr = ProtoField.bytes ("bmw.macaddr", "MAC Addr", base.COLON),
wifiAddr = ProtoField.bytes ("bmw.wifiaddr", "WIFI Addr", base.COLON),
appType = ProtoField.uint16 ("bmw.appType", "App Type", base.DEC, APPTYPE_NAMES),
param1 = ProtoField.uint16 ("bmw.param1", "Param", base.DEC),
-- dataack field
dataAck = ProtoField.uint32 ("bmw.ack", "Ack", base.DEC)
}
bmw_proto.fields = hdr_fields
dprint2("bmw_proto ProtoFields registered")
local partial_packets = {} -- currently assembling this packet, during the first pass, keyed by bluetooth direction+channel
local assembled_packets = {} -- assembly state for each packet (and subpacket), keyed by pktinfo.number and packet offset
local etch_channels = {} -- once etch traffic is seen on a channel, keep parsing that channel as etch
function bmw_proto.init()
partial_packets = {}
assembled_packets = {}
end
local BMW_MSG_HDR_LEN = 8
-- mention future helper methods
local check_bmw_length
local rfcomm_fields = {
direction = Field.new("btrfcomm.direction"),
command = Field.new("btrfcomm.cr"),
channel = Field.new("btrfcomm.channel")
}
function btrfcomm_directed_channel(pktinfo)
local direction = rfcomm_fields.direction()()
local command = rfcomm_fields.command()()
local channel = rfcomm_fields.channel()()
dprint2("btrfcomm_directed_channel decided " .. tostring(direction) .. ":" .. tostring(command) .. ":" .. tostring(channel))
if direction == 0 then
direction = ">"
end
if command == 1 then
command = ">"
end
return direction .. command .. tostring(channel)
end
local function heuristic(tvbuf, pktinfo, root)
local pktlen = tvbuf:len()
if pktlen == 4 and tvbuf:bytes():tohex() == "12345678" then
return true
end
if pktlen < 8 then
return false
end
-- check if this packet is parseable
local magic = tvbuf:bytes(5,1):tohex()
if magic == "89" or magic == "A4" then
-- found a packet we know how to handle
return true
end
-- next check if the length is right
local offset = 0
local valid_size = true
while offset < tvbuf:len() do
local parsed_length = tvbuf:range(offset + 6,2):uint()
if pktlen < offset + BMW_MSG_HDR_LEN + parsed_length then
valid_size = false
end
offset = offset + BMW_MSG_HDR_LEN + parsed_length
end
if offset == tvbuf:len() then
-- multiple sub packets, all with the correct size
return true
end
-- check if we have an etch packet
ETCH_MAGIC = ByteArray.new("de ad be ef")
if tvbuf:len() >= BMW_MSG_HDR_LEN + 4 then
local etch_magic = tvbuf:bytes(BMW_MSG_HDR_LEN, 4)
return etch_magic == ETCH_MAGIC
end
return false
end
-- so sometimes a single SPP packet contains multiple BCL packets
-- the main dissector function goes through the SPP and identifies subpackets
-- and calls a function to dissect a single subpacket at a time
function bmw_proto.dissector(tvbuf, pktinfo, root)
--dprint2("bmw_proto.dissector called")
-- load up packet address
local address = btrfcomm_directed_channel(pktinfo)
-- check packet length
local pktlen = tvbuf:len()
if pktlen == 4 and tvbuf:bytes():tohex() == "12345678" then
pktinfo.cols.protocol:set("BMW BCL")
pktinfo.cols.info:set("BMW BCL Startup")
local tree = root:add(bmw_proto, tvbuf:range(0, 4))
tree:add(hdr_fields.magic, tvbuf:range(0, 4))
return 4
end
-- if we have the start of a new packet, not part of ongoing assembly, check if we can handle it
local packet_number = tostring(pktinfo.number) .. ":" .. tostring(0)
if partial_packets[address] == nil and assembled_packets[packet_number] == nil then
-- check if we know how to handle this packet
local understandable = heuristic(tvbuf, pktinfo, root)
if understandable == false then
dprint2("Don't know how to handle this packet: " .. tvbuf:bytes(0,8):tohex())
return 0
end
end
-- begin processing the packet(s)
local consumed = 0
while consumed < pktlen do
consumed = consumed + dissect_subpackets(tvbuf, pktinfo, root, consumed)
end
return consumed
end
function dissect_subpackets(tvbuf, pktinfo, root, offset)
local address = btrfcomm_directed_channel(pktinfo)
local pktlen = tvbuf:len()
-- load up any reassembly state for this packet
local packet_number = tostring(pktinfo.number) .. ":" .. tostring(offset)
local state = assembled_packets[packet_number]
local packet_fragment_type = -1
if state ~= nil then
packet_fragment_type = state.partial_type
dprint2("Already know that this packet is fragment type " .. tostring(packet_fragment_type))
end
-- handle a new packet
if packet_fragment_type ~= -1 and partial_packets[address] ~= nil then
dprint2("Encountered the start of a known new packet while collecting reassembly!")
partial_packets[address] = nil
end
if packet_fragment_type < 2 and partial_packets[address] == nil then
local data_length = tvbuf:range(offset+6,2):uint()
if pktlen >= offset + BMW_MSG_HDR_LEN + data_length then
dprint2("Parsing complete packet on channel " .. tostring(address))
local result = dissect_full_packet(tvbuf, pktinfo, root, offset)
if offset > 0 or pktlen > offset + BMW_MSG_HDR_LEN + data_length then
if tostring(pktinfo.cols.protocol) == "BMW BCL" and string.find(tostring(pktinfo.cols.info), "multiple packets") == nil then
pktinfo.cols.info:set(tostring(pktinfo.cols.info) .. ", multiple packets")
end
end
state = {
partial_type = 0,
start_size = 0,
total_size = BMW_MSG_HDR_LEN + data_length
}
assembled_packets[packet_number] = state
return result
else
-- save the packet for reassembly
dprint2("Saving packet for future assembly on channel " .. tostring(address))
if state == nil then
partial_packets[address] = {}
partial_packets[address].command = tvbuf:range(offset+0, 2):uint()
partial_packets[address].src = tvbuf:range(offset+2, 2):uint()
partial_packets[address].dst = tvbuf:range(offset+4, 2):uint()
partial_packets[address].number = pktinfo.number
partial_packets[address].bytes = tvbuf:bytes(offset)
partial_packets[address].total_size = BMW_MSG_HDR_LEN + data_length
state = {
partial_type = 1,
start_size = 0,
total_size = partial_packets[address].total_size
}
assembled_packets[packet_number] = state
end
local result = dissect_start_fragment_packet(tvbuf, pktinfo, root, offset)
return result
end
end
-- parsing a partial packet
local reassembly = partial_packets[address] -- first pass assembly state
local total_size
local current_size
if state == nil then -- first time seeing the packet, use the assembly state
total_size = reassembly.total_size
current_size = reassembly.bytes:len()
else -- already parsed the packet, use what was remembered before
total_size = state.total_size
current_size = state.start_size
end
local needed_size = total_size - current_size
local this_packet_size = math.min(pktlen-offset, needed_size)
local received_size = current_size + this_packet_size
if received_size < total_size then
-- accrue more data
if state == nil then
state = {
command = reassembly.command,
src = reassembly.src,
dst = reassembly.dst,
partial_type = 2,
start_size = reassembly.bytes:len(),
total_size = reassembly.total_size
}
assembled_packets[packet_number] = state
-- assemble the packet
reassembly.bytes:append(tvbuf:bytes(offset, this_packet_size))
end
dprint2("Collecting more data on src " .. tostring(address) .. " - " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size))
pktinfo.cols.protocol:set("BMW BCL")
if string.find(tostring(pktinfo.cols.info), "^BMW") == nil then
local info = "BMW BCL (continuing fragment " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size) .. ")"
pktinfo.cols.info:set(info)
end
if root ~= nil then
local tree = root:add(bmw_proto, tvbuf:range(offset, this_packet_size))
tree:add(hdr_fields.command, state.command)
tree:add(hdr_fields.src, state.src)
tree:add(hdr_fields.dst, state.dst)
tree:add(hdr_fields.len, state.total_size)
tree:add(tvbuf:range(offset, this_packet_size), "[Fragment of Data " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size) .. "]")
end
return this_packet_size
elseif received_size >= total_size then
-- make an assembled packet and analyze it
if state == nil then
state = {
command = reassembly.command,
src = reassembly.src,
dst = reassembly.dst,
partial_type = 3,
start_size = reassembly.bytes:len(),
total_size = reassembly.total_size
}
assembled_packets[packet_number] = state
-- assemble the packet
reassembly.bytes:append(tvbuf:bytes(offset, this_packet_size))
state.assembled_bytes = reassembly.bytes
partial_packets[address] = nil
end
dprint2("Found a complete packet on src " .. tostring(address) .. " - " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size))
if string.find(tostring(pktinfo.cols.info), "^BMW") == nil then
local info = "BMW BCL (final fragment " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size) .. ")"
pktinfo.cols.info:set(info)
end
if root ~= nil then
local tree = root:add(bmw_proto, tvbuf:range(offset, this_packet_size))
tree:add(hdr_fields.command, state.command)
tree:add(hdr_fields.src, state.src)
tree:add(hdr_fields.dst, state.dst)
tree:add(hdr_fields.len, state.total_size)
tree:add(tvbuf:range(offset, this_packet_size), "[Fragment of Data " .. tostring(state.start_size) .. "-" .. tostring(state.start_size + this_packet_size) .. "/" .. tostring(state.total_size) .. "]")
end
local assembled_tvbuf = state.assembled_bytes:tvb("SPP Assembled")
local result = dissect_full_packet(assembled_tvbuf, pktinfo, root, 0)
return needed_size
end
end
function dissect_full_packet(tvbuf, pktinfo, root, offset)
dprint2("dissect_full_packet function called at offset " .. tostring(offset))
dprint2(tvbuf:bytes(offset):tohex())
-- update the packet list info
if tostring(pktinfo.cols.protocol) ~= "ETCH" then
pktinfo.cols.protocol:set("BMW BCL")
end
if tostring(pktinfo.cols.protocol) == "BMW BCL" and string.find(tostring(pktinfo.cols.info), "^BMW") == nil then
pktinfo.cols.info:set("BMW BCL")
end
local command = tvbuf:range(offset+0, 2):uint()
local src = tvbuf:range(offset+2, 2):uint()
local dst = tvbuf:range(offset+4, 2):uint()
local data_len = tvbuf:range(offset+6, 2):uint()
remaining_tvb = tvbuf(offset + BMW_MSG_HDR_LEN, data_len):tvb()
local tree
if root ~= nil then
-- create the protocol tree field
tree = root:add(bmw_proto, tvbuf:range(offset, BMW_MSG_HDR_LEN + data_len))
-- get the vals
tree:add(hdr_fields.command, tvbuf:range(offset+0, 2))
tree:add(hdr_fields.src, tvbuf:range(offset+2, 2))
tree:add(hdr_fields.dst, tvbuf:range(offset+4, 2))
tree:add(hdr_fields.len, tvbuf:range(offset+6, 2))
end
-- try to parse the inner data
ETCH_MAGIC = ByteArray.new("de ad be ef")
local is_etch = data_len > 4 and remaining_tvb:bytes(0,4) == ETCH_MAGIC
local dataoffset = offset + BMW_MSG_HDR_LEN
if tree ~= nil and command == 8 then
-- knock command
local field_len
field_len = tvbuf:range(dataoffset, 2):uint()
tree:add(hdr_fields.serialNumber, tvbuf:range(dataoffset+2, field_len))
dataoffset = dataoffset + 2 + field_len
field_len = tvbuf:range(dataoffset, 2):uint()
tree:add(hdr_fields.btAddr, tvbuf:range(dataoffset+2, field_len))
dataoffset = dataoffset + 2 + field_len
field_len = tvbuf:range(dataoffset, 2):uint()
tree:add(hdr_fields.macAddr, tvbuf:range(dataoffset+2, field_len))
dataoffset = dataoffset + 2 + field_len
field_len = tvbuf:range(dataoffset, 2):uint()
tree:add(hdr_fields.wifiAddr, tvbuf:range(dataoffset+2, field_len))
dataoffset = dataoffset + 2 + field_len
tree:add(hdr_fields.appType, tvbuf:range(dataoffset+0, 2))
tree:add(hdr_fields.param1, tvbuf:range(dataoffset+2, 2))
elseif tree ~= nil and command == 6 then
-- dataack command
tree:add(hdr_fields.dataAck, tvbuf:range(dataoffset, 4))
elseif etch_channels[src] or is_etch then
-- etch data
etch_channels[src] = true
local dissect_etch = Dissector.get("bmw_bcl_etch")
dissect_etch:call(tvbuf(offset, data_len + BMW_MSG_HDR_LEN):tvb(), pktinfo, root)
else
local dissect_data = Dissector.get("data")
dissect_data:call(remaining_tvb, pktinfo, root)
end
return BMW_MSG_HDR_LEN + data_len
end
function dissect_start_fragment_packet(tvbuf, pktinfo, root, offset)
dprint2("dissect_start_fragment_packet function called at offset " .. tostring(offset))
dprint2(tvbuf:bytes(offset):tohex())
data_len = tvbuf:range(offset+6, 2):uint()
-- update the packet list info
if tostring(pktinfo.cols.protocol) ~= "ETCH" then
pktinfo.cols.protocol:set("BMW BCL")
end
if tostring(pktinfo.cols.protocol) == "BMW BCL" then
local info = "(initial fragment 0-" .. tostring(tvbuf:len() - offset) .. "/" .. tostring(BMW_MSG_HDR_LEN + data_len) .. ")"
if string.find(tostring(pktinfo.cols.info), "^BMW") == nil then
pktinfo.cols.info:set("BMW BCL " .. info)
else
-- found a partial packet at the end of a combined packet
pktinfo.cols.info:set(tostring(pktinfo.cols.info) .. ", " .. info)
end
end
-- create the protocol tree field
local tree = root:add(bmw_proto, tvbuf:range(offset))
-- get the vals
tree:add(hdr_fields.command, tvbuf:range(offset+0, 2))
tree:add(hdr_fields.src, tvbuf:range(offset+2, 2))
tree:add(hdr_fields.dst, tvbuf:range(offset+4, 2))
tree:add(hdr_fields.len, tvbuf:range(offset+6, 2))
tree:add(tvbuf:range(offset + BMW_MSG_HDR_LEN), "[Fragment of Data 0-" .. tostring(tvbuf:len() - offset) .. "/" .. tostring(BMW_MSG_HDR_LEN + data_len) .. "]")
return tvbuf:len() - offset
end
function enable_dissector()
DissectorTable.get("btrfcomm.dlci"):add_for_decode_as(bmw_proto)
end
enable_dissector()
--bmw_proto:register_heuristic("btspp", heuristic)