-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.zeek
405 lines (363 loc) · 19.1 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
##! main.zeek
##!
##! Binpac Ethernet/IP (ENIP) Analyzer - Contains the base script-layer functionality for
##! processing events emitted from the analyzer.
##!
##! Author: Stephen Kleinheider
##! Contact: [email protected]
##!
##! Copyright (c) 2023 Battelle Energy Alliance, LLC. All rights reserved.
module ENIP;
export{
redef enum Log::ID += { LOG_ENIP,
LOG_CIP,
LOG_CIP_IO,
LOG_CIP_IDENTITY };
###############################################################################################
################################## ENIP_Header -> enip.log ##################################
###############################################################################################
type ENIP_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
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
packet_correlation_id : string &log; # A correlation ID that ties ENIP headers to associated CIP packets (packet rather than connection based)
enip_command_code : string &log; # Ethernet/IP Command Code (in hex)
enip_command : string &log; # Ethernet/IP Command Name (see enip_commands)
length : count &log; # Length of ENIP data following header
session_handle : string &log; # Sesesion identifier
enip_status : string &log; # Status code (see enip_statuses)
sender_context : string &log; # Sender context
options : string &log; # Options flags
};
global log_enip: event(rec: ENIP_Header);
global log_policy_enip: Log::PolicyHook;
###############################################################################################
################################### CIP_Header -> cip.log ###################################
###############################################################################################
type CIP_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
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
packet_correlation_id : string &log; # A correlation ID that ties ENIP headers to associated CIP packets (packet rather than connection based)
cip_sequence_count : count &log; # CIP sequence number for transport
direction : string &log; # Request or Response
cip_service_code : string &log; # CIP service code (in hex)
cip_service : string &log; # CIP service name (see cip_services)
cip_status_code : string &log; # CIP status code (in hex)
cip_status : string &log; # CIP status description (see cip_statuses)
cip_extended_status_code : string &log; # CIP extended status code (in hex)
cip_extended_status : string &log; # CIP extended status description (see cip_extended_statuses)
class_id : string &log; # CIP Request Path - Class ID
class_name : string &log; # CIP Request Path - Class Name (see cip_classes)
instance_id : string &log; # CIP Request Path - Instance ID
attribute_id : string &log; # CIP Request Path - Attribute ID
};
global log_cip: event(rec: CIP_Header);
global log_policy_cip: Log::PolicyHook;
###############################################################################################
################################## CIP_IO_Log -> cip_io.log #################################
###############################################################################################
type CIP_IO_Log: 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
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
packet_correlation_id : string &log; # A correlation ID that ties ENIP headers to associated CIP packets (packet rather than connection based)
connection_id : string &log; # CIP Connection Identifier
sequence_number : count &log; # CIP Sequence Number with Connection
data_length : count &log; # Length of io_data field
io_data : string &log; # CIP IO Data
};
global log_cip_io: event(rec: CIP_IO_Log);
global log_policy_cip_io: Log::PolicyHook;
###############################################################################################
######################### CIP_Identity_Item_Log -> cip_identity.log #########################
###############################################################################################
type CIP_Identity_Item_Log: 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
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
packet_correlation_id : string &log; # A correlation ID that ties ENIP headers to associated CIP packets (packet rather than connection based)
encapsulation_version : count &log; # Encapsulation protocol version supported
socket_address : addr &log; # Socket address IP address
socket_port : count &log; # Socket address port number
vendor_id : count &log; # Vendor ID
vendor_name : string &log; # Name of Vendor (see cip_vendors)
device_type_id : count &log; # Device type ID
device_type_name : string &log; # Name of device type (see cip_device_types)
product_code : count &log; # Product code assigned to device
revision : string &log; # Device revision (major.minor)
device_status : string &log; # Current status of device (see cip_statuses)
serial_number : string &log; # Serial number of device
product_name : string &log; # Human readable description of device
device_state : string &log; # Current state of the device
};
global log_cip_identity: event(rec: CIP_Identity_Item_Log);
global log_policy_cip_identity: Log::PolicyHook;
}
# Defines ENIP/CIP ports
const ports = {
2222/udp,
44818/tcp,
44818/udp,
};
# Defines ENIP/CIP UDP ports
const udp_ports = {
2222/udp,
44818/udp,
};
# Defines Implicit UDP ENIP/CIP I/O ports
const udp_implicit_ports = {
2222/udp,
};
# Defines ENIP/CIP TCP ports
const tcp_ports = {
44818/tcp,
};
redef likely_server_ports += { ports };
###################################################################################################
################ Defines Log Streams for enip.log, cip.log, and cip_identity.log ################
###################################################################################################
event zeek_init() &priority=5 {
Log::create_stream(ENIP::LOG_ENIP, [$columns=ENIP_Header,
$ev=log_enip,
$path="enip",
$policy=log_policy_enip]);
Log::create_stream(ENIP::LOG_CIP, [$columns=CIP_Header,
$ev=log_cip,
$path="cip",
$policy=log_policy_cip]);
Log::create_stream(ENIP::LOG_CIP_IO, [$columns=CIP_IO_Log,
$ev=log_cip_io,
$path="cip_io",
$policy=log_policy_cip_io]);
Log::create_stream(ENIP::LOG_CIP_IDENTITY, [$columns=CIP_Identity_Item_Log,
$ev=log_cip_identity,
$path="cip_identity",
$policy=log_policy_cip_identity]);
#Analyzer::register_for_ports(Analyzer::ANALYZER_ENIP_TCP, tcp_ports);
#Analyzer::register_for_ports(Analyzer::ANALYZER_ENIP_UDP, udp_ports);
# Monitor only the UDP Port assigned to implicit ENIP/CIP IO Messages
Analyzer::register_for_ports(Analyzer::ANALYZER_ENIP_UDP, udp_implicit_ports);
}
###################################################################################################
####### Ensure that conn.log:service is set if it has not already been #######
###################################################################################################
function set_service(c: connection, service: string) {
if ((!c?$service) || (|c$service| == 0))
add c$service[service];
}
###################################################################################################
####################### Defines logging of enip_header event -> enip.log ########################
###################################################################################################
event enip_header(c: connection,
is_orig: bool,
packet_correlation_id: string,
command: count,
length: count,
session_handle: count,
status: count,
sender_context: string,
options: count) {
set_service(c, "enip");
local enip_item: ENIP_Header;
enip_item$ts = network_time();
enip_item$uid = c$uid;
enip_item$id = c$id;
enip_item$is_orig = is_orig;
if(is_orig)
{
enip_item$source_h = c$id$orig_h;
enip_item$source_p = c$id$orig_p;
enip_item$destination_h = c$id$resp_h;
enip_item$destination_p = c$id$resp_p;
}else
{
enip_item$source_h = c$id$resp_h;
enip_item$source_p = c$id$resp_p;
enip_item$destination_h = c$id$orig_h;
enip_item$destination_p = c$id$orig_p;
}
enip_item$packet_correlation_id = packet_correlation_id;
enip_item$enip_command_code = fmt("0x%02x",command);
enip_item$enip_command = enip_commands[command];
enip_item$length = length;
enip_item$session_handle = fmt("0x%08x", session_handle);
enip_item$enip_status = enip_statuses[status];
enip_item$sender_context = fmt("0x%s", bytestring_to_hexstr(sender_context));
enip_item$options = fmt("0x%08x", options);
Log::write(LOG_ENIP, enip_item);
}
###################################################################################################
######################## Defines logging of cip_header event -> cip.log #########################
###################################################################################################
event cip_header(c: connection,
is_orig: bool,
packet_correlation_id: string,
cip_sequence_count: count,
service: count,
response: bool,
status: count,
status_extended: count,
class_id: count,
instance_id: count,
attribute_id: count){
set_service(c, "cip");
local cip_header_item: CIP_Header;
cip_header_item$ts = network_time();
cip_header_item$uid = c$uid;
cip_header_item$id = c$id;
cip_header_item$is_orig = is_orig;
if(is_orig)
{
cip_header_item$source_h = c$id$orig_h;
cip_header_item$source_p = c$id$orig_p;
cip_header_item$destination_h = c$id$resp_h;
cip_header_item$destination_p = c$id$resp_p;
}else
{
cip_header_item$source_h = c$id$resp_h;
cip_header_item$source_p = c$id$resp_p;
cip_header_item$destination_h = c$id$orig_h;
cip_header_item$destination_p = c$id$orig_p;
}
if (cip_sequence_count != 0)
cip_header_item$cip_sequence_count = cip_sequence_count;
cip_header_item$packet_correlation_id = packet_correlation_id;
cip_header_item$cip_service_code = fmt("0x%02x",service);
cip_header_item$cip_service = cip_services[service];
if(response)
{
cip_header_item$direction = "response";
if (status != UINT32_MAX )
{
cip_header_item$cip_status_code = fmt("0x%02x", status);
cip_header_item$cip_status = cip_statuses[status];
}
if (status_extended != UINT32_MAX )
{
cip_header_item$cip_extended_status_code = fmt("0x%04x", status_extended);
cip_header_item$cip_extended_status = cip_extended_status[status_extended];
}
}else
{
cip_header_item$direction = "request";
if(class_id != UINT32_MAX){
cip_header_item$class_id = fmt("0x%02x",class_id);
cip_header_item$class_name = cip_classes[class_id];
}
if(instance_id != UINT32_MAX)
cip_header_item$instance_id = fmt("0x%02x",instance_id);
if(attribute_id != UINT32_MAX)
cip_header_item$attribute_id = fmt("0x%02x",attribute_id);
}
Log::write(LOG_CIP, cip_header_item);
}
###################################################################################################
######################### Defines logging of cip_io event -> cip_io.log #########################
###################################################################################################
event cip_io(c: connection,
is_orig: bool,
packet_correlation_id: string,
connection_identifier: count,
sequence_number: count,
data_length: count,
data: string){
set_service(c, "cip");
local cip_io_item: CIP_IO_Log;
cip_io_item$ts = network_time();
cip_io_item$uid = c$uid;
cip_io_item$id = c$id;
cip_io_item$is_orig = is_orig;
if(is_orig)
{
cip_io_item$source_h = c$id$orig_h;
cip_io_item$source_p = c$id$orig_p;
cip_io_item$destination_h = c$id$resp_h;
cip_io_item$destination_p = c$id$resp_p;
}else
{
cip_io_item$source_h = c$id$resp_h;
cip_io_item$source_p = c$id$resp_p;
cip_io_item$destination_h = c$id$orig_h;
cip_io_item$destination_p = c$id$orig_p;
}
cip_io_item$packet_correlation_id = packet_correlation_id;
cip_io_item$connection_id = fmt("0x%08x", connection_identifier);;
cip_io_item$sequence_number = sequence_number;
cip_io_item$data_length = data_length;
cip_io_item$io_data = bytestring_to_hexstr(data);
Log::write(LOG_CIP_IO, cip_io_item);
}
###################################################################################################
################### Defines logging of cip_identity event -> cip_identity.log ###################
###################################################################################################
event cip_identity(c: connection,
is_orig: bool,
packet_correlation_id: string,
encapsulation_version: count,
socket_address: count,
socket_port: count,
vendor_id: count,
device_type: count,
product_code: count,
revision_major: count,
revision_minor: count,
status: count,
serial_number: count,
product_name: string,
state: count ){
set_service(c, "cip");
local cip_identity_item: CIP_Identity_Item_Log;
cip_identity_item$ts = network_time();
cip_identity_item$uid = c$uid;
cip_identity_item$id = c$id;
cip_identity_item$is_orig = is_orig;
if(is_orig)
{
cip_identity_item$source_h = c$id$orig_h;
cip_identity_item$source_p = c$id$orig_p;
cip_identity_item$destination_h = c$id$resp_h;
cip_identity_item$destination_p = c$id$resp_p;
}else
{
cip_identity_item$source_h = c$id$resp_h;
cip_identity_item$source_p = c$id$resp_p;
cip_identity_item$destination_h = c$id$orig_h;
cip_identity_item$destination_p = c$id$orig_p;
}
cip_identity_item$packet_correlation_id = packet_correlation_id;
cip_identity_item$encapsulation_version = encapsulation_version;
cip_identity_item$socket_address = count_to_v4_addr(socket_address);
cip_identity_item$socket_port = socket_port;
cip_identity_item$vendor_id = vendor_id;
cip_identity_item$vendor_name = cip_vendors[vendor_id];
cip_identity_item$device_type_id = device_type;
cip_identity_item$device_type_name = cip_device_types[device_type];
cip_identity_item$product_code = product_code;
cip_identity_item$revision = fmt("%d.%d", revision_major, revision_minor);
cip_identity_item$device_status = fmt("0x%04x", status);
cip_identity_item$serial_number = fmt("0x%08x", serial_number);
cip_identity_item$product_name = product_name;
cip_identity_item$device_state = fmt("0x%04x", state);
Log::write(LOG_CIP_IDENTITY, cip_identity_item);
}