-
Notifications
You must be signed in to change notification settings - Fork 0
/
openwebnet_dissector.lua
346 lines (292 loc) · 8.21 KB
/
openwebnet_dissector.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
-- cmd aren't actually openwebnet commands, but are used to identify the type of message
local cmd = {
[1] = "OPEN_SESSION",
[2] = "HMAC",
[3] = "NONCE",
[4] = "PASSWORD",
[5] = "ACK",
[6] = "NACK",
[7] = "NORMAL",
[8] = "STATUS_REQUEST",
[9] = "DIMENSION_REQUEST",
[10] = "DIMENSION_WRITING"
}
local session = {
[0] = "COMMAND_SESSION",
[1] = "EVENT_SESSION"
}
local auth_type = {
[1] = "SHA1",
[2] = "SHA256"
}
-- from documentation
local who = {
[0] = "Scenarios",
[1] = "Lighting",
[2] = "Automation",
[3] = "Power Management",
[4] = "Temperature Control",
[5] = "Alarm",
[6] = "VDES",
[7] = "Video Door Entry System/multimedia",
[9] = "Auxiliary",
[13] = "Gateway Management",
[14] = "Light+shutters actuators lock",
[15] = "CEN commands",
[16] = "Sound System/Audio",
[17] = "MH200N scenarios",
[18] = "Energy management",
[22] = "Sound diffusion",
[24] = "Lighting management",
[25] = "CEN plus / scenarios plus / dry contacts",
[1001] = "Automation diagnostic",
[1004] = "Thermoregulation diagnostic",
[1013] = "Device diagnostic"
}
local own = Proto("own", "OpenWebNet protocol")
local f = own.fields
f.msg = ProtoField.string("own.msg", "Message")
f.cmd = ProtoField.uint8("own.cmd", "Command", base.HEX, cmd)
f.session = ProtoField.uint8("own.session", "Session Type", base.DEC, session)
f.auth_type = ProtoField.uint8("own.auth_type", "Auth Type", base.DEC, auth_type)
f.nonce = ProtoField.uint16("own.nonce", "Nonce", base.DEC)
f.password = ProtoField.uint16("own.password", "Password", base.DEC)
f.who = ProtoField.uint16("own.who", "Who", base.DEC, who)
f.what = ProtoField.string("own.what", "What")
f.where = ProtoField.string("own.where", "Where")
f.dimension = ProtoField.string("own.dimension", "Dimension")
f.values = ProtoField.string("own.value", "Values")
f.value = ProtoField.string("own.value", "Value")
f.tag = ProtoField.uint8("own.tag", "Tag", base.DEC)
f.param = ProtoField.uint8("own.param", "Param", base.DEC)
function own.dissector(buffer, pkt_info, root_tree)
if buffer:len() < 4 then return end
pkt_info.cols.protocol = "OpenWebNet"
main_tree = root_tree:add(own, buffer)
local payload = buffer(0, buffer:len()):string()
local prev_i = 0
local i = 0
while true do
_, i = string.find(payload, "##", i+1)
if i == nil then break end
msg = buffer(prev_i, i-prev_i):string()
prev_i = i
msg_tree = main_tree:add(f.msg, msg)
dissect_message(msg, pkt_info, msg_tree)
end
end
function dissect_message(msg, pkt_info, main_tree)
if m_open_session(msg) then
main_tree:add(f.cmd, 1)
local type = string.match(msg, "^[*]99[*](%d)##")
main_tree:add(f.session, tonumber(type))
elseif m_rqt_hmac(msg) then
main_tree:add(f.cmd, 2)
local type = string.match(msg, "^[*]98[*](%d)##")
main_tree:add(f.auth_type, tonumber(type))
elseif m_nonce(msg) then
local nonce = string.match(msg, "%d+")
if pkt_info.src_port == 20000 then
main_tree:add(f.cmd, 3)
main_tree:add(f.nonce, tonumber(nonce))
else
main_tree:add(f.cmd, 4)
main_tree:add(f.password, tonumber(nonce))
end
elseif m_ack(msg) then
main_tree:add(f.cmd, 5)
elseif m_nack(msg) then
main_tree:add(f.cmd, 6)
elseif m_norm(msg) then
main_tree:add(f.cmd, 7)
local t = parse_message(msg)
main_tree:add(f.who, t[1])
parse_what(t[2], main_tree)
parse_where(t[3], main_tree)
elseif m_sts(msg) then
main_tree:add(f.cmd, 8)
local t = parse_message(msg)
main_tree:add(f.who, t[1])
parse_where(t[2], main_tree)
elseif m_dim_req(msg) then
main_tree:add(f.cmd,9)
local t = parse_message(msg)
main_tree:add(f.who, t[1])
parse_where(t[2], main_tree)
main_tree:add(f.dimension, t[3])
elseif m_dim_wtr(msg) then
main_tree:add(f.cmd, 10)
local t = parse_message(msg)
main_tree:add(f.who, t[1])
parse_where(t[2], main_tree)
parse_dimension(t[3], main_tree)
parse_values(t, main_tree)
end
end
function parse_values(table, tree)
local field = ""
for i, value in ipairs(table) do
if i > 3 then
if i == 4 then
field = value
else
field = field .. "*".. value
end
end
end
local values = tree:add(f.values, field)
for i, value in ipairs(table) do
if i > 3 then
values:add(f.value, value)
end
end
end
function parse_where(field, tree)
if field == nil or field == "" then return end
local t = parse_params(field)
local where_tree = tree:add(f.where, field)
if tag then
where_tree:add(f.tag, t[1])
end
for i, value in ipairs(t) do
if i ~= 1 or tag ~= true then
where_tree:add(f.param, value)
end
end
end
function parse_what(field, tree)
if field == nil or field == "" then return end
local t = parse_params(field)
local what_tree = tree:add(f.what, field)
if tag then
what_tree:add(f.tag, t[1])
end
for i, value in ipairs(t) do
if i ~= 1 or tag ~= true then
what_tree:add(f.param, value)
end
end
end
function parse_dimension(field, tree)
if field == nil or field == "" then return end
t, tag = parse_params(field)
local dim_tree = tree:add(f.dimension, field)
if tag then
dim_tree:add(f.tag, t[1])
end
for i, value in ipairs(t) do
if i ~= 1 or tag ~= true then
dim_tree:add(f.param, value)
end
end
end
function parse_params(field)
local t = {}
local tag = false
t[1] = tonumber(string.match(field, "^([0-9]*)"))
if t[1] ~= nil then
tag = true
end
for str in string.gmatch(field, "#([0-9]*)") do
if str:len() > 0 and str ~= nil then
table.insert(t, tonumber(str))
end
end
return t, tag
end
function parse_message(msg)
local t = {}
local i = 0
for str in string.gmatch(msg, "([*][0-9#]*)") do
if str:len() > 0 then
if i == 0 then
local p = string.match(str, "[*]#?(%d*)")
table.insert(t, tonumber(p))
elseif str == "*" then
table.insert(t, "")
else
local p = string.match(str, "[*](.*)")
if string.match(p, "##$") then
p = string.match(p, "(.*)##$")
end
table.insert(t, p)
end
i = i + 1
end
end
return t
end
function m_open_session(string)
local str = string:match("^[*]99[*]%d##")
if str then
return true
else
return false
end
end
function m_rqt_hmac(string)
local str = string:match("^[*]98[*]%d##")
if str then
return true
else
return false
end
end
function m_nonce(string)
local str = string:match("^[*]#%d+##")
if str then
return true
else
return false
end
end
function m_ack(string)
local str = string:match("^[*]#[*]1##")
if str then
return true
else
return false
end
end
function m_nack(string)
local str = string:match("^[*]#[*]0##")
if str then
return true
else
return false
end
end
function m_norm(string)
local str = string:match("^[*]%d+[*]%d*[#%d]*[*]%d+[#%d]*##")
if str then
return true
else
return false
end
end
function m_sts(string)
local str = string:match("^[*]#%d+[*]%d*[#%d]*##")
if str then
return true
else
return false
end
end
function m_dim_req(string)
local str = string:match("^[*]#%d+[*]%d*[#%d]*[*]%d+[#%d]*##")
if str then
return true
else
return false
end
end
function m_dim_wtr(string)
local str = string:match("^[*]#%d+[*]%d*[#%d]*[*]%d*[#%d]*[%d*]*##")
if str then
return true
else
return false
end
end
tcp_table = DissectorTable.get("tcp.port")
tcp_table:add(20000, own)