/
Zeek-Pcap-Feature-Extractor.zeek
1511 lines (1224 loc) · 44.7 KB
/
Zeek-Pcap-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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@load base/utils/site
@load base/utils/strings
@load base/utils/numbers
@load base/utils/files
@load base/frameworks/tunnels
@load base/protocols/conn/removal-hooks
@load base/protocols/http
@load base/packet-protocols/tcp
@load base/frameworks/packet-filter
@load base/protocols/conn
module Extractor_Feautures;
export {
## The connection logging stream identifier.
redef enum Log::ID += {
LOG1,
LOG2,
LOG3,
LOG4,
LOG5,
LOG6
};
## A default logging policy hook for the stream.
global log_policy1: Log::PolicyHook;
## The record type which contains column fields of the connection log.
type Info_: record {
## This is the time of the first packet.
ts: string &log;
## A unique identifier of the connection.
uid: string &log;
## The connection's 4-tuple of endpoint addresses/ports.
id: conn_id &log;
## The transport layer protocol of the connection.
proto: transport_proto &log;
## An identification of an application protocol being sent over
## the connection.
service: string &log &optional;
## How long the connection lasted.
duration: string &log &optional;
## The number of payload bytes the originator sent.
orig_bytes: count &log &optional;
## The number of payload bytes the responder sent.
resp_bytes: count &log &optional;
## Possible *conn_state* values:
conn_state: string &log &optional;
## If the connection is originated locally, this value will be T.
## If it was originated remotely it will be F.
local_orig: bool &log &optional;
## If the connection is responded to locally, this value will be T.
## If it was responded to remotely it will be F.
local_resp: bool &log &optional;
## Indicates the number of bytes missed in content gaps, which
## is representative of packet loss.
missed_bytes: count &log &default=0;
## Records the state history of connections as a string of
## letters.
history: string &log &optional;
## Number of packets that the originator sent.
orig_pkts: count &log &optional;
## Number of IP level bytes that the originator sent (as seen on
## the wire, taken from the IP total_length header field).
orig_ip_bytes: count &log &optional;
## Number of packets that the responder sent.
resp_pkts: count &log &optional;
## Number of IP level bytes that the responder sent (as seen on
## the wire, taken from the IP total_length header field).
resp_ip_bytes: count &log &optional;
## If this connection was over a tunnel, indicate the
## *uid* values for any encapsulating parent connections
## used over the lifetime of this inner connection.
tunnel_parents: set[string] &log &optional;
##Source bits per second
sload: double &log &optional;
##Destination bits per second
dload: double &log &optional;
##Mean of the flow packet size transmitted by the src
smeansz: double &log &optional;
##Mean of the flow packet size transmitted by the dst
dmeansz: double &log &optional;
## Represents the pipelined depth into the connection of this
## request/response transaction.
trans_depth:count &log &default=0;
##The content size of the data transferred from the server’s http service
reb_bdy_len: count &log &default=0;
##start time
start_time: string &log &optional;
##last time
last_time: string &log &optional;
## If source (1) equals to destination (3)IP addresses and port numbers (2)(4)
## are equal, this variable takes value 1 else 0
is_sm_ips_ports: string &log &optional;
##If the ftp session is accessed by user and password then 1 else 0
is_ftp_login: count &log &optional;
##user ftp
user_ftp: string &log &optional; ##&default = "<unknown>"
##password ftp if captured
pwd_ftp: string &log &optional;
##No of flows that has a command in ftp session
ct_ftp_cmd:count &log &optional;
##Packets reported dropped by the system.
pkts_dropped_: count &default = 0 &log &optional;
##Source jitter (mSec)
sjit: interval &log &optional;
##Destination jitter (mSec)
djit: interval &log &optional;
## Source inter-packet arrival time (mSec)
sinpkt: interval &log &optional;
##Destination inter-packet arrival time (mSec)
dinpkt: interval &log &optional;
};
type FlowFeatures: record {
## The connection's 4-tuple of endpoint addresses/ports.
id: conn_id &log;
## The transport layer protocol of the connection.
proto: transport_proto &log;
};
# type Info3: record {
# ## If source (1) equals to destination (3)IP addresses and port numbers (2)(4)
# ## are equal, this variable takes value 1 else 0
# is_sm_ips_ports: string &log &optional;
# };
type eachPackets: record {
# dwin: count &log &optional;
stcpb: count &log &optional;
dtcpb: count &log &optional;
##Time to live (Source to destination time to live and Destination to source time to live)
ttl: count &log &optional;
##Source TCP window advertisement and Destination TCP window advertisement
win: count &log &optional;
##Source packets retransmitted (TCP) and Destination packets retransmitted (TCP)
};
type each_TCP_Conn: record {
##The time between the SYN and the SYN_ACK packets of the TCP
synack: interval &log &optional;
##The time between the SYN_ACK and the ACK packets of the TCP
ackdat: interval &log &optional;
##The sum of ’synack’ and ’ackdat’ of the TCP.
tcprtt: interval &log &optional;
##For each connection the mean interval between two packets (Source - in mSec)
m_int_s: interval &log &optional;
##For each connection the mean interval between two packets (Destination - in mSec)
m_int_d: interval &log &optional;
};
type infoAllC: record {
##Number of flows that has method Post in http service
http_post: count &log &default=0;
##Number of flows that has method Get in http service
http_get: count &log &default=0;
};
global log_1: event(rec: Info_);
global log_2: event(rec: FlowFeatures);
}
redef record connection += {
conn1: Info_ &optional;
conn2: FlowFeatures &optional;
};
global eachP: eachPackets;
global each_TCP_C: each_TCP_Conn;
##record info to print in file infoAllConn.log (Features about the entire file pcap)
global allC: infoAllC;
##
global ts_SYN: time;
global ts_SYNACK: time;
global ts_ACK: time;
##info to print m_int_s
global ts_Packet_Prev_S: time;
global tmp_meanInterval_S: interval = 0.0secs;
global contS: count = 0;
global firstP_S: bool = T;
##info to print m_int_d
global ts_Packet_Prev_D: time;
global tmp_meanInterval_D: interval = 0.0secs;
global contD: count = 0;
global firstP_D: bool = T;
##counter of flows that has a command in ftp session
global counter_ftp_cmd:count = 0;
##counter of packets loss before
global last_stat_pkts_dropped:count = 0;
##
global set_connection: vector of connection;
##
global last_100_connection: vector of connection;
##different service in the last 100 conncetions
global service_last_100_connection: set[string];
##different Destination port in the last 100 conncetions
global port_D_last_100_connection: set[string];
##different Source port in the last 100 conncetions
global port_S_last_100_connection: set[string];
##different states of connection
global different_states: set[string];
global tmp_table: table[string] of count &default=0;
##info to print Source jiiter
global last_packet_timestamp_S: time;
global average_inter_packet_time_S: interval = 0.0secs ;
global packet_count_S: count = 0;
global jitterS: interval = 0.0secs;
global firstP_S_J: bool = T;
##info to print Destination jiiter
global last_packet_timestamp_D: time;
global average_inter_packet_time_D: interval = 0.0secs ;
global packet_count_D: count = 0;
global jitterD: interval = 0.0secs;
global firstP_D_J: bool = T;
function get_last_100_Conn(s: vector of connection): vector of connection
{
if(|s| >= 100)
{
local tmp:count = |s| - 100;
return s[|s| - 100 : |s|-1];
}
else
{
print "There aren't 100 connections in the analyzed file.pcap ";
return s;
}
}
function get_service_last_100_Conn(s: vector of connection): set[string]
{
local different_services: set[string];
# check the last 100 connections and detect the different types of service
for ( i, c in s )
{
if(c$conn?$service)
{
if(!(c$conn$service in different_services))
add different_services[c$conn$service];
}
}
return different_services;
}
function get_port_D_last_100_Conn(s: vector of connection): set[string]
{
local different_ports: set[string];
# check the last 100 connections and detect the different types of service
for ( i, c in s )
{
if(c$conn?$id)
{
if(c$conn$id?$resp_p)
{
local port_string = fmt("%s", c$conn$id$resp_p);
if(!(port_string in different_ports))
add different_ports[port_string];
}
}
}
return different_ports;
}
function get_port_S_last_100_Conn(s: vector of connection): set[string]
{
local different_ports: set[string];
# check the last 100 connections and detect the different types of service
for ( i, c in s )
{
if(c$conn?$id)
{
if(c$conn$id?$orig_p)
{
local port_string = fmt("%s", c$conn$id$orig_p);
if(!(port_string in different_ports))
add different_ports[port_string];
}
}
}
return different_ports;
}
function print_feature_37(tmp_table: table[string] of count)
{
# print feature 37;
print "-----------Feature 37-----------";
for(i in tmp_table)
{
if(tmp_table[i] == 1) print fmt("There is %d connection whose state is '%s' ", tmp_table[i], i);
else print fmt("There are %d connections whose state is '%s'", tmp_table[i], i);
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_41(last_100_connection: vector of connection, service_last_100_connection: set[string])
{
# print feature 41;
print "-----------Feature 41-----------";
for(s in service_last_100_connection)
{
print fmt("############################### %s ###############################", s);
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$orig_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn?$service)
{
if(c1$conn$id$orig_h == c$conn$id$orig_h && s == c1$conn$service)
cont += 1;
}
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection whose service is '%s' and same IP source '%s'", cont, s, c$conn$id$orig_h);
else print fmt("There are %d connections whose service is '%s' and same IP source '%s'", cont, s, c$conn$id$orig_h);
}
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_42(last_100_connection: vector of connection, service_last_100_connection: set[string])
{
# print feature 42;
print "-----------Feature 42-----------";
for(s in service_last_100_connection)
{
print fmt("############################### %s ###############################", s);
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$resp_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn?$service)
{
if(c1$conn$id$resp_h == c$conn$id$resp_h && s == c1$conn$service)
cont += 1;
}
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection whose service is '%s' and same IP destination '%s'", cont, s, c$conn$id$resp_h);
else print fmt("There are %d connections whose service is '%s' and same IP destination '%s'", cont, s, c$conn$id$resp_h);
}
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_43(last_100_connection: vector of connection)
{
# print feature 43;
print "-----------Feature 43-----------";
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$resp_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn$id$resp_h == c$conn$id$resp_h)
cont += 1;
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection that has the same IP destination '%s'", cont, c$conn$id$resp_h);
else print fmt("There are %d connections that have the same IP destination '%s'", cont, c$conn$id$resp_h);
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_44(last_100_connection: vector of connection)
{
# print feature 44;
print "-----------Feature 44-----------";
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$orig_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn$id$orig_h == c$conn$id$orig_h)
cont += 1;
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection that has the same IP source '%s'", cont, c$conn$id$orig_h);
else print fmt("There are %d connections that have the same IP source '%s'", cont, c$conn$id$orig_h);
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_45(last_100_connection: vector of connection, port_D_last_100_connection: set[string])
{
# print feature 45;
print "-----------Feature 45-----------";
for(p in port_D_last_100_connection)
{
print fmt("############################### %s ###############################", p);
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$orig_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn?$id)
{
if(c1$conn$id?$resp_p)
{
local port_str = fmt("%s", c1$conn$id$resp_p);
if(c1$conn$id$orig_h == c$conn$id$orig_h && p == port_str)
cont += 1;
}
}
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection whose same IP source is '%s' and destination port is '%s'", cont, c$conn$id$orig_h, p);
else print fmt("There are %d connections whose same IP source is '%s' and destination port is '%s'", cont, c$conn$id$orig_h, p);
}
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_46(last_100_connection: vector of connection, port_S_last_100_connection: set[string])
{
# print feature 46;
print "-----------Feature 46-----------";
for(p in port_S_last_100_connection)
{
print fmt("############################### %s ###############################", p);
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$resp_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn?$id)
{
if(c1$conn$id?$orig_p)
{
local port_str = fmt("%s", c1$conn$id$orig_p);
if(c1$conn$id$resp_h == c$conn$id$resp_h && p == port_str)
cont += 1;
}
}
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection whose same IP destination is '%s' and source port is '%s'", cont, c$conn$id$resp_h, p);
else print fmt("There are %d connections whose same IP destination is '%s' and source port is '%s'", cont, c$conn$id$resp_h, p);
}
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_feature_47(last_100_connection: vector of connection)
{
# print feature 47;
print "-----------Feature 47-----------";
local ip_seen: set[string] = set();
# print ip_seen;
for (j, c in last_100_connection)
{
local cont:count = 0;
local conn_id_str = fmt("%s", c$conn$id$orig_h);
if(!(conn_id_str in ip_seen))
{
for (k, c1 in last_100_connection)
{
if(c1$conn$id$orig_h == c$conn$id$orig_h && c1$conn$id$orig_h == c1$conn$id$resp_h)
cont += 1;
}
add ip_seen[conn_id_str];
if (cont > 0) {
if(cont == 1) print fmt("There is %d connection that has the same IP source and destination '%s' ", cont, c$conn$id$orig_h);
else print fmt("There are %d connections that have the same IP source and destination'%s'", cont, c$conn$id$orig_h);
}
}
}
print "----------------------------------------";
print "";
print "";
}
function print_infoAllC()
{
print "";
print "-----------Number of flows that has method Post in http service-----------";
print fmt("There are '%d' flows that has method Post in http service", allC$http_post);
print "----------------------------------------";
print "";
print "";
print "-----------Number of flows that has method Get in http service-----------";
print fmt("There are '%d' flows that has method Get in http service", allC$http_get);
print "----------------------------------------";
print "";
print "";
}
event zeek_init() &priority=5
{
Log::create_stream(Extractor_Feautures::LOG1, [$columns=Info_, $ev=log_1, $path="fullLog", $policy=log_policy1]);
Log::create_stream(Extractor_Feautures::LOG2, [$columns=FlowFeatures, $ev=log_2, $path="flowFeatures", $policy=log_policy1]);
Log::create_stream(Extractor_Feautures::LOG4, [$columns=eachPackets, $path="infoPackets"]);
Log::create_stream(Extractor_Feautures::LOG5, [$columns=each_TCP_Conn, $path="infoTCPConn"]);
# Log::create_stream(Extractor_Feautures::LOG3, [$columns=Info3, $ev=log_conn3, $path="additionalFeatures", $policy=log_policy1]);
# Log::create_stream(Extractor_Feautures::LOG3, [$columns=Info3, $path="additionalFeatures"]);
# Log::create_stream(Extractor_Feautures::LOG6, [$columns=infoAllC, $path="infoAllConn"]);
}
event zeek_done() &priority=5
{
##we can also print these informations in a log file, but the file.log would have only one line
# Log::write(Extractor_Feautures::LOG6, allC);
##print on terminal the information about Number of flows that has method Post in http service
##and Number of flows that has method Get in http service
print_infoAllC();
##initialize vector of the last 100 connection detected by zeek in the file.pcap
last_100_connection = get_last_100_Conn(set_connection);
##initialize set of different types of service
service_last_100_connection = get_service_last_100_Conn(last_100_connection);
##initialize set of different types of Dest port
port_D_last_100_connection = get_port_D_last_100_Conn(last_100_connection);
##initialize set of different types of Source port
port_S_last_100_connection = get_port_S_last_100_Conn(last_100_connection);
# print feature 37
print_feature_37(tmp_table);
# print feature 41
print_feature_41(last_100_connection, service_last_100_connection);
# print feature 42
print_feature_42(last_100_connection, service_last_100_connection);
# print feature 43
print_feature_43(last_100_connection);
# print feature 44
print_feature_44(last_100_connection);
# print feature 45
print_feature_45(last_100_connection, port_D_last_100_connection);
# print feature 46
print_feature_46(last_100_connection, port_S_last_100_connection);
# print feature 47
print_feature_47(last_100_connection);
##other possibility
# for ( i, c in last_100_connection )
# {
# if(c$conn?$service)
# print i, c$conn$service, c$conn$id$orig_h;
# }
# for(c in last_100_connection)
# {
# print c;
# }
}
function conn_state1(c: connection, trans: transport_proto): string
{
local os = c$orig$state;
local rs = c$resp$state;
local o_inactive = os == TCP_INACTIVE || os == TCP_PARTIAL;
local r_inactive = rs == TCP_INACTIVE || rs == TCP_PARTIAL;
if ( trans == tcp )
{
if ( rs == TCP_RESET )
{
if ( os == TCP_SYN_SENT || os == TCP_SYN_ACK_SENT ||
(os == TCP_RESET &&
c$orig$size == 0 && c$resp$size == 0) )
return "REJ";
else if ( o_inactive )
return "RSTRH";
else
return "RSTR";
}
else if ( os == TCP_RESET )
{
if ( r_inactive )
{
if ( /\^?S[^HAFGIQ]*R.*/ == c$history )
return "RSTOS0";
return "OTH";
}
return "RSTO";
}
else if ( rs == TCP_CLOSED && os == TCP_CLOSED )
return "SF";
else if ( os == TCP_CLOSED )
return r_inactive ? "SH" : "S2";
else if ( rs == TCP_CLOSED )
return o_inactive ? "SHR" : "S3";
else if ( os == TCP_SYN_SENT && rs == TCP_INACTIVE )
return "S0";
else if ( os == TCP_ESTABLISHED && rs == TCP_ESTABLISHED )
return "S1";
else
return "OTH";
}
else if ( trans == udp )
{
if ( os == UDP_ACTIVE )
return rs == UDP_ACTIVE ? "SF" : "S0";
else
return rs == UDP_ACTIVE ? "SHR" : "OTH";
}
else
return "OTH";
}
## Fill out the c$conn record for logging
function set_conn1(c: connection, eoc: bool)
{
if ( ! c?$conn1 )
{
local p = get_port_transport_proto(c$id$resp_p);
c$conn1 = Info_($ts=strftime("%Y-%m-%d %H:%M:%S", c$start_time), $uid=c$uid, $proto=p);
# print fmt("conversion: %s", strftime("%Y-%m-%d %H:%M:%S", c$start_time));
}
c$conn1$id=c$id;
# print c$conn1$id;
if ( c?$tunnel && |c$tunnel| > 0 )
{
if ( ! c$conn1?$tunnel_parents )
c$conn1$tunnel_parents = set();
add c$conn1$tunnel_parents[c$tunnel[|c$tunnel|-1]$uid];
}
if( |Site::local_nets| > 0 )
{
c$conn1$local_orig=Site::is_local_addr(c$id$orig_h);
c$conn1$local_resp=Site::is_local_addr(c$id$resp_h);
}
if ( eoc )
{
if ( c$duration > 0secs )
{
c$conn1$duration=duration_to_mins_secs(c$duration);
c$conn1$orig_bytes=c$orig$size;
c$conn1$resp_bytes=c$resp$size;
# local bps = (count_to_double(c$orig$size) * 8) / c$duration;
local s_bps = (c$orig$size * 8) / interval_to_double(c$duration);
local d_bps = (c$resp$size * 8) / interval_to_double(c$duration); ## *8 to convert bytes into bits
##DEBUG
# print fmt("bits per second source: %f", s_bps);
# print fmt("bits per second source: %f", d_bps);
c$conn1$sload = s_bps;
c$conn1$dload = d_bps;
}
if ( c$orig?$num_pkts )
{
# these are set if use_conn_size_analyzer=T
# we can have counts in here even without duration>0
c$conn1$orig_pkts = c$orig$num_pkts;
c$conn1$orig_ip_bytes = c$orig$num_bytes_ip;
c$conn1$resp_pkts = c$resp$num_pkts;
c$conn1$resp_ip_bytes = c$resp$num_bytes_ip;
}
if ( |c$service| > 0 )
c$conn1$service=to_lower(join_string_set(c$service, ","));
c$conn1$conn_state=conn_state1(c, get_port_transport_proto(c$id$resp_p));
##count number of connession for each different type of state
if(!(c$conn1$conn_state in different_states))
{
add different_states[c$conn1$conn_state];
tmp_table[c$conn1$conn_state] += 1 ;
}
else
{
tmp_table[c$conn1$conn_state] += 1 ;
}
if ( c$history != "" )
c$conn1$history=c$history;
if(c$orig?$num_pkts)
{
if(c$conn1$orig_pkts > 0 && c$duration > 0secs )
{
c$conn1$smeansz = c$conn1$orig_bytes / c$conn1$orig_pkts;
}
if(c$conn1$resp_pkts > 0 && c$duration > 0secs )
{
c$conn1$dmeansz = c$conn1$resp_bytes / c$conn1$resp_pkts;
}
}
c$conn1$start_time = strftime("%Y-%m-%d %H:%M:%S", c$start_time);
c$conn1$last_time = strftime("%Y-%m-%d %H:%M:%S", c$start_time + c$duration);
local ns = get_net_stats();
# print ns;
local new_dropped = ns$pkts_dropped - last_stat_pkts_dropped;
c$conn1$pkts_dropped_ = new_dropped;
last_stat_pkts_dropped = new_dropped;
}
}
function set_conn2(c: connection, eoc: bool)
{
if ( ! c?$conn2 )
{
local p = get_port_transport_proto(c$id$resp_p);
c$conn2 = FlowFeatures($proto=p);
# print fmt("conversion: %s", strftime("%Y-%m-%d %H:%M:%S", c$start_time));
}
c$conn2$id=c$id;
}
function set_3(c: connection, eoc: bool)
{
local ip_s = c$id$orig_h;
local port_s = c$id$orig_p;
local ip_d = c$id$resp_h;
local port_d = c$id$resp_p;
if(c?$conn1)
{
if(ip_s == ip_d && port_s == port_d)
c$conn1$is_sm_ips_ports= "1";
else
c$conn1$is_sm_ips_ports= "0";
}
}
event content_gap(c: connection, is_orig: bool, seq: count, length: count) &priority=5
{
set_conn1(c, F);
c$conn1$missed_bytes = c$conn1$missed_bytes + length;
}
event tunnel_changed(c: connection, e: EncapsulatingConnVector) &priority=5
{
set_conn1(c, F);
if ( |e| > 0 )
{
if ( ! c$conn1?$tunnel_parents )
c$conn1$tunnel_parents = set();
add c$conn1$tunnel_parents[e[|e|-1]$uid];
}
c$tunnel = e;
}
event connection_state_remove(c: connection) &priority=5
{
set_conn1(c, T);
set_conn2(c, T);
set_3(c, T);
# local orig_window = c$tcp$orig$win_size;
# local resp_window = c$tcp$resp$win_size;
# print "orig_window" orig_window;
# print "resp_window" resp_window;
# print "get_orig_seq ", get_orig_seq(c$id);
# print "get_resp_seq ", get_orig_seq(c$id);
##add the connection in temp set of connection
set_connection += c;
}
event connection_state_remove(c: connection) &priority=-5
{
if(c?$conn1 && c?$ftp)
c$conn1$ct_ftp_cmd = counter_ftp_cmd;
Log::write(Extractor_Feautures::LOG1, c$conn1);
Log::write(Extractor_Feautures::LOG2, c$conn2);
# Log::write(Extractor_Feautures::LOG3, conn3);
##initialize to 0 the counter of flows ftp with command for the next ftp connection
if(c?$conn1 && c?$ftp)
counter_ftp_cmd = 0;
}
event tcp_packet(c: connection, is_orig: bool, flags: string, seq: count, ack: count, len: count, payload: string)
{
if(is_orig)
{
##DEBUG
# print "Source TCP sequence number", seq;
if(firstP_S)
{
ts_Packet_Prev_S = network_time();
firstP_S = F;
contS += 1;
}
else
{
tmp_meanInterval_S = tmp_meanInterval_S + (network_time() - ts_Packet_Prev_S);
contS += 1;
ts_Packet_Prev_S = network_time();
}
eachP$stcpb = seq;
}
else
{
##DEBUG
# print "Destination TCP sequence number", seq;
if(firstP_D)
{
ts_Packet_Prev_D = network_time();
firstP_D = F;
contD += 1;
}
else
{
tmp_meanInterval_D = tmp_meanInterval_D + (network_time() - ts_Packet_Prev_D);
contD += 1;
ts_Packet_Prev_D = network_time();
}
eachP$dtcpb = seq;
}