-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase_db_common.php
211 lines (173 loc) · 5.98 KB
/
base_db_common.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
<?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 <[email protected]>, <[email protected]>
//
// Purpose: database schema manipulation
//
// Author(s): Nathan Gibbs
// Kevin Johnson
function createDBIndex($db, $table, $field, $index_name)
{
$sql = 'CREATE INDEX '.$index_name.' ON '.$table.' ('.$field.')';
$db->baseExecute($sql, -1, -1, false);
if ( $db->baseErrorMessage() != "" )
ErrorMessage(_ERRDBINDEXCREATE." '".$field."' : ".$db->baseErrorMessage());
else
ErrorMessage(_DBINDEXCREATE." '".$field."'");
}
function verify_db($db, $alert_dbname, $alert_host){
$msg = '<B>'._ERRSNORTVER1.' '.$alert_dbname.'@'.$alert_host.' '._ERRSNORTVER2.'</B>';
$sql = "SELECT ip_src FROM iphdr";
$result = $db->baseExecute($sql, 0, 1, false);
if ( $db->baseErrorMessage() != "" )
return $msg.'<BR>'.$db->baseErrorMessage().'
<P>'._ERRSNORTVER;
$base_table = array ("acid_ag",
"acid_ag_alert",
"acid_ip_cache",
"acid_event",
"base_users",
"base_roles");
for ( $i = 0; $i < count($base_table); $i++)
{
if ( !$db->baseTableExists($base_table[$i]) )
return $msg.'. <P>'._ERRDBSTRUCT1.'
(table: '.$base_table[$i].')'._ERRDBSTRUCT2;
}
return '';
}
function verify_php_build( $DBtype ){
// Checks that the necessary libraries are built into PHP.
$Ret = ''; // Default return Value.
$PHPVer = GetPHPSV(); // Check PHP version >= 4.0.4
// @codeCoverageIgnoreStart
if(
$PHPVer[0] < 4
|| ( $PHPVer[0] == 4 && $PHPVer[1] == 0 && $PHPVer[2] < 4 )
){ // Only executes on PHP < 4.0.4. Cannot test in CI.
return '<b>' . _ERRPHPERROR1 . '</b>: ' . _ERRVERSION
. ' ' . phpversion() . ' ' . _ERRPHPERROR2;
}
// @codeCoverageIgnoreEnd
if( $DBtype == 'mysql' || $DBtype == 'mysqlt' || $DBtype == 'maxsql' ){
// On PHP 5.5+, use mysqli ADODB driver & gracefully deprecate the
// mysql, mysqlt & maxsql drivers.
if( $PHPVer[0] > 5 || ( $PHPVer[0] == 5 && $PHPVer[1] > 4) ){
if( !(function_exists('mysqli_connect')) ){
$Ret = returnBuildError('MySQLi', '--with-mysqli');
$Ret .= NLI('Unable to read ALERT DB.<br/>'); // TD This.
}
}else{
if( !(function_exists("mysql_connect")) ){
return _ERRPHPMYSQLSUP;
}
}
}elseif( $DBtype == "postgres" ){
if( !(function_exists("pg_connect")) ){
return _ERRPHPPOSTGRESSUP;
}
}elseif( $DBtype == "mssql" ){
if( !(function_exists("mssql_connect")) ){
return _ERRPHPMSSQLSUP;
}
}elseif( $DBtype == "oci8" ){
if( !(function_exists("ocilogon")) ){
return _ERRPHPORACLESUP;
}
// Additional DB Support would tie in here.
}else{
return '<b>' . _ERRSQLDBTYPE . '</b>: ' . _ERRSQLDBTYPEINFO1
. "'$DBtype'." . _ERRSQLDBTYPEINFO2;
}
return $Ret;
}
/* ******************* DB Query Routines ************************************ */
function EventsByAddr($db, $i, $ip)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT signature FROM acid_event (ip_src='$ip32') OR (ip_dst='$ip32')");
while ( $myrow = $result->baseFetchRow() )
$sig[] = $myrow[0];
$result->baseFreeRows();
return $sig[$i];
}
function EventCntByAddr($db, $ip)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT count(ip_src) FROM acid_event WHERE ".
"(ip_src='$ip32') OR (ip_dst='$ip32')");
$myrow = $result->baseFetchRow();
$event_cnt = $myrow[0];
$result->baseFreeRows();
return $event_cnt;
}
function UniqueEventsByAddr($db, $i, $ip)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT DISTINCT signature FROM acid_event WHERE ".
"(ip_src='$ip32') OR (ip_dst='$ip32')");
while ($myrow = $result->baseFetchRow())
$sig[] = $myrow[0];
$result->baseFreeRows();
return $sig[$i];
}
function UniqueEventCntByAddr($db, $ip)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT DISTINCT signature FROM acid_event WHERE ".
"(ip_src='$ip32') OR (ip_dst='$ip32')");
while ($myrow = $result->baseFetchRow())
$sig[] = $myrow[0];
$result->baseFreeRows();
return $sig;
}
function UniqueEventTotalsByAddr($db, $ip, $current_event)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT count(signature) FROM acid_event WHERE ".
"( (ip_src='$ip32' OR ip_dst='$ip32') AND signature='$current_event')");
$myrow = $result->baseFetchRow();
$tmp = $myrow[0];
$result->baseFreeRows();
return $tmp;
}
function UniqueSensorCntByAddr($db, $ip, $current_event)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT DISTINCT sid FROM acid_event WHERE ".
"( (ip_src='$ip32' OR ip_dst='$ip32') AND signature='$current_event')");
while ($myrow = $result->baseFetchRow())
$sid[] = $myrow[0];
$count = count($sid);
$result->baseFreeRows();
return $count;
}
function StartTimeForUniqueEventByAddr($db, $ip, $current_event)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT min(timestamp) FROM acid_event WHERE ".
"((ip_src='$ip32' OR ip_dst='$ip32') AND signature = '$current_event');");
$myrow = $result->baseFetchRow();
$start_time = $myrow[0];
$result->baseFreeRows();
return $start_time;
}
function StopTimeForUniqueEventByAddr($db, $ip, $current_event)
{
$ip32 = baseIP2long($ip);
$result = $db->baseExecute("SELECT max(timestamp) FROM acid_event WHERE ".
"((ip_src='$ip32' OR ip_dst='$ip32') AND signature = '$current_event');");
$myrow = $result->baseFetchRow();
$stop_time = $myrow[0];
$result->baseFreeRows();
return $stop_time;
}
?>