forked from cisagov/icsnpp-enip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.zeek
283 lines (249 loc) · 13.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
##! 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) 2020 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
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);
###############################################################################################
################################### 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
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 : string &log; # CIP status code (see cip_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);
###############################################################################################
################################## 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
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);
###############################################################################################
######################### 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)
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);
}
# 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 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"]);
Log::create_stream(ENIP::LOG_CIP, [$columns=CIP_Header,
$ev=log_cip,
$path="cip"]);
Log::create_stream(ENIP::LOG_CIP_IO, [$columns=CIP_IO_Log,
$ev=log_cip_io,
$path="cip_io"]);
Log::create_stream(ENIP::LOG_CIP_IDENTITY, [$columns=CIP_Identity_Item_Log,
$ev=log_cip_identity,
$path="cip_identity"]);
Analyzer::register_for_ports(Analyzer::ANALYZER_ENIP_TCP, tcp_ports);
Analyzer::register_for_ports(Analyzer::ANALYZER_ENIP_UDP, udp_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,
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;
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,
cip_sequence_count: count,
service: count,
response: bool,
status: 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 (cip_sequence_count != 0)
cip_header_item$cip_sequence_count = cip_sequence_count;
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";
cip_header_item$cip_status = cip_statuses[status];
}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,
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;
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, 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$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);
}