-
Notifications
You must be signed in to change notification settings - Fork 2
/
irc_feature_extractor.zeek
631 lines (547 loc) · 17.2 KB
/
irc_feature_extractor.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
@load base/bif/plugins/Zeek_IRC.events.bif.zeek
module IRC_Feature_Extractor;
type IRC_Event: record {
src: string &log;
src_ip: addr &log;
src_port: port &log;
dst: string &log;
dst_ip: addr &log;
dst_port: port &log;
msg: string &log;
req_size: int &log;
start_time: time &log;
duration: interval &log;
};
type IRC_Session: record {
src: string &log;
saddr: addr &log;
src_ports_count: count &log;
dst: string &log;
daddr: addr &log;
dport: port &log;
ts: time &log;
end_time: time &log;
duration: double &log;
msg_count: count &log;
size_total: int &log;
periodicity: double &optional &log;
spec_chars_username_mean: double &log;
spec_chars_msg_mean: double &log;
msg_word_entropy: double &log;
msgs: vector of IRC_Event;
};
type IRC_EventKey: record {
src_ip: addr;
dst_ip: addr;
dst_port: port;
};
export {
redef enum Log::ID += { LOG };
global log_irc_session: event(rec: IRC_Session);
}
type Complex: record {
real: double;
imag: double;
};
type event_vec: vector of IRC_Event;
type double_vec: vector of double;
global irc_logs: vector of IRC_Event = vector();
global VERBOSE = F;
# uncomment to use json as output
redef LogAscii::use_json = T;
event zeek_init()
{
if (VERBOSE) {
print "zeek init";
}
Log::create_stream(IRC_Feature_Extractor::LOG, [$columns=IRC_Session, $path="irc_features"]);
}
event irc_privmsg_message(c: connection, is_orig: bool, source: string, target: string, message: string) {
local ev: IRC_Event = IRC_Event($src=source, $src_ip=c$id$orig_h, $src_port=c$id$orig_p, $dst=target, $dst_ip=c$id$resp_h, $dst_port=c$id$resp_p, $msg=message, $start_time=c$start_time, $duration=c$duration, $req_size=c$orig$size);
irc_logs += ev;
}
global organize_events: function(): table[IRC_EventKey] of event_vec;
global extract_sessions: function(): vector of IRC_Session;
global argmax_f: function(x: vector of double): count;
global mean_f: function(x:vector of double): double;
global mean_vec_f: function(x: vector of double_vec): vector of double;
global norm_f: function(x: vector of double): double;
global norm_vec_f: function(x: vector of double_vec): double;
global sub_vec_f: function(x_vec: vector of double_vec, y: vector of double): vector of double_vec;
global div_vec_f: function(x: vector of double, y: vector of double): vector of double;
global sum_f: function(x:vector of double): double;
global ln_f: function(x:vector of double): vector of double;
global fft: function(x: vector of Complex): vector of Complex;
global compute_session_periodicity: function(ts_vec: vector of time): double;
global slice_c: function(x: vector of Complex, start: int, step:int): vector of Complex;
global mult_cc: function(a:Complex, b:Complex): Complex;
global mult_cd: function(a:Complex, b:double): Complex;
global exp_c: function(c: Complex): Complex;
global sin: function(x: double): double;
global cos: function(x: double): double;
global cosh: function(x: double): double ;
global sinh: function(x: double): double ;
global pow: function(x:double, p:int): double;
global add_cc: function(a: Complex, b: Complex): Complex;
global sub_cc: function(a: Complex, b:Complex): Complex;
global fft_preprocess_seq: function(x: vector of Complex): vector of Complex;
event zeek_done()
{
local sessions_vec: vector of IRC_Session = extract_sessions();
if (VERBOSE) {
print "zeek done.";
}
for (i in sessions_vec ) {
Log::write( IRC_Feature_Extractor::LOG, sessions_vec[i]);
}
}
local irc_sessions: vector of IRC_Session;
local add_cd: function(a: Complex, b: double): Complex;
local div_cc: function(a:Complex, b:Complex): Complex;
local div_cd: function(a:Complex, b:double): Complex;
local get_key: function(ev: IRC_Event): IRC_EventKey;
local extract_features: function(out:file);
extract_sessions = function(): vector of IRC_Session
{
if (VERBOSE) {
print "extract sessions...";
}
local events: table[IRC_EventKey] of event_vec = organize_events();
local session_vec: vector of IRC_Session;
local i_count: count = 0;
local size_total: int;
for (i in events) {
local ev: IRC_Event = events[i][0];
local src: string = ev$src;
local src_ip: addr = ev$src_ip;
local start_time: time = ev$start_time;
local last_msg_idx: count = |events[i]| - 1;
local end_time = events[i][last_msg_idx]$start_time + events[i][last_msg_idx]$duration;
local dst: string = ev$dst;
local dst_ip: addr = ev$dst_ip;
local dst_port: port = ev$dst_port;
local msg_ts_vec: vector of time;
local msg_count: count = |events[i]|;
local word_occurency_table: table[string] of count;
if (VERBOSE) {
print "######################################";
print "#session: ",i_count+1,"/",|events|;
print "src: ", src;
print "src IP: ", src_ip;
print "start time: ", start_time;
print "end time: ", start_time;
print "dst: ", dst;
print "dst IP: ", dst_ip;
print "msg_count: ", msg_count;
}
size_total = 0;
local rgx_str_tmp: string = src;
local user_rgx_str: string = "";
local user_rgx: PatternMatchResult;
while (T) {
user_rgx = match_pattern(rgx_str_tmp, /([^a-zA-Z])*/);
if (!user_rgx$matched) {
break;
}
user_rgx_str = user_rgx_str + user_rgx$str;
rgx_str_tmp = rgx_str_tmp[user_rgx$off+|user_rgx$str|-1:];
}
local spec_chars_username_mean: double = |user_rgx_str| / (|src|+0.00001);
local msg_special_chars: vector of double;
local src_ports: set[port];
for (j in events[i])
{
local ev2: IRC_Event = events[i][j];
msg_ts_vec += ev2$start_time;
local msg: string = ev2$msg;
local split_msg: string_vec = split_string(msg,/ /);
size_total += ev2$req_size;
# compute word occurency
for (k in split_msg) {
local w: string = split_msg[k];
if (w in word_occurency_table) {
word_occurency_table[w] += 1;
} else {
word_occurency_table[w] = 1;
}
}
# compute msg special chars
local msg_rgx_str: string = "";
local msg_rgx: PatternMatchResult;
rgx_str_tmp = msg;
while (T) {
msg_rgx = match_pattern(rgx_str_tmp, /([^A-Za-z])*/);
if (!msg_rgx$matched) {
break;
}
msg_rgx_str = msg_rgx_str + msg_rgx$str;
rgx_str_tmp = rgx_str_tmp[msg_rgx$off+|msg_rgx$str|-1:];
}
local msg_spec: double = |msg_rgx_str| / (|msg|+0.00001);
msg_special_chars += msg_spec;
add src_ports[ev2$src_port];
}
# compute msg word entropy
local word_count_sum: count = 0;
local word_count: count = |word_occurency_table|;
local p: vector of double;
local c: count;
local word: string;
for (word, c in word_occurency_table)
{
p += c;
word_count_sum += c;
}
for (el in p) {
p[el] = p[el] / word_count_sum;
}
local spec_chars_msg_mean: double = mean_f(msg_special_chars);
local msg_word_entropy: double = 0;
for (el in p) {
msg_word_entropy -= p[el] * (ln(p[el]/ln(2)));
}
local duration: double = interval_to_double(end_time - start_time);
local periodicity: double = compute_session_periodicity(msg_ts_vec);
if (VERBOSE) {
print "special characters username mean: ", spec_chars_username_mean;
print "special characters message mean: ", spec_chars_msg_mean;
print "message word entropy: ", msg_word_entropy;
print "duration: ", duration;
print "periodicity: ", periodicity;
}
local session: IRC_Session = IRC_Session($src = src, $saddr = src_ip, $src_ports_count = |src_ports|,$dst = dst,$daddr = dst_ip,
$dport = dst_port,
$ts = start_time,
$end_time = end_time,
$duration = duration,
$msg_count = msg_count,
$size_total = size_total,
$spec_chars_username_mean = spec_chars_username_mean,
$spec_chars_msg_mean = spec_chars_msg_mean,
$msg_word_entropy = msg_word_entropy,
$msgs = events[i]
);
if (periodicity != -1) {
session$periodicity = periodicity;
}
session_vec += session;
i_count += 1;
}
return session_vec;
};
organize_events = function(): table[IRC_EventKey] of event_vec
{
if (VERBOSE) {
print "organize events...";
print "|events|: ", |irc_logs|;
}
local key_set: table[IRC_EventKey] of event_vec;
for (i in irc_logs) {
local ev: IRC_Event = irc_logs[i];
local src_ip: addr = ev$src_ip;
local dst_ip: addr = ev$dst_ip;
local dst_port: port = ev$dst_port;
local ev_key: IRC_EventKey = IRC_EventKey($src_ip = src_ip, $dst_ip = dst_ip, $dst_port = dst_port);
if (ev_key in key_set) {
local vv: event_vec = key_set[ev_key];
vv += ev;
key_set[ev_key] = vv;
} else {
local vv2: event_vec = vector(ev);
key_set[ev_key] = vv2;
}
}
return key_set;
};
compute_session_periodicity = function(ts_vec: vector of time): double
{
if (VERBOSE) {
print "compute_session_periodicity...";
}
if (|ts_vec| < 3) {
return -1;
}
local ts_vecsize: count = |ts_vec|;
local time_diff_vec: vector of double;
local td_vec: vector of double;
local td_vec_c: vector of Complex;
for (i in ts_vec)
{
if (i+1 == ts_vecsize) break;
local td: double = interval_to_double(ts_vec[i+1] - ts_vec[i]);
td_vec += td;
td_vec_c += Complex($real=td, $imag=0);
}
if (|td_vec_c| > 0) {
local td_vec_c2: vector of Complex = fft_preprocess_seq(td_vec_c);
if (VERBOSE) {
print "fft...";
}
local per_vec: vector of Complex = fft(td_vec_c2);
}
local per_vec_real: vector of double;
for (i in per_vec)
{
per_vec_real += per_vec[i]$real;
}
local t: count = argmax_f(per_vec_real) + 2;
local rng_size: count = |td_vec|;
local td_T: vector of double_vec = vector();
local x: count = 0;
local x_idx: count;
local x_start: count;
local x_end: count;
if (VERBOSE) {
print "dividing td into boxes....";
}
while (x*t+t <= rng_size) {
local x_vec: vector of double = vector();
x_start = x * t;
x_end = x * t + t;
x_idx = x_start;
while (x_idx != x_end) {
x_vec += td_vec[x_idx];
x_idx += 1;
}
td_T += x_vec;
x += 1;
}
local td_T_avg: vector of double = mean_vec_f(td_T);
local td_T_norm: double = norm_vec_f(td_T);
local td_T_sub: vector of double_vec = sub_vec_f(td_T, td_T_avg);
local td_T_sub_norm: double = norm_vec_f(td_T_sub);
local td_nmse: double = 0;
if (td_T_norm != 0) {
td_nmse = td_T_sub_norm / td_T_norm;
}
return 1 - td_nmse;
};
# fast fourier transform
fft = function(x: vector of Complex): vector of Complex
{
local N: count = |x|;
if (N <= 1) return x;
local x_odd: vector of Complex = slice_c(x, 0, 2);
local x_even: vector of Complex = slice_c(x, 1, 2);
local fft_even: vector of Complex = fft(x_even);
local fft_odd: vector of Complex = fft(x_odd);
local T_vec: vector of Complex = vector();
local nn: int = N/2;
local pi: double = 3.14159265;
local c: Complex = Complex($real=0, $imag=-2);
local k: int = 0;
while (k != nn)
{
local tmp_d: double = pi * k/N;
local c2: Complex = mult_cd(c, tmp_d);
local c3: Complex = exp_c(c2);
T_vec += mult_cc(c3,fft_odd[k]);
k += 1;
}
local res: vector of Complex = vector();
local k2: count = 0;
while (k2 != nn)
{
res += add_cc(fft_even[k2], T_vec[k2]);
k2 += 1;
}
local k3: count = 0;
while (k3 != nn)
{
res += sub_cc(fft_even[k3], T_vec[k3]);
k3 += 1;
}
return res;
};
fft_preprocess_seq = function(x: vector of Complex): vector of Complex
{
local x_len: int = |x|;
local x_new: vector of Complex = vector();
local x_pow: int = ln(x_len)/ln(2);
local x_len_new : double = pow(2,x_pow);
local i: count = 0;
while (i < x_len_new)
{
x_new += x[i];
i += 1;
}
return x_new;
};
add_cc = function(a: Complex, b: Complex): Complex
{
local r: double = a$real + b$real;
local i: double = a$imag + b$imag;
local c: Complex = Complex($real=r, $imag=i);
return c;
};
sub_cc = function(a: Complex, b:Complex): Complex
{
local r: double = a$real - b$real;
local i: double = a$imag - b$imag;
local c: Complex = Complex($real=r, $imag=i);
return c;
};
mult_cc = function(a:Complex, b:Complex): Complex
{
local r: double = a$real * b$real - a$imag * b$imag;
local i: double = a$imag * b$real + a$real * b$imag;
local c: Complex = Complex($real=r, $imag=i);
return c;
};
mult_cd = function(a:Complex, b:double): Complex
{
local r: double = a$real * b;
local i: double = a$imag *b;
local c: Complex = Complex($real=r, $imag=i);
return c;
};
cosh = function(x: double): double
{
local r: double = (exp(x) + exp(-x))/2;
return r;
};
sinh = function(x: double): double
{
local r: double = (exp(x) - exp(-x))/2;
return r;
};
sin = function(x: double): double
{
local a: double = x;
local s: double = a;
local i:count = 1;
while (i != 100) {
local a_c: double = -1 * pow(x,2);
local a_j: double = (2 * i) * (2 * i + 1);
a = a * (a_c / a_j);
s += a;
i += 1;
}
return s;
};
cos = function(x: double): double
{
local offset: double = 3.14159265/2.0;
return sin(x+offset);
};
exp_c = function(c: Complex) : Complex
{
local r: double = cosh(c$real) + sinh(c$real);
local imcos: double = cos(c$imag);
local imsin: double = sin(c$imag);
local cc: Complex = Complex($real=imcos, $imag=imsin);
local cc2: Complex = mult_cd(cc, r);
return cc2;
};
slice_c = function(x: vector of Complex, start: int, step:int): vector of Complex
{
local slice_x: vector of Complex = vector();
for (i in x) {
if (i >= start && (i-start) % step == 0) {
slice_x += x[i];
}
}
return slice_x;
};
pow = function(x:double, p:int) : double {
local x_p: double = x;
local i: count = 0;
while (i != p-1)
{
x_p = x_p*x;
i += 1;
}
return x_p;
};
sub_vec_f = function(x_vec: vector of double_vec, y: vector of double): vector of double_vec {
for (i in x_vec) {
for (j in y) {
x_vec[i][j] -= y[j];
}
}
return x_vec;
};
div_vec_f = function(x: vector of double, y: vector of double): vector of double {
local div_vec: vector of double = vector();
for (i in x) {
div_vec += x[i] / y[i];
}
return div_vec;
};
norm_f = function(x: vector of double): double {
local norm_sq: double = 0;
for (i in x) {
norm_sq += x[i]*x[i];
}
local norm: double = sqrt(norm_sq);
return norm;
};
norm_vec_f = function(x: vector of double_vec): double {
local i: int = 0;
local j: int = 0;
local norm: double = 0;
local x_len: int = |x|;
local x0_len: int = |x[0]|;
local v: vector of double = vector();
while (i < x0_len) {
v = vector();
j = 0;
while (j < x_len) {
v += x[j][i];
j += 1;
}
norm += norm_f(v);
i += 1;
}
return norm;
};
mean_vec_f = function(x: vector of double_vec): vector of double {
local i: int = 0;
local j: int;
local mean_vec: vector of double = vector();
local v: vector of double = vector();
local vi: vector of double = vector();
local x_len: int = |x|;
local x0_len: int = |x[0]|;
while (i < x0_len) {
v = vector();
j = 0;
while (j < x_len) {
v += x[j][i];
j += 1;
}
mean_vec += mean_f(v);
i += 1;
}
return mean_vec;
};
sum_f = function(x:vector of double): double {
local sum_r: double = 0;
for (i in x)
{
sum_r += x[i];
}
return sum_r;
};
mean_f = function(x:vector of double): double {
return sum_f(x) / |x|;
};
ln_f = function(x:vector of double): vector of double {
local ln_vec: vector of double;
for (i in x) {
ln_vec += ln(x[i]);
}
return ln_vec;
};
argmax_f = function(x: vector of double): count {
local max_idx: count;
local max_val: double;
max_val = -9999;
for (i in x) {
if (x[i] > max_val){
max_idx = i;
max_val = x[i];
}
}
return max_idx;
};