-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.zeek
563 lines (521 loc) · 25.6 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
##! main.zeek
##!
##! Binpac BSAP (BSAP) Analyzer - Contains the base script-layer functionality for
##! processing events emitted from the analyzer.
##! For use with BSAP communication over serial
##! using serial to ethernet tap, or for use with
##! BSAP communication over ethernet directly.
##!
##!
##! Author: Devin Vollmer
##! Contact: [email protected]
##!
##! Copyright (c) 2023 Battelle Energy Alliance, LLC. All rights reserved."
module Bsap;
export {
redef enum Log::ID += { LOG_BSAP_IP,
LOG_BSAP_IP_RDB,
LOG_BSAP_IP_UNKNOWN,
LOG_BSAP_SERIAL_HEADER,
LOG_BSAP_SERIAL_RDB,
LOG_BSAP_SERIAL_RDB_EXT,
LOG_BSAP_SERIAL_UNKNOWN};
###############################################################################################
############################# BSAP_IP_Header -> bsap_ip_header.log ###########################
###############################################################################################
type BSAP_IP_Header: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
num_msg : count &log; ## Number of function calls in message packet
type_name : string &log;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_ip_header: event(rec: BSAP_IP_Header);
global log_policy_bsap_ip_header: Log::PolicyHook;
###############################################################################################
################################ BSAP_IP_RDB -> bsap_ip_rdb.log ################################
###############################################################################################
type BSAP_IP_RDB: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
header_size : count &log; ## The connection's 4-tuple of endpoint addresses/ports.
mes_seq : count &log;
res_seq : count &log;
data_len : count &log;
sequence : count &log;
app_func_code : string &log;
node_status : count &log;
func_code : string &log;
variable_count : count &log;
variables : vector of string &log &optional;
variable_value : vector of string &log &optional;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_ip_rdb: event(rec: BSAP_IP_RDB);
global log_policy_bsap_ip_rdb: Log::PolicyHook;
###############################################################################################
############################ BSAP_IP_UNKNOWN -> bsap_ip_unknown.log ##########################
###############################################################################################
type BSAP_IP_UNKNOWN: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
data : string &log;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_ip_unknown: event(rec: BSAP_IP_UNKNOWN);
global log_policy_bsap_ip_unknown: Log::PolicyHook;
###############################################################################################
########################### BSAP_SERIAL_HEADER -> bsap_serial_header.log ###########################
###############################################################################################
type BSAP_SERIAL_HEADER: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
ser : count &log;
dadd : count &log;
sadd : count &log;
ctl : count &log;
dfun : string &log;
seq : count &log;
sfun : string &log;
nsb : count &log;
type_name : string &log;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_serial_header: event(rec: BSAP_SERIAL_HEADER);
global log_policy_bsap_serial_header: Log::PolicyHook;
###############################################################################################
############################## BSAP_SERIAL_RDB -> bsap_serial_rdb.log ##############################
###############################################################################################
type BSAP_SERIAL_RDB: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
func_code : string &log;
variable_count : count &log;
variables : vector of string &log &optional;
variable_value : vector of string &log &optional;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_serial_rdb: event(rec: BSAP_SERIAL_RDB);
global log_policy_bsap_serial_rdb: Log::PolicyHook;
###############################################################################################
######################### BSAP_SERIAL_RDB_EXT -> bsap_serial_rdb_ext.log ###########################
###############################################################################################
type BSAP_SERIAL_RDB_EXT: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
dfun : string &log;
seq : count &log;
sfun : string &log;
nsb : count &log;
extfun : string &log;
data : string &log;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_serial_rdb_ext: event(rec: BSAP_SERIAL_RDB_EXT);
global log_policy_bsap_serial_rdb_ext: Log::PolicyHook;
###############################################################################################
########################## BSAP_SERIAL_UNKNOWN -> bsap_serial_unknown.log ##########################
###############################################################################################
type BSAP_SERIAL_UNKNOWN: record {
ts : time &log; ## Timestamp for when the event happened.
uid : string &log; ## Unique ID for the connection.
id : conn_id &log; ## The connection's 4-tuple of endpoint addresses/ports.
is_orig : bool &log; ## the message came from the originator/client or the responder/server
source_h : addr &log; ## Source IP Address
source_p : port &log; ## Source Port
destination_h : addr &log; ## Destination IP Address
destination_p : port &log; ## Destination Port
data : string &log;
# ## TODO: Add other fields here that you'd like to log.
};
global log_bsap_serial_unknown: event(rec: BSAP_SERIAL_UNKNOWN);
global log_policy_bsap_serial_unknown: Log::PolicyHook;
}
#port 1234,1235 are default port numbers used by BSAPIPDRV
const ports = { 1234/udp,
1235/udp
};
redef likely_server_ports += { ports };
###################################################################################################
########### Defines Log Streams for bsap_ip_header.log, bsapip_rdb.log, bsap_ip_unknown ############
###################################################################################################
event zeek_init() &priority=5
{
Log::create_stream(Bsap::LOG_BSAP_IP, [$columns=BSAP_IP_Header, $ev=log_bsap_ip_header, $path="bsap_ip_header", $policy=log_policy_bsap_ip_header]);
Log::create_stream(Bsap::LOG_BSAP_IP_RDB, [$columns=BSAP_IP_RDB, $ev=log_bsap_ip_rdb, $path="bsap_ip_rdb", $policy=log_policy_bsap_ip_rdb]);
Log::create_stream(Bsap::LOG_BSAP_IP_UNKNOWN, [$columns=BSAP_IP_UNKNOWN, $ev=log_bsap_ip_unknown, $path="bsap_ip_unknown", $policy=log_policy_bsap_ip_unknown]);
Log::create_stream(Bsap::LOG_BSAP_SERIAL_HEADER, [$columns=BSAP_SERIAL_HEADER, $ev=log_bsap_serial_header, $path="bsap_serial_header", $policy=log_policy_bsap_serial_header]);
Log::create_stream(Bsap::LOG_BSAP_SERIAL_RDB, [$columns=BSAP_SERIAL_RDB, $ev=log_bsap_serial_rdb, $path="bsap_serial_rdb", $policy=log_policy_bsap_serial_rdb]);
Log::create_stream(Bsap::LOG_BSAP_SERIAL_RDB_EXT, [$columns=BSAP_SERIAL_RDB_EXT, $ev=log_bsap_serial_rdb_ext, $path="bsap_serial_rdb_ext", $policy=log_policy_bsap_serial_rdb_ext]);
Log::create_stream(Bsap::LOG_BSAP_SERIAL_UNKNOWN, [$columns=BSAP_SERIAL_UNKNOWN, $ev=log_bsap_serial_unknown, $path="bsap_serial_unknown", $policy=log_policy_bsap_serial_unknown]);
# TODO: If you're using port-based DPD, uncomment this.
Analyzer::register_for_ports(Analyzer::ANALYZER_BSAP, 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["bsap"];
}
###############################################################################################
################ Defines logging of bsap_ip_header event -> bsap_ip_header.log #################
###############################################################################################
event bsap_ip_header(c: connection, is_orig: bool, id: count, Num_Messages: count,
Message_Func: count)
{
set_service(c);
local info: BSAP_IP_Header;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$num_msg = Num_Messages;
info$type_name = msg_types[Message_Func];
Log::write(Bsap::LOG_BSAP_IP, info);
}
###############################################################################################
############### Defines logging of bsap_ip_rdb_response event -> bsap_rdb.log #################
###############################################################################################
event bsap_ip_rdb_response(c: connection, is_orig: bool, message_seq: count, response_seq: count,
data_length: count, header_size: count, sequence: count,
func_code: count, resp_status: count, variables: string_vec,
variable_value: string_vec, variable_cnt: count, data: string)
{
set_service(c);
local info: BSAP_IP_RDB;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$mes_seq = message_seq;
info$res_seq = response_seq;
info$data_len = data_length;
info$header_size = header_size;
info$sequence = sequence;
info$app_func_code = "RDB";
info$node_status = func_code;
info$func_code = rdb_functions[func_code];
info$variable_count = variable_cnt;
info$variables = variables;
info$variable_value = variable_value;
Log::write(Bsap::LOG_BSAP_IP_RDB, info);
}
###############################################################################################
################ Defines logging of bsap_ip_rdb_request event -> bsap_rdb.log #################
###############################################################################################
event bsap_ip_rdb_request(c: connection, is_orig: bool, response_seq: count, message_seq: count,
node_status: count, func_code: count, data_length: count,
var_cnt: count, variables: string_vec,
variable_value: string_vec, data: string)
{
set_service(c);
local info: BSAP_IP_RDB;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$mes_seq = message_seq;
info$res_seq = response_seq;
info$data_len = data_length;
info$app_func_code = "RDB";
info$node_status = node_status;
info$func_code = rdb_functions[func_code];
info$variable_count = var_cnt;
info$variables = variables;
info$variable_value = variable_value;
Log::write(Bsap::LOG_BSAP_IP_RDB, info);
}
###############################################################################################
############## Defines logging of bsap_ip_unknown event -> bsap_ip_unknown.log ###############
###############################################################################################
event bsap_ip_unknown(c: connection, is_orig: bool, data: string)
{
set_service(c);
local info: BSAP_IP_UNKNOWN;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$data = data;
Log::write(Bsap::LOG_BSAP_IP_UNKNOWN, info);
}
###############################################################################################
############### Defines logging of bsap_local_header event -> bsap_header.log ################
###############################################################################################
event bsap_serial_local_header(c: connection, is_orig: bool, SER: count, DFUN: count, SEQ: count, SFUN: count, NSB: count)
{
set_service(c);
local info: BSAP_SERIAL_HEADER;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$ser = SER;
info$dfun = app_functions[DFUN];
info$seq = SEQ;
info$sfun = app_functions[SFUN];
info$nsb = NSB;
info$type_name = "Local Message";
Log::write(Bsap::LOG_BSAP_SERIAL_HEADER, info);
}
###############################################################################################
############## Defines logging of bsap_global_header event -> bsap_header.log ################
###############################################################################################
event bsap_serial_global_header(c: connection, is_orig: bool, SER: count, DADD: count, SADD: count, CTL: count, DFUN: count,SEQ: count,
SFUN: count, NSB: count)
{
set_service(c);
local info: BSAP_SERIAL_HEADER;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$ser = SER;
info$dadd = DADD;
info$sadd = SADD;
info$ctl = CTL;
info$dfun = app_functions[DFUN];
info$seq = SEQ;
info$sfun = app_functions[SFUN];
info$nsb = NSB;
info$type_name = "Global Message";
Log::write(Bsap::LOG_BSAP_SERIAL_HEADER, info);
}
###############################################################################################
############## Defines logging of bsap_rdb_response event -> bsap_cnv_rdb.log ################
###############################################################################################
event bsap_serial_rdb_response(c: connection, is_orig: bool, func_code: count,
variable_cnt: count, variables: string_vec,
variable_value: string_vec, data: string)
{
set_service(c);
local info: BSAP_SERIAL_RDB;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$func_code = rdb_functions[func_code];
info$variable_count = variable_cnt;
info$variables = variables;
info$variable_value = variable_value;
Log::write(Bsap::LOG_BSAP_SERIAL_RDB, info);
}
###############################################################################################
############### Defines logging of bsap_rdb_request event -> bsap_cnv_rdb.log ################
###############################################################################################
event bsap_serial_rdb_request(c: connection, is_orig: bool, func_code: count,
variable_cnt: count, variables: string_vec,
variable_value: string_vec, data: string)
{
set_service(c);
local info: BSAP_SERIAL_RDB;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$func_code = rdb_functions[func_code];
info$variable_count = variable_cnt;
info$variables = variables;
info$variable_value = variable_value;
Log::write(Bsap::LOG_BSAP_SERIAL_RDB, info);
}
###############################################################################################
############ Defines logging of bsap_rdb_extension event -> bsap_cnv_rdb_ext.log #############
###############################################################################################
event bsap_serial_rdb_extension(c: connection, is_orig: bool, DFUN: count, SEQ: count, SFUN: count, NSB: count, XFUN: count, data: string)
{
set_service(c);
local info: BSAP_SERIAL_RDB_EXT;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$dfun = app_functions[DFUN];
info$seq = SEQ;
info$sfun = app_functions[SFUN];
info$nsb = NSB;
info$extfun = rdb_ext_functions[XFUN];
info$data = data;
Log::write(Bsap::LOG_BSAP_SERIAL_RDB_EXT, info);
}
###############################################################################################
################# Defines logging of bsap_unknown event -> bsap_unknown.log ##################
###############################################################################################
event bsap_serial_unknown(c: connection, is_orig: bool, data: string)
{
set_service(c);
local info: BSAP_SERIAL_UNKNOWN;
info$ts = network_time();
info$uid = c$uid;
info$id = c$id;
info$is_orig = is_orig;
if(is_orig)
{
info$source_h = c$id$orig_h;
info$source_p = c$id$orig_p;
info$destination_h = c$id$resp_h;
info$destination_p = c$id$resp_p;
}else
{
info$source_h = c$id$resp_h;
info$source_p = c$id$resp_p;
info$destination_h = c$id$orig_h;
info$destination_p = c$id$orig_p;
}
info$data = data;
Log::write(Bsap::LOG_BSAP_SERIAL_UNKNOWN, info);
}