-
Notifications
You must be signed in to change notification settings - Fork 9
/
spp_ai.c
1579 lines (1279 loc) · 46.5 KB
/
spp_ai.c
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
/*
* =====================================================================================
*
* Filename: spp_ai.c
*
* Description: Main file for the spp_ai Snort preprocessor module
*
* Version: 0.1
* Created: 26/07/2010 11:00:41
* Revision: none
* Compiler: gcc
*
* Author: BlackLight (http://0x00.ath.cx), <blacklight@autistici.org>
* Licence: GNU GPL v.3
* Company: DO WHAT YOU WANT CAUSE A PIRATE IS FREE, YOU ARE A PIRATE!
*
* =====================================================================================
*/
#include "spp_ai.h"
#include "sfPolicyUserData.h"
#include "sf_preproc_info.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
/** \defgroup spp_ai Main file for spp_ai module
* @{ */
AI_snort_alert* (*get_alerts)(void);
AI_config *config = NULL;
tSfPolicyUserContextId ex_config = NULL;
static void* (*alertparser_thread)(void*) = NULL;
#ifdef SNORT_RELOAD
tSfPolicyUserContextId ex_swap_config = NULL;
#endif
static void AI_init(char *);
static void AI_process(void *, void *);
static AI_config * AI_parse(char *);
#ifdef SNORT_RELOAD
static void AI_reload(char *);
static int AI_reloadSwapPolicyFree(tSfPolicyUserContextId, tSfPolicyId, void *);
static void * AI_reloadSwap(void);
static void AI_reloadSwapFree(void *);
#endif
/**
* \brief Function called when the module experiences a fatal error
* \param msg Error message
* \param file File where the error occurred
* \param line Line number where the error occurred
*/
void
AI_fatal_err ( const char *msg, const char *file, const int line )
{
_dpd.fatalMsg ( "%s: %s at %s:%d (%s)\n",
PREPROC_NAME, msg, file, line,
((errno != 0) ? strerror(errno) : ""));
} /* ----- end of function AI_fatal_err ----- */
/**
* \brief Set up the preprocessor module
*/
void AI_setup(void)
{
#ifndef SNORT_RELOAD
_dpd.registerPreproc("ai", AI_init);
#else
_dpd.registerPreproc("ai", AI_init, AI_reload,
AI_reloadSwap, AI_reloadSwapFree);
#endif
DEBUG_WRAP(_dpd.debugMsg(DEBUG_PLUGIN, "Preprocessor: AI is setup\n"););
} /* ----- end of function AI_setup ----- */
/**
* \brief Initialize the preprocessor module
* \param args Configuration arguments passed to the module
*/
static void AI_init(char *args)
{
pthread_t cleanup_thread,
logparse_thread,
webserv_thread,
neural_thread,
correlation_thread;
tSfPolicyId policy_id = _dpd.getParserPolicy();
_dpd.logMsg("AI dynamic preprocessor configuration\n");
if (ex_config == NULL)
{
ex_config = sfPolicyConfigCreate();
if (ex_config == NULL)
AI_fatal_err ("Could not allocate configuration struct", __FILE__, __LINE__);
}
config = AI_parse(args);
sfPolicyUserPolicySet(ex_config, policy_id);
sfPolicyUserDataSetCurrent(ex_config, config);
/* Initialize the extra correlation modules */
AI_init_corr_modules();
/* If the hash_cleanup_interval or stream_expire_interval options are set to zero,
* no cleanup will be made on the streams */
if ( config->hashCleanupInterval != 0 && config->streamExpireInterval != 0 )
{
if ( pthread_create ( &cleanup_thread, NULL, AI_hashcleanup_thread, config ) != 0 )
{
AI_fatal_err ( "Failed to create the hash cleanup thread", __FILE__, __LINE__ );
}
}
/* If the correlation_graph_interval option is set to zero, no correlation
* algorithm will be run over the alerts */
if ( config->correlationGraphInterval != 0 )
{
if ( pthread_create ( &correlation_thread, NULL, AI_alert_correlation_thread, config ) != 0 )
{
AI_fatal_err ( "Failed to create the alert correlation thread", __FILE__, __LINE__ );
}
}
if ( strlen ( config->alertfile ) > 0 )
{
if ( pthread_create ( &logparse_thread, NULL, alertparser_thread, config ) != 0 )
{
AI_fatal_err ( "Failed to create the alert parser thread", __FILE__, __LINE__ );
}
}
/* If webserv_port is != 0, start the web server */
if ( config->webserv_port != 0 )
{
if ( pthread_create ( &webserv_thread, NULL, AI_webserv_thread, NULL ) != 0 )
{
AI_fatal_err ( "Failed to create the web server thread", __FILE__, __LINE__ );
}
}
/* If neural_network_training_interval != 0, start the thread for the neural network */
if ( config->neuralNetworkTrainingInterval != 0 )
{
if ( pthread_create ( &neural_thread, NULL, AI_neural_thread, NULL ) != 0 )
{
AI_fatal_err ( "Failed to create the neural network thread", __FILE__, __LINE__ );
}
}
/* Register the preprocessor function, Transport layer, ID 10000 */
_dpd.addPreproc(AI_process, PRIORITY_TRANSPORT, 10000, PROTO_BIT__TCP | PROTO_BIT__UDP);
DEBUG_WRAP(_dpd.debugMsg(DEBUG_PLUGIN, "Preprocessor: AI is initialized\n"););
} /* ----- end of function AI_init ----- */
/**
* \brief Parse the arguments passed to the module saving them to a valid configuration struct
* \param args Arguments passed to the module
* \return Pointer to AI_config keeping the configuration for the module
*/
static AI_config * AI_parse(char *args)
{
char *arg;
char *match;
char alertfile[1024] = { 0 },
alert_history_file[1024] = { 0 },
clusterfile[1024] = { 0 },
corr_alerts_dir[1024] = { 0 },
corr_modules_dir[1024] = { 0 },
corr_rules_dir[1024] = { 0 },
webserv_dir[1024] = { 0 },
webserv_banner[1024] = { 0 };
char **matches = NULL;
int nmatches = 0;
int i;
int len;
unsigned long int offset;
double corr_threshold_coefficient = DEFAULT_CORR_THRESHOLD;
uint32_t netmask;
int min_val;
int max_val;
char label[256];
cluster_type type;
hierarchy_node **hierarchy_nodes = NULL;
int n_hierarchy_nodes = 0;
unsigned short webserv_port = 0;
unsigned long alertfile_len = 0,
alert_bufsize = 0,
alert_clustering_interval = 0,
alert_correlation_weight = 0,
alert_history_file_len = 0,
alert_serialization_interval = 0,
bayesian_correlation_cache_validity = 0,
bayesian_correlation_interval = 0,
cleanup_interval = 0,
clusterfile_len = 0,
cluster_max_alert_interval = 0,
corr_alerts_dir_len = 0,
corr_modules_dir_len = 0,
corr_rules_dir_len = 0,
correlation_graph_interval = 0,
database_parsing_interval = 0,
manual_correlations_parsing_interval = 0,
max_hash_pkt_number = 0,
neural_clustering_interval = 0,
neural_network_training_interval = 0,
neural_train_steps = 0,
output_neurons_per_side = 0,
stream_expire_interval = 0,
use_knowledge_base_correlation_index = 0,
use_stream_hash_table = 0,
webserv_banner_len = 0,
webserv_dir_len = 0;
BOOL has_cleanup_interval = false,
has_stream_expire_interval = false,
has_correlation_interval = false,
has_corr_alerts_dir = false,
has_corr_modules_dir = false,
has_database_interval = false,
has_webserv_dir = false,
has_webserv_banner = false,
has_alertfile = false,
has_clusterfile = false,
has_corr_rules_dir = false,
has_clustering = false,
has_database_log = false,
has_database_output = false,
has_alert_history_file = false;
if ( !( config = ( AI_config* ) malloc ( sizeof( AI_config )) ))
AI_fatal_err( "Could not allocate configuration struct", __FILE__, __LINE__ );
memset ( config, 0, sizeof ( AI_config ));
/* Parsing the hashtable_cleanup_interval option */
if (( arg = (char*) strcasestr( args, "hashtable_cleanup_interval" ) ))
{
has_cleanup_interval = true;
for ( arg += strlen("hashtable_cleanup_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "Hashtable_cleanup_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
cleanup_interval = strtoul(arg, NULL, 10);
config->hashCleanupInterval = cleanup_interval;
_dpd.logMsg(" Hash table cleanup interval: %d\n", config->hashCleanupInterval);
}
/* Parsing the tcp_stream_expire_interval option */
if (( arg = (char*) strcasestr( args, "tcp_stream_expire_interval" ) ))
{
has_stream_expire_interval = true;
for ( arg += strlen("tcp_stream_expire_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err( "tcp_stream_expire_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
stream_expire_interval = strtoul(arg, NULL, 10);
config->streamExpireInterval = stream_expire_interval;
_dpd.logMsg(" TCP stream expire interval: %d\n", config->streamExpireInterval);
}
/* Parsing the alert_clustering_interval option */
if (( arg = (char*) strcasestr( args, "alert_clustering_interval" ) ))
{
for ( arg += strlen("alert_clustering_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "alert_clustering_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
alert_clustering_interval = strtoul(arg, NULL, 10);
config->alertClusteringInterval = alert_clustering_interval;
_dpd.logMsg(" Alert clustering interval: %d\n", config->alertClusteringInterval);
}
/* Parsing the database_parsing_interval option */
if (( arg = (char*) strcasestr( args, "database_parsing_interval" ) ))
{
has_database_interval = true;
for ( arg += strlen("database_parsing_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "database_parsing_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
database_parsing_interval = strtoul(arg, NULL, 10);
config->databaseParsingInterval = database_parsing_interval;
_dpd.logMsg(" Database parsing interval: %d\n", config->databaseParsingInterval);
}
/* Parsing the correlation_graph_interval option */
if (( arg = (char*) strcasestr( args, "correlation_graph_interval" ) ))
{
has_correlation_interval = true;
for ( arg += strlen("correlation_graph_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "correlation_graph_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
correlation_graph_interval = strtoul(arg, NULL, 10);
config->correlationGraphInterval = correlation_graph_interval;
_dpd.logMsg(" Correlation graph thread interval: %d\n", config->correlationGraphInterval);
}
/* Parsing the alert_serialization_interval option */
if (( arg = (char*) strcasestr( args, "alert_serialization_interval" ) ))
{
for ( arg += strlen("alert_serialization_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "alert_serialization_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
alert_serialization_interval = strtoul(arg, NULL, 10);
config->alertSerializationInterval = alert_serialization_interval;
_dpd.logMsg(" Alert serialization thread interval: %d\n", config->correlationGraphInterval);
}
/* Parsing the alert_bufsize option */
if (( arg = (char*) strcasestr( args, "alert_bufsize" ) ))
{
for ( arg += strlen("alert_bufsize");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err( "alert_bufsize option used but "
"no value specified", __FILE__, __LINE__ );
}
alert_bufsize = strtoul(arg, NULL, 10);
config->alert_bufsize= alert_bufsize;
_dpd.logMsg(" Alert buffer size: %d\n", config->alert_bufsize );
}
/* Parsing the webserv_port option */
if (( arg = (char*) strcasestr( args, "webserv_port" ) ))
{
for ( arg += strlen("webserv_port");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err( "webserv_port option used but "
"no value specified", __FILE__, __LINE__ );
}
webserv_port = (unsigned short) strtoul(arg, NULL, 10);
config->webserv_port= webserv_port;
} else {
config->webserv_port = DEFAULT_WEBSERV_PORT;
}
_dpd.logMsg(" Web server port: %d\n", config->webserv_port );
/* Parsing the correlation_threshold_coefficient option */
if (( arg = (char*) strcasestr( args, "correlation_threshold_coefficient" ) ))
{
for ( arg += strlen("correlation_threshold_coefficient");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err( "correlation_threshold_coefficient option used but "
"no value specified", __FILE__, __LINE__ );
}
corr_threshold_coefficient = strtod ( arg, NULL );
}
config->correlationThresholdCoefficient = corr_threshold_coefficient;
_dpd.logMsg( " Correlation threshold coefficient: %f\n", corr_threshold_coefficient );
/* Parsing the bayesian_correlation_interval option */
if (( arg = (char*) strcasestr( args, "bayesian_correlation_interval" ) ))
{
for ( arg += strlen("bayesian_correlation_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "bayesian_correlation_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
bayesian_correlation_interval = strtoul ( arg, NULL, 10 );
config->bayesianCorrelationInterval = bayesian_correlation_interval;
} else {
bayesian_correlation_interval = DEFAULT_BAYESIAN_CORRELATION_INTERVAL;
}
config->bayesianCorrelationInterval = bayesian_correlation_interval;
_dpd.logMsg( " Bayesian correlation interval: %u\n", config->bayesianCorrelationInterval );
/* Parsing the manual_correlations_parsing_interval option */
if (( arg = (char*) strcasestr( args, "manual_correlations_parsing_interval" ) ))
{
for ( arg += strlen("manual_correlations_parsing_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "manual_correlations_parsing_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
manual_correlations_parsing_interval = strtoul ( arg, NULL, 10 );
} else {
manual_correlations_parsing_interval = DEFAULT_MANUAL_CORRELATIONS_PARSING_INTERVAL;
}
config->manualCorrelationsParsingInterval = manual_correlations_parsing_interval;
_dpd.logMsg( " Manual correlations parsing interval: %u\n", config->manualCorrelationsParsingInterval );
/* Parsing the bayesian_correlation_cache_validity option */
if (( arg = (char*) strcasestr( args, "bayesian_correlation_cache_validity" ) ))
{
for ( arg += strlen("bayesian_correlation_cache_validity");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "bayesian_correlation_cache_validity option used but "
"no value specified", __FILE__, __LINE__ );
}
bayesian_correlation_cache_validity = strtoul ( arg, NULL, 10 );
config->bayesianCorrelationCacheValidity = bayesian_correlation_cache_validity;
} else {
bayesian_correlation_cache_validity = DEFAULT_BAYESIAN_CORRELATION_CACHE_VALIDITY;
}
config->bayesianCorrelationCacheValidity = bayesian_correlation_cache_validity;
_dpd.logMsg( " Bayesian cache validity interval: %u\n", config->bayesianCorrelationCacheValidity );
/* Parsing the cluster_max_alert_interval option */
if (( arg = (char*) strcasestr( args, "cluster_max_alert_interval" ) ))
{
for ( arg += strlen("cluster_max_alert_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "cluster_max_alert_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
cluster_max_alert_interval = strtoul ( arg, NULL, 10 );
} else {
cluster_max_alert_interval = DEFAULT_CLUSTER_MAX_ALERT_INTERVAL;
}
config->clusterMaxAlertInterval = cluster_max_alert_interval;
_dpd.logMsg( " Cluster alert max interval: %u\n", config->clusterMaxAlertInterval );
/* Parsing the neural_network_training_interval option */
if (( arg = (char*) strcasestr( args, "neural_network_training_interval" ) ))
{
for ( arg += strlen("neural_network_training_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "neural_network_training_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
neural_network_training_interval = strtoul ( arg, NULL, 10 );
} else {
neural_network_training_interval = DEFAULT_NEURAL_NETWORK_TRAINING_INTERVAL;
}
config->neuralNetworkTrainingInterval = neural_network_training_interval;
_dpd.logMsg( " Neural network training interval: %u\n", config->neuralNetworkTrainingInterval );
/* Parsing the neural_clustering_interval option */
if (( arg = (char*) strcasestr( args, "neural_clustering_interval" ) ))
{
for ( arg += strlen("neural_clustering_interval");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "neural_clustering_interval option used but "
"no value specified", __FILE__, __LINE__ );
}
neural_clustering_interval = strtoul ( arg, NULL, 10 );
} else {
neural_clustering_interval = DEFAULT_NEURAL_CLUSTERING_INTERVAL;
}
config->neuralClusteringInterval = neural_clustering_interval;
_dpd.logMsg( " Neural network clustering interval: %u\n", config->neuralClusteringInterval );
/* Parsing the output_neurons_per_side option */
if (( arg = (char*) strcasestr( args, "output_neurons_per_side" ) ))
{
for ( arg += strlen("output_neurons_per_side");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "output_neurons_per_side option used but "
"no value specified", __FILE__, __LINE__ );
}
output_neurons_per_side = strtoul ( arg, NULL, 10 );
} else {
output_neurons_per_side = DEFAULT_OUTPUT_NEURONS_PER_SIDE;
}
config->outputNeuronsPerSide = output_neurons_per_side;
_dpd.logMsg( " Output neurons per side: %u\n", config->outputNeuronsPerSide );
/* Parsing the neural_train_steps option */
if (( arg = (char*) strcasestr( args, "neural_train_steps" ) ))
{
for ( arg += strlen("neural_train_steps");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "neural_train_steps option used but "
"no value specified", __FILE__, __LINE__ );
}
neural_train_steps = strtoul ( arg, NULL, 10 );
} else {
neural_train_steps = DEFAULT_NEURAL_TRAIN_STEPS;
}
config->neural_train_steps = neural_train_steps;
_dpd.logMsg( " Neural train steps: %u\n", config->neural_train_steps );
/* Parsing the max_hash_pkt_number option */
if (( arg = (char*) strcasestr( args, "max_hash_pkt_number" ) ))
{
for ( arg += strlen("max_hash_pkt_number");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "max_hash_pkt_number option used but "
"no value specified", __FILE__, __LINE__ );
}
max_hash_pkt_number = strtoul ( arg, NULL, 10 );
} else {
max_hash_pkt_number = DEFAULT_MAX_HASH_PKT_NUMBER;
}
config->max_hash_pkt_number = max_hash_pkt_number;
_dpd.logMsg( " Maximum number of packets stored in the hash table: %u\n", config->max_hash_pkt_number );
/* Parsing the use_knowledge_base_correlation_index option */
if (( arg = (char*) strcasestr( args, "use_knowledge_base_correlation_index" ) ))
{
for ( arg += strlen("use_knowledge_base_correlation_index");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "use_knowledge_base_correlation_index option used but "
"no value specified", __FILE__, __LINE__ );
}
use_knowledge_base_correlation_index = strtoul ( arg, NULL, 10 );
} else {
use_knowledge_base_correlation_index = DEFAULT_USE_KNOWLEDGE_BASE_CORRELATION_INDEX;
}
config->use_knowledge_base_correlation_index = use_knowledge_base_correlation_index;
_dpd.logMsg( " Using knowledge base alert correlation index: %u\n", config->use_knowledge_base_correlation_index );
/* Parsing the use_stream_hash_table option */
if (( arg = (char*) strcasestr( args, "use_stream_hash_table" ) ))
{
for ( arg += strlen("use_stream_hash_table");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "use_stream_hash_table option used but "
"no value specified", __FILE__, __LINE__ );
}
use_stream_hash_table = strtoul ( arg, NULL, 10 );
} else {
use_stream_hash_table = DEFAULT_USE_STREAM_HASH_TABLE;
}
config->use_stream_hash_table = use_stream_hash_table;
_dpd.logMsg( " Using the stream hash table: %u\n", config->use_stream_hash_table );
/* Parsing the alert_correlation_weight option */
if (( arg = (char*) strcasestr( args, "alert_correlation_weight" ) ))
{
for ( arg += strlen("alert_correlation_weight");
*arg && (*arg < '0' || *arg > '9');
arg++ );
if ( !(*arg) )
{
AI_fatal_err ( "alert_correlation_weight option used but "
"no value specified", __FILE__, __LINE__ );
}
alert_correlation_weight = strtoul ( arg, NULL, 10 );
} else {
alert_correlation_weight = DEFAULT_ALERT_CORRELATION_WEIGHT;
}
config->alert_correlation_weight = alert_correlation_weight;
_dpd.logMsg( " Alert correlation weight: %u\n", config->alert_correlation_weight );
/* Parsing the alertfile option */
if (( arg = (char*) strcasestr( args, "alertfile" ) ))
{
for ( arg += strlen("alertfile");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "alertfile option used but no filename specified", __FILE__, __LINE__ );
}
for ( alertfile[ (++alertfile_len)-1 ] = *arg;
*arg && *arg != '"' && alertfile_len < 1024;
arg++, alertfile[ (++alertfile_len)-1 ] = *arg );
if ( alertfile[0] == 0 || alertfile_len <= 1 ) {
has_alertfile = false;
} else {
if ( alertfile_len >= 1024 ) {
AI_fatal_err ( "alertfile path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( alertfile ) == 0 ) {
has_alertfile = false;
} else {
has_alertfile = true;
alertparser_thread = AI_file_alertparser_thread;
alertfile[ alertfile_len-1 ] = 0;
strncpy ( config->alertfile, alertfile, alertfile_len );
_dpd.logMsg(" alertfile path: %s\n", config->alertfile);
}
}
}
/* Parsing the alert_history_file option */
if (( arg = (char*) strcasestr( args, "alert_history_file" ) ))
{
for ( arg += strlen("alert_history_file");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "alert_history_file option used but no filename specified", __FILE__, __LINE__ );
}
for ( alert_history_file[ (++alert_history_file_len)-1 ] = *arg;
*arg && *arg != '"' && alert_history_file_len < 1024;
arg++, alert_history_file[ (++alert_history_file_len)-1 ] = *arg );
if ( alert_history_file[0] == 0 || alert_history_file_len <= 1 ) {
has_alert_history_file = false;
} else {
if ( alert_history_file_len >= 1024 ) {
AI_fatal_err ( "alert_history_file path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( alert_history_file ) == 0 ) {
has_alert_history_file = false;
} else {
has_alert_history_file = true;
alert_history_file [ alert_history_file_len-1 ] = 0;
strncpy ( config->alert_history_file, alert_history_file, alert_history_file_len );
_dpd.logMsg(" alert_history_file path: %s\n", config->alert_history_file);
}
}
}
/* Parsing the clusterfile option */
if (( arg = (char*) strcasestr( args, "clusterfile" ) ))
{
for ( arg += strlen("clusterfile");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "clusterfile option used but no filename specified", __FILE__, __LINE__ );
}
for ( clusterfile[ (++clusterfile_len)-1 ] = *arg;
*arg && *arg != '"' && clusterfile_len < 1024;
arg++, clusterfile[ (++clusterfile_len)-1 ] = *arg );
if ( clusterfile[0] == 0 || clusterfile_len <= 1 ) {
has_clusterfile = false;
} else {
if ( clusterfile_len >= 1024 ) {
AI_fatal_err ( "clusterfile path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( clusterfile ) == 0 ) {
has_clusterfile = false;
} else {
has_clusterfile = true;
clusterfile[ clusterfile_len-1 ] = 0;
strncpy ( config->clusterfile, clusterfile, clusterfile_len );
_dpd.logMsg(" clusterfile path: %s\n", config->clusterfile);
}
}
}
/* Parsing the correlation_rules_dir option */
if (( arg = (char*) strcasestr( args, "correlation_rules_dir" ) ))
{
for ( arg += strlen("correlation_rules_dir");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "correlation_rules_dir option used but no filename specified", __FILE__, __LINE__ );
}
for ( corr_rules_dir[ (++corr_rules_dir_len)-1 ] = *arg;
*arg && *arg != '"' && corr_rules_dir_len < 1024;
arg++, corr_rules_dir[ (++corr_rules_dir_len)-1 ] = *arg );
if ( corr_rules_dir[0] == 0 || corr_rules_dir_len <= 1 ) {
has_corr_rules_dir = false;
} else {
if ( corr_rules_dir_len >= 1024 ) {
AI_fatal_err ( "corr_rules_dir path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( corr_rules_dir ) == 0 ) {
has_corr_rules_dir = false;
} else {
has_corr_rules_dir = true;
corr_rules_dir[ corr_rules_dir_len-1 ] = 0;
strncpy ( config->corr_rules_dir, corr_rules_dir, corr_rules_dir_len );
_dpd.logMsg(" corr_rules_dir path: %s\n", config->corr_rules_dir);
}
}
}
/* Parsing the correlated_alerts_dir option */
if (( arg = (char*) strcasestr( args, "correlated_alerts_dir" ) ))
{
for ( arg += strlen("correlated_alerts_dir");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "correlated_alerts_dir option used but no filename specified", __FILE__, __LINE__ );
}
for ( corr_alerts_dir[ (++corr_alerts_dir_len)-1 ] = *arg;
*arg && *arg != '"' && corr_alerts_dir_len < 1024;
arg++, corr_alerts_dir[ (++corr_alerts_dir_len)-1 ] = *arg );
if ( corr_alerts_dir[0] == 0 || corr_alerts_dir_len <= 1 ) {
has_corr_alerts_dir = false;
} else {
if ( corr_alerts_dir_len >= 1024 ) {
AI_fatal_err ( "correlated_alerts_dir path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( corr_alerts_dir ) == 0 ) {
has_corr_alerts_dir = false;
} else {
has_corr_alerts_dir = true;
corr_alerts_dir[ corr_alerts_dir_len-1 ] = 0;
strncpy ( config->corr_alerts_dir, corr_alerts_dir, corr_alerts_dir_len );
_dpd.logMsg(" correlated_alerts_dir: %s\n", config->corr_alerts_dir);
}
}
}
/* Parsing the webserv_dir option */
if (( arg = (char*) strcasestr( args, "webserv_dir" ) ))
{
for ( arg += strlen("webserv_dir");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "webserv_dir option used but no filename specified", __FILE__, __LINE__ );
}
for ( webserv_dir[ (++webserv_dir_len)-1 ] = *arg;
*arg && *arg != '"' && webserv_dir_len < sizeof ( webserv_dir );
arg++, webserv_dir[ (++webserv_dir_len)-1 ] = *arg );
if ( webserv_dir[0] == 0 || webserv_dir_len <= 1 ) {
has_webserv_dir = false;
} else {
if ( webserv_dir_len >= sizeof ( webserv_dir )) {
AI_fatal_err ( "webserv_dir path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( webserv_dir ) == 0 ) {
has_webserv_dir = false;
} else {
has_webserv_dir = true;
webserv_dir[ webserv_dir_len-1 ] = 0;
strncpy ( config->webserv_dir, webserv_dir, webserv_dir_len );
}
}
}
if ( ! has_webserv_dir )
{
#ifndef HAVE_CONFIG_H
AI_fatal_err ( "Unable to read PREFIX from config.h", __FILE__, __LINE__ );
#endif
snprintf ( config->webserv_dir, sizeof ( config->webserv_dir ), "%s/share/snort_ai_preprocessor/htdocs", PREFIX );
}
/* Remove unnecessary '/' at the end of the web server directory */
for ( i = strlen ( config->webserv_dir ) - 1; i >= 0 && config->webserv_dir[i] == '/'; i-- )
config->webserv_dir[i] = 0;
_dpd.logMsg(" webserv_dir: %s\n", config->webserv_dir);
snprintf ( config->neural_clusters_log, sizeof ( config->neural_clusters_log ), "%s/neural_clusters.xml", config->webserv_dir );
_dpd.logMsg(" neural_clusters_log: %s\n", config->neural_clusters_log);
/* Parsing the corr_modules_dir option */
if (( arg = (char*) strcasestr( args, "corr_modules_dir" ) ))
{
for ( arg += strlen("corr_modules_dir");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "corr_modules_dir option used but no filename specified", __FILE__, __LINE__ );
}
for ( corr_modules_dir[ (++corr_modules_dir_len)-1 ] = *arg;
*arg && *arg != '"' && corr_modules_dir_len < sizeof ( corr_modules_dir );
arg++, corr_modules_dir[ (++corr_modules_dir_len)-1 ] = *arg );
if ( corr_modules_dir[0] == 0 || corr_modules_dir_len <= 1 ) {
has_corr_modules_dir = false;
} else {
if ( corr_modules_dir_len >= sizeof ( corr_modules_dir )) {
AI_fatal_err ( "corr_modules_dir path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( corr_modules_dir ) == 0 ) {
has_corr_modules_dir = false;
} else {
has_corr_modules_dir = true;
corr_modules_dir[ corr_modules_dir_len-1 ] = 0;
strncpy ( config->corr_modules_dir, corr_modules_dir, corr_modules_dir_len );
}
}
}
if ( ! has_corr_modules_dir )
{
#ifndef HAVE_CONFIG_H
AI_fatal_err ( "Unable to read PREFIX from config.h", __FILE__, __LINE__ );
#endif
snprintf ( config->corr_modules_dir, sizeof ( config->corr_modules_dir ), "%s/share/snort_ai_preprocessor/corr_modules", PREFIX );
}
/* Neural network output file */
if ( config->neuralNetworkTrainingInterval != 0 )
{
#ifndef HAVE_DB
AI_fatal_err ( "Neural network based correlation support set but the module was compiled with no database support "
"(recompile the module with database support or set the neural_network_training_interval option in snort.conf to 0",
__FILE__, __LINE__ );
#endif
#ifndef HAVE_CONFIG_H
AI_fatal_err ( "Unable to read PREFIX from config.h", __FILE__, __LINE__ );
#endif
snprintf ( config->netfile, sizeof ( config->netfile ), "%s/share/snort_ai_preprocessor/som.dat", PREFIX );
}
/* Parsing the webserv_banner option */
if (( arg = (char*) strcasestr( args, "webserv_banner" ) ))
{
for ( arg += strlen("webserv_banner");
*arg && *arg != '"';
arg++ );
if ( !(*(arg++)) )
{
AI_fatal_err ( "webserv_banner option used but no value specified", __FILE__, __LINE__ );
}
for ( webserv_banner[ (++webserv_banner_len)-1 ] = *arg;
*arg && *arg != '"' && webserv_banner_len < sizeof ( webserv_banner );
arg++, webserv_banner[ (++webserv_banner_len)-1 ] = *arg );
if ( webserv_banner[0] == 0 || webserv_banner_len <= 1 ) {
has_webserv_banner = false;
} else {
if ( webserv_banner_len >= sizeof ( webserv_banner )) {
AI_fatal_err ( "webserv_banner path too long ( >= 1024 )", __FILE__, __LINE__ );
} else if ( strlen( webserv_banner ) == 0 ) {
has_webserv_banner = false;
} else {
has_webserv_banner = true;
webserv_banner[ webserv_banner_len-1 ] = 0;
strncpy ( config->webserv_banner, webserv_banner, webserv_banner_len );
}
}
}
if ( ! has_webserv_banner )
{
strncpy ( config->webserv_banner, DEFAULT_WEBSERV_BANNER, webserv_banner_len );
}
_dpd.logMsg(" webserv_banner: %s\n", config->webserv_banner);
/* Parsing database option */
if ( preg_match ( "\\s+database\\s*\\(\\s*([^\\)]+)\\)", args, &matches, &nmatches ) > 0 )
{
if ( ! has_database_log )
has_database_log = true;
match = strdup ( matches[0] );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
if ( preg_match ( "type\\s*=\\s*\"([^\"]+)\"", match, &matches, &nmatches ) > 0 )
{