forked from redBorder/Snort_GeoIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnort-2.9.4.rc.patch
880 lines (854 loc) · 32.1 KB
/
snort-2.9.4.rc.patch
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
diff -Nru snort-2.9.4.rc.old/configure snort-2.9.4.rc.new/configure
--- snort-2.9.4.rc.old/configure 2012-10-17 16:24:25.000000000 +0200
+++ snort-2.9.4.rc.new/configure 2012-11-27 23:13:17.405670102 +0100
@@ -636,6 +636,8 @@
BUILD_PROCPIDSTATS_TRUE
HAVE_TARGET_BASED_FALSE
HAVE_TARGET_BASED_TRUE
+HAVE_REPUTATION_GEOIP_FALSE
+HAVE_REPUTATION_GEOIP_TRUE
HAVE_ZLIB_FALSE
HAVE_ZLIB_TRUE
BUILD_DYNAMIC_EXAMPLES_FALSE
@@ -802,6 +804,7 @@
enable_gre
enable_mpls
enable_targetbased
+enable_reputationgeoip
enable_ppm
enable_perfprofiling
enable_linux_smp_stats
@@ -1485,6 +1488,7 @@
--disable-gre Disable GRE and IP in IP encapsulation support
--disable-mpls Disable MPLS support
--disable-targetbased Disable Target-Based Support in Stream, Frag, and Rules (adds pthread support implicitly)
+ --enable-reputationgeoip Enable Reputation GeoIP Support (adds GeoIP support implicitly)
--disable-ppm Disable packet/rule performance monitor
--disable-perfprofiling Disable preprocessor and rule performance profiling
--enable-linux-smp-stats Enable statistics reporting through proc
@@ -15773,6 +15777,26 @@
fi
fi
+# Check whether --enable-reputationgeoip was given.
+if test "${enable_reputationgeoip+set}" = set; then :
+ enableval=$enable_reputationgeoip; enable_reputationgeoip="$enableval"
+else
+ enable_reputationgeoip="no"
+fi
+
+if test "x$enable_reputationgeoip" = "xyes"; then
+ HAVE_REPUTATION_GEOIP_TRUE=
+ HAVE_REPUTATION_GEOIP_FALSE='#'
+else
+ HAVE_REPUTATION_GEOIP_TRUE='#'
+ HAVE_REPUTATION_GEOIP_FALSE=
+fi
+
+if test "x$enable_reputationgeoip" = "xyes"; then
+ CONFIGFLAGS="$CONFIGFLAGS -DREPUTATION_GEOIP"
+ LIBS="$LIBS -lGeoIP"
+fi
+
# Check whether --enable-ppm was given.
if test "${enable_ppm+set}" = set; then :
enableval=$enable_ppm; enable_ppm="$enableval"
@@ -16701,6 +16725,10 @@
as_fn_error $? "conditional \"HAVE_TARGET_BASED\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
fi
+if test -z "${HAVE_REPUTATION_GEOIP_TRUE}" && test -z "${HAVE_REPUTATION_GEOIP_FALSE}"; then
+ as_fn_error $? "conditional \"HAVE_REPUTATION_GEOIP\" was never defined.
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
+fi
if test -z "${BUILD_PROCPIDSTATS_TRUE}" && test -z "${BUILD_PROCPIDSTATS_FALSE}"; then
as_fn_error $? "conditional \"BUILD_PROCPIDSTATS\" was never defined.
Usually this means the macro was only invoked conditionally." "$LINENO" 5
diff -Nru snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/reputation_config.c snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/reputation_config.c
--- snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/reputation_config.c 2012-09-21 02:09:13.000000000 +0200
+++ snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/reputation_config.c 2012-11-27 23:13:13.556669741 +0100
@@ -82,6 +82,25 @@
#define REPUTATION_SHAREDREFRESH_KEYWORD "shared_refresh"
#define REPUTATION_WHITEACTION_KEYWORD "white"
+//redBorder: New Options: default action, keep ordered and reputation geoip (this option must be enabled at configure)
+#define REPUTATION_DEFAULT_ACTION_KEYWORD "default_action"
+#define REPUTATION_ORDERED_KEYWORD "ordered"
+
+#ifdef REPUTATION_GEOIP
+
+#define REPUTATION_GEOIP_DB_KEYWORD "geoip_db"
+#define REPUTATION_GEOIP_PATH_KEYWORD "geoip_path"
+#define REPUTATION_GEOIP_MANIFEST_SEPARATORS ",\r\n"
+#define REPUTATION_GEOIP_MIN_MANIFEST_COLUMNS 2
+
+#define REPUTATION_GEOIP_WHITE_TYPE_KEYWORD "white"
+#define REPUTATION_GEOIP_BLACK_TYPE_KEYWORD "block"
+#define REPUTATION_GEOIP_MONITOR_TYPE_KEYWORD "monitor"
+#define GEOIP_MANIFEST_FILENAME "geo.info"
+
+#endif
+//end redBorder options
+
#define REPUTATION_CONFIG_SECTION_SEPERATORS ",;"
#define REPUTATION_CONFIG_VALUE_SEPERATORS " "
#define REPUTATION_SEPARATORS " \t\r\n"
@@ -125,6 +144,14 @@
static void DisplayIPlistStats(ReputationConfig *);
static void DisplayReputationConfig(ReputationConfig *);
+//redBorder GeoIP prototype funtions
+#ifdef REPUTATION_GEOIP
+static void DisplayGeoIPStats(ReputationConfig *config);
+void GeoReputationPrintRepInfo(ReputationConfig *config);
+char *ignoreStartSpace(char *str);
+#endif
+//end redBorder GeoIP prototype funtions
+
/* ********************************************************************
* Function: estimateSizeFromEntries
*
@@ -501,6 +528,18 @@
_dpd.logMsg(" Shared memory is Not supported.\n");
}
+ //redBorder: Print ordered option value
+ _dpd.logMsg(" Ordered Lists: %s\n",
+ config->ordered ? "ENABLED":"DISABLED (Default)");
+#ifdef REPUTATION_GEOIP
+ //redBorder: Print geolocalization info
+ if (config->geoip)
+ {
+ _dpd.logMsg(" Geolocalization DB: %s\n", config->geoip_db);
+ _dpd.logMsg(" Geolocalization Path: %s\n", config->geoip_path);
+ }
+#endif
+ //end of redBorder options
_dpd.logMsg("\n");
}
@@ -1145,6 +1184,29 @@
if(config->sharedMem.path)
free(config->sharedMem.path);
+
+#ifdef REPUTATION_GEOIP
+ if (config->geoip_db) {
+ free(config->geoip_db);
+ config->geoip_db = NULL;
+ }
+
+ if (config->geoip_path) {
+ free(config->geoip_path);
+ config->geoip_path = NULL;
+ }
+
+ if (config->geoip) {
+ free(config->geoip_actions);
+ config->geoip_actions = NULL;
+ }
+
+ if (config->geoip) {
+ GeoIP_delete(config->geoip);
+ config->geoip=NULL;
+ }
+#endif
+
free(config);
}
@@ -1364,6 +1426,17 @@
config->emptySegment = NULL;
config->memsize = 0;
config->memCapReached = false;
+ //redBorder default options
+ config->defaultAction = DECISION_NULL; // by default no active decission will be taken
+ config->ordered = 0; // by default the order will not mind
+#ifdef REPUTATION_GEOIP
+ config->geoip_db = "/usr/local/share/GeoIP/GeoIP.dat"; // by default the gelocalization ip database
+ config->geoip_path = "/etc/snort/geoips"; // by default gelocalization path for geoip files
+ config->geoip = NULL; // by default geolocalization is not enabled
+ config->geoip_actions = NULL; // actions by country code
+ config->geoip_enabled = 0; // by default geoip is disabled. Only if proper country files has been included it will be enabled
+#endif
+ //end of redBorder default options
/* Sanity check(s) */
if ( !argp )
@@ -1420,6 +1493,12 @@
{
config->scanlocal = 1;
}
+ //redBorder REPUTATION_ORDERED_KEYWORD option process
+ else if ( !strcasecmp( cur_tokenp, REPUTATION_ORDERED_KEYWORD ))
+ {
+ config->ordered = 1;
+ }
+ //end of redBorder REPUTATION_ORDERED_KEYWORD option
else if ( !strcasecmp( cur_tokenp, REPUTATION_MEMCAP_KEYWORD ))
{
cur_tokenp = strtok( NULL, REPUTATION_CONFIG_VALUE_SEPERATORS);
@@ -1541,6 +1620,76 @@
/* processed before */
}
+ //redBorder: REPUTATION_DEFAULT_ACTION_KEYWORD option process
+ else if ( !strcasecmp( cur_tokenp, REPUTATION_DEFAULT_ACTION_KEYWORD ) )
+ {
+ cur_tokenp = strtok( NULL, REPUTATION_CONFIG_VALUE_SEPERATORS);
+ if (!cur_tokenp)
+ {
+ DynamicPreprocessorFatalMessage(" %s(%d) => Missing argument for %s\n",
+ *(_dpd.config_file), *(_dpd.config_line), REPUTATION_DEFAULT_ACTION_KEYWORD);
+ return;
+ }
+ if((strlen(REPUTATION_BLACKLIST_KEYWORD) == strlen (cur_tokenp))
+ && !strcasecmp(REPUTATION_BLACKLIST_KEYWORD,cur_tokenp))
+ {
+ config->defaultAction = BLACKLISTED;
+ }
+ else if((strlen(REPUTATION_WHITELIST_KEYWORD) == strlen (cur_tokenp))
+ && !strcasecmp(REPUTATION_WHITELIST_KEYWORD,cur_tokenp))
+ {
+ config->defaultAction = WHITELISTED_TRUST;
+ }
+ else if((strlen(REPUTATION_MONITORLIST_KEYWORD) == strlen (cur_tokenp))
+ && !strcasecmp(REPUTATION_MONITORLIST_KEYWORD,cur_tokenp))
+ {
+ config->defaultAction = MONITORED;
+ }
+ else
+ {
+ DynamicPreprocessorFatalMessage(" %s(%d) => Invalid argument: %s for %s,"
+ " Use [%s] or [%s] or [%s]\n",
+ *(_dpd.config_file), *(_dpd.config_line), cur_tokenp,
+ REPUTATION_DEFAULT_ACTION_KEYWORD,
+ REPUTATION_BLACKLIST_KEYWORD, REPUTATION_WHITELIST_KEYWORD, REPUTATION_MONITORLIST_KEYWORD);
+ return;
+ }
+ }
+ //redBorder: end of REPUTATION_DEFAULT_ACTION_KEYWORD option
+#ifdef REPUTATION_GEOIP
+ //redBorder: REPUTATION_GEOIP_DB_KEYWORD option process
+ else if ( !strcasecmp( cur_tokenp, REPUTATION_GEOIP_DB_KEYWORD ) )
+ {
+ cur_tokenp = strtok( NULL, REPUTATION_CONFIG_VALUE_SEPERATORS);
+ if (!cur_tokenp)
+ {
+ DynamicPreprocessorFatalMessage(" %s(%d) => Missing argument for %s\n",
+ *(_dpd.config_file), *(_dpd.config_line), REPUTATION_GEOIP_DB_KEYWORD);
+ return;
+ }
+ config->geoip_db = (char *) malloc( (strlen(cur_tokenp) +1 )* sizeof(char) );
+ if (config->geoip_db)
+ strcpy(config->geoip_db, cur_tokenp);
+ }
+ //redBorder: end of REPUTATION_GEOIP_DB_KEYWORD option
+
+ //redBorder: REPUTATION_GEOIP_PATH_KEYWORD option process
+ else if ( !strcasecmp( cur_tokenp, REPUTATION_GEOIP_PATH_KEYWORD ) )
+ {
+ cur_tokenp = strtok( NULL, REPUTATION_CONFIG_VALUE_SEPERATORS);
+ if (!cur_tokenp)
+ {
+ DynamicPreprocessorFatalMessage(" %s(%d) => Missing argument for %s\n",
+ *(_dpd.config_file), *(_dpd.config_line), REPUTATION_GEOIP_PATH_KEYWORD);
+ return;
+ }
+ config->geoip_path = (char *) malloc( (strlen(cur_tokenp) +1 )* sizeof(char) );
+ if (config->geoip_path)
+ strcpy(config->geoip_path, cur_tokenp);
+ }
+ //redBorder: end of REPUTATION_GEOIP_DB_KEYWORD option
+
+#endif
#ifdef SHARED_REP
else if ( !strcasecmp( cur_tokenp, REPUTATION_SHAREMEM_KEYWORD ))
{
@@ -1607,8 +1756,47 @@
cur_sectionp = strtok_r( next_sectionp, REPUTATION_CONFIG_SECTION_SEPERATORS, &next_sectionp);
DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Arguments token: %s\n",cur_sectionp ););
}
+
+
+#ifdef REPUTATION_GEOIP
+ //redBorder: Open GeoIP database
+ if (config->geoip_db) {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation Info: Opening Geolocalization database on %s\n", config->geoip_db););
+ //config->geoip = GeoIP_open(config->geoip_db, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE); //need to check performance of this option
+ config->geoip = GeoIP_open(config->geoip_db, GEOIP_MEMORY_CACHE ); //160% more eficcient than before one -> http://www.maxmind.com/en/benchmark
+
+ if (config->geoip) {
+ //Check the database is correct.
+ const char * returnedCountry = GeoIP_country_code_by_addr(config->geoip,NULL);
+ if (returnedCountry!=NULL) {
+ GeoIP_delete(config->geoip);
+ config->geoip = NULL;
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation Info: Geolocalization disabled\n"););
+ } else {
+ //Inicialize geoip_actions to DECISION_NULL for all available countries. Total countries -> GeoIP_num_countries()
+ unsigned i;
+ config->geoip_actions = (IPdecision *) malloc(GeoIP_num_countries() * sizeof(IPdecision));
+
+ if (config->geoip_actions) {
+ for ( i = 0; i < GeoIP_num_countries(); ++i)
+ {
+ config->geoip_actions[i] = DECISION_NULL;
+ }
+ } else {
+ GeoIP_delete(config->geoip);
+ config->geoip = NULL;
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation Info: Geolocalization disabled\n"););
+ }
+ }
+ } else {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation Info: Geolocalization disabled\n"););
+ }
+ }
+#endif
+
DisplayIPlistStats(config);
DisplayReputationConfig(config);
+
free(argcpyp);
}
@@ -1697,3 +1885,291 @@
repInfoBuff););
}
#endif
+
+#ifdef REPUTATION_GEOIP
+//redBorder: GeoIP function definitions. Extracted most of them from sharedmem
+
+static void DisplayGeoIPStats(ReputationConfig *config)
+{
+ unsigned count_monitored = 0;
+ unsigned count_white = 0;
+ unsigned count_black = 0;
+ unsigned count_none = 0;
+ unsigned count_total = 0;
+ unsigned i;
+
+ for ( i = 0; i < GeoIP_num_countries(); ++i)
+ {
+ if (DECISION_NULL == config->geoip_actions[i]) {
+ count_none++;
+ } else if (MONITORED == config->geoip_actions[i]) {
+ count_monitored++;
+ } else if (BLACKLISTED == config->geoip_actions[i]) {
+ count_black++;
+ } else {
+ count_white++;
+ }
+
+ if (config->geoip_actions[i]!=DECISION_NULL)
+ _dpd.logMsg(" %s -> %s\n", GeoIP_code_by_id(i), (config->geoip_actions[i]==MONITORED?"monitored":(config->geoip_actions[i]==BLACKLISTED?"drop":"bypass")));
+
+ count_total++;
+ }
+
+ /*Print out the summary*/
+ _dpd.logMsg(" Reputation GeoIP total countries loaded: %u, monitored: %u, black: %u, white: %u, none: %u\n",
+ count_total, count_monitored, count_black, count_white, count_none);
+}
+
+char *ignoreStartSpace(char *str)
+{
+ while((*str) && (isspace((int)*str)))
+ {
+ str++;
+ }
+ return str;
+}
+
+int getGeoFileTypeFromName (char *typeName)
+{
+ int type = UNKNOW_LIST;
+
+ /* Trim the starting spaces */
+ if (!typeName)
+ return type;
+
+ typeName = ignoreStartSpace(typeName);
+
+ if (strncasecmp(typeName, REPUTATION_GEOIP_WHITE_TYPE_KEYWORD, strlen(REPUTATION_GEOIP_WHITE_TYPE_KEYWORD)) == 0)
+ {
+ type = WHITE_LIST;
+ typeName += strlen(REPUTATION_GEOIP_WHITE_TYPE_KEYWORD);
+ }
+ else if (strncasecmp(typeName, REPUTATION_GEOIP_BLACK_TYPE_KEYWORD, strlen(REPUTATION_GEOIP_BLACK_TYPE_KEYWORD)) == 0)
+ {
+ type = BLACK_LIST;
+ typeName += strlen(REPUTATION_GEOIP_BLACK_TYPE_KEYWORD);
+ }
+ else if (strncasecmp(typeName, REPUTATION_GEOIP_MONITOR_TYPE_KEYWORD, strlen(REPUTATION_GEOIP_MONITOR_TYPE_KEYWORD)) == 0)
+ {
+ type = MONITOR_LIST;
+ typeName += strlen(REPUTATION_GEOIP_MONITOR_TYPE_KEYWORD);
+ }
+
+ if (UNKNOW_LIST != type )
+ {
+ /*Ignore spaces in the end*/
+ typeName = ignoreStartSpace(typeName);
+
+ if ( *typeName )
+ {
+ type = UNKNOW_LIST;
+ }
+ }
+ return type;
+}
+
+DataGeoFileList * processLineInGeoManifest(char *manifest, char *line, int linenumber, ReputationConfig *config)
+{
+ char* token;
+ int tokenIndex = 0;
+ char* nextPtr = line;
+ char filename[PATH_MAX];
+ DataGeoFileList * listItem;
+
+ if ((listItem = (DataGeoFileList*)calloc(1,sizeof(DataGeoFileList))) == NULL)
+ {
+ DynamicPreprocessorFatalMessage("%s(%d) => Cannot allocate memory to "
+ "store geo reputation manifest file information\n", manifest, linenumber);
+ return NULL;
+ }
+
+ while((token = strtok_r(nextPtr, REPUTATION_GEOIP_MANIFEST_SEPARATORS, &nextPtr)) != NULL)
+ {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Process GeoIP reputation list token: %s\n", token););
+ switch (tokenIndex)
+ {
+ case 0: /* File name */
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation GeoIP list filename: %s\n", token););
+ snprintf(filename, sizeof(filename), "%s/%s", config->geoip_path, token);
+ listItem->filename = strdup(filename);
+ if (listItem->filename == NULL)
+ {
+ DynamicPreprocessorFatalMessage("%s(%d) => Failed to allocate memory for "
+ "reputation manifest\n", manifest, linenumber);
+ }
+ break;
+
+ case 1: /* Action */
+ listItem->filetype = getGeoFileTypeFromName(token);
+ if (UNKNOW_LIST == listItem->filetype)
+ {
+ DynamicPreprocessorFatalMessage(" %s(%d) => Unknown action specified (%s)."
+ " Please specify a value: %s | %s | %s.\n", manifest, linenumber, token,
+ REPUTATION_GEOIP_WHITE_TYPE_KEYWORD, REPUTATION_GEOIP_BLACK_TYPE_KEYWORD, REPUTATION_GEOIP_MONITOR_TYPE_KEYWORD);
+ }
+ break;
+ }
+ tokenIndex++;
+ }
+
+ if (tokenIndex < REPUTATION_GEOIP_MIN_MANIFEST_COLUMNS)
+ {
+ /* Too few columns*/
+ free(listItem);
+ if (tokenIndex)
+ {
+ DynamicPreprocessorFatalMessage("%s(%d) => Too few columns in line: %s.\n ",
+ manifest, linenumber, line);
+ }
+ return NULL;
+ }
+
+ return listItem;
+}
+
+void LoadGeoListFile(DataGeoFileList *listItem, ReputationConfig *config)
+{
+ FILE *fp;
+ char line[MAX_MANIFEST_LINE_LENGTH];
+
+
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Processing geoip file: %s\n", listItem->filename););
+
+
+ if ((fp = fopen(listItem->filename, "r")) == NULL)
+ {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Error opening file at: %s\n", listItem->filename););
+ return;
+ }
+
+ while (fgets(line, sizeof(line), fp))
+ {
+ char * nextPtr = line;
+ if (line!=NULL && strlen(line) >= 2) {
+
+ /* remove comments */
+ if((nextPtr = strchr(line, '#')) != NULL) {
+ *nextPtr = '\0';
+ }
+
+ nextPtr = ignoreStartSpace(line);
+
+ if (strlen(nextPtr) >= 2) {
+ char code[3];
+ int id;
+ code[0] = nextPtr[0];
+ code[1] = nextPtr[1];
+ code[2] = '\0';
+
+ id = GeoIP_id_by_code(code);
+
+ if (id>0) {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation GeoIP configurations: %s (code: %d)\n", code, id););
+
+ if (config->geoip_actions[id]==DECISION_NULL) {
+ config->geoip_enabled = 1;
+
+ if (WHITE_LIST == listItem->filetype) {
+ config->geoip_actions[id] = WHITELISTED_TRUST;
+ } else if (BLACK_LIST == listItem->filetype) {
+ config->geoip_actions[id] = BLACKLISTED;
+ } else {
+ config->geoip_actions[id] = MONITORED;
+ }
+ }
+ } else {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation GeoIP configurations: %s (code: %d) -> IGNORED!!\n", code, id););
+ }
+ }
+ }
+ }
+
+ fclose(fp);
+}
+
+void initGeoFilesWithManifiest(void *conf)
+{
+ FILE *fp;
+ char line[MAX_MANIFEST_LINE_LENGTH];
+ char manifest_file[PATH_MAX];
+ int counter = 0;
+ int line_number = 0;
+ ReputationConfig *config = (ReputationConfig *) conf;
+
+ if (!config->geoip_path) {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Geolocalization not enabled (geoip_path is not present)\n"););
+ return;
+ }
+
+ snprintf(manifest_file, sizeof(manifest_file), "%s/%s", config->geoip_path, GEOIP_MANIFEST_FILENAME);
+
+ if ((fp = fopen(manifest_file, "r")) == NULL)
+ {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION,
+ "Error opening file at: %s\n", manifest_file););
+ return;
+ }
+
+ while (fgets(line, sizeof(line),fp))
+ {
+ char* nextPtr = NULL;
+ DataGeoFileList * listItem;
+
+ line_number++;
+
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation GeoIP manifest: %s\n",line ););
+ /* remove comments */
+ if((nextPtr = strchr(line, '#')) != NULL)
+ {
+ *nextPtr = '\0';
+ }
+
+ /* allocate memory if necessary*/
+ counter++;
+
+ /*Processing the line*/
+ listItem = processLineInGeoManifest(manifest_file, line, line_number, config);
+
+ //process list item
+ if (listItem)
+ {
+ LoadGeoListFile(listItem, config);
+ free(listItem);
+ } else {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Reputation GeoIP file ignored\n"););
+ }
+ }
+
+ fclose(fp);
+
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "Successfully processed GeoIP manifest file: %s\n", GEOIP_MANIFEST_FILENAME););
+
+ _dpd.logMsg("Reputation GeoIP files processed: \n");
+ DisplayGeoIPStats(config);
+
+#ifdef DEBUG_MSGS
+ GeoReputationPrintRepInfo(config);
+#endif
+
+}
+
+void GeoReputationPrintRepInfo(ReputationConfig *config)
+{
+ int i;
+
+ if (config->geoip_actions && config->geoip_enabled) {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "GeoIP Reputation Info:\n"););
+ for ( i = 0; i < GeoIP_num_countries(); ++i)
+ {
+ if (config->geoip_actions[i] != DECISION_NULL) {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, " * %s -> %s\n", GeoIP_code_by_id(i), (config->geoip_actions[i]==MONITORED?"monitored":(config->geoip_actions[i]==BLACKLISTED?"drop":"bypass"))););
+ }
+ }
+ } else {
+ DEBUG_WRAP(DebugMessage(DEBUG_REPUTATION, "GeoIP Reputation disabled because there is no valid countries\n"););
+ }
+}
+
+
+#endif
diff -Nru snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/reputation_config.h snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/reputation_config.h
--- snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/reputation_config.h 2012-09-21 02:09:13.000000000 +0200
+++ snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/reputation_config.h 2012-11-27 23:13:13.557669783 +0100
@@ -31,6 +31,12 @@
#include "reputation_debug.h"
#include "sf_ip.h"
#include "sfrt_flat.h"
+
+#ifdef REPUTATION_GEOIP
+//GeoIP library from http://www.maxmind.com/
+#include <GeoIP.h>
+#endif
+
#ifdef SHARED_REP
#include "./shmem/shmem_mgmt.h"
#endif
@@ -55,6 +61,13 @@
uint32_t updateInterval;
}SharedMem;
+#ifdef REPUTATION_GEOIP
+typedef struct _GeoFileList
+{
+ char* filename;
+ int filetype;
+} DataGeoFileList;
+#endif
typedef enum _IPdecision
{
@@ -110,6 +123,15 @@
int ref_count;
char *statusBuf;
int statusBuf_len;
+ IPdecision defaultAction; //redBorder -> default action. If the ip is not in any list default action will be taken
+ uint8_t ordered; //redBorder -> if ordered, it will keep list ordered action occurrence (analice source ip first)
+#ifdef REPUTATION_GEOIP
+ GeoIP * geoip; //redBorder -> Added GeoIP support
+ char * geoip_db; //redBorder -> GeoIP database location
+ char * geoip_path; //redBorder -> GeoIP files location
+ IPdecision * geoip_actions; //redBorder -> Actions for each country. DECISION_NULL (no decission) should be the default action for each country
+ int geoip_enabled; //redBorder -> Enable GeoIP flag. If 1, geoip is enabled. To have it enabled it must have valid countries
+#endif
} ReputationConfig;
@@ -130,4 +152,9 @@
void initShareMemory(void *config);
void ReputationRepInfo(IPrepInfo *, uint8_t *, char *, int);
DEBUG_WRAP(void ReputationPrintRepInfo(IPrepInfo * repInfo, uint8_t *base);)
+
+#ifdef REPUTATION_GEOIP
+void initGeoFilesWithManifiest(void *config);
+#endif
+
#endif
diff -Nru snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/spp_reputation.c snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/spp_reputation.c
--- snort-2.9.4.rc.old/src/dynamic-preprocessors/reputation/spp_reputation.c 2012-09-21 02:09:13.000000000 +0200
+++ snort-2.9.4.rc.new/src/dynamic-preprocessors/reputation/spp_reputation.c 2012-11-27 23:13:13.559669619 +0100
@@ -84,6 +84,10 @@
static inline IPrepInfo* ReputationLookup(snort_ip_p ip);
static inline IPdecision GetReputation(IPrepInfo *, SFSnortPacket *, uint32_t *);
+#ifdef REPUTATION_GEOIP
+static inline IPdecision GetGeoReputation(snort_ip_p ip, SFSnortPacket *p, uint32_t *listid);
+#endif
+
#ifdef SHARED_REP
Swith_State switch_state = NO_SWITCH;
int available_segment = NO_DATASEG;
@@ -473,11 +477,17 @@
IPtables = &pPolicyConfig->localSegment;
_dpd.addPreproc( ReputationMain, PRIORITY_FIRST, PP_REPUTATION, PROTO_BIT__IP );
+
#ifdef SHARED_REP
if (pPolicyConfig->sharedMem.path)
_dpd.addPostConfigFunc(initShareMemory, pPolicyConfig);
#endif
+#ifdef REPUTATION_GEOIP
+ if (pPolicyConfig->geoip_db && pPolicyConfig->geoip_path) //Processing white and black list geoip files
+ _dpd.addPostConfigFunc(initGeoFilesWithManifiest, pPolicyConfig);
+#endif
+
}
#ifdef REG_TEST
@@ -546,6 +556,11 @@
if (listInfo[list_index].zones[ingressZone] || listInfo[list_index].zones[egressZone])
#endif
{
+ //redBorder: If the decision must be ordered and it has taken a decision it will return this one
+ if (reputation_eval_config->ordered && ((IPdecision)listInfo[list_index].listType) != DECISION_NULL ) {
+ *listid = listInfo[list_index].listId;
+ return ((IPdecision)listInfo[list_index].listType);
+ }
if (WHITELISTED_UNBLACK == (IPdecision)listInfo[list_index].listType)
return DECISION_NULL;
if (reputation_eval_config->priority == (IPdecision)listInfo[list_index].listType )
@@ -642,21 +657,36 @@
return decision;
decision_final = decision;
}
+#ifdef REPUTATION_GEOIP
+ else {
+ decision_final = GetGeoReputation(ip, p, &p->iplist_id);
+ }
+#endif
- ip = GET_INNER_DST_IP(((SFSnortPacket *)p));
- result = ReputationLookup(ip);
- if(result)
+ if (!reputation_eval_config->ordered || (reputation_eval_config->ordered && !decision_final))
{
- DEBUG_WRAP(ReputationPrintRepInfo(result,(uint8_t *) reputation_eval_config->iplist););
- decision = GetReputation(result,p, &p->iplist_id);
-
- p->iprep_layer = IP_INNER_LAYER;
- p->flags &=~FLAG_IPREP_SOURCE_TRIGGERED;
- if ( reputation_eval_config->priority == decision)
- return decision;
- decision_final = decision;
+ ip = GET_INNER_DST_IP(((SFSnortPacket *)p));
+ result = ReputationLookup(ip);
+ if(result)
+ {
+ DEBUG_WRAP(ReputationPrintRepInfo(result,(uint8_t *) reputation_eval_config->iplist););
+ decision = GetReputation(result,p, &p->iplist_id);
+
+ p->iprep_layer = IP_INNER_LAYER;
+ p->flags &=~FLAG_IPREP_SOURCE_TRIGGERED;
+ if ( reputation_eval_config->priority == decision)
+ return decision;
+ decision_final = decision;
+ }
+#ifdef REPUTATION_GEOIP
+ else {
+ decision_final = GetGeoReputation(ip, p, &p->iplist_id);
+ }
+#endif
}
}
+
+
/*Check OUTER IP*/
if (( p->outer_family) &&
((OUTER == reputation_eval_config->nestedIP)
@@ -674,21 +704,34 @@
return decision;
decision_final = decision;
}
+#ifdef REPUTATION_GEOIP
+ else {
+ decision_final = GetGeoReputation(ip, p, &p->iplist_id);
+ }
+#endif
- ip = GET_OUTER_DST_IP(((SFSnortPacket *)p));
- result = ReputationLookup(ip);
- if(result)
+ if (!reputation_eval_config->ordered || (reputation_eval_config->ordered && !decision_final))
{
- decision = GetReputation(result,p, &p->iplist_id);
-
- p->iprep_layer = IP_OUTTER_LAYER;
- p->flags &=~FLAG_IPREP_SOURCE_TRIGGERED;
- if ( reputation_eval_config->priority == decision)
- return decision;
- decision_final = decision;
+ ip = GET_OUTER_DST_IP(((SFSnortPacket *)p));
+ result = ReputationLookup(ip);
+ if(result)
+ {
+ decision = GetReputation(result,p, &p->iplist_id);
+
+ p->iprep_layer = IP_OUTTER_LAYER;
+ p->flags &=~FLAG_IPREP_SOURCE_TRIGGERED;
+ if ( reputation_eval_config->priority == decision)
+ return decision;
+ decision_final = decision;
+ }
+#ifdef REPUTATION_GEOIP
+ else {
+ decision_final = GetGeoReputation(ip, p, &p->iplist_id);
+ }
+#endif
}
-
}
+
return (decision_final);
}
@@ -710,6 +753,18 @@
reputation_eval_config->iplist = (table_flat_t *)*IPtables;
decision = ReputationDecision(p);
+ //redBorder. If reputation_eval_config->whiteAction it will change WHITELISTED_UNBLACK for WHITELISTED_TRUST
+ if (reputation_eval_config->whiteAction && WHITELISTED_UNBLACK==decision) {
+ decision=WHITELISTED_TRUST;
+ }
+
+ DEBUG_WRAP( DebugMessage(DEBUG_REPUTATION, "Final decision taken -> %d (DECISION_NULL=%d, MONITORED=%d, BLACKLISTED=%d, WHITELISTED_UNBLACK=%d, WHITELISTED_TRUST=%d)\n", decision, DECISION_NULL, MONITORED, BLACKLISTED, WHITELISTED_UNBLACK, WHITELISTED_TRUST););
+
+ //redBorder: If the user has specified a default action and it has no action defined it will use default action
+ if (DECISION_NULL == decision) {
+ decision=reputation_eval_config->defaultAction;
+ }
+
if (DECISION_NULL == decision)
{
return;
@@ -912,6 +967,12 @@
pPolicyConfig->memcap = pDefaultPolicyConfig->memcap;
_dpd.addPreproc( ReputationMain, PRIORITY_FIRST, PP_REPUTATION, PROTO_BIT__IP );
+
+#ifdef REPUTATION_GEOIP
+ if (pPolicyConfig->geoip_db && pPolicyConfig->geoip_path) //Processing white and black list geoip files
+ _dpd.addPostConfigFunc(initGeoFilesWithManifiest, pPolicyConfig);
+#endif
+
_dpd.addPreprocReloadVerify(ReputationReloadVerify);
}
@@ -962,6 +1023,7 @@
}
#endif
+
return 0;
}
@@ -1016,3 +1078,38 @@
ReputationFreeConfig((tSfPolicyUserContextId)data);
}
#endif
+
+#ifdef REPUTATION_GEOIP
+
+/*********************************************************************
+ * Lookup the IP at GeoIP database and return the decissión read from
+ * geoip manifiest file
+ *
+ * Returns:
+ * IPdecision -
+ * DECISION_NULL
+ * BLACKLISTED
+ * WHITELISTED_UNBLACK
+ * MONITORED
+ * WHITELISTED_TRUST
+ *********************************************************************/
+
+static inline IPdecision GetGeoReputation(snort_ip_p ip, SFSnortPacket *p, uint32_t *listid) {
+ IPdecision decision = DECISION_NULL;
+
+ if (reputation_eval_config->geoip_actions && reputation_eval_config->geoip_enabled) {
+ if (!sfip_is_private(ip)) {
+ // Searching the country with the IP
+ int country_id = GeoIP_id_by_addr(reputation_eval_config->geoip, sfip_to_str(ip));
+ if (country_id>0) {
+ // GeoIP_id_by_addr(reputation_eval_config->geoip,
+ decision = reputation_eval_config->geoip_actions[country_id];
+ DEBUG_WRAP( if (!decision) DebugMessage(DEBUG_REPUTATION, "Country founded for %s -> %s (action->%s)\n", sfip_to_str(ip), GeoIP_code_by_id(country_id), (reputation_eval_config->geoip_actions[country_id]==MONITORED?"monitored":(reputation_eval_config->geoip_actions[country_id]==BLACKLISTED?"drop":"bypass"))););
+ }
+ }
+ }
+
+ return decision;
+}
+
+#endif