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

NathanGibbs3 / BASE / 590

pending completion
590

push

travis-ci-com

NathanGibbs3
20230420 Fix CI build breakage. 2

2755 of 16977 relevant lines covered (16.23%)

21.61 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
require_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
// @codeCoverageIgnoreStart
56
// These code paths are installation dependent.
57
// Testing would be problematic.
58
function VerifyGraphingLib(){
59
        $Ret = false; // Lib Error
60
        if( !function_exists('imagecreate') ){// Is GD compiled into PHP.
61
                BuildError (
62
                        '<b>PHP build incomplete</b>: GD support required.<br/>'."\n".
63
                        'Recompile PHP with GD support (<code>--with-gd</code>)'."\n".
64
                        'PHP build incomplete: GD support required.'
65
                );
66
        }else{
67
                $Ret = PearInc('Graphing', 'Image', 'Graph');
68
        }
69
        if ( $Ret == false ){ // Keep Issue #100 from happening here.
70
                sleep(60);
71
        }
72
        return $Ret;
73
}
74
// @codeCoverageIgnoreEnd
75

76
function ProcessChartTimeConstraint(
77
        $start_hour, $start_day, $start_month, $start_year,
78
        $stop_hour,  $stop_day,  $stop_month,  $stop_year
79
){ //Generates the required SQL from the chart time criteria.
80
        GLOBAL $debug_mode;
81
        $start_hour = trim($start_hour);
×
82
        $stop_hour = trim($stop_hour);
×
83
        $start_day = trim($start_day);
×
84
        $stop_day = trim($stop_day);
×
85
        $tmp_sql = '';
×
86
        if (
×
87
                empty($start_month) && empty($start_day) && empty($start_year) &&
88
                empty($stop_month) && empty($stop_day) && empty($stop_year)
89
        ){
90
                return '';
×
91
        }
92
        $start = 0;
×
93
        $end = 1;
×
94
        $op = 1;
×
95
        $month = 2;
×
96
        $day = 3;
×
97
        $year = 4;
×
98
        $hour = 5;
×
99
        $minute = 6;
×
100
        $second = 7;
×
101
        $stop = 8;
×
102
        $SQLOP = 9;
×
103
        InitArray($tmp_time,2,10,''); //Setup Time Array
×
104
        // Array is based on TimeCriteria class as defined in:
105
        // ./includes/base_state_citems.inc.php
106
        if( empty($start_month) && empty($start_day) && empty($start_year) ){
×
107
                $tmp_time[$end][$op] = '<=';
×
108
                $tmp_time[$end][$month] = $stop_month;
×
109
                $tmp_time[$end][$day] = $stop_day;
×
110
                $tmp_time[$end][$year] = $stop_year;
×
111
                $tmp_time[$end][$hour] = $stop_hour;
×
112
                $cnt = 2;
×
113
        }elseif( empty($stop_month) && empty($stop_day) && empty($stop_year) ){
×
114
                $tmp_time[$start][$op] = '>=';
×
115
                $tmp_time[$start][$month] = $start_month;
×
116
                $tmp_time[$start][$day] = $start_day;
×
117
                $tmp_time[$start][$year] = $start_year;
×
118
                $tmp_time[$start][$hour] = $start_hour;
×
119
                $cnt = 1;
×
120
        }else{
×
121
                $tmp_time[$start][$op] = '>=';
×
122
                $tmp_time[$start][$month] = $start_month;
×
123
                $tmp_time[$start][$day] = $start_day;
×
124
                $tmp_time[$start][$year] = $start_year;
×
125
                $tmp_time[$start][$hour] = $start_hour;
×
126
                $tmp_time[$start][$SQLOP] = 'AND';
×
127
                $tmp_time[$end][$op] = '<=';
×
128
                $tmp_time[$end][$month] = $stop_month;
×
129
                $tmp_time[$end][$day] = $stop_day;
×
130
                $tmp_time[$end][$year] = $stop_year;
×
131
                $tmp_time[$end][$hour] = $stop_hour;
×
132
                $cnt = 2;
×
133
        }
134
        DateTimeRows2sql($tmp_time, $cnt, $tmp_sql);
×
135
        if ( $debug_mode > 0 ){
×
136
                var_dump($tmp_time);
×
137
                ErrorMessage(__FUNCTION__ . "() Returned SQL: $tmp_sql");
×
138
        }
139
        return $tmp_sql;
×
140
}
141
function StoreAlertNum( $sql, $label, &$xdata, &$cnt, $min_threshold ){
142
        GLOBAL $db, $debug_mode;
143
        if ( $debug_mode > 0 ){
×
144
                ErrorMessage( $sql, 'black', 1 );
×
145
        }
146
        $result = $db->baseExecute($sql);
×
147
        if ( $result != false ){ // Error Check
×
148
                $myrow = $result->baseFetchRow();
×
149
                if ( $myrow[0] >= $min_threshold ){
×
150
                        $xdata [ $cnt ][0] = $label;
×
151
                        $xdata [ $cnt ][1] = $myrow[0];
×
152
                        $cnt++;
×
153
                }
154
                $result->baseFreeRows();
×
155
        }
156
}
157

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

376
function GetIPDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
377
{
378
   GLOBAL $db, $debug_mode;
379

380
   if ( $chart_type == 6 ) 
×
381
      $sql = "SELECT DISTINCT ip_src, COUNT(acid_event.cid) ".
×
382
             "FROM acid_event ".$criteria[0].
×
383
             "WHERE ".$criteria[1]." AND ip_src is NOT NULL ".
×
384
             "GROUP BY ip_src ORDER BY ip_src";
385
   else if ( $chart_type == 7 )
×
386
      $sql = "SELECT DISTINCT ip_dst, COUNT(acid_event.cid) ".
×
387
             "FROM acid_event ".$criteria[0].
×
388
             "WHERE ".$criteria[1]." AND ip_dst is NOT NULL ".
×
389
             "GROUP BY ip_dst ORDER BY ip_dst";
390

391
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
392
   
393
   $result = $db->baseExecute($sql);
×
394

395
   $cnt = 0;
×
396
   while ( $myrow = $result->baseFetchRow() )
×
397
   {
398
      if ( $myrow[1] >= $min_threshold )
×
399
      {
400
         $xdata[$cnt][0] = baseLong2IP($myrow[0]); 
×
401
         $xdata[$cnt][1] = $myrow[1]; 
×
402
         ++$cnt;
×
403
      }
404
   }
405

406
   $result->baseFreeRows();
×
407
   return $cnt;
×
408
}
409

410
function GetPortDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
411
{
412
   GLOBAL $db, $debug_mode;
413

414
   if ( ($chart_type == 8) || ($chart_type == 9) ) 
×
415
      $sql = "SELECT DISTINCT layer4_dport, COUNT(acid_event.cid) ".
×
416
             "FROM acid_event ".$criteria[0].
×
417
             "WHERE ".$criteria[1]." AND layer4_dport is NOT NULL ".
×
418
             "GROUP BY layer4_dport ORDER BY layer4_dport";
419
   else if ( ($chart_type == 10) || ($chart_type == 11) ) 
×
420
      $sql = "SELECT DISTINCT layer4_sport, COUNT(acid_event.cid) ".
×
421
             "FROM acid_event ".$criteria[0].
×
422
             "WHERE ".$criteria[1]." AND layer4_sport is NOT NULL ".
×
423
             "GROUP BY layer4_sport ORDER BY layer4_sport";
424

425
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
426
   
427
   $result = $db->baseExecute($sql);
×
428

429
   $cnt = 0;
×
430
   while ( $myrow = $result->baseFetchRow() )
×
431
   {
432
      if ( $myrow[1] >= $min_threshold )
×
433
      {
434
         $xdata[$cnt][0] = $myrow[0]; 
×
435
         $xdata[$cnt][1] = $myrow[1]; 
×
436
         ++$cnt;
×
437
      }
438
   }
439

440
   $result->baseFreeRows();
×
441
   return $cnt;
×
442
}
443

444
function GetClassificationDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
445
{
446
   GLOBAL $db, $debug_mode;
447
  
448
   $sql = "SELECT DISTINCT sig_class_id, COUNT(acid_event.cid) ".
×
449
          "FROM acid_event ".$criteria[0].
×
450
          "WHERE ".$criteria[1].
×
451
          " GROUP BY sig_class_id ORDER BY sig_class_id";
452

453
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
454
   
455
   $result = $db->baseExecute($sql);
×
456

457
   $cnt = 0;
×
458
   while ( $myrow = $result->baseFetchRow() )
×
459
   {
460
      if ( $myrow[1] >= $min_threshold )
×
461
      {
462
         if ($debug_mode > 0)
×
463
         {
464
           // Sig. classification vs. number of alerts
465
           error_log(__FILE__ . ":" . __LINE__ . ": \$myrow[0] = \""  . $myrow[0] . "\"");
×
466
         }
467

468

469
         $xdata[$cnt][0] = strip_tags(GetSigClassName($myrow[0], $db)); 
×
470
         if ($debug_mode > 0)
×
471
         {
472
           // Sig. classification vs. number of alerts
473
           error_log(__FILE__ . ":" . __LINE__ . ": \$xdata[\$cnt][0] = \""  . $xdata[$cnt][0] . "\"");
×
474
         }
475

476
         if (empty($xdata[$cnt][0]) || $xdata[$cnt][0] == "unclassified")
×
477
         {
478
           $xdata[$cnt][0] = $myrow[0];
×
479
         }
480

481
         
482

483
         $xdata[$cnt][1] = $myrow[1];
×
484
         ++$cnt;
×
485
      }
486
   }
487

488
   $result->baseFreeRows();
×
489
   return $cnt;
×
490
}
491

492

493

494
function GetUniqueDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
495
{
496
  GLOBAL $db, $debug_mode;
497

498

499
  $cnt = 0;
×
500
  $sql = "SELECT signature, " .
×
501
         "sig_name, " .
502
         "COUNT(signature) " .
503
         "FROM acid_event " . $criteria[0] . " " .
×
504
         "WHERE " . $criteria[1] . " " .
×
505
         "GROUP BY signature, sig_name " . 
506
         "ORDER BY sig_name";
507

508
  if ($debug_mode > 0)
×
509
  {
510
    echo "<BR>\n\$sql = \"" . $sql . "\"<BR><BR>\n\n";
×
511
  }
512

513
  $result = $db->baseExecute($sql);
×
514
  
515
  while($myrow = $result->baseFetchRow())
×
516
  {
517

518
  #  echo "<BR><BR>-------&lt;row&gt;---------<BR><pre>";
519
  #  var_dump($myrow);
520
  #  echo "<BR><BR><BR>";
521
  #  print_r($myrow);
522
  #  echo "</PRE><BR>---------&lt;/row&gt;--------<BR>";
523

524
    if ( $myrow[2] >= $min_threshold )
×
525
    {
526
      $xdata[$cnt][0] = strip_tags($myrow[1]); 
×
527
      $xdata[$cnt][1] = $myrow[2];
×
528

529
      if ($debug_mode > 0)
×
530
      {
531
        print $xdata[$cnt][0] . ": " . $xdata[$cnt][1] . " alerts <BR>\n";
×
532
      }
533
      ++$cnt;
×
534
    }
535
  }
536
  
537
  $result->baseFreeRows();
×
538

539
  return $cnt;
×
540
}
541

542

543

544
function GetSensorDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
545
{
546
   GLOBAL $db, $debug_mode;
547

548
   $sql = "SELECT DISTINCT acid_event.sid, COUNT(acid_event.cid) ".
×
549
          "FROM acid_event ".$criteria[0].
×
550
          "WHERE ".$criteria[1].
×
551
          " GROUP BY acid_event.sid ORDER BY acid_event.sid";
552

553
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
554
   
555
   $result = $db->baseExecute($sql);
×
556

557
   $cnt = 0;
×
558
   while ( $myrow = $result->baseFetchRow() )
×
559
   {
560
      if ( $myrow[1] >= $min_threshold )
×
561
      {
562
         $result2 = $db->baseExecute("SELECT hostname FROM sensor where sid=".$myrow[0]);
×
563
         $sensor_name = $result2->baseFetchRow();
×
564
         $xdata[$cnt][0] = $sensor_name[0];
×
565
         $result2->baseFreeRows();
×
566
 
567
         $xdata[$cnt][1] = $myrow[1];
×
568
         ++$cnt;
×
569
      }
570
   }
571

572
   $result->baseFreeRows();
×
573
   return $cnt;
×
574
}
575

576
// xxx jl
577
function ReadGeoIPfreeFileAscii(&$Geo_IPfree_array){
578
        GLOBAL $Geo_IPfree_file_ascii, $db, $debug_mode, $iso_3166;
579
        if (
×
580
                empty($Geo_IPfree_file_ascii)
581
                || !ChkAccess($Geo_IPfree_file_ascii)
×
582
        ){
583
                return 0;
×
584
        }
585
        ini_set("memory_limit", "256M");
×
586
  $lines = file($Geo_IPfree_file_ascii);
×
587
  if ($lines == FALSE)
×
588
  {
589
    print "WARNING: " . $Geo_IPfree_file_ascii . " could not be opened.<BR>\n";
×
590
    return 0;
×
591
  }
592
 
593
  foreach ($lines as $line_num => $line) 
×
594
  {
595
    $line_array[$line_num] = split(' ', rtrim($line));
×
596
    $index = rtrim($line_array[$line_num][0], ':');
×
597
    $begin = sprintf("%u", ip2long($line_array[$line_num][1]));
×
598
    $end = sprintf("%u", ip2long($line_array[$line_num][2]));
×
599

600
    if (!isset($iso_3166))
×
601
    {
602
      ErrorMessage("<BR>ERROR: \$iso_3166 has not been defined.<BR>\n");
×
603
      return 0;
×
604
                }else{
×
605
                        if ( !base_array_key_exists($index, $iso_3166) ){
×
606
        $estr = "ERROR: index \"" . $index . "\" = ascii codes ";
×
607
        $estr .= ord($index[0]) . ", " . ord($index[1]) . " ";
×
608
        $estr .= "does not exist. Ignoring.<BR>\n";
×
609
        ErrorMessage($estr);
×
610
                        }else{
×
611
                                if ($debug_mode > 1){
×
612
          print "Full name of " . $index . " = \"" . $iso_3166[$index]. "\"<BR>\n";
×
613
                                }
614
        $index .= " (" . $iso_3166[$index] . ")";
×
615
                        }
616
                        if (
×
617
                                !isset($Geo_IPfree_array)
618
                                || !base_array_key_exists($index, $Geo_IPfree_array)
×
619
                        ){
620
        $Geo_IPfree_array[$index][0] = array($begin, $end);
×
621
                        }else{
×
622
        {
623
          array_push($Geo_IPfree_array[$index], array($begin, $end));
×
624
        }
625
      }
626
    }    
627
  }
628
}
629

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

669
/**
670
 * Second method how to lookup the country corresponding to an ip address:
671
 * Makes use of the perl module IP::Country
672
 * http://search.cpan.org/dist/IP-Country/
673
 * The web server needs permission to execute "ip2cc".
674
 * Quoting from the php manual: 
675
 * "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."
676
 *
677
 * $IP2CC must contain the absolute path to this executable.
678
 *
679
 *
680
 */
681
function run_ip2cc($address_with_dots, &$country)
682
{
683
  GLOBAL $db, $debug_mode, $IP2CC, $iso_3166;
684

685

686
  if (empty($address_with_dots))
×
687
  {
688
    ErrorMessage("ERROR: \$address_with_dots is empty<BR>\n");
×
689
    return 0;
×
690
  }
691

692
  if ((!is_file($IP2CC)) || (!is_executable($IP2CC)))
×
693
  {
694
    ErrorMessage("ERROR: with \$IP2CC = \"" . $IP2CC . "\"<BR>\n");
×
695
    return 0;
×
696
  }
697

698
  $cmd = $IP2CC . " " . $address_with_dots;
×
699
  unset($lastline);
×
700
  unset($output);
×
701
  unset($rv);
×
702

703
  $lastline = exec($cmd, $output, $rv);
×
704

705
  if ($rv != 0)
×
706
  {
707
    ErrorMessage("ERROR with " . $cmd . "<BR>\n");
×
708
    print "\$rv = " . $rv . "<BR>\n";
×
709
    print_r($output);
×
710
    return 0;
×
711
  }
712

713
  $result = explode(" ", $output[6]);
×
714
  $max = count($result);
×
715
  $country = "";
×
716
  for ($i = 3; $i < $max; $i++)
×
717
  {
718
    $country .= $result[$i] . " ";
×
719
  }
720

721
  if ($debug_mode > 0)
×
722
  {
723
    print "Found: " . $address_with_dots . " belongs to " . $country . "<BR>\n" ;
×
724
  }
725

726
  return 1;
×
727
}
728

729
function IncreaseCountryValue( &$countries, $to_search, $number_of_alerts ){
730
        GLOBAL $db, $debug_mode;
731
        if (count($countries) == 0 ){
×
732
                $countries[$to_search] = $number_of_alerts;
×
733
                return;
×
734
        }
735
        $tmp = '';
×
736
        if ( base_array_key_exists($to_search, $countries) ){
×
737
                $countries[$to_search] += $number_of_alerts;
×
738
        }else{
×
739
                $tmp = 'NOT ';
×
740
                $countries[$to_search] = $number_of_alerts;
×
741
        }
742
        if ( $debug_mode > 1 ){
×
743
                ErrorMessage($to_search . ' does ' . $tmp .'exist.', 0, 1);
×
744
        }
745
}
746

747
function GetCountryDataSet(
748
        &$xdata, $chart_type, $data_source, $min_threshold, $criteria
749
){
750
        GLOBAL $db, $debug_mode, $Geo_IPfree_file_ascii, $IP2CC;
751
        $country_method = 0;
×
752
        $EMPfx = __FUNCTION__ . ': ';
×
753
  if (($chart_type == 14) || ($chart_type == 15))
×
754
  // 14 =  Src Countries vs. Num Alerts
755
  // 15 = dto., but on worldmap
756
  {
757
      $sql = "SELECT DISTINCT ip_src, COUNT(acid_event.cid) ".
×
758
             "FROM acid_event ".$criteria[0].
×
759
             "WHERE ".$criteria[1]." AND ip_src is NOT NULL ".
×
760
             "GROUP BY ip_src ORDER BY ip_src";
761
  }
762
  else if (($chart_type == 16) || ($chart_type == 17))
×
763
  // 16 = Dst Countries vs. Num Alerts
764
  // 17 = dto., but on worldmap
765
  {
766
      $sql = "SELECT DISTINCT ip_dst, COUNT(acid_event.cid) ".
×
767
             "FROM acid_event ".$criteria[0].
×
768
             "WHERE ".$criteria[1]." AND ip_dst is NOT NULL ".
×
769
             "GROUP BY ip_dst ORDER BY ip_dst";
770
  }
771

772
  if ($debug_mode > 0)  echo $sql."<BR>";
×
773
   
774
  $result = $db->baseExecute($sql);
×
775

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

861
    if ($myrow[1] >= $min_threshold)
×
862
    {
863
      $addresses[$cnt][0] = baseLong2IP($myrow[0]); 
×
864
      $addresses[$cnt][1] = $myrow[1]; 
×
865
      
866
      // xxx jl
867
      // Which country belongs this ip address to?
868
      switch($country_method)
×
869
      {
870
        case 1:
×
871
                GeoIPfree_IP2Country($Geo_IPfree_array, $addresses[$cnt][0], $mycountry);      
×
872
                break;
×
873

874
              case 2:
×
875
                run_ip2cc($addresses[$cnt][0], $mycountry);
×
876
                break;
×
877

878
              default:
×
879
                print "WARNING: country_method no. " . $country_method . " is not supported.<BR>\n";
×
880
              return 0;
×
881
      }
882

883

884
      if ($debug_mode > 0)
×
885
      {
886
              print "\"" . $mycountry . "\": " . $addresses[$cnt][1] . " alerts<BR>\n";
×
887
      }
888

889

890
      // Either GeoIPfree_IP2Country() or run_ip2cc() should have set
891
      // this variable:
892
      if (!isset($mycountry) || empty($mycountry))
×
893
      {
894
        ErrorMessage("ERROR: \$mycountry has not been set as expected.<BR>\n");
×
895
        return 0;
×
896
      }
897

898

899
      // Increase number of alerts for this country 
900
      IncreaseCountryValue($countries, $mycountry, $addresses[$cnt][1]);
×
901

902
      ++$cnt;
×
903
    }
904
  }
905

906
  if ($cnt <= 0)
×
907
  {
908
    // then there are no data points to plot.
909
    return $cnt;
×
910
  }
911

912

913
  if (!isset($countries))
×
914
  {
915
    print "ERROR: \$countries has not even been defined. Returning 0.\n";
×
916
    return 0;
×
917
  }
918

919

920
  if (!is_array($countries))
×
921
  {
922
    print "ERROR: \$countries is not an array. Returning 0.\n";
×
923
    print "<BR><PRE>\n";
×
924
    var_dump($countries);
×
925
    print "</PRE><BR>\n";    
×
926
    return 0;
×
927
  }
928

929

930
  if ($debug_mode > 1)
×
931
  { 
932
    print "<pre>############\n";
×
933
    //var_dump($countries);
934
    print_r($countries);
×
935
    print "###########</pre>\n";
×
936
  }
937
  // Now setup the chart array:
938
  $cnt2 = 0;
×
939
        foreach ( $countries as $key => $val ){ // Issue #153
×
940
                $xdata[$cnt2][0] = $key;
×
941
                $xdata[$cnt2][1] = $val;
×
942
                $cnt2++;
×
943
        }
944
        $result->baseFreeRows();
×
945
        // return number of countries rather than number of addresses!
946
        return $cnt2;
×
947
}
948
?>
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