forked from cisagov/icsnpp-bacnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.zeek
516 lines (440 loc) · 21.5 KB
/
main.zeek
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
##! main.zeek
##!
##! Binpac BACnet Protocol Analyzer - Contains the base script-layer functionality for processing
##! events emitted from the analyzer.
##!
##! Author: Stephen Kleinheider
##! Contact: [email protected]
##!
##! Copyright (c) 2020 Battelle Energy Alliance, LLC. All rights reserved.
module Bacnet;
export {
redef enum Log::ID += { LOG_BACNET,
LOG_BACNET_DISCOVERY,
LOG_BACNET_PROPERTY};
###############################################################################################
################################ BACnet_Header -> bacnet.log ################################
###############################################################################################
type BACnet_Header: record {
ts : time &log; # Timestamp of event
uid : string &log; # Zeek unique ID for connection
id : conn_id &log; # Zeek connection struct (addresses and ports)
is_orig : bool &log; # the message came from the originator/client or the responder/server
bvlc_function : string &log; # BVLC function (see bvlc_functions)
pdu_type : string &log; # APDU type (see apdu_types)
pdu_service : string &log; # APDU service (see unconfirmed_service_choice and confirmed_service_choice)
invoke_id : count &log; # Invoke ID
result_code : string &log; # See (abort_reasons, reject_reasons, and error_codes)
};
global log_bacnet: event(rec: BACnet_Header);
###############################################################################################
################## Who-Is, I-Am, Who-Has, & I-Have -> bacnet_discovery.log ##################
###############################################################################################
type BACnet_Discovery: record {
ts : time &log; # Timestamp of event
uid : string &log; # Zeek unique ID for connection
id : conn_id &log; # Zeek connection struct (addresses and ports)
is_orig : bool &log; # the message came from the originator/client or the responder/server
pdu_service : string &log; # who-is, i-am, who-has, or i-have
object_type : string &log; # BACnetObjectIdentifier object (see object_types)
instance_number : count &log; # BACnetObjectIdentifier instance number
vendor : string &log; # Vendor Name (i-am and i-have requests)
range : string &log; # Specify range of devices to return (in who-is and who-has requests)
object_name : string &log; # Object name searching for (who-has) or responding with (i-have)
};
global log_bacnet_discovery: event(rec: BACnet_Discovery);
###############################################################################################
################### Read-Property & Write-Property -> bacnet_property.log ###################
###############################################################################################
type BACnet_Property: record {
ts : time &log; # Timestamp of event
uid : string &log; # Zeek unique ID for connection
id : conn_id &log; # Zeek connection struct (addresses and ports)
is_orig : bool &log; # the message came from the originator/client or the responder/server
pdu_service : string &log; # read-property-request/ack, write-property-request
object_type : string &log; # BACnetObjectIdentifier object (see object_types)
instance_number : count &log; # BACnetObjectIdentifier instance number
property : string &log; # Property type (see property_identifiers)
array_index : count &log; # Array index of property
value : string &log; # Value of property
};
global log_bacnet_property: event(rec: BACnet_Property);
}
## Defines BACnet Ports
const ports = { 47808/udp };
redef likely_server_ports += { ports };
###################################################################################################
####### Defines Log Streams for bacnet.log, bacnet_discovery.log, and bacnet_property.log #######
###################################################################################################
event zeek_init() &priority=5{
Log::create_stream(Bacnet::LOG_BACNET, [$columns=BACnet_Header,
$ev=log_bacnet,
$path="bacnet"]);
Log::create_stream(Bacnet::LOG_BACNET_DISCOVERY, [$columns=BACnet_Discovery,
$ev=log_bacnet_discovery,
$path="bacnet_discovery"]);
Log::create_stream(Bacnet::LOG_BACNET_PROPERTY, [$columns=BACnet_Property,
$ev=log_bacnet_property,
$path="bacnet_property"]);
Analyzer::register_for_ports(Analyzer::ANALYZER_BACNET, ports);
}
###################################################################################################
####### Ensure that conn.log:service is set if it has not already been #######
###################################################################################################
function set_service(c: connection) {
if ((!c?$service) || (|c$service| == 0))
add c$service["bacnet"];
}
###################################################################################################
##################### Defines logging of bacnet_header event -> bacnet.log ######################
###################################################################################################
event bacnet_header(c: connection,
is_orig: bool,
bvlc_function: count,
pdu_type: count,
pdu_service: count,
invoke_id: count,
result_code: count){
set_service(c);
local bacnet_log: BACnet_Header;
bacnet_log$is_orig = is_orig;
bacnet_log$ts = network_time();
bacnet_log$uid = c$uid;
bacnet_log$id = c$id;
bacnet_log$bvlc_function = bvlc_functions[bvlc_function];
if (bvlc_function == 0)
bacnet_log$result_code = bvlc_results[result_code];
if(pdu_type in apdu_types){
bacnet_log$pdu_type = apdu_types[pdu_type];
if (pdu_type != 1)
bacnet_log$invoke_id = invoke_id;
}
switch(pdu_type){
case 5:
bacnet_log$result_code = error_codes[result_code];
fallthrough;
case 0:
fallthrough;
case 2:
fallthrough;
case 3:
fallthrough;
case 4:
bacnet_log$pdu_service = confirmed_service_choice[pdu_service];
break;
case 1:
bacnet_log$pdu_service = unconfirmed_service_choice[pdu_service];
break;
case 6:
bacnet_log$result_code = reject_reasons[result_code];
break;
case 7:
bacnet_log$result_code = abort_reasons[result_code];
break;
default:
break;
}
Log::write(LOG_BACNET, bacnet_log);
}
###################################################################################################
################ Defines logging of bacnet_who_is event -> bacnet_discovery.log #################
###################################################################################################
event bacnet_who_is(c: connection,
is_orig: bool,
low_limit: count,
high_limit: count){
set_service(c);
local bacnet_discovery: BACnet_Discovery;
bacnet_discovery$is_orig = is_orig;
bacnet_discovery$ts = network_time();
bacnet_discovery$uid = c$uid;
bacnet_discovery$id = c$id;
bacnet_discovery$pdu_service = "who-is";
if(low_limit == UINT32_MAX)
bacnet_discovery$range = "All";
else
bacnet_discovery$range = fmt("%d-%d", low_limit, high_limit);
Log::write(LOG_BACNET_DISCOVERY, bacnet_discovery);
}
###################################################################################################
################# Defines logging of bacnet_i_am event -> bacnet_discovery.log ##################
###################################################################################################
event bacnet_i_am(c: connection,
is_orig: bool,
object_type: count,
instance_number: count,
max_apdu: count,
segmentation: count,
vendor_id: count){
set_service(c);
local bacnet_discovery: BACnet_Discovery;
bacnet_discovery$is_orig = is_orig;
bacnet_discovery$ts = network_time();
bacnet_discovery$uid = c$uid;
bacnet_discovery$id = c$id;
bacnet_discovery$pdu_service = "i-am";
if(object_type != UINT32_MAX)
bacnet_discovery$object_type = object_types[object_type];
if(instance_number != UINT32_MAX)
bacnet_discovery$instance_number = instance_number;
bacnet_discovery$vendor = vendors[vendor_id];
Log::write(LOG_BACNET_DISCOVERY, bacnet_discovery);
}
###################################################################################################
################ Defines logging of bacnet_who_has event -> bacnet_discovery.log ################
###################################################################################################
event bacnet_who_has(c: connection,
is_orig: bool,
low_limit: count,
high_limit: count,
object_type: count,
instance_number: count,
object_name: string){
set_service(c);
local bacnet_discovery: BACnet_Discovery;
bacnet_discovery$is_orig = is_orig;
bacnet_discovery$ts = network_time();
bacnet_discovery$uid = c$uid;
bacnet_discovery$id = c$id;
bacnet_discovery$pdu_service = "who-has";
if(instance_number != UINT32_MAX){
bacnet_discovery$object_type = object_types[object_type];
bacnet_discovery$instance_number = instance_number;
}
if(object_name == "")
bacnet_discovery$object_name = "N/A";
else
bacnet_discovery$object_name = object_name;
if(low_limit == UINT32_MAX)
bacnet_discovery$range = "All";
else
bacnet_discovery$range = fmt("%d-%d", low_limit, high_limit);
Log::write(LOG_BACNET_DISCOVERY, bacnet_discovery);
}
###################################################################################################
################ Defines logging of bacnet_i_have event -> bacnet_discovery.log #################
###################################################################################################
event bacnet_i_have(c: connection,
is_orig: bool,
device_object_type: count,
device_instance_num: count,
object_object_type: count,
object_instance_num: count,
object_name: string){
set_service(c);
local bacnet_discovery: BACnet_Discovery;
bacnet_discovery$is_orig = is_orig;
bacnet_discovery$ts = network_time();
bacnet_discovery$uid = c$uid;
bacnet_discovery$id = c$id;
bacnet_discovery$pdu_service = "i-have";
if(object_object_type != UINT32_MAX)
bacnet_discovery$object_type = object_types[object_object_type];
if(object_instance_num != UINT32_MAX)
bacnet_discovery$instance_number = object_instance_num;
bacnet_discovery$object_name = object_name;
Log::write(LOG_BACNET_DISCOVERY, bacnet_discovery);
}
###################################################################################################
############# Defines logging of bacnet_read_property event -> bacnet_property.log ##############
###################################################################################################
event bacnet_read_property(c: connection,
is_orig: bool,
pdu_service: string,
object_type: count,
instance_number: count,
property_identifier: count,
property_array_index: count){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = pdu_service;
bacnet_property$object_type = object_types[object_type];
bacnet_property$instance_number = instance_number;
bacnet_property$property = property_identifiers[property_identifier];
if( property_array_index != UINT32_MAX )
bacnet_property$array_index = property_array_index;
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}
###################################################################################################
########### Defines logging of bacnet_read_property_ack event -> bacnet_property.log ############
###################################################################################################
event bacnet_read_property_ack(c: connection,
is_orig: bool,
pdu_service: string,
object_type: count,
instance_number: count,
property_identifier: count,
property_array_index: count,
property_value: string){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = pdu_service;
bacnet_property$object_type = object_types[object_type];
bacnet_property$instance_number = instance_number;
bacnet_property$property = property_identifiers[property_identifier];
if( property_array_index != UINT32_MAX )
bacnet_property$array_index = property_array_index;
if (property_value != "" && is_num(property_value)) {
switch(property_identifier){
case 36:
bacnet_property$value = event_states[to_count(property_value)];
break;
case 72:
bacnet_property$value = notify_type[to_count(property_value)];
break;
case 79:
bacnet_property$value = object_types[to_count(property_value)];
break;
case 103:
bacnet_property$value = reliability[to_count(property_value)];
break;
case 107:
bacnet_property$value = segmentation_supported_status[to_count(property_value)];
break;
case 112:
bacnet_property$value = device_status[to_count(property_value)];
break;
case 117:
bacnet_property$value = bacnet_units[to_count(property_value)];
break;
case 197:
bacnet_property$value = logging_type[to_count(property_value)];
break;
default:
if (property_value != "")
bacnet_property$value = property_value;
break;
}
} else if (property_value != "") {
bacnet_property$value = property_value;
}
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}
###################################################################################################
############# Defines logging of bacnet_write_property event -> bacnet_property.log #############
###################################################################################################
event bacnet_write_property(c: connection,
is_orig: bool,
object_type: count,
instance_number: count,
property_identifier: count,
property_array_index: count,
priority: count,
property_value: string){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = "write-property";
bacnet_property$object_type = object_types[object_type];
bacnet_property$instance_number = instance_number;
bacnet_property$property = property_identifiers[property_identifier];
if( property_array_index != UINT32_MAX )
bacnet_property$array_index = property_array_index;
if (property_value != "" && is_num(property_value)) {
switch(property_identifier){
case 36:
bacnet_property$value = event_states[to_count(property_value)];
break;
case 72:
bacnet_property$value = notify_type[to_count(property_value)];
break;
case 79:
bacnet_property$value = object_types[to_count(property_value)];
break;
case 103:
bacnet_property$value = reliability[to_count(property_value)];
break;
case 107:
bacnet_property$value = segmentation_supported_status[to_count(property_value)];
break;
case 112:
bacnet_property$value = device_status[to_count(property_value)];
break;
case 117:
bacnet_property$value = bacnet_units[to_count(property_value)];
break;
case 197:
bacnet_property$value = logging_type[to_count(property_value)];
break;
default:
if (property_value != "")
bacnet_property$value = property_value;
break;
}
} else if (property_value != "") {
bacnet_property$value = property_value;
}
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}
###################################################################################################
############# Defines logging of bacnet_property_error event -> bacnet_property.log #############
###################################################################################################
event bacnet_property_error(c: connection,
is_orig: bool,
pdu_type: count,
pdu_service: count,
result_code: count){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = "ERROR: " + confirmed_service_choice[pdu_service];
bacnet_property$object_type = error_codes[result_code];
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}
event bacnet_read_range(c: connection,
is_orig: bool,
object_type: count,
instance_number: count,
property_identifier: count,
property_array_index: count){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = "read-range-request";
bacnet_property$object_type = object_types[object_type];
bacnet_property$instance_number = instance_number;
bacnet_property$property = property_identifiers[property_identifier];
if( property_array_index != UINT32_MAX )
bacnet_property$array_index = property_array_index;
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}
event bacnet_read_range_ack(c: connection,
is_orig: bool,
object_type: count,
instance_number: count,
property_identifier: count,
property_array_index: count,
result_flags: count,
item_count: count){
set_service(c);
local bacnet_property: BACnet_Property;
bacnet_property$is_orig = is_orig;
bacnet_property$ts = network_time();
bacnet_property$uid = c$uid;
bacnet_property$id = c$id;
bacnet_property$pdu_service = "read-range-ack";
bacnet_property$object_type = object_types[object_type];
bacnet_property$instance_number = instance_number;
bacnet_property$property = property_identifiers[property_identifier];
if( property_array_index != UINT32_MAX )
bacnet_property$array_index = property_array_index;
bacnet_property$value = fmt("item_count: %d",item_count);
Log::write(LOG_BACNET_PROPERTY, bacnet_property);
}