File: acid_db_common.php

package info (click to toggle)
acidlab 0.9.6b20-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 696 kB
  • ctags: 1,462
  • sloc: php: 9,625; sql: 140
file content (223 lines) | stat: -rw-r--r-- 6,910 bytes parent folder | download
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
<?php
/*  
 * Analysis Console for Incident Databases (ACID)
 *
 * Author: Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
 *
 * Copyright (C) 2000 Carnegie Mellon University
 * (see the file 'acid_main.php' for license details)
 *
 * Purpose:   
 *
 */


function createDBIndex($db, $table, $field, $index_name)
{
   $sql = 'CREATE INDEX '.$index_name.' ON '.$table.' ('.$field.')';

   $db->acidExecute($sql, -1, -1, false);
   if ( $db->acidErrorMessage() != "" )
      ErrorMessage("Unable to CREATE INDEX for '".$field."' : ".$db->acidErrorMessage());
   else
      ErrorMessage("Successfully created INDEX for '".$field."'");
}

function verify_db($db, $alert_dbname, $alert_host)
{
  $msg = '<B>The underlying database '.$alert_dbname.'@'.$alert_host.' appears to be incomplete/invalid</B>';

  $sql = "SELECT ip_src FROM iphdr";
  $result = $db->acidExecute($sql, 0, 1, false);
  if ( $db->acidErrorMessage() != "" )
     return $msg.'<BR>'.$db->acidErrorMessage().'
            <P>It might be an older version.  Only alert databases created by Snort 1.7-beta0
            or later are supported';

  $acid_table = array ("acid_ag",
                       "acid_ag_alert",
                       "acid_ip_cache",
                       "acid_event");

  for ( $i = 0; $i < count($acid_table); $i++)
  { 
     if ( !$db->acidTableExists($acid_table[$i]) )
       return $msg.'.  <P>The database version is valid, but the ACID DB structure 
              (table: '.$acid_table[$i].')
              is not present. Use the <A HREF="acid_db_setup.php">Setup page</A> to configure
              and optimize the DB.';
  }
  
  return "";
}

function verify_php_build($DBtype)
/* Checks whether the necessary libraries is built into PHP */
{
  /* Check PHP version >= 4.0.4 */
  $current_php_version = phpversion();
  $version = explode(".", $current_php_version);

  /* account for x.x.xXX subversions possibly having text like 4.0.4pl1 */
  if ( is_numeric(substr($version[2], 1, 1)) ) 
     $version[2] = substr($version[2], 0, 2);
  else
     $version[2] = substr($version[2], 0, 1);

  /* only version PHP 4.0.4+ or 4.1+.* are valid */ 
  if ( !( ($version[0] >= 4) && ( ( ($version[1] == 0) && ($version[2] >= 4) ) ||
                                    ($version[1] > 0)  ) ) )
  {
     return "<FONT COLOR=\"#FF0000\">PHP ERROR</FONT>: ".
            "<B>Incompatible version</B>: <FONT>Version ".$current_php_version.
            " of PHP is too old.  Please upgrade to version 4.0.4 or later</FONT>";
  }

  if ( ($DBtype == "mysql") || ($DBtype == "mysqlt") )
  {
     if ( !(function_exists("mysql_connect")) )
     {
        return "<FONT COLOR=\"#FF0000\">PHP ERROR</FONT>: ".
               "<B>PHP build incomplete</B>: <FONT>the prerequisite MySQL support required to ".
               "read the alert database was not built into PHP.  ".
               "Please recompile PHP with the necessary library (<CODE>--with-mysql</CODE>)</FONT>";
     }
  }
  else if ( $DBtype == "postgres" )
  {
     if ( !(function_exists("pg_connect")) )
     {
        return "<FONT COLOR=\"#FF0000\">PHP ERROR</FONT>: ".
               "<B>PHP build incomplete</B>: <FONT>the prerequisite PostgreSQL support required to ".
               "read the alert database was not built into PHP.  ".
               "Please recompile PHP with the necessary library (<CODE>--with-pgsql</CODE>)</FONT>";
     }
  }
  else if ( $DBtype == "mssql" )
  {
      if ( !(function_exists("mssql_connect")) )
       {
            return "<FONT COLOR=\"#FF0000\">PHP ERROR</FONT>: ".
                   "<B>PHP build incomplete</B>: <FONT>the prerequisite MS SQL Server support required to ".
                   "read the alert database was not built into PHP.  ".
                   "Please recompile PHP with the necessary library (<CODE>--enable-mssql</CODE>)</FONT>";
       }
   }
  else
     return "<B>Unknown Database type specified</B>:  a <CODE>DBtype</CODE> of ".
            " '$DBtype' was specified";

  return "";
}

/* ******************* DB Query Routines ************************************ */
function EventsByAddr($db, $i, $ip)
{
   $ip32 = acidIP2long($ip);

   $result = $db->acidExecute("SELECT signature FROM acid_event (ip_src=$ip32) OR (ip_dst=$ip32)");

   while ( $myrow = $result->acidFetchRow() ) 
      $sig[] = $myrow[0];

   $result->acidFreeRows();

   return $sig[$i];
}

function EventCntByAddr($db, $ip)
{
   $ip32 = acidIP2long($ip);

   $result = $db->acidExecute("SELECT count(ip_src) FROM acid_event WHERE ".
                  "(ip_src=$ip32) OR (ip_dst=$ip32)");

   $myrow = $result->acidFetchRow();
   $event_cnt = $myrow[0];
   $result->acidFreeRows();

   return $event_cnt;
}

function UniqueEventsByAddr($db, $i, $ip)
{
     $ip32 = acidIP2long($ip);
     $result = $db->acidExecute("SELECT DISTINCT signature FROM acid_event WHERE ".
                  "(ip_src=$ip32) OR (ip_dst=$ip32)");

   while ($myrow = $result->acidFetchRow())
      $sig[] = $myrow[0];

   $result->acidFreeRow();

   return $sig[$i];
}

function UniqueEventCntByAddr($db, $ip)
{
     $ip32 = acidIP2long($ip);
     $result = $db->acidExecute("SELECT DISTINCT signature FROM acid_event WHERE ".
                  "(ip_src=$ip32) OR (ip_dst=$ip32)");

   while ($myrow = $result->acidFetchRow())
      $sig[] = $myrow[0];

   $result->acidFreeRows();

   return $sig;
}

function UniqueEventTotalsByAddr($db, $ip, $current_event)
{
   $ip32 = acidIP2long($ip);
   $result = $db->acidExecute("SELECT count(signature) FROM acid_event WHERE ".
                  "( (ip_src=$ip32 OR ip_dst=$ip32) AND signature='$current_event')"); 

   $myrow = $result->acidFetchRow();
   $tmp = $myrow[0];
   
   $result->acidFreeRows();
   return $tmp;
}

function UniqueSensorCntByAddr($db, $ip, $current_event)
{
   $ip32 = acidIP2long($ip);
   $result = $db->acidExecute("SELECT DISTINCT sid FROM acid_event WHERE ".
                  "( (ip_src=$ip32 OR ip_dst=$ip32) AND signature='$current_event')");

   while ($myrow = $result->acidFetchRow())
      $sid[] = $myrow[0];

   $count = count($sid);
   $result->acidFreeRows();

   return $count;
}

function StartTimeForUniqueEventByAddr($db, $ip, $current_event)
{
   $ip32 = acidIP2long($ip);
   $result = $db->acidExecute("SELECT min(timestamp) FROM acid_event WHERE ".
                  "((ip_src=$ip32 OR ip_dst=$ip32) AND signature = '$current_event');");
   $myrow = $result->acidFetchRow();
   $start_time = $myrow[0];

   $result->acidFreeRows();
   return $start_time;
}

function StopTimeForUniqueEventByAddr($db, $ip, $current_event)
{
   $ip32 = acidIP2long($ip);
   $result = $db->acidExecute("SELECT max(timestamp) FROM acid_event WHERE ".
                  "((ip_src=$ip32 OR ip_dst=$ip32) AND signature = '$current_event');");

   $myrow = $result->acidFetchRow();
   $stop_time = $myrow[0];

   $result->acidFreeRows();
   return $stop_time;
}

?>