• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NathanGibbs3 / BASE / 587

pending completion
587

push

travis-ci-com

NathanGibbs3
Merge branch 'devel'

504 of 504 new or added lines in 21 files covered. (100.0%)

2594 of 16816 relevant lines covered (15.43%)

20.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/base_graph_common.php
1
<?php
2
/*******************************************************************************
3
** Basic Analysis and Security Engine (BASE)
4
** Copyright (C) 2004 BASE Project Team
5
** Copyright (C) 2000 Carnegie Mellon University
6
**
7
** (see the file 'base_main.php' for license details)
8
**
9
** Project Leads: Kevin Johnson <kjohnson@secureideas.net>
10
**                Sean Muller <samwise_diver@users.sourceforge.net>
11
** Built upon work by Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
12
**
13
** Purpose: extracts and calculates the data to plot
14
********************************************************************************
15
** Authors:
16
********************************************************************************
17
** Kevin Johnson <kjohnson@secureideas.net
18
**
19
********************************************************************************
20
*/
21

22
include_once("base_conf.php");
×
23
include_once("$BASE_path/includes/base_constants.inc.php");
×
24
include_once("$BASE_path/base_common.php");
×
25
include_once("$BASE_path/base_qry_common.php");
×
26
include_once("$BASE_path/includes/base_log_error.inc.php");
×
27
include_once("$BASE_path/includes/base_signature.inc.php");
×
28
include_once("$BASE_path/includes/base_iso3166.inc.php");
×
29

30
// Some colors to be used in graphs.
31
$named_colors = array('aliceblue','antiquewhite','aqua','aquamarine','azure','beige','bisque','black','blanchedalmond','blue','blueviolet','brown','burlywood','cadetblue','chartreuse','chocolate','coral','cornflowerblue','cornsilk','crimson','cyan','darkblue','darkcyan','darkgoldenrod','darkdray','darkgreen','darkhaki','darkorange','darkolivegreen','darkmagenta','darkorchid','darkred','darksalmon','darkseagreen','darkviolet','deeppink','deepskyblue','dimgray','dodgerblue','firebrick','floralwhite','forestgreen','fuchsia','gainsboro','ghostwhite','gold','goldenrod','gray','green','greenyellow','indianred','indigo','ivory');
×
32

33
// Chart type constants:
34
// Not prefixed with '_' so we don't interfere with PHP define's.
35
SetConst('CHARTTYPE_DEFAULT', 0);
×
36
SetConst('CHARTTYPE_HOUR', 1);
×
37
SetConst('CHARTTYPE_DAY', 2);
×
38
SetConst('CHARTTYPE_WEEK', 3);
×
39
SetConst('CHARTTYPE_MONTH', 4);
×
40
SetConst('CHARTTYPE_YEAR', 5);
×
41
SetConst('CHARTTYPE_SRC_IP', 6);
×
42
SetConst('CHARTTYPE_DST_IP', 7);
×
43
SetConst('CHARTTYPE_DST_UDP_PORT', 8);
×
44
SetConst('CHARTTYPE_DST_TCP_PORT', 9);
×
45
SetConst('CHARTTYPE_SRC_UDP_PORT', 10);
×
46
SetConst('CHARTTYPE_SRC_TCP_PORT', 11);
×
47
SetConst('CHARTTYPE_CLASSIFICATION', 12);
×
48
SetConst('CHARTTYPE_SENSOR', 13);
×
49
SetConst('CHARTTYPE_SRC_COUNTRY', 14);
×
50
SetConst('CHARTTYPE_SRC_COUNTRY_ON_MAP', 15);
×
51
SetConst('CHARTTYPE_DST_COUNTRY', 16);
×
52
SetConst('CHARTTYPE_DST_COUNTRY_ON_MAP', 17);
×
53
SetConst('CHARTTYPE_UNIQUE_SIGNATURE', 18);
×
54

55
function VerifyGraphingLib(){
56
        $EMPfx = __FUNCTION__ . ': ';
×
57
        $IGL = false;
×
58
        if ( !(function_exists("ImageDestroy")) ){// Is GD compiled into PHP.
×
59
                BuildError (
×
60
                        '<b>PHP build incomplete</b>: GD support required.<br/>'."\n".
×
61
                        'Recompile PHP with GD support (<code>--with-gd</code>)',
62
                        'PHP build incomplete: GD support required.'
63
                );
64
        }
65
        $sc = DIRECTORY_SEPARATOR;
×
66
        $LibLoc = 'Image';
×
67
        $LibFile = 'Graph';
×
68
        $tmp = ChkLib('', $LibLoc , $LibFile);
×
69
        if ( LoadedString($tmp) == true ){
×
70
                $IGL = include_once($tmp);
×
71
        }
72
        if ( $tmp == '' || $IGL == false){
×
73
                $Lib = implode( $sc, array($LibLoc, $LibFile) ).'.php';
×
74
                $EMsg = "Graphing Lib: $Lib not ";
×
75
                if ( $tmp == '' ){
×
76
                        $EMsg .= 'accessable';
×
77
                }elseif ( $IGL == false ){
×
78
                        $EMsg .= 'loaded';
×
79
                }
80
                $EMsg .= '.<br/>';
×
81
                // Sorry dude, you haven't finished your home work. -- Alejandro
82
                $LibName = 'Image_Graph';
×
83
                $tmp = "https://pear.php.net/package/$LibName";
×
84
                LibIncError ('Graphing', $LibLoc, $Lib, $EMsg, $LibName, $tmp, 1, 1 );
×
85
        }
86
}
87
function ProcessChartTimeConstraint(
88
        $start_hour, $start_day, $start_month, $start_year,
89
        $stop_hour,  $stop_day,  $stop_month,  $stop_year
90
){ //Generates the required SQL from the chart time criteria.
91
        GLOBAL $debug_mode;
92
        $start_hour = trim($start_hour);
×
93
        $stop_hour = trim($stop_hour);
×
94
        $start_day = trim($start_day);
×
95
        $stop_day = trim($stop_day);
×
96
        $tmp_sql = '';
×
97
        if (
×
98
                empty($start_month) && empty($start_day) && empty($start_year) &&
99
                empty($stop_month) && empty($stop_day) && empty($stop_year)
100
        ){
101
                return '';
×
102
        }
103
        $start = 0;
×
104
        $end = 1;
×
105
        $op = 1;
×
106
        $month = 2;
×
107
        $day = 3;
×
108
        $year = 4;
×
109
        $hour = 5;
×
110
        $minute = 6;
×
111
        $second = 7;
×
112
        $stop = 8;
×
113
        $SQLOP = 9;
×
114
        InitArray($tmp_time,2,10,''); //Setup Time Array
×
115
        // Array is based on TimeCriteria class as defined in:
116
        // ./includes/base_state_citems.inc.php
117
        if( empty($start_month) && empty($start_day) && empty($start_year) ){
×
118
                $tmp_time[$end][$op] = '<=';
×
119
                $tmp_time[$end][$month] = $stop_month;
×
120
                $tmp_time[$end][$day] = $stop_day;
×
121
                $tmp_time[$end][$year] = $stop_year;
×
122
                $tmp_time[$end][$hour] = $stop_hour;
×
123
                $cnt = 2;
×
124
        }elseif( empty($stop_month) && empty($stop_day) && empty($stop_year) ){
×
125
                $tmp_time[$start][$op] = '>=';
×
126
                $tmp_time[$start][$month] = $start_month;
×
127
                $tmp_time[$start][$day] = $start_day;
×
128
                $tmp_time[$start][$year] = $start_year;
×
129
                $tmp_time[$start][$hour] = $start_hour;
×
130
                $cnt = 1;
×
131
        }else{
×
132
                $tmp_time[$start][$op] = '>=';
×
133
                $tmp_time[$start][$month] = $start_month;
×
134
                $tmp_time[$start][$day] = $start_day;
×
135
                $tmp_time[$start][$year] = $start_year;
×
136
                $tmp_time[$start][$hour] = $start_hour;
×
137
                $tmp_time[$start][$SQLOP] = 'AND';
×
138
                $tmp_time[$end][$op] = '<=';
×
139
                $tmp_time[$end][$month] = $stop_month;
×
140
                $tmp_time[$end][$day] = $stop_day;
×
141
                $tmp_time[$end][$year] = $stop_year;
×
142
                $tmp_time[$end][$hour] = $stop_hour;
×
143
                $cnt = 2;
×
144
        }
145
        DateTimeRows2sql($tmp_time, $cnt, $tmp_sql);
×
146
        if ( $debug_mode > 0 ){
×
147
                var_dump($tmp_time);
×
148
                ErrorMessage(__FUNCTION__ . "() Returned SQL: $tmp_sql");
×
149
        }
150
        return $tmp_sql;
×
151
}
152
function StoreAlertNum( $sql, $label, &$xdata, &$cnt, $min_threshold ){
153
        GLOBAL $db, $debug_mode;
154
        if ( $debug_mode > 0 ){
×
155
                ErrorMessage( $sql, 'black', 1 );
×
156
        }
157
        $result = $db->baseExecute($sql);
×
158
        if ( $result != false ){ // Error Check
×
159
                $myrow = $result->baseFetchRow();
×
160
                if ( $myrow[0] >= $min_threshold ){
×
161
                        $xdata [ $cnt ][0] = $label;
×
162
                        $xdata [ $cnt ][1] = $myrow[0];
×
163
                        $cnt++;
×
164
                }
165
                $result->baseFreeRows();
×
166
        }
167
}
168

169
function GetTimeDataSet(
170
        &$xdata, $chart_type, $data_source, $min_threshold, $criteria
171
){
172
        GLOBAL $db, $debug_mode, $chart_begin_year, $chart_begin_month,
173
        $chart_begin_day, $chart_begin_hour, $chart_end_year, $chart_end_month,
174
        $chart_end_day, $chart_end_hour;
175
        if ( $debug_mode > 0 ){
×
176
                ErrorMessage( "chart_type = $chart_type",'black',1 );
×
177
                ErrorMessage( "data_source = $data_source",'black',1 );
×
178
        }
179
        // Get time range for whole DB.
180
        $sql = "SELECT min(timestamp), max(timestamp) FROM acid_event " .
×
181
        $criteria[0] . " WHERE ".$criteria[1];
×
182
        $result = $db->baseExecute($sql);
×
183
        $myrow = $result->baseFetchRow();
×
184
        $start_time = $myrow[0];
×
185
        $stop_time = $myrow[1];
×
186
        $result->baseFreeRows();
×
187
        if ( $debug_mode > 0 ){
×
188
                ErrorMessage(
×
189
                        __FUNCTION__ . "() DB Time Range: $start_time - $stop_time", '', 1
×
190
                );
191
        }
192
        // Get Time range parts.
193
        $year_start  = date("Y", strtotime($start_time)); // Start
×
194
        $month_start = date("m", strtotime($start_time));
×
195
        $day_start   = date("d", strtotime($start_time));
×
196
        $hour_start  = date("H", strtotime($start_time));
×
197
        $year_end  = date("Y", strtotime($stop_time)); // End
×
198
        $month_end = date("m", strtotime($stop_time));
×
199
        $day_end   = date("d", strtotime($stop_time));
×
200
        $hour_end  = date("H", strtotime($stop_time));
×
201
        // using the settings from begin_xyz and end_xyz
202
        // minutes are not supported actually
203
        // begin
204
        if ( is_numeric($chart_begin_year) && $year_start < $chart_begin_year ){
×
205
                $year_start = $chart_begin_year;
×
206
        }
207
        if ( is_numeric($chart_begin_month) && $month_start < $chart_begin_month ){
×
208
                $month_start = $chart_begin_month;
×
209
        }
210
        if ( is_numeric($chart_begin_day) && $day_start < $chart_begin_day ){
×
211
                $day_start = $chart_begin_day;
×
212
        }
213
        if ( is_numeric($chart_begin_hour) && $hour_start < $chart_begin_hour ) {
×
214
                $hour_start = $chart_begin_hour;
×
215
        }
216
        //end
217
        if ( is_numeric($chart_end_year) && $year_end < $chart_end_year ){
×
218
                $year_end = $chart_end_year;
×
219
        }
220
        if ( is_numeric($chart_end_month) && $month_end < $chart_end_month ){
×
221
                $month_end = $chart_end_month;
×
222
        }
223
        if ( is_numeric($chart_end_day) && $day_end < $chart_end_day ){
×
224
                $day_end = $chart_end_day;
×
225
        }
226
        if ( is_numeric($chart_end_hour) && $hour_end < $chart_end_hour ) {
×
227
                $hour_end = $chart_end_hour;
×
228
        }
229
        switch ( $chart_type ){
×
230
                case 1: // hour
×
231
                        if ( $debug_mode > 0 ){
×
232
                                ErrorMessage(
×
233
                                        "chart_begin_hour = \"$chart_begin_hour\", hour_start = \"$hour_start\"",
×
234
                                        'black',1
235
                                );
236
                                ErrorMessage(
×
237
                                        "chart_end_hour = \"$chart_end_hour\", hour_end = \"$hour_end\"",
×
238
                                        'black',1
239
                                );
240
                        }
241
                        if ( !is_numeric($chart_end_hour) || $chart_end_hour == '' ){
×
242
                                // hour_start = -1 is NOT possible, because with
243
                                // chart_type == 1 each hour is to be queried.
244
                                // We want bars hour by hour.
245
                                $hour_end = 23;
×
246
                        }
247
                        break;
×
248
                case 2: // day
×
249
                        $hour_start = -1;
×
250
                        break;
×
251
                case 4: // month
×
252
                        $day_start = -1;
×
253
                        $hour_start = -1;
×
254
                        break;
×
255
        }
256
        if ( $debug_mode > 0 ){
×
257
                $TK = array ( 'year', 'month', 'day', 'hour' );
×
258
                $DI = array();
×
259
                $DD = array();
×
260
                foreach ( $TK as $val ){
×
261
                        foreach ( array( 'start', 'end' ) as $vsf ){
×
262
                                $tmp = $val . '_' . $vsf;
×
263
                                array_push($DD, $tmp);
×
264
                                array_push($DI, $$tmp);
×
265
                        }
266
                }
267
                DDT($DI,$DD,'Time Constraints');
×
268
        }
269
        $cnt = 0;
×
270
        $ag = $criteria[0];
×
271
        $ag_criteria = $criteria[1];
×
272
        // SQL peices
273
        $ts = 'timestamp';
×
274
        $A = ' AND ';
×
275
        $W = ' WHERE ';
×
276
//        $sqlpfx = "SELECT count(*) FROM acid_event ";
277
//        if ( $ag != '' ){ // Not Querying Alert Groups
278
//                $sqlpfx .= "$ag$W$ag_criteria";
279
//        }else{
280
//                $sqlpfx .= $W;
281
//        }
282
//        $sqlpfx .= $A;
283
        $sqlpfx = "SELECT count(*) FROM acid_event $ag WHERE $ag_criteria$A";
×
284
        for ( $i_year = $year_start; $i_year <= $year_end; $i_year++ ){
×
285
                // Catch 2 digit years, default to YYYY in current century.
286
                if ( strlen($i_year) <= 2 ){
×
287
                        $i_year = substr(date("Y"),0,2).FormatTimeDigit($year);
×
288
                }
289
                // removed AND below
290
                // !!! AVN !!!
291
                // to_date() must used!
292
                $sql = $sqlpfx.$db->baseSQL_YEAR( $ts, '=', $i_year );
×
293
                if ( $month_start != -1 ){
×
294
                        if ( $i_year == $year_start ){
×
295
                                $month_start2 = $month_start;
×
296
                        }else{
×
297
                                $month_start2 = 1;
×
298
                        }
299
                        if ( $i_year == $year_end ){
×
300
                                $month_end2 = $month_end;
×
301
                        }else{
×
302
                                $month_end2 = 12;
×
303
                        }
304
                        for (
×
305
                                $i_month = $month_start2; $i_month <= $month_end2; $i_month++
×
306
                        ){
307
                                $i_month = FormatTimeDigit($i_month);
×
308
                                $sql = $sqlpfx.$db->baseSQL_YEAR( $ts, '=', $i_year ) . $A.
×
309
                                $db->baseSQL_MONTH( $ts, '=', $i_month );
×
310
                                if ( $day_start != -1 ){
×
311
                                        if ( $i_month == $month_start ){
×
312
                                                $day_start2 = $day_start;
×
313
                                        }else{
×
314
                                                $day_start2 = 1;
×
315
                                        }
316
                                        if ( $i_month == $month_end ){
×
317
                                                $day_end2 = $day_end;
×
318
                                        }else{
×
319
                                                $day_end2 = 31;
×
320
                                                while (
×
321
                                                        !checkdate( $i_month, $day_end2, $i_year )
×
322
                                                ){ // Bring it into reality.
323
                                                        --$day_end2;
×
324
                                                }
325
                                        }
326
                                        for (
×
327
                                                $i_day = $day_start2; $i_day <= $day_end2; $i_day++
×
328
                                        ){
329
                                                $i_day = FormatTimeDigit($i_day);
×
330
                                                $sql = $sqlpfx.
×
331
                                                $db->baseSQL_YEAR( $ts, '=', $i_year ) . $A.
×
332
                                                $db->baseSQL_MONTH( $ts, '=', $i_month ) . $A.
×
333
                                                $db->baseSQL_DAY( $ts, '=', $i_day );
×
334
                                                $Lbl = implode ('/',array( $i_month, $i_day, $i_year ));
×
335
                                                if ( $hour_start != -1 ){
×
336
                                                        // jl: The condition "i_hour <= hour_end" is
337
                                                        // correct ONLY if the first day is equal to the
338
                                                        // last day of the query.
339
                                                        // Otherwise we want 24 hours of all the days
340
                                                        // preceding the last day of the query.
341
                                                        // Analogously for hour_start.
342
                                                        if ( $i_day == $day_start2 ){
×
343
                                                                $hour_start2 = $hour_start;
×
344
                                                        }else{
×
345
                                                                $hour_start2 = 0;
×
346
                                                        }
347
                                                        if ( $i_day == $day_end2 ){
×
348
                                                                $hour_end2 = $hour_end;
×
349
                                                        }else{
×
350
                                                                $hour_end2 = 23;
×
351
                                                        }
352
                                                        for (
×
353
                                                                $i_hour = $hour_start2;
×
354
                                                                $i_hour <= $hour_end2; $i_hour++
355
                                                        ){
356
                                                                $i_hour = FormatTimeDigit($i_hour);
×
357
                                                                $sql = $sqlpfx.
×
358
                                                                $db->baseSQL_YEAR( $ts, '=', $i_year ) . $A.
×
359
                                                                $db->baseSQL_MONTH( $ts, '=', $i_month ) . $A.
×
360
                                                                $db->baseSQL_DAY( $ts, '=', $i_day ) . $A.
×
361
                                                                $db->baseSQL_HOUR( $ts, '=', $i_hour );
×
362
                                                                StoreAlertNum( $sql,
×
363
                                                                        "$Lbl $i_hour:00:00 - $i_hour:59:59",
×
364
                                                                        $xdata, $cnt, $min_threshold
365
                                                                );
366
                                                        } // end hour
367
                                                }else{
×
368
                                                        StoreAlertNum(
×
369
                                                                $sql, $Lbl, $xdata, $cnt, $min_threshold
370
                                                        );
371
                                                }
372
                                        } // end day
373
                                }else{
×
374
                                        StoreAlertNum(
×
375
                                                $sql, implode ('/',array( $i_month, $i_year )), $xdata,
×
376
                                                $cnt, $min_threshold
377
                                        );
378
                                }
379
                        } // end month
380
                }else{
×
381
                        StoreAlertNum($sql, $i_year, $xdata, $cnt, $min_threshold);
×
382
                }
383
        } // end year
384
        return $cnt;
×
385
}
386

387
function GetIPDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
388
{
389
   GLOBAL $db, $debug_mode;
390

391
   if ( $chart_type == 6 ) 
×
392
      $sql = "SELECT DISTINCT ip_src, COUNT(acid_event.cid) ".
×
393
             "FROM acid_event ".$criteria[0].
×
394
             "WHERE ".$criteria[1]." AND ip_src is NOT NULL ".
×
395
             "GROUP BY ip_src ORDER BY ip_src";
396
   else if ( $chart_type == 7 )
×
397
      $sql = "SELECT DISTINCT ip_dst, COUNT(acid_event.cid) ".
×
398
             "FROM acid_event ".$criteria[0].
×
399
             "WHERE ".$criteria[1]." AND ip_dst is NOT NULL ".
×
400
             "GROUP BY ip_dst ORDER BY ip_dst";
401

402
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
403
   
404
   $result = $db->baseExecute($sql);
×
405

406
   $cnt = 0;
×
407
   while ( $myrow = $result->baseFetchRow() )
×
408
   {
409
      if ( $myrow[1] >= $min_threshold )
×
410
      {
411
         $xdata[$cnt][0] = baseLong2IP($myrow[0]); 
×
412
         $xdata[$cnt][1] = $myrow[1]; 
×
413
         ++$cnt;
×
414
      }
415
   }
416

417
   $result->baseFreeRows();
×
418
   return $cnt;
×
419
}
420

421
function GetPortDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
422
{
423
   GLOBAL $db, $debug_mode;
424

425
   if ( ($chart_type == 8) || ($chart_type == 9) ) 
×
426
      $sql = "SELECT DISTINCT layer4_dport, COUNT(acid_event.cid) ".
×
427
             "FROM acid_event ".$criteria[0].
×
428
             "WHERE ".$criteria[1]." AND layer4_dport is NOT NULL ".
×
429
             "GROUP BY layer4_dport ORDER BY layer4_dport";
430
   else if ( ($chart_type == 10) || ($chart_type == 11) ) 
×
431
      $sql = "SELECT DISTINCT layer4_sport, COUNT(acid_event.cid) ".
×
432
             "FROM acid_event ".$criteria[0].
×
433
             "WHERE ".$criteria[1]." AND layer4_sport is NOT NULL ".
×
434
             "GROUP BY layer4_sport ORDER BY layer4_sport";
435

436
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
437
   
438
   $result = $db->baseExecute($sql);
×
439

440
   $cnt = 0;
×
441
   while ( $myrow = $result->baseFetchRow() )
×
442
   {
443
      if ( $myrow[1] >= $min_threshold )
×
444
      {
445
         $xdata[$cnt][0] = $myrow[0]; 
×
446
         $xdata[$cnt][1] = $myrow[1]; 
×
447
         ++$cnt;
×
448
      }
449
   }
450

451
   $result->baseFreeRows();
×
452
   return $cnt;
×
453
}
454

455
function GetClassificationDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
456
{
457
   GLOBAL $db, $debug_mode;
458
  
459
   $sql = "SELECT DISTINCT sig_class_id, COUNT(acid_event.cid) ".
×
460
          "FROM acid_event ".$criteria[0].
×
461
          "WHERE ".$criteria[1].
×
462
          " GROUP BY sig_class_id ORDER BY sig_class_id";
463

464
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
465
   
466
   $result = $db->baseExecute($sql);
×
467

468
   $cnt = 0;
×
469
   while ( $myrow = $result->baseFetchRow() )
×
470
   {
471
      if ( $myrow[1] >= $min_threshold )
×
472
      {
473
         if ($debug_mode > 0)
×
474
         {
475
           // Sig. classification vs. number of alerts
476
           error_log(__FILE__ . ":" . __LINE__ . ": \$myrow[0] = \""  . $myrow[0] . "\"");
×
477
         }
478

479

480
         $xdata[$cnt][0] = strip_tags(GetSigClassName($myrow[0], $db)); 
×
481
         if ($debug_mode > 0)
×
482
         {
483
           // Sig. classification vs. number of alerts
484
           error_log(__FILE__ . ":" . __LINE__ . ": \$xdata[\$cnt][0] = \""  . $xdata[$cnt][0] . "\"");
×
485
         }
486

487
         if (empty($xdata[$cnt][0]) || $xdata[$cnt][0] == "unclassified")
×
488
         {
489
           $xdata[$cnt][0] = $myrow[0];
×
490
         }
491

492
         
493

494
         $xdata[$cnt][1] = $myrow[1];
×
495
         ++$cnt;
×
496
      }
497
   }
498

499
   $result->baseFreeRows();
×
500
   return $cnt;
×
501
}
502

503

504

505
function GetUniqueDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
506
{
507
  GLOBAL $db, $debug_mode;
508

509

510
  $cnt = 0;
×
511
  $sql = "SELECT signature, " .
×
512
         "sig_name, " .
513
         "COUNT(signature) " .
514
         "FROM acid_event " . $criteria[0] . " " .
×
515
         "WHERE " . $criteria[1] . " " .
×
516
         "GROUP BY signature, sig_name " . 
517
         "ORDER BY sig_name";
518

519
  if ($debug_mode > 0)
×
520
  {
521
    echo "<BR>\n\$sql = \"" . $sql . "\"<BR><BR>\n\n";
×
522
  }
523

524
  $result = $db->baseExecute($sql);
×
525
  
526
  while($myrow = $result->baseFetchRow())
×
527
  {
528

529
  #  echo "<BR><BR>-------&lt;row&gt;---------<BR><pre>";
530
  #  var_dump($myrow);
531
  #  echo "<BR><BR><BR>";
532
  #  print_r($myrow);
533
  #  echo "</PRE><BR>---------&lt;/row&gt;--------<BR>";
534

535
    if ( $myrow[2] >= $min_threshold )
×
536
    {
537
      $xdata[$cnt][0] = strip_tags($myrow[1]); 
×
538
      $xdata[$cnt][1] = $myrow[2];
×
539

540
      if ($debug_mode > 0)
×
541
      {
542
        print $xdata[$cnt][0] . ": " . $xdata[$cnt][1] . " alerts <BR>\n";
×
543
      }
544
      ++$cnt;
×
545
    }
546
  }
547
  
548
  $result->baseFreeRows();
×
549

550
  return $cnt;
×
551
}
552

553

554

555
function GetSensorDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
556
{
557
   GLOBAL $db, $debug_mode;
558

559
   $sql = "SELECT DISTINCT acid_event.sid, COUNT(acid_event.cid) ".
×
560
          "FROM acid_event ".$criteria[0].
×
561
          "WHERE ".$criteria[1].
×
562
          " GROUP BY acid_event.sid ORDER BY acid_event.sid";
563

564
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
565
   
566
   $result = $db->baseExecute($sql);
×
567

568
   $cnt = 0;
×
569
   while ( $myrow = $result->baseFetchRow() )
×
570
   {
571
      if ( $myrow[1] >= $min_threshold )
×
572
      {
573
         $result2 = $db->baseExecute("SELECT hostname FROM sensor where sid=".$myrow[0]);
×
574
         $sensor_name = $result2->baseFetchRow();
×
575
         $xdata[$cnt][0] = $sensor_name[0];
×
576
         $result2->baseFreeRows();
×
577
 
578
         $xdata[$cnt][1] = $myrow[1];
×
579
         ++$cnt;
×
580
      }
581
   }
582

583
   $result->baseFreeRows();
×
584
   return $cnt;
×
585
}
586

587
// xxx jl
588
function ReadGeoIPfreeFileAscii(&$Geo_IPfree_array){
589
        GLOBAL $Geo_IPfree_file_ascii, $db, $debug_mode, $iso_3166;
590
        if (
×
591
                empty($Geo_IPfree_file_ascii)
592
                || !ChkAccess($Geo_IPfree_file_ascii)
×
593
        ){
594
                return 0;
×
595
        }
596
        ini_set("memory_limit", "256M");
×
597
  $lines = file($Geo_IPfree_file_ascii);
×
598
  if ($lines == FALSE)
×
599
  {
600
    print "WARNING: " . $Geo_IPfree_file_ascii . " could not be opened.<BR>\n";
×
601
    return 0;
×
602
  }
603
 
604
  foreach ($lines as $line_num => $line) 
×
605
  {
606
    $line_array[$line_num] = split(' ', rtrim($line));
×
607
    $index = rtrim($line_array[$line_num][0], ':');
×
608
    $begin = sprintf("%u", ip2long($line_array[$line_num][1]));
×
609
    $end = sprintf("%u", ip2long($line_array[$line_num][2]));
×
610

611
    if (!isset($iso_3166))
×
612
    {
613
      ErrorMessage("<BR>ERROR: \$iso_3166 has not been defined.<BR>\n");
×
614
      return 0;
×
615
                }else{
×
616
                        if ( !base_array_key_exists($index, $iso_3166) ){
×
617
        $estr = "ERROR: index \"" . $index . "\" = ascii codes ";
×
618
        $estr .= ord($index[0]) . ", " . ord($index[1]) . " ";
×
619
        $estr .= "does not exist. Ignoring.<BR>\n";
×
620
        ErrorMessage($estr);
×
621
                        }else{
×
622
                                if ($debug_mode > 1){
×
623
          print "Full name of " . $index . " = \"" . $iso_3166[$index]. "\"<BR>\n";
×
624
                                }
625
        $index .= " (" . $iso_3166[$index] . ")";
×
626
                        }
627
                        if (
×
628
                                !isset($Geo_IPfree_array)
629
                                || !base_array_key_exists($index, $Geo_IPfree_array)
×
630
                        ){
631
        $Geo_IPfree_array[$index][0] = array($begin, $end);
×
632
                        }else{
×
633
        {
634
          array_push($Geo_IPfree_array[$index], array($begin, $end));
×
635
        }
636
      }
637
    }    
638
  }
639
}
640

641
// First method how to look up the country corresponding to an ip address:
642
// http://search.cpan.org/CPAN/authors/id/G/GM/GMPASSOS/Geo-IPfree-0.2.tar.gz
643
// Requires the transformation of the included database into human readable
644
// ASCII format, similarly to:
645
//          cd /usr/lib/perl5/site_perl/5.8.8/Geo/
646
//          perl ipct2txt.pl ./ipscountry.dat /tmp/ips-ascii.txt
647
// $Geo_IPfree_file_ascii must contain the absolute path to
648
// ips-ascii.txt. The Web server needs read access to this file.
649
function GeoIPfree_IP2Country(
650
        $Geo_IPfree_array, $address_with_dots, &$country
651
){
652
        GLOBAL $db, $debug_mode;
653
        if ( empty($Geo_IPfree_array) || empty($address_with_dots) ){
×
654
                return 0;
×
655
        }
656
        $address = sprintf("%u", ip2long($address_with_dots));
×
657
        foreach ( $Geo_IPfree_array as $key => $val ){ // Issue #153
×
658
                $nelements = count($val);
×
659
                if ( count($val) > 0 ){
×
660
                        foreach ( $val as $key2 => $val2 ){ // Issue #153
×
661
                                if ( $debug_mode > 1 ){
×
662
                                        if ( $val2[0] > $val2[1] ){
×
663
                                                print "WARNING: Inconsistency with $key array element no. " . $key2 . ": " . long2ip($val2[0]) . " - " . long2ip($val2[1]) . "<BR>\n";
×
664
                                        }
665
                                }
666
                                if ( $address >= $val2[0] && $address <= $val2[1] ){
×
667
                                        if ( $debug_mode > 0 ){
×
668
                                                print "Found: " . $address_with_dots . " belongs to " . $key;
×
669
                                                print ": " . long2ip($val2[0]) . " - " . long2ip($val2[1]);
×
670
                                                print "<BR>\n";
×
671
                                        }
672
                                        $country = $key;
×
673
                                        return 1;
×
674
                                }
675
                        }
676
                }
677
        }
678
}
679

680
/**
681
 * Second method how to lookup the country corresponding to an ip address:
682
 * Makes use of the perl module IP::Country
683
 * http://search.cpan.org/dist/IP-Country/
684
 * The web server needs permission to execute "ip2cc".
685
 * Quoting from the php manual: 
686
 * "Note: When safe mode is enabled, you can only execute executables within the safe_mode_exec_dir. For practical reasons it is currently not allowed to have .. components in the path to the executable."
687
 *
688
 * $IP2CC must contain the absolute path to this executable.
689
 *
690
 *
691
 */
692
function run_ip2cc($address_with_dots, &$country)
693
{
694
  GLOBAL $db, $debug_mode, $IP2CC, $iso_3166;
695

696

697
  if (empty($address_with_dots))
×
698
  {
699
    ErrorMessage("ERROR: \$address_with_dots is empty<BR>\n");
×
700
    return 0;
×
701
  }
702

703
  if ((!is_file($IP2CC)) || (!is_executable($IP2CC)))
×
704
  {
705
    ErrorMessage("ERROR: with \$IP2CC = \"" . $IP2CC . "\"<BR>\n");
×
706
    return 0;
×
707
  }
708

709
  $cmd = $IP2CC . " " . $address_with_dots;
×
710
  unset($lastline);
×
711
  unset($output);
×
712
  unset($rv);
×
713

714
  $lastline = exec($cmd, $output, $rv);
×
715

716
  if ($rv != 0)
×
717
  {
718
    ErrorMessage("ERROR with " . $cmd . "<BR>\n");
×
719
    print "\$rv = " . $rv . "<BR>\n";
×
720
    print_r($output);
×
721
    return 0;
×
722
  }
723

724
  $result = explode(" ", $output[6]);
×
725
  $max = count($result);
×
726
  $country = "";
×
727
  for ($i = 3; $i < $max; $i++)
×
728
  {
729
    $country .= $result[$i] . " ";
×
730
  }
731

732
  if ($debug_mode > 0)
×
733
  {
734
    print "Found: " . $address_with_dots . " belongs to " . $country . "<BR>\n" ;
×
735
  }
736

737
  return 1;
×
738
}
739

740
function IncreaseCountryValue( &$countries, $to_search, $number_of_alerts ){
741
        GLOBAL $db, $debug_mode;
742
        if (count($countries) == 0 ){
×
743
                $countries[$to_search] = $number_of_alerts;
×
744
                return;
×
745
        }
746
        $tmp = '';
×
747
        if ( base_array_key_exists($to_search, $countries) ){
×
748
                $countries[$to_search] += $number_of_alerts;
×
749
        }else{
×
750
                $tmp = 'NOT ';
×
751
                $countries[$to_search] = $number_of_alerts;
×
752
        }
753
        if ( $debug_mode > 1 ){
×
754
                ErrorMessage($to_search . ' does ' . $tmp .'exist.', 0, 1);
×
755
        }
756
}
757

758
function GetCountryDataSet(
759
        &$xdata, $chart_type, $data_source, $min_threshold, $criteria
760
){
761
        GLOBAL $db, $debug_mode, $Geo_IPfree_file_ascii, $IP2CC;
762
        $country_method = 0;
×
763
        $EMPfx = __FUNCTION__ . ': ';
×
764
  if (($chart_type == 14) || ($chart_type == 15))
×
765
  // 14 =  Src Countries vs. Num Alerts
766
  // 15 = dto., but on worldmap
767
  {
768
      $sql = "SELECT DISTINCT ip_src, COUNT(acid_event.cid) ".
×
769
             "FROM acid_event ".$criteria[0].
×
770
             "WHERE ".$criteria[1]." AND ip_src is NOT NULL ".
×
771
             "GROUP BY ip_src ORDER BY ip_src";
772
  }
773
  else if (($chart_type == 16) || ($chart_type == 17))
×
774
  // 16 = Dst Countries vs. Num Alerts
775
  // 17 = dto., but on worldmap
776
  {
777
      $sql = "SELECT DISTINCT ip_dst, COUNT(acid_event.cid) ".
×
778
             "FROM acid_event ".$criteria[0].
×
779
             "WHERE ".$criteria[1]." AND ip_dst is NOT NULL ".
×
780
             "GROUP BY ip_dst ORDER BY ip_dst";
781
  }
782

783
  if ($debug_mode > 0)  echo $sql."<BR>";
×
784
   
785
  $result = $db->baseExecute($sql);
×
786

787
        if ( LoadedString($Geo_IPfree_file_ascii) ){
×
788
                $tmp = ChkAccess($Geo_IPfree_file_ascii);
×
789
                if ( $tmp != 1 ){
×
790
                        $EMsg = $EMPfx . "ERROR: $Geo_IPfree_file_ascii not ";
×
791
                        if ( $tmp == -1 ){
×
792
                                $EMsg .= 'found';
×
793
                        }elseif ( $tmp == -2 ){
×
794
                                $EMsg .= 'readable';
×
795
                        }
796
                        $$EMsg .= '.';
×
797
                        ErrorMessage($EMsg, 0, 1);
×
798
                        return 0;
×
799
                }else{
×
800
                        $country_method = 1;
×
801
                        if ( $debug_mode > 0 ){
×
802
                                ErrorMessage(
×
803
                                        $EMPfx . 'Country method 1: We use the database of Geo::IPfree.',
804
                                        0, 1
805
                                );
806
                        }
807
                        // Read in database with country data for ip addresses
808
                        ReadGeoIPfreeFileAscii($Geo_IPfree_array);
×
809
                }
810
        }elseif( LoadedString($IP2CC) ){
×
811
                $rv = ini_get("safe_mode");
×
812
                if ( !is_file($IP2CC) ){
×
813
          ErrorMessage("ERROR: " . $IP2CC . " could not be found. Wrong path, perhaps?<BR>\n");
×
814
                        if ($rv == 1){
×
815
            print "In &quot;safe_mode&quot; &quot; the file " . $Geo_IPfree_file_ascii . "&quot; must be owned by the user under which the web server is running. Adding it to both safe_mode_exec_dir and to include_path in /etc/php.ini does NOT seem to be sufficient.<BR>\n";
×
816
                        }
817
                        return 0;
×
818
                }else{
×
819
                        if (!is_executable($IP2CC)){
×
820
            ErrorMessage("ERROR: " . $IP2CC . " does exist, but is not executable. Wrong permissions, perhaps?<BR>\n");
×
821
                                if ($rv == 1){
×
822
              ErrorMessage("In &quot;safe_mode&quot; the path &quot;" . 
×
823
              dirname($IP2CC) . 
×
824
              "&quot; must also be part of safe_mode_exec_dir in /etc/php.ini:<BR><BR>\n" .
825
              "safe_mode_exec_dir = &quot;" . dirname($IP2CC) . 
×
826
              "&quot;<BR><BR>" .
827
              "It seems that not more than ONE SINGLE directory may be assigned to safe_mode_exec_dir.<BR>\n");
828
                                }
829
                                return 0;
×
830
                        }else{
×
831
                                $country_method = 2;
×
832
                                if ( $debug_mode > 0 ){
×
833
                                        ErrorMessage(
×
834
                                                $EMPfx . 'Country method 2: We use ip2cc.', 0, 1
835
                                        );
836
                                }
837
                        }
838
                }
839
        }else{
×
840
                ErrorMessage(
×
841
                        $EMPfx . "ERROR: Conf Var \$Geo_IPfree_file_ascii or \$IP2CC not configured.",
842
                        0, 1
843
                );
844
                return 0;
×
845
        }
846
        if ( $country_method == 0 ){ // should not be reached
×
847
    ErrorMessage("ERROR: No \$country_method available.<BR>\n");
×
848
    return 0;
×
849
  }
850
  // Loop through all the ip addresses returned by the sql query
851
  $cnt = 0;
×
852
  $not_an_array = 0;
×
853
  while ($myrow = $result->baseFetchRow())
×
854
  {
855
    if (!is_array($myrow))
×
856
    {
857
      $not_an_array += 1;
×
858
      if ($not_an_array <= 3)
×
859
      {
860
        // Ok. We accept getting something that is not an array,
861
        // if this happens not more than three times.        
862
        next;
×
863
      }
864
      else
×
865
      {
866
        // Now we are fed up with getting something that is not
867
        // even an array. Break!
868
        break; 
×
869
      }
870
    }
871

872
    if ($myrow[1] >= $min_threshold)
×
873
    {
874
      $addresses[$cnt][0] = baseLong2IP($myrow[0]); 
×
875
      $addresses[$cnt][1] = $myrow[1]; 
×
876
      
877
      // xxx jl
878
      // Which country belongs this ip address to?
879
      switch($country_method)
×
880
      {
881
        case 1:
×
882
                GeoIPfree_IP2Country($Geo_IPfree_array, $addresses[$cnt][0], $mycountry);      
×
883
                break;
×
884

885
              case 2:
×
886
                run_ip2cc($addresses[$cnt][0], $mycountry);
×
887
                break;
×
888

889
              default:
×
890
                print "WARNING: country_method no. " . $country_method . " is not supported.<BR>\n";
×
891
              return 0;
×
892
      }
893

894

895
      if ($debug_mode > 0)
×
896
      {
897
              print "\"" . $mycountry . "\": " . $addresses[$cnt][1] . " alerts<BR>\n";
×
898
      }
899

900

901
      // Either GeoIPfree_IP2Country() or run_ip2cc() should have set
902
      // this variable:
903
      if (!isset($mycountry) || empty($mycountry))
×
904
      {
905
        ErrorMessage("ERROR: \$mycountry has not been set as expected.<BR>\n");
×
906
        return 0;
×
907
      }
908

909

910
      // Increase number of alerts for this country 
911
      IncreaseCountryValue($countries, $mycountry, $addresses[$cnt][1]);
×
912

913
      ++$cnt;
×
914
    }
915
  }
916

917
  if ($cnt <= 0)
×
918
  {
919
    // then there are no data points to plot.
920
    return $cnt;
×
921
  }
922

923

924
  if (!isset($countries))
×
925
  {
926
    print "ERROR: \$countries has not even been defined. Returning 0.\n";
×
927
    return 0;
×
928
  }
929

930

931
  if (!is_array($countries))
×
932
  {
933
    print "ERROR: \$countries is not an array. Returning 0.\n";
×
934
    print "<BR><PRE>\n";
×
935
    var_dump($countries);
×
936
    print "</PRE><BR>\n";    
×
937
    return 0;
×
938
  }
939

940

941
  if ($debug_mode > 1)
×
942
  { 
943
    print "<pre>############\n";
×
944
    //var_dump($countries);
945
    print_r($countries);
×
946
    print "###########</pre>\n";
×
947
  }
948
  // Now setup the chart array:
949
  $cnt2 = 0;
×
950
        foreach ( $countries as $key => $val ){ // Issue #153
×
951
                $xdata[$cnt2][0] = $key;
×
952
                $xdata[$cnt2][1] = $val;
×
953
                $cnt2++;
×
954
        }
955
        $result->baseFreeRows();
×
956
        // return number of countries rather than number of addresses!
957
        return $cnt2;
×
958
}
959
?>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc