-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathopcua_binary-analyzer.pac
336 lines (280 loc) · 13.8 KB
/
opcua_binary-analyzer.pac
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
## opcua_binary-analyzer.pac
##
## OPCUA Binary Protocol Analyzer
##
## Message type processing and analyzer utilitiy functions.
##
## Author: Kent Kvarfordt
## Contact: [email protected]
##
## Copyright (c) 2024 Battelle Energy Alliance, LLC. All rights reserved.
%extern{
#include <set>
#include <sstream>
#include <iomanip>
#include <random>
#include <time.h>
#include "types.bif.h"
#include "index-consts.h"
#include "headers/attribute_id.h"
#include "headers/built_in_data_types.h"
#include "headers/extension_object_node_id.h"
#include "headers/filter-enums.h"
#include "headers/node_classes.h"
#include "headers/node_identifiers.h"
#include "headers/status_codes.h"
#include "headers/statuscode-diagnostic-source-consts.h"
#include "headers/timestamps_to_return.h"
#include "headers/variant-source-consts.h"
#include "types/filters/index-consts.h"
#include "types/variants/index-consts.h"
#include "types/statuscode-diagnostic-index-consts.h"
#include "services/activate-session/index-consts.h"
#include "services/browse/index-consts.h"
#include "services/close-session/index-consts.h"
#include "services/create-monitored-items/index-consts.h"
#include "services/create-session/index-consts.h"
#include "services/create-subscription/index-consts.h"
#include "services/get-endpoints/index-consts.h"
#include "services/read/index-consts.h"
#include "services/secure-channel/index-consts.h"
#include "services/service-fault/index-consts.h"
#include "services/write/index-consts.h"
#include "req-res-header/index-consts.h"
%}
%header{
zeek::RecordValPtr assignMsgHeader(OPCUA_Binary_Conn *connection, zeek::RecordValPtr info, Msg_Header *msg_header);
zeek::RecordValPtr assignMsgType(zeek::RecordValPtr info, Msg_Header *msg_header);
zeek::RecordValPtr assignMsgHEL(zeek::RecordValPtr info, Msg_HEL *msg_hel);
zeek::RecordValPtr assignMsgACK(zeek::RecordValPtr info, Msg_ACK *msg_ack);
zeek::RecordValPtr assignMsgERR(zeek::RecordValPtr info, Msg_ERR *msg_err);
zeek::RecordValPtr assignMsgOPN(zeek::RecordValPtr info, Msg_OPN *msg_opn);
zeek::RecordValPtr assignMsgMSG(zeek::RecordValPtr info, Msg_MSG *msg_msg);
zeek::RecordValPtr assignMsgCLO(zeek::RecordValPtr info, Msg_CLO *msg_clo);
zeek::RecordValPtr assignService(zeek::RecordValPtr info, Service *service);
zeek::RecordValPtr assignSourceDestination(bool is_orig, zeek::RecordValPtr target, zeek::RecordValPtr id_val);
%}
%code{
//
// Common code for assigning Source and Deistionation information based on the is_orig flag
//
zeek::RecordValPtr assignSourceDestination(bool is_orig, zeek::RecordValPtr target, zeek::RecordValPtr id_val) {
target->Assign(IS_ORIG_IDX, zeek::val_mgr->Bool(is_orig));
if (is_orig) {
target->Assign(SOURCE_H_IDX, id_val->GetField<zeek::AddrVal>(0));
target->Assign(SOURCE_P_IDX, id_val->GetField<zeek::PortVal>(1));
target->Assign(DESTINATION_H_IDX, id_val->GetField<zeek::AddrVal>(2));
target->Assign(DESTINATION_P_IDX, id_val->GetField<zeek::PortVal>(3));
} else {
target->Assign(SOURCE_H_IDX, id_val->GetField<zeek::AddrVal>(2));
target->Assign(SOURCE_P_IDX, id_val->GetField<zeek::PortVal>(3));
target->Assign(DESTINATION_H_IDX, id_val->GetField<zeek::AddrVal>(0));
target->Assign(DESTINATION_P_IDX, id_val->GetField<zeek::PortVal>(1));
}
return target;
}
//
// Common code used to assign the message header information to a zeek::RecordVal
// for future logging.
//
zeek::RecordValPtr assignMsgHeader(OPCUA_Binary_Conn *connection, zeek::RecordValPtr info, Msg_Header *msg_header) {
// Source/Destination
const zeek::RecordValPtr conn_val = connection->bro_analyzer()->Conn()->GetVal();
const zeek::RecordValPtr id_val = conn_val->GetField<zeek::RecordVal>(0);
info = assignSourceDestination(msg_header->is_orig(), info, id_val);
// OpcUA_id
info->Assign(OPCUA_LINK_ID_SRC_IDX, zeek::make_intrusive<zeek::StringVal>(generateId()));
// Msg header: msg_type
stringstream ssMsgType;
for ( uint8 i = 0; i < msg_header->msg_type()->size(); ++i ) {
ssMsgType << msg_header->msg_type()->at(i);
}
info->Assign(MSG_TYPE_IDX, zeek::make_intrusive<zeek::StringVal>(ssMsgType.str()));
// Msg header: is_final
stringstream ssIsFinal;
ssIsFinal << msg_header->is_final();
info->Assign(IS_FINAL_IDX, zeek::make_intrusive<zeek::StringVal>(ssIsFinal.str()));
// Msg header: msg_size
info->Assign(MSG_SIZE_IDX, zeek::val_mgr->Count(msg_header->msg_size()));
return info;
}
//
// Common code used to determine the message type and assign the information to a zeek::RecordVal
// for future logging.
//
zeek::RecordValPtr assignMsgType(zeek::RecordValPtr info, Msg_Header *msg_header) {
switch (uint8VectorToUint32(msg_header->msg_type())) {
case HEL: info = assignMsgHEL(info, msg_header->hel());
break;
case ACK: info = assignMsgACK(info, msg_header->ack());
break;
case ERR: info = assignMsgERR(info, msg_header->err());
break;
case OPN: info = assignMsgOPN(info, msg_header->opn());
break;
case MSG: info = assignMsgMSG(info, msg_header->msg());
break;
case CLO: info = assignMsgCLO(info, msg_header->clo());
break;
}
return info;
}
//
// Common code used to assign the message HEL information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgHEL(zeek::RecordValPtr info, Msg_HEL *msg_hel) {
info->Assign(VERSION_IDX, zeek::val_mgr->Count(msg_hel->version()));
info->Assign(RCV_BUF_SIZE_IDX, zeek::val_mgr->Count(msg_hel->rcv_buf_size()));
info->Assign(SND_BUF_SIZE_IDX, zeek::val_mgr->Count(msg_hel->snd_buf_size()));
info->Assign(MAX_MSG_SIZE_IDX, zeek::val_mgr->Count(msg_hel->max_msg_size()));
info->Assign(MAX_CHUNK_CNT_IDX, zeek::val_mgr->Count(msg_hel->max_chunk_cnt()));
info->Assign(ENDPOINT_URL_IDX, zeek::make_intrusive<zeek::StringVal>(getEndpointUrl(msg_hel->endpoint_url())));
return info;
}
//
// Common code used to assign the message ACK information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgACK(zeek::RecordValPtr info, Msg_ACK *msg_ack) {
info->Assign(VERSION_IDX, zeek::val_mgr->Count(msg_ack->version()));
info->Assign(RCV_BUF_SIZE_IDX, zeek::val_mgr->Count(msg_ack->rcv_buf_size()));
info->Assign(SND_BUF_SIZE_IDX, zeek::val_mgr->Count(msg_ack->snd_buf_size()));
info->Assign(MAX_MSG_SIZE_IDX, zeek::val_mgr->Count(msg_ack->max_msg_size()));
info->Assign(MAX_CHUNK_CNT_IDX, zeek::val_mgr->Count(msg_ack->max_chunk_cnt()));
return info;
}
//
// Common code used to assign the message ERR information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgERR(zeek::RecordValPtr info, Msg_ERR *msg_err) {
info->Assign(ERROR_IDX, zeek::val_mgr->Count(msg_err->error()));
info->Assign(REASON_IDX, zeek::make_intrusive<zeek::StringVal>(std_str(msg_err->reason())));
return info;
}
//
// Common code used to assign the message OPN information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgOPN(zeek::RecordValPtr info, Msg_OPN *msg_opn) {
info->Assign(SEC_CHANNEL_ID_IDX, zeek::val_mgr->Count(msg_opn->sec_channel_id()));
// Security Header
info->Assign(SEC_POLICY_URI_LEN_IDX, zeek::val_mgr->Int(msg_opn->sec_header()->sec_policy_uri_len()));
info->Assign(SEC_POLICY_URI_IDX, zeek::make_intrusive<zeek::StringVal>(std_str(msg_opn->sec_header()->sec_policy_uri())));
info->Assign(SND_CERT_LEN_IDX, zeek::val_mgr->Int(msg_opn->sec_header()->snd_cert_len()));
if (msg_opn->sec_header()->snd_cert_len() > 0) {
info->Assign(SND_CERT_IDX, zeek::make_intrusive<zeek::StringVal>(bytestringToHexstring(msg_opn->sec_header()->snd_cert())));
}
info->Assign(RCV_CERT_LEN_IDX, zeek::val_mgr->Int(msg_opn->sec_header()->rcv_cert_len()));
if (msg_opn->sec_header()->rcv_cert_len() > 0) {
info->Assign(RCV_CERT_IDX, zeek::make_intrusive<zeek::StringVal>(bytestringToHexstring(msg_opn->sec_header()->rcv_cert())));
}
// Sequence Header
info->Assign(SEQ_NUMBER_IDX, zeek::val_mgr->Count(msg_opn->seq_header()->seq_number()));
info->Assign(REQUEST_ID_IDX, zeek::val_mgr->Count(msg_opn->seq_header()->request_id()));
return info;
}
//
// Common code used to assign the message CLO information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgCLO(zeek::RecordValPtr info, Msg_CLO *msg_clo) {
// printf("assignMsgCLO\n");
// Secure Channel Id and Secure Token Id
info->Assign(SEC_CHANNEL_ID_IDX, zeek::val_mgr->Count(msg_clo->sec_channel_id()));
info->Assign(SEC_TOKEN_ID_IDX, zeek::val_mgr->Count(msg_clo->sec_token_id()));
// Sequence Header
info->Assign(SEQ_NUMBER_IDX, zeek::val_mgr->Count(msg_clo->seq_header()->seq_number()));
info->Assign(REQUEST_ID_IDX, zeek::val_mgr->Count(msg_clo->seq_header()->request_id()));
return info;
}
//
// Common code used to assign the message MSG information to a zeek::RecordValPtr
// for future logging.
//
zeek::RecordValPtr assignMsgMSG(zeek::RecordValPtr info, Msg_MSG *msg_msg) {
//Debug printf("assignMsgMSG - begin\n");
// Secure Channel Id and Secure Token Id
info->Assign(SEC_CHANNEL_ID_IDX, zeek::val_mgr->Count(msg_msg->sec_channel_id()));
info->Assign(SEC_TOKEN_ID_IDX, zeek::val_mgr->Count(msg_msg->sec_channel_id()));
// Sequence Header
info->Assign(SEQ_NUMBER_IDX, zeek::val_mgr->Count(msg_msg->seq_header()->seq_number()));
info->Assign(REQUEST_ID_IDX, zeek::val_mgr->Count(msg_msg->seq_header()->request_id()));
//Debug printf("assignMsgMSG - end\n");
return info;
}
zeek::RecordValPtr assignService(zeek::RecordValPtr info, Service *service) {
info->Assign(ENCODING_MASK_IDX, zeek::val_mgr->Count(service->msg_body()->encoding_mask()));
info->Assign(NAMESPACE_IDX, zeek::val_mgr->Count(service->namespace_idx()));
info->Assign(IDENTIFIER_IDX, zeek::val_mgr->Count(service->identifier()));
info->Assign(IDENTIFIER_STR_IDX, zeek::make_intrusive<zeek::StringVal>(NODE_IDENTIFIER_MAP.find(service->identifier())->second));
return info;
}
%}
refine flow OPCUA_Binary_Flow += {
#
# Message Hello
#
function deliver_Msg_HEL(msg_hel: Msg_HEL): bool
%{
//Debug printf("\tdeliver_Msg_HEL - begin\n");
zeek::RecordValPtr info = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::OPCUA_Binary::Info);
info = assignMsgHeader(connection(), info, msg_hel->header());
info = assignMsgType(info, msg_hel->header());
zeek::BifEvent::enqueue_opcua_binary_event(connection()->bro_analyzer(),
connection()->bro_analyzer()->Conn(),
info);
//Debug printf("\tdeliver_Msg_HEL - end\n");
return true;
%}
#
# Message Acknowledge
#
function deliver_Msg_ACK(msg_ack: Msg_ACK): bool
%{
//Debug printf("\tdeliver_Msg_ACK - begin\n");
zeek::RecordValPtr info = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::OPCUA_Binary::Info);
info = assignMsgHeader(connection(), info, msg_ack->header());
info = assignMsgType(info, msg_ack->header());
zeek::BifEvent::enqueue_opcua_binary_event(connection()->bro_analyzer(),
connection()->bro_analyzer()->Conn(),
info);
//Debug printf("\tdeliver_Msg_ACK - end\n");
return true;
%}
#
# Message Error
#
function deliver_Msg_ERR(msg_err: Msg_ERR): bool
%{
//Debug printf("\tdeliver_Msg_ERR - begin\n");
zeek::RecordValPtr info = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::OPCUA_Binary::Info);
info = assignMsgHeader(connection(), info, msg_err->header());
info = assignMsgType(info, msg_err->header());
zeek::BifEvent::enqueue_opcua_binary_event(connection()->bro_analyzer(),
connection()->bro_analyzer()->Conn(),
info);
//Debug printf("\tdeliver_Msg_ERR - end\n");
return true;
%}
#
# Message Body
#
function deliver_Msg_Body(msg_body: Msg_Body): bool
%{
//Debug printf("\tdeliver_Msg_Body - begin\n");
// If the encoding mask is NOT valid, then dump out the message body
// info we have so far
if (! valid_encoding(msg_body->encoding_mask())) {
zeek::RecordValPtr info = zeek::make_intrusive<zeek::RecordVal>(zeek::BifType::Record::OPCUA_Binary::Info);
info = assignMsgHeader(connection(), info, msg_body->header());
info = assignMsgType(info, msg_body->header());
zeek::BifEvent::enqueue_opcua_binary_event(connection()->bro_analyzer(),
connection()->bro_analyzer()->Conn(),
info);
}
//Debug printf("\tdeliver_Msg_Body - end\n");
return true;
%}
};