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

NathanGibbs3 / BASE / 625

pending completion
625

push

travis-ci-com

NathanGibbs3
20230512 Standardized BASE Iconography.
         Using 3dicons from @realvjy
         https://github.com/realvjy/3dicons
         Thank You. :smile:

33 of 33 new or added lines in 3 files covered. (100.0%)

648 of 10178 relevant lines covered (6.37%)

71.31 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
$sc = DIRECTORY_SEPARATOR;
×
23
require_once("includes$sc" . 'base_krnl.php');
×
24
include_once("$BASE_path/base_qry_common.php");
×
25
include_once("$BASE_path/includes/base_log_error.inc.php");
×
26
include_once("$BASE_path/includes/base_signature.inc.php");
×
27
include_once("$BASE_path/includes/base_iso3166.inc.php");
×
28

29
// Some colors to be used in graphs.
30
$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');
×
31

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

54
// @codeCoverageIgnoreStart
55
// These code paths are installation dependent.
56
// Testing would be problematic.
57
function VerifyGraphingLib(){
58
        $Ret = false; // Lib Error
59
        if( !function_exists('imagecreate') ){// Is GD compiled into PHP.
60
                print returnBuildError('GD', '--with-gd');
61
        }else{
62
                $Ret = PearInc('Graphing', 'Image', 'Graph');
63
        }
64
        if ( $Ret == false ){ // Keep Issue #100 from happening here.
65
                sleep(60);
66
        }
67
        return $Ret;
68
}
69
// @codeCoverageIgnoreEnd
70

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

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

371
function GetIPDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
372
{
373
   GLOBAL $db, $debug_mode;
374

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

386
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
387
   
388
   $result = $db->baseExecute($sql);
×
389

390
   $cnt = 0;
×
391
   while ( $myrow = $result->baseFetchRow() )
×
392
   {
393
      if ( $myrow[1] >= $min_threshold )
×
394
      {
395
         $xdata[$cnt][0] = baseLong2IP($myrow[0]); 
×
396
         $xdata[$cnt][1] = $myrow[1]; 
×
397
         ++$cnt;
×
398
      }
399
   }
400

401
   $result->baseFreeRows();
×
402
   return $cnt;
×
403
}
404

405
function GetPortDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
406
{
407
   GLOBAL $db, $debug_mode;
408

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

420
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
421
   
422
   $result = $db->baseExecute($sql);
×
423

424
   $cnt = 0;
×
425
   while ( $myrow = $result->baseFetchRow() )
×
426
   {
427
      if ( $myrow[1] >= $min_threshold )
×
428
      {
429
         $xdata[$cnt][0] = $myrow[0]; 
×
430
         $xdata[$cnt][1] = $myrow[1]; 
×
431
         ++$cnt;
×
432
      }
433
   }
434

435
   $result->baseFreeRows();
×
436
   return $cnt;
×
437
}
438

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

448
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
449
   
450
   $result = $db->baseExecute($sql);
×
451

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

463

464
         $xdata[$cnt][0] = strip_tags(GetSigClassName($myrow[0], $db)); 
×
465
         if ($debug_mode > 0)
×
466
         {
467
           // Sig. classification vs. number of alerts
468
           error_log(__FILE__ . ":" . __LINE__ . ": \$xdata[\$cnt][0] = \""  . $xdata[$cnt][0] . "\"");
×
469
         }
470

471
         if (empty($xdata[$cnt][0]) || $xdata[$cnt][0] == "unclassified")
×
472
         {
473
           $xdata[$cnt][0] = $myrow[0];
×
474
         }
475

476
         
477

478
         $xdata[$cnt][1] = $myrow[1];
×
479
         ++$cnt;
×
480
      }
481
   }
482

483
   $result->baseFreeRows();
×
484
   return $cnt;
×
485
}
486

487

488

489
function GetUniqueDataSet(&$xdata, $chart_type, $data_source, $min_threshold, $criteria)
490
{
491
  GLOBAL $db, $debug_mode;
492

493

494
  $cnt = 0;
×
495
  $sql = "SELECT signature, " .
×
496
         "sig_name, " .
497
         "COUNT(signature) " .
498
         "FROM acid_event " . $criteria[0] . " " .
×
499
         "WHERE " . $criteria[1] . " " .
×
500
         "GROUP BY signature, sig_name " . 
501
         "ORDER BY sig_name";
502

503
  if ($debug_mode > 0)
×
504
  {
505
    echo "<BR>\n\$sql = \"" . $sql . "\"<BR><BR>\n\n";
×
506
  }
507

508
  $result = $db->baseExecute($sql);
×
509
  
510
  while($myrow = $result->baseFetchRow())
×
511
  {
512

513
  #  echo "<BR><BR>-------&lt;row&gt;---------<BR><pre>";
514
  #  var_dump($myrow);
515
  #  echo "<BR><BR><BR>";
516
  #  print_r($myrow);
517
  #  echo "</PRE><BR>---------&lt;/row&gt;--------<BR>";
518

519
    if ( $myrow[2] >= $min_threshold )
×
520
    {
521
      $xdata[$cnt][0] = strip_tags($myrow[1]); 
×
522
      $xdata[$cnt][1] = $myrow[2];
×
523

524
      if ($debug_mode > 0)
×
525
      {
526
        print $xdata[$cnt][0] . ": " . $xdata[$cnt][1] . " alerts <BR>\n";
×
527
      }
528
      ++$cnt;
×
529
    }
530
  }
531
  
532
  $result->baseFreeRows();
×
533

534
  return $cnt;
×
535
}
536

537
function GetSensorDataSet(
538
        &$xdata, $chart_type, $data_source, $min_threshold, $criteria
539
){
540
        GLOBAL $db, $debug_mode;
541

542
   $sql = "SELECT DISTINCT acid_event.sid, COUNT(acid_event.cid) ".
×
543
          "FROM acid_event ".$criteria[0].
×
544
          "WHERE ".$criteria[1].
×
545
          " GROUP BY acid_event.sid ORDER BY acid_event.sid";
546

547
   if ( $debug_mode > 0)  echo $sql."<BR>";
×
548
   
549
   $result = $db->baseExecute($sql);
×
550

551
   $cnt = 0;
×
552
   while ( $myrow = $result->baseFetchRow() )
×
553
   {
554
      if ( $myrow[1] >= $min_threshold )
×
555
      {
556
         $result2 = $db->baseExecute("SELECT hostname FROM sensor where sid=".$myrow[0]);
×
557
         $sensor_name = $result2->baseFetchRow();
×
558
         $xdata[$cnt][0] = $sensor_name[0];
×
559
         $result2->baseFreeRows();
×
560
 
561
         $xdata[$cnt][1] = $myrow[1];
×
562
         ++$cnt;
×
563
      }
564
   }
565

566
   $result->baseFreeRows();
×
567
   return $cnt;
×
568
}
569

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

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

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

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

678

679
  if (empty($address_with_dots))
×
680
  {
681
    ErrorMessage("ERROR: \$address_with_dots is empty<BR>\n");
×
682
    return 0;
×
683
  }
684

685
  if ((!is_file($IP2CC)) || (!is_executable($IP2CC)))
×
686
  {
687
    ErrorMessage("ERROR: with \$IP2CC = \"" . $IP2CC . "\"<BR>\n");
×
688
    return 0;
×
689
  }
690

691
  $cmd = $IP2CC . " " . $address_with_dots;
×
692
  unset($lastline);
×
693
  unset($output);
×
694
  unset($rv);
×
695

696
  $lastline = exec($cmd, $output, $rv);
×
697

698
  if ($rv != 0)
×
699
  {
700
    ErrorMessage("ERROR with " . $cmd . "<BR>\n");
×
701
    print "\$rv = " . $rv . "<BR>\n";
×
702
    print_r($output);
×
703
    return 0;
×
704
  }
705

706
  $result = explode(" ", $output[6]);
×
707
  $max = count($result);
×
708
  $country = "";
×
709
  for ($i = 3; $i < $max; $i++)
×
710
  {
711
    $country .= $result[$i] . " ";
×
712
  }
713

714
  if ($debug_mode > 0)
×
715
  {
716
    print "Found: " . $address_with_dots . " belongs to " . $country . "<BR>\n" ;
×
717
  }
718

719
  return 1;
×
720
}
721

722
function IncreaseCountryValue( &$countries, $to_search, $number_of_alerts ){
723
        GLOBAL $db, $debug_mode;
724
        if( count($countries) == 0 ){
×
725
                $countries[$to_search] = $number_of_alerts;
×
726
                return;
×
727
        }
728
        $tmp = '';
×
729
        if( is_key($to_search, $countries) ){
×
730
                $countries[$to_search] += $number_of_alerts;
×
731
        }else{
×
732
                $tmp = 'NOT ';
×
733
                $countries[$to_search] = $number_of_alerts;
×
734
        }
735
        if( $debug_mode > 1 ){
×
736
                ErrorMessage($to_search . ' does ' . $tmp .'exist.', 0, 1);
×
737
        }
738
}
739

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

765
  if ($debug_mode > 0)  echo $sql."<BR>";
×
766
   
767
  $result = $db->baseExecute($sql);
×
768

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

854
    if ($myrow[1] >= $min_threshold)
×
855
    {
856
      $addresses[$cnt][0] = baseLong2IP($myrow[0]); 
×
857
      $addresses[$cnt][1] = $myrow[1]; 
×
858
      
859
      // xxx jl
860
      // Which country belongs this ip address to?
861
      switch($country_method)
×
862
      {
863
        case 1:
×
864
                GeoIPfree_IP2Country($Geo_IPfree_array, $addresses[$cnt][0], $mycountry);      
×
865
                break;
×
866

867
              case 2:
×
868
                run_ip2cc($addresses[$cnt][0], $mycountry);
×
869
                break;
×
870

871
              default:
×
872
                print "WARNING: country_method no. " . $country_method . " is not supported.<BR>\n";
×
873
              return 0;
×
874
      }
875

876

877
      if ($debug_mode > 0)
×
878
      {
879
              print "\"" . $mycountry . "\": " . $addresses[$cnt][1] . " alerts<BR>\n";
×
880
      }
881

882

883
      // Either GeoIPfree_IP2Country() or run_ip2cc() should have set
884
      // this variable:
885
      if (!isset($mycountry) || empty($mycountry))
×
886
      {
887
        ErrorMessage("ERROR: \$mycountry has not been set as expected.<BR>\n");
×
888
        return 0;
×
889
      }
890

891

892
      // Increase number of alerts for this country 
893
      IncreaseCountryValue($countries, $mycountry, $addresses[$cnt][1]);
×
894

895
      ++$cnt;
×
896
    }
897
  }
898

899
  if ($cnt <= 0)
×
900
  {
901
    // then there are no data points to plot.
902
    return $cnt;
×
903
  }
904

905

906
  if (!isset($countries))
×
907
  {
908
    print "ERROR: \$countries has not even been defined. Returning 0.\n";
×
909
    return 0;
×
910
  }
911

912

913
  if (!is_array($countries))
×
914
  {
915
    print "ERROR: \$countries is not an array. Returning 0.\n";
×
916
    print "<BR><PRE>\n";
×
917
    var_dump($countries);
×
918
    print "</PRE><BR>\n";    
×
919
    return 0;
×
920
  }
921

922

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