This repository was 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_stat_ports.php
355 lines (305 loc) · 11.6 KB
/
base_stat_ports.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
<?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: Displays statistics on the detected source and
** destination TCP/UDP ports
**
** Input GET/POST variables
** - port_type: sets the type of address on which stats will
** be generated
** = 1: source port
** = 2: destination port
** - proto: sets the protocol
** = 6: TCP
** = 17: UDP
** = -1: either
** - caller: specifies the canned snapshot to run
** = most_frequent: Most frequent Port
** - submit:
********************************************************************************
** 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_qry_common.php");
$et = new EventTiming($debug_time_mode);
/* 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);
$cs = new CriteriaState("base_stat_ports.php");
$cs->ReadState();
// Check role out and redirect if needed -- Kevin
$roleneeded = 10000;
$port_proto = "TCP";
$BUser = new BaseUser();
if (($BUser->hasRole($roleneeded) == 0) && ($Use_Auth_System == 1))
base_header("Location: ". $BASE_urlpath . "/index.php");
$qs = new QueryState();
$qs->AddCannedQuery("most_frequent", $freq_num_uports, _MOSTFREQPORTS, "occur_d");
$qs->AddCannedQuery("last_ports", $last_num_uports, _LASTPORTS, "last_d");
$submit = ImportHTTPVar("submit", VAR_ALPHA | VAR_SPACE, array(_SELECTED, _ALLONSCREEN, _ENTIREQUERY));
$port_type = ImportHTTPVar("port_type", VAR_DIGIT);
$proto = ImportHTTPVar("proto", VAR_DIGIT);
$sort_order=ImportHTTPVar("sort_order", VAR_LETTER | VAR_USCORE);
$action = ImportHTTPVar("action", VAR_ALPHA);
$qs->MoveView($submit); /* increment the view if necessary */
$page_title = "";
switch ($proto)
{
case TCP:
$page_title = _UNIQ." TCP ";
break;
case UDP:
$page_title = _UNIQ." UDP ";
break;
case -1:
$page_title = _UNIQ." ";
break;
}
switch ($port_type)
{
case SOURCE_PORT:
$page_title = $page_title._SRCPS;
break;
case DEST_PORT:
$page_title = $page_title._DSTPS;
break;
}
if ( $qs->isCannedQuery() )
{
if ($action == "")
{
PrintBASESubHeader($page_title.": ".$qs->GetCurrentCannedQueryDesc(),
$page_title.": ".$qs->GetCurrentCannedQueryDesc(),
$cs->GetBackLink(), 1);
}
else
{
PrintBASESubHeader($page_title.": ".$qs->GetCurrentCannedQueryDesc(),
$page_title.": ".$qs->GetCurrentCannedQueryDesc(),
$cs->GetBackLink(), $refresh_all_pages);
}
}
else
{
if ($action == "")
{
PrintBASESubHeader($page_title, $page_title, $cs->GetBackLink(), 1);
}
else
{
PrintBASESubHeader($page_title, $page_title, $cs->GetBackLink(), $refresh_all_pages);
}
}
if ( $event_cache_auto_update == 1 ) UpdateAlertCache($db);
$criteria_clauses = ProcessCriteria();
PrintCriteria("");
$criteria = $criteria_clauses[0]." ".$criteria_clauses[1];
$qs->AddValidAction("ag_by_id");
$qs->AddValidAction("ag_by_name");
$qs->AddValidAction("add_new_ag");
$qs->AddValidAction("del_alert");
$qs->AddValidAction("email_alert");
$qs->AddValidAction("email_alert2");
$qs->AddValidAction("csv_alert");
$qs->AddValidAction("archive_alert");
$qs->AddValidAction("archive_alert2");
$qs->AddValidActionOp(_SELECTED);
$qs->AddValidActionOp(_ALLONSCREEN);
$et->Mark("Initialization");
$qs->RunAction($submit, PAGE_STAT_PORTS, $db);
$et->Mark("Alert Action");
switch($proto)
{
case TCP:
$proto_sql = " ip_proto = ".TCP;
break;
case UDP:
$proto_sql = " ip_proto = ".UDP;
break;
default:
$proto_sql = " ip_proto IN (".TCP.", ".UDP.")";
break;
}
if ( $criteria_clauses[1] != "" )
$proto_sql = $proto_sql." AND ";
switch($port_type)
{
case SOURCE_PORT:
$port_type_sql = "layer4_sport";
break;
case DEST_PORT:
default:
$port_type_sql = "layer4_dport";
break;
}
/* create SQL to get Unique Alerts */
$cnt_sql = "SELECT count(DISTINCT $port_type_sql) ".
" FROM acid_event ".$criteria_clauses[0].
" WHERE $proto_sql".$criteria_clauses[1];
/* Run the query to determine the number of rows (No LIMIT)*/
$qs->GetNumResultRows($cnt_sql, $db);
$et->Mark("Counting Result size");
/* Setup the Query Results Table */
$qro = new QueryResultsOutput("base_stat_ports.php?caller=$caller".
"&sort_order=".$sort_order.
"&port_type=$port_type&proto=$proto");
$qro->AddTitle(" ");
$qro->AddTitle(_PORT,
"port_a", " ", " ORDER BY $port_type_sql ASC",
"port_d", " ", " ORDER BY $port_type_sql DESC");
$qro->AddTitle(_SENSOR,
"sensor_a", " ", " ORDER BY num_sensors ASC",
"sensor_d", " ", " ORDER BY num_sensors DESC");
$qro->AddTitle(_OCCURRENCES,
"occur_a", " ", " ORDER BY num_events ASC",
"occur_d", " ", " ORDER BY num_events DESC");
$qro->AddTitle(_UNIALERTS,
"alerts_a", " ", " ORDER BY num_sig ASC",
"alerts_d", " ", " ORDER BY num_sig DESC");
$qro->AddTitle(_SUASRCADD,
"sip_a", " ", " ORDER BY num_sip ASC",
"sip_d", " ", " ORDER BY num_sip DESC");
$qro->AddTitle(_SUADSTADD,
"dip_a", " ", " ORDER BY num_dip ASC",
"dip_d", " ", " ORDER BY num_dip DESC");
$qro->AddTitle(_FIRST,
"first_a", " ", " ORDER BY first_timestamp ASC",
"first_d", " ", " ORDER BY first_timestamp DESC");
$qro->AddTitle(_LAST,
"last_a", " ", " ORDER BY last_timestamp ASC",
"last_d", " ", " ORDER BY last_timestamp DESC");
$sort_sql = $qro->GetSortSQL($qs->GetCurrentSort(), $qs->GetCurrentCannedQuerySort());
$sql = "SELECT DISTINCT $port_type_sql, MIN(ip_proto), ".
" COUNT(acid_event.cid) as num_events,".
" COUNT( DISTINCT acid_event.sid) as num_sensors, ".
" COUNT( DISTINCT signature ) as num_sig, ".
" COUNT( DISTINCT ip_src ) as num_sip, ".
" COUNT( DISTINCT ip_dst ) as num_dip, ".
" MIN(timestamp) as first_timestamp, ".
" MAX(timestamp) as last_timestamp ".
$sort_sql[0].
" FROM acid_event ".$criteria_clauses[0]." WHERE ".$proto_sql.$criteria_clauses[1].
" GROUP BY ".$port_type_sql." ".$sort_sql[1];
/* Run the Query again for the actual data (with the LIMIT) */
$result = $qs->ExecuteOutputQuery($sql, $db);
$et->Mark("Retrieve Query Data");
if ( $debug_mode == 1 )
{
$qs->PrintCannedQueryList();
$qs->DumpState();
echo "$sql<BR>";
echo '<HR><TABLE BORDER=1>
<TR><TD>port_type</TD>
<TD>proto</TD></TR>
<TR><TD>'.$port_type.'</TD>
<TD>'.$proto.'</TD></TR>
</TABLE>';
}
/* Print the current view number and # of rows */
$qs->PrintResultCnt();
echo '<FORM METHOD="post" NAME="PacketForm" ACTION="base_stat_ports.php">'."\n";
$qro->PrintHeader();
echo "<input type='hidden' name='port_type' value='$port_type'>\n";
$i = 0;
while ( ($myrow = $result->baseFetchRow()) && ($i < $qs->GetDisplayRowCnt()) )
{
$currentPort = $url_port = $myrow[0].' ';
if ( $port_proto == TCP )
{
$currentPort = $currentPort.'/ tcp ';
}
if ( $port_proto == UDP )
{
$currentPort = $currentPort.'/ udp ';
}
// Go here to change the format of the Port lookup stuff! -- Kevin Johnson
foreach ($external_port_link as $name => $baseurl) {
$currentPort = $currentPort.'[<A HREF="'.$baseurl.$myrow[0].'" TARGET="_ACID_PORT_">'.$name.'</A>] ';
}
$port_proto = $myrow[1];
$num_events = $myrow[2];
$num_sensors = $myrow[3];
$num_sig = $myrow[4];
$num_sip = $myrow[5];
$num_dip = $myrow[6];
$first_time = $myrow[7];
$last_time = $myrow[8];
if ( $port_proto == TCP )
{
$url_port_type = "tcp";
$url_layer4 = "TCP";
}
if ( $port_proto == UDP )
{
$url_port_type = "udp";
$url_layer4 = "UDP";
}
$url_param = $url_port_type."_port%5B0%5D%5B0%5D=+".
"&".$url_port_type."_port%5B0%5D%5B1%5D=".$port_type_sql.
"&".$url_port_type."_port%5B0%5D%5B2%5D=%3D".
"&".$url_port_type."_port%5B0%5D%5B3%5D=".$url_port.
"&".$url_port_type."_port%5B0%5D%5B4%5D=+".
"&".$url_port_type."_port%5B0%5D%5B5%5D=+".
"&".$url_port_type."_port_cnt=1".
"&layer4=".$url_layer4.
"&sort_order=".$sort_order.
"&num_result_rows=-1&current_view=-1";
qroPrintEntryHeader($i);
/* Generating checkbox value -- nikns */
if ($proto == TCP)
$tmp_rowid = TCP ."_";
else if ($proto == UDP)
$tmp_rowid = UDP ."_";
else
$tmp_rowid = -1 ."_";
($port_type == SOURCE_PORT) ? ($tmp_rowid .= SOURCE_PORT) : ($tmp_rowid .= DEST_PORT);
$tmp_rowid .= "_" . $myrow[0];
echo ' <TD><INPUT TYPE="checkbox" NAME="action_chk_lst['.$i.']" VALUE="'.$tmp_rowid.'"></TD>';
echo ' <INPUT TYPE="hidden" NAME="action_lst['.$i.']" VALUE="'.$tmp_rowid.'">';
qroPrintEntry($currentPort);
qroPrintEntry('<A HREF="base_stat_sensor.php?'.$url_param.'">'.$num_sensors.'</A>');
qroPrintEntry('<A HREF="base_qry_main.php?'.$url_param.'&new=1&submit='._QUERYDBP.
'&sort_order=sig_a">'.$num_events.'</A>');
qroPrintEntry('<A HREF="base_stat_alerts.php?'.$url_param.'&sort_order=sig_a">'.
$num_sig.'</A>');
qroPrintEntry('<A HREF="base_stat_uaddr.php?'.$url_param.'&addr_type=1'.
'&sort_order=addr_a">'.$num_sip);
qroPrintEntry('<A HREF="base_stat_uaddr.php?'.$url_param.'&addr_type=2'.
'&sort_order=addr_a">'.$num_dip);
qroPrintEntry($first_time);
qroPrintEntry($last_time);
qroPrintEntryFooter();
++$i;
}
$result->baseFreeRows();
$qro->PrintFooter();
$qs->PrintBrowseButtons();
$qs->PrintAlertActionButtons();
$qs->SaveState();
ExportHTTPVar("port_type", $port_type);
ExportHTTPVar("proto", $proto);
ExportHTTPVar("sort_order", $sort_order);
echo "\n</FORM>\n";
PrintBASESubFooter();
$et->Mark("Get Query Elements");
$et->PrintTiming();
echo "</body>\r\n</html>";
?>