-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbase_graph_main.php
More file actions
435 lines (427 loc) · 15.8 KB
/
base_graph_main.php
File metadata and controls
435 lines (427 loc) · 15.8 KB
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
<?php
// Basic Analysis and Security Engine (BASE)
// Copyright (C) 2019-2023 Nathan Gibbs
// Copyright (C) 2004 BASE Project Team
// Copyright (C) 2000 Carnegie Mellon University
//
// For license info: See the file 'base_main.php'
//
// Project Lead: Nathan Gibbs
// Built upon work by: Kevin Johnson & the BASE Project Team
// Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
//
// Purpose: BASE Graphing System.
//
// Input GET/POST:
// - submit:
// - time:
// - time_sep:
//
// Author(s): Nathan Gibbs
// Kevin Johnson
$sc = DIRECTORY_SEPARATOR;
require_once("includes$sc" . 'base_krnl.php');
include_once(BASE_IPath . 'base_include.inc.php');
include_once("$BASE_path/base_db_common.php");
include_once("$BASE_path/base_graph_common.php");
AuthorizedRole(10000);
$db = NewBASEDBConnection($DBlib_path, $DBtype); // Connect to the Alert DB.
$db->baseDBConnect(
$db_connect_method,$alert_dbname, $alert_host, $alert_port, $alert_user,
$alert_password
);
UpdateAlertCache($db);
$EMPfx = __FILE__ . ': '; // Error Message Prefix.
if( VerifyGraphingLib() ){ // Graphics Libs Check
$cs = new CriteriaState("base_stat_alerts.php");
$cs->ReadState();
$new = ImportHTTPVar("new", VAR_DIGIT);
$submit = ImportHTTPVar("submit", VAR_ALPHA | VAR_SPACE);
// Set default values if the submit button hasn't been pressed.
if( $new == 1 && $submit == '' ){ // New Graph
$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{ // Retrieve data from submit and store for later use.
$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);
$page_title = _GRAPHALERTDATA;
PrintBASESubHeader(
$page_title, $page_title, $cs->GetBackLink(), $refresh_all_pages
);
include("$BASE_path/base_graph_form.php");
$data_pnt_cnt = 0;
if( $submit != '' && $chart_type == ' ' ){ // Error Conditions.
ErrorMessage('<b>'._ERRCHRTNOTYPE.'</b>.');
}elseif( $submit != '' ){ // Calculate the data set.
if ( $data_source == ' ' ){
ErrorMessage(_ERRNOAGSPEC);
$data_source = NULL;
}
unset($xdata);
unset($xlabel);
if( $debug_mode > 1 ){
echo "<H3>"._CHRTDATAIMPORT."...</H3>";
}
// Building Criteria.
$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";
}else{
$criteria[0] = '';
// $tmp = "acid_event.sid > 0 ";
$tmp = " 1 = 1 "; // Shim SQL when not querying alert groups.
$criteria[1] = $tmp;
}
// Adding Time Constraint
$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
);
if( !empty($time_constraint) ){
$criteria[1] .= $time_constraint;
}
if( $debug_mode > 0 ){
echo "<H3>Chart criteria</H3><PRE>";
print_r($criteria);
echo "</PRE>";
}
$WorldMap = false;
if( $chart_type == 15 || $chart_type == 17 ){
$WorldMap = true; // CHARTTYPE_*_COUNTRY_ON_MAP
}
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, $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, $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, $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, $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, $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, $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, $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, $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, $min_size, $criteria
);
break;
case CHARTTYPE_SRC_COUNTRY; // Src Countries vs. Num Alerts
case CHARTTYPE_SRC_COUNTRY_ON_MAP; // dto., but on worldmap
if ( $WorldMap ){
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, $min_size, $criteria
);
break;
case CHARTTYPE_DST_COUNTRY; // Dst Countries vs. Num Alerts
case CHARTTYPE_DST_COUNTRY_ON_MAP; // dto., but on worldmap
if ( $WorldMap ){
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, $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, $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 ){
$TK = array (
'chart_type', 'data_source', 'chart_interval',
'element_start'
);
$DI = array();
$DD = array();
foreach( $TK as $val ){
array_push($DD, $val);
array_push($DI, $$val);
}
array_push($DD,'X-data Items');
array_push($DI, $number_array_elements);
DDT($DI,$DD,'Chart criteria', '', '', 1);
echo "<H3>"._CHRTHANDLEPERIOD."...</H3>\n";
}
if( $chart_interval || $number_array_elements ){
// Validity check
if( $element_start >= $number_array_elements ){
if( $debug_mode > 0 ){
ErrorMessage("WARNING: i >= number_array_elements");
}
ErrorMessage(_ERRCHRTNODATAPOINTS);
PrintBASESubFooter();
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>";
}
if( isset($xdata[$i][0]) ){ // define x-axis value:
$chart_array [$j][0] = $xdata[$i][0];
}else{
$chart_array[$j][0] = '';
}
if( isset($xdata[$i][1]) ){ // define y-axis value:
$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
* https://pear.php.net/bugs/bug.php?id=16335
* 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>";
}
$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( $WorldMap ){
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( $WorldMap ){
echo "</A><BR>\n";
}
echo "</CENTER>";
PrintFramedBoxFooter(1, 2);
NLIO('<br/>', 2);
if( $WorldMap ){
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>';
$et->Mark('Rendering graph.');
}else{
ErrorMessage(_ERRCHRTNODATAPOINTS);
}
}
PrintBASESubFooter();
}else{ // Graphics Libs Check failed.
error_log($EMPfx . 'Graphics Libs check failed.');
HTTP_header('Location: base_main.php');
}
?>