This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase_graph_main.php
633 lines (551 loc) · 20.9 KB
/
base_graph_main.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
<?php
/*******************************************************************************
** Basic Analysis and Security Engine (BASE)
** Copyright (C) 2004 BASE Project Team
** Copyright (C) 2000 Carnegie Mellon University
**
** (see the file 'base_main.php' for license details)
**
** Project Leads: Kevin Johnson <[email protected]>
** Sean Muller <[email protected]>
** Built upon work by Roman Danyliw <[email protected]>, <[email protected]>
**
** Purpose: Input GET/POST variables
** - submit:
** - time:
** - time_sep:
********************************************************************************
** Authors:
********************************************************************************
** Kevin Johnson <[email protected]
**
********************************************************************************
*/
include ("base_conf.php");
include ("$BASE_path/includes/base_constants.inc.php");
include ("$BASE_path/includes/base_include.inc.php");
include_once ("$BASE_path/base_db_common.php");
include_once ("$BASE_path/base_common.php");
include_once ("$BASE_path/base_graph_common.php");
function check_worldmap()
{
GLOBAL $debug_mode;
$ok = 0;
$php_path = ini_get('include_path');
$php_path_array = explode(':', $php_path);
if ($debug_mode > 0)
{
print "Where is the worldmap?<BR>\n";
}
foreach($php_path_array as $single_path)
{
$where_is_it = "$single_path/Image/Graph/Images/Maps/world_map6.png";
if ($debug_mode > 0)
{
print """ . $where_is_it . ""<BR>\n";
}
if (file_exists($where_is_it))
// then we ASSUME, that this is the correct worldmap file. Not necessarily true, though. A simplification, therefore.
{
if (is_readable($where_is_it))
{
$where_is_it2 = "$single_path/Image/Graph/Images/Maps/world_map6.txt";
if (file_exists($where_is_it2))
{
if (is_readable($where_is_it2))
{
$ok = 1;
break;
}
else
{
ErrorMessage("ERROR: $where_is_it2 does exist, but it is NOT READABLE.<BR>\n");
}
}
else
{
ErrorMessage("ERROR: $where_is_it could be found, but $where_is_it2 does NOT exist.<BR>\n");
$rv = ini_get("safe_mode");
if ($rv == 1)
{
ErrorMessage("In "safe_mode" both world_map6.png and world_map6.txt must be owned by the user under which the web server is running.<BR>\n");
}
}
}
else
{
ErrorMessage("ERROR: $where_is_it does exist, but it is NOT READABLE.<BR>\n");
}
}
}
if ($ok != 1)
{
ErrorMessage("ERROR: The worldmap function is not available, because world_map6.png and world_map6.txt could not be found. Go into the \"PEAR directory\", as can be found by \"pear config-show\", and then into the subdirectory Image/Graph/Images/Maps/. This is the location where world_map6.png and world_map6.txt must be installed.<BR>\n");
$rv = ini_get("safe_mode");
if ($rv == 1)
{
ErrorMessage("In "safe_mode" both world_map6.png and world_map6.txt must be owned by the user under which the web server is running.<BR>\n");
}
return 0;
}
return 1;
}
($debug_time_mode >= 1) ? $et = new EventTiming($debug_time_mode) : '';
$cs = new CriteriaState("base_stat_alerts.php");
$cs->ReadState();
$submit = ImportHTTPVar("submit", VAR_ALPHA | VAR_SPACE);
/**
* Set default values if the submit button hasn't been pressed
*/
if ( $submit == "" )
{
$height = 800;
$width = 600;
$pmargin0 = 50;
$pmargin1 = 50;
$pmargin2 = 70;
$pmargin3 = 80;
$user_chart_title = _CHRTTITLE;
$min_size = 0;
$rotate_xaxis_lbl = 0;
$xaxis_label_inc = 1;
$yaxis_scale = 0;
$chart_style = "bar";
$use_alerts = 0;
$xaxis_grid = 0;
$yaxis_grid = 1;
$element_start = 0;
} else {
/**
* Otherwise, retrieve the data from the submit and
* store it in local variables for use later
*/
$height = ImportHTTPVar("height", VAR_DIGIT);
$width = ImportHTTPVar("width", VAR_DIGIT);
$pmargin0 = ImportHTTPVar("pmargin0", VAR_DIGIT);
$pmargin1 = ImportHTTPVar("pmargin1", VAR_DIGIT);
$pmargin2 = ImportHTTPVar("pmargin2", VAR_DIGIT);
$pmargin3 = ImportHTTPVar("pmargin3", VAR_DIGIT);
$user_chart_title = ImportHTTPVar("user_chart_title", VAR_ALPHA | VAR_SPACE);
$min_size = ImportHTTPVar("min_size", VAR_DIGIT);
$rotate_xaxis_lbl = ImportHTTPVar("rotate_xaxis_lbl", VAR_DIGIT);
$xaxis_label_inc = ImportHTTPVar("xaxis_label_inc", VAR_DIGIT);
$yaxis_scale = ImportHTTPVar("yaxis_scale", VAR_DIGIT);
$chart_style = ImportHTTPVar("chart_style", VAR_ALPHA);
$xaxis_grid = ImportHTTPVar("xaxis_grid", VAR_DIGIT);
$yaxis_grid = ImportHTTPVar("yaxis_grid", VAR_DIGIT);
$element_start = ImportHTTPVar("element_start", VAR_DIGIT);
}
$data_source = ImportHTTPVar("data_source", VAR_DIGIT);
$chart_type = ImportHTTPVar("chart_type", VAR_DIGIT);
$chart_interval = ImportHTTPVar("chart_interval", VAR_DIGIT);
$chart_begin_hour = ImportHTTPVar("chart_begin_hour", VAR_DIGIT);
$chart_begin_month = ImportHTTPVar("chart_begin_month", VAR_DIGIT);
$chart_begin_day = ImportHTTPVar("chart_begin_day", VAR_DIGIT);
$chart_begin_year = ImportHTTPVar("chart_begin_year", VAR_DIGIT);
$chart_end_hour = ImportHTTPVar("chart_end_hour", VAR_DIGIT);
$chart_end_month = ImportHTTPVar("chart_end_month", VAR_DIGIT);
$chart_end_day = ImportHTTPVar("chart_end_day", VAR_DIGIT);
$chart_end_year = ImportHTTPVar("chart_end_year", VAR_DIGIT);
$aggregate_type = ImportHTTPVar("aggregate_type", VAR_DIGIT);
// Check role out and redirect if needed -- Kevin
$roleneeded = 10000;
$BUser = new BaseUser();
if (($BUser->hasRole($roleneeded) == 0) && ($Use_Auth_System == 1))
base_header("Location: ". $BASE_urlpath . "/index.php");
$page_title = _GRAPHALERTDATA;
PrintBASESubHeader($page_title, $page_title, $cs->GetBackLink(), $refresh_all_pages);
// Check if Image_Graph install is ok -- Alejandro
VerifyGraphingLib();
/* Connect to the Alert database */
$db = NewBASEDBConnection($DBlib_path, $DBtype);
$db->baseDBConnect($db_connect_method,
$alert_dbname, $alert_host, $alert_port, $alert_user, $alert_password);
if ( $event_cache_auto_update == 1 ) UpdateAlertCache($db);
if ( ini_get("safe_mode") != true )
set_time_limit($max_script_runtime);
include("$BASE_path/base_graph_form.php");
$data_pnt_cnt = 0;
/* Error Conditions */
if ( $submit != "" && $chart_type == " " )
echo '<B>'._ERRCHRTNOTYPE.'</B>.';
/* Calculate the data set */
else if ($submit != "")
{
if ( $data_source == " " )
{
ErrorMessage(_ERRNOAGSPEC);
$data_source = NULL;
}
unset($xdata);
unset($xlabel);
if ( $debug_mode > 1 ) {
echo "<H3>"._CHRTDATAIMPORT."...</H3>";
}
/* Building Criteria */
$time_constraint = ProcessChartTimeConstraint($chart_begin_hour,
$chart_begin_day,
$chart_begin_month,
$chart_begin_year,
$chart_end_hour,
$chart_end_day,
$chart_end_month,
$chart_end_year );
$criteria = array(2);
if (!empty($data_source))
{
$criteria[0] = "LEFT JOIN acid_ag_alert ".
"ON (acid_event.sid=acid_ag_alert.ag_sid AND acid_event.cid=acid_ag_alert.ag_cid) ";
$criteria[1] = "acid_ag_alert.ag_id = $data_source";
if (!empty($time_constraint))
$criteria[1] = $criteria[1].$time_constraint;
}
else
{
$criteria[0] = "";
// $criteria[1] = "acid_event.sid > 0 ".$time_constraint;
if (empty($time_constraint))
$criteria[1] = " 1 = 1 ";
else
$criteria[1] = " 1 = 1 " . $time_constraint;
}
if ( $debug_mode > 0 )
{
echo "<H3>Chart criteria</H3><PRE>";
print_r($criteria);
echo "</PRE>";
}
switch ($chart_type)
{
case CHARTTYPE_HOUR:
// hours vs num of alerts
case CHARTTYPE_DAY:
// days vs num of alerts
case CHARTTYPE_WEEK:
// weeks vs num of alerts
case CHARTTYPE_MONTH:
// months vs num of alerts
case CHARTTYPE_YEAR:
// years vs num of alerts
{
$chart_title = _CHRTTIMEVNUMBER;
$xaxis_label = _CHRTTIME;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetTimeDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
/* Any kinds of special characters, like slashes, two points
* and so on seem to NOT work with Image_Graph */
//$chart_title = $chart_title."\n ( ".$xdata[0][0]." - ".$xdata[count($xdata)-1][0]." )";
//$xaxis_label .= " from ". $xdata[0][0] . " to " . $xdata[count($xdata)-1][0] . " ";
break;
}
case CHARTTYPE_SRC_IP: // Src. IP vs. Num Alerts
{
$chart_title = _CHRTSIPNUMBER;
$xaxis_label = _CHRTSIP;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetIPDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_DST_IP: // Dst. IP vs. Num Alerts
{
$chart_title = _CHRTDIPALERTS;
$xaxis_label = _CHRTDIP;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetIPDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_DST_UDP_PORT: // UDP Port vs. Num Alerts
{
$chart_title = _CHRTUDPPORTNUMBER;
$xaxis_label = _CHRTDUDPPORT;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetPortDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_SRC_UDP_PORT: // UDP Port vs. Num Alerts
{
$chart_title = _CHRTSUDPPORTNUMBER;
$xaxis_label = _CHRTSUDPPORT;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetPortDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_DST_TCP_PORT: // TCP Port vs. Num Alerts
{
$chart_title = _CHRTPORTDESTNUMBER;
$xaxis_label = _CHRTPORTDEST;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetPortDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_SRC_TCP_PORT: // TCP Port vs. Num Alerts
{
$chart_title = _CHRTPORTSRCNUMBER;
$xaxis_label = _CHRTPORTSRC;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetPortDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_CLASSIFICATION: // Classification vs. Num Alerts
{
$chart_title = _CHRTSIGNUMBER;
$xaxis_label = _CHRTCLASS;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetClassificationDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_SENSOR: // Sensor vs. Num Alerts
{
$chart_title = _CHRTSENSORNUMBER;
$xaxis_label = _SENSOR;
$yaxis_label = _CHRTALERTOCCUR;
$data_pnt_cnt = GetSensorDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_SRC_COUNTRY: // Src Countries vs. Num Alerts
case CHARTTYPE_SRC_COUNTRY_ON_MAP: // dto., but on worldmap
{
if ($chart_type == CHARTTYPE_SRC_COUNTRY_ON_MAP)
{
if (!check_worldmap())
{
return 0;
}
}
$chart_title = "Countries of origin vs. number of alerts";
$xaxis_label = "Src countries";
$yaxis_label = "Number of alerts";
$data_pnt_cnt = GetCountryDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_DST_COUNTRY: // Dst Countries vs. Num Alerts
case CHARTTYPE_DST_COUNTRY_ON_MAP: // dto., but on worldmap
{
if ($chart_type == CHARTTYPE_DST_COUNTRY_ON_MAP)
{
if (!check_worldmap())
{
return 0;
}
}
$chart_title = "Destination Countries vs. number of alerts";
$xaxis_label = "Dst countries";
$yaxis_label = "Number of alerts";
$data_pnt_cnt = GetCountryDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
case CHARTTYPE_UNIQUE_SIGNATURE: // Unique alerts vs. number of alerts
{
$chart_title = "Unique alerts vs. number of alerts";
$xaxis_label = "Unique alerts";
$yaxis_label = "Number of alerts";
$data_pnt_cnt = GetUniqueDataSet($xdata, $chart_type, $data_source, $min_size, $criteria);
break;
}
default:
{
print "WARNING: charttype \"$charttype\" is not supported. Returning.";
return 0;
}
}
if ( $data_pnt_cnt > 0 )
{
$number_array_elements = count($xdata);
if ( $debug_mode > 0 )
{
echo "chart_type = $chart_type<BR>
data_source = $data_source<BR>
chart_interval = $chart_interval<BR>
element_start = $element_start<BR>
count(\$xdata) = $number_array_elements<BR>\n";
echo "<H3>"._CHRTHANDLEPERIOD."...</H3>\n";
}
if ( $chart_interval || $number_array_elements) {
// quick validity check
if ($element_start >= $number_array_elements)
{
if ($debug_mode > 0)
{
print "WARNING: i >= number_array_elements<BR>";
}
ErrorMessage(_ERRCHRTNODATAPOINTS);
($debug_time_mode >= 1) ? $et->PrintTiming() : '';
PrintBASESubFooter();
echo "</body>\r\n</html>";
return;
}
// From which element on should the "for"-loop start:
if (
(ctype_digit($element_start)) &&
($element_start > 0) &&
($element_start < $number_array_elements)
)
{
$i = $element_start;
}
else
{
$i = 0;
}
if ($debug_mode > 0)
{
print "i = $i<BR>";
print "element_start = $element_start<BR>";
print "number_array_elements = " . $number_array_elements . "<BR>";
print "count(xdata) = " . count($xdata) . "<BR><BR>";
}
// set up array
for ($j = 0;
$i < $number_array_elements;
$i++, $j++)
{
// "How many columns/elements do you want to see?"
if ($chart_interval > 0)
{
if ($j >= $chart_interval)
{
break;
}
}
if ($debug_mode > 1)
{
print $i . ": " . $xdata[$i][0] . " - " . $xdata[$i][1] . "<BR>";
}
// define x-axis value:
if (isset($xdata[$i][0]))
{
$chart_array [$j][0] = $xdata[$i][0];
}
else
{
$chart_array[$j][0] = "";
}
// define y-axis value:
if (isset($xdata[$i][1]))
{
$chart_array [$j][1] = $xdata[$i][1];
}
else
{
$chart_array[$j][1] = 0;
}
} // for-loop
if (
(
($chart_style == "bar") ||
($chart_style == "line")
) &&
(count($chart_array) == 1)
)
/* then there's is a bug in PEAR::Image_Graph;
* Cf. http://pear.php.net/bugs/bug.php?id=12763
* http://pear.php.net/bugs/7423
* the following
* appends one element, that does, of course, not really exist,
* as a workaround: */
{
if ($debug_mode > 0)
{
print "WARNING: Workaround: Adding one dummy element, that does NOT really exist!<BR>\n";
}
$chart_array[1][0] = "";
$chart_array[1][1] = 0;
}
// finally, set up xdata
$xdata = $chart_array;
} // if ( $chart_interval || $number_array_elements) {
if ( $debug_mode > 0 )
{
print "count(xdata) = " . count($xdata) . "<BR>\n";
// disabled because does not work as expected
//echo "<H3>"._CHRTDUMP." $xaxis_label_inc)</H3>";
}
for ( $i = 0; $i < count($xdata); $i++)
{
if ( $debug_mode > 0 )
{
echo $i." -- ".$xdata[$i][0]." - ".$xdata[$i][1]."<BR>";
}
// The following does not work as expected with PEAR::Image_Graph-0.7.2
// Although as many pieces of data are added to the plot
// as count($xdata) suggests, in the end are only those
// bars (lines) displayed, that do NOT have an empty string
// in $xdata[$i][0] (and even not a space!). I'm inclined to
// consider this as one more bug of Image_Graph library.
/*
// Apply the X-Axis label clean-up --
// only write every N axis labels (erase the rest)
if (($xaxis_label_inc != 0) && ( ($i % $xaxis_label_inc ) != 0 ))
{
$xdata[$i][0] = "";
}
*/
}
if ( $debug_mode > 0 )
{
echo "<H3>"._CHRTDRAW." ($width x $height)</H3>";
}
($debug_time_mode >= 1) ? $et->Mark("Extracting data") : '';
echo '<CENTER>
<TABLE BGCOLOR="#000000" CELLSPACING=0 CELLPADDING=2 BORDER=0 SUMMARY="table from base_graph_main.php">
<TR>
<TD>';
$_SESSION['xdata'] = $xdata;
echo "<CENTER>";
if (($chart_type == CHARTTYPE_SRC_COUNTRY_ON_MAP) || ($chart_type == CHARTTYPE_DST_COUNTRY_ON_MAP))
{
echo "<A HREF=\"base_graph_display.php?";
echo "&pmargin0=$pmargin0&pmargin1=$pmargin1".
"&pmargin2=$pmargin2&pmargin3=$pmargin3".
"&title=".rawurlencode($user_chart_title." \n".$chart_title).
"&xaxis_label=".rawurlencode($xaxis_label).
"&yaxis_label=".rawurlencode($yaxis_label).
// "&yaxis_scale=".rawurlencode($yaxis_scale).
"&yaxis_scale=" . $yaxis_scale .
"&rotate_xaxis_lbl=".rawurlencode($rotate_xaxis_lbl).
"&xaxis_grid=".$xaxis_grid.
"&yaxis_grid=".$yaxis_grid.
"&chart_type=".$chart_type.
"&style=".$chart_style."\">";
echo "<IMG WIDTH=600 HEIGHT=300 SRC=\"base_graph_display.php?";
}
else
{
echo "<IMG SRC=\"base_graph_display.php?width=$width&height=$height";
}
echo "&pmargin0=$pmargin0&pmargin1=$pmargin1".
"&pmargin2=$pmargin2&pmargin3=$pmargin3".
"&title=".rawurlencode($user_chart_title." \n".$chart_title).
"&xaxis_label=".rawurlencode($xaxis_label).
"&yaxis_label=".rawurlencode($yaxis_label).
// "&yaxis_scale=".rawurlencode($yaxis_scale).
"&yaxis_scale=" . $yaxis_scale .
"&rotate_xaxis_lbl=".rawurlencode($rotate_xaxis_lbl).
"&xaxis_grid=".$xaxis_grid.
"&yaxis_grid=".$yaxis_grid.
"&chart_type=".$chart_type.
"&style=".$chart_style."\">";
if (($chart_type == CHARTTYPE_SRC_COUNTRY_ON_MAP) || ($chart_type == CHARTTYPE_DST_COUNTRY_ON_MAP))
{
echo "</A><BR>\n";
}
echo "</CENTER>";
echo '</TD>
</TR>
</TABLE>
<BR>';
if (($chart_type == CHARTTYPE_SRC_COUNTRY_ON_MAP) || ($chart_type == CHARTTYPE_DST_COUNTRY_ON_MAP))
{
echo '(click at the image or - after it has been reloaded - click at it for a second time to get a bigger size of it)<BR><BR>';
}
echo '</CENTER>';
($debug_time_mode >= 1) ? $et->Mark("Rendering graph") : '';
}
else
ErrorMessage(_ERRCHRTNODATAPOINTS);
}
($debug_time_mode >= 1) ? $et->PrintTiming() : '';
PrintBASESubFooter();
echo "</body>\r\n</html>";
// vim:shiftwidth=2:tabstop=2:expandtab
?>