• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NathanGibbs3 / BASE / 628

pending completion
628

push

travis-ci-com

NathanGibbs3
Merge branch 'documentation' into devel

3240 of 17613 relevant lines covered (18.4%)

17.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/includes/base_cache.inc.php
1
<?php
2
/*******************************************************************************
3
** Basic Analysis and Security Engine (BASE)
4
** Copyright (C) 2004 BASE Project Team
5
** Copyright (C) 2000 Carnegie Mellon University
6
**
7
** (see the file 'base_main.php' for license details)
8
**
9
** Project Lead: Kevin Johnson <kjohnson@secureideas.net>
10
**                Sean Muller <samwise_diver@users.sourceforge.net>
11
** Built upon work by Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
12
**
13
** Purpose: IP DNS, whois, event cache library   
14
********************************************************************************
15
** Authors:
16
********************************************************************************
17
** Kevin Johnson <kjohnson@secureideas.net
18
**
19
********************************************************************************
20
*/
21
// Ensure the conf file has been loaded. Prevent direct access to this file.
22
defined('_BASE_INC') or die('Accessing this file directly is not allowed.');
23

24
include_once("$BASE_path/base_stat_common.php");
25
include_once("$BASE_path/includes/base_log_error.inc.php");
26

27
function UpdateDNSCache ( $db ){
28
        GLOBAL $debug_mode, $dns_cache_lifetime;
29
        $cnt = 0;
×
30

31
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_src FROM acid_event ".
×
32
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_src ".
33
                                "WHERE ipc_fqdn IS NULL");
34

35
  while ( ($row = $ip_result->baseFetchRow()) != "")
×
36
  {
37
     if ( $debug_mode > 0 )  
×
38
        echo $row[0]." - ".baseLong2IP($row[0])."<BR>";
×
39
     baseGetHostByAddr(baseLong2IP($row[0]), $db, $dns_cache_lifetime);
×
40
     ++$cnt;
×
41
  }
42
  $ip_result->baseFreeRows();
×
43

44
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_dst FROM acid_event ".
×
45
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_dst ".
46
                                "WHERE ipc_fqdn IS NULL");
47
  while ( ($row = $ip_result->baseFetchRow()) != "")
×
48
  {
49
     if ( $debug_mode > 0 )  
×
50
        echo $row[0]." - ".baseLong2IP($row[0])."<BR>";  
×
51
     baseGetHostByAddr(baseLong2IP($row[0]), $db, $dns_cache_lifetime);
×
52
     ++$cnt;
×
53
  }
54
  $ip_result->baseFreeRows();
×
55

56
  ErrorMessage(_ADDED.$cnt._HOSTNAMESDNS);
×
57
}
58

59
function UpdateWhoisCache($db)
60
{
61
  GLOBAL $debug_mode, $whois_cache_lifetime;
62

63
  $cnt = 0;
×
64

65
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_src FROM acid_event ".
×
66
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_src ".
67
                                "WHERE ipc_whois IS NULL");
68

69
  while ( ($row = $ip_result->baseFetchRow()) != "")
×
70
  {
71
     if ( $debug_mode > 0 )  echo $row[0]." - ".baseLong2IP($row[0])."<BR>";
×
72
     baseGetWhois(baseLong2IP($row[0]), $db, $whois_cache_lifetime);
×
73
     ++$cnt;
×
74
  }
75
  $ip_result->baseFreeRows();
×
76

77
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_dst FROM acid_event ".
×
78
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_dst ".
79
                                "WHERE ipc_whois IS NULL");
80

81
  while ( ($row = $ip_result->baseFetchRow()) != "")
×
82
  {
83
     if ( $debug_mode > 0 )  echo $row[0]." - ".baseLong2IP($row[0])."<BR>";  
×
84
     baseGetWhois(baseLong2IP($row[0]), $db, $whois_cache_lifetime);
×
85
     ++$cnt;
×
86
  }
87
  $ip_result->baseFreeRows();
×
88

89
  ErrorMessage(_ADDED.$cnt._HOSTNAMESWHOIS);
×
90
}
91

92
function CacheAlert($sid, $cid, $db)
93
{
94
  $signature = $timestamp = $ip_src = $ip_dst = null;
×
95
  $ip_proto = $layer4_sport = $layer4_dport = $sig_name = null;
×
96
  $sig_class_id = $sig_priority = null;
×
97

98
  $sql = "SELECT signature, timestamp, ip_src, ip_dst, ip_proto FROM event ".
99
         "LEFT JOIN iphdr ON (event.sid=iphdr.sid AND event.cid = iphdr.cid) ".
100
         "WHERE (event.sid='".$sid."' AND event.cid='".$cid."') ORDER BY event.cid";
101

102
  $result = $db->baseExecute($sql);
×
103

104
  $row = $result->baseFetchRow();
×
105
  if ( $row )
106
  {
107
     $signature = $row[0];
×
108
     $timestamp = $row[1];
×
109
     $ip_src    = $row[2];
×
110
     $ip_dst    = $row[3];
×
111
     $ip_proto  = $row[4];
×
112
     $result->baseFreeRows();
×
113

114
     if ( $ip_proto == TCP )
×
115
     {
116
        $result = $db->baseExecute("SELECT tcp_sport, tcp_dport FROM
×
117
                                    tcphdr WHERE sid='".$sid."' AND cid='".$cid."'");
118
        $row = $result->baseFetchRow();
×
119
        if ( $row )
120
        {
121
           $layer4_sport = $row[0];
×
122
           $layer4_dport = $row[1];
×
123
           $result->baseFreeRows();
×
124
        }
125
     }
126

127
     else if ( $ip_proto == UDP )
×
128
     {
129
        $result = $db->baseExecute("SELECT udp_sport, udp_dport FROM
×
130
                                    udphdr WHERE sid='".$sid."' AND cid='".$cid."'");
131
        $row = $result->baseFetchRow();
×
132
        if ( $row )
133
        {
134
           $layer4_sport = $row[0];
×
135
           $layer4_dport = $row[1];
×
136
           $result->baseFreeRows();
×
137
        }
138
     }
139

140
     if ( $db->baseGetDBversion() >= 100 )
×
141
     {
142
        if ( $db->baseGetDBversion() >= 103 )
×
143
           $result = $db->baseExecute("SELECT sig_name, sig_class_id, sig_priority ".
×
144
                                      " FROM signature ".
145
                                      "WHERE sig_id = '".$signature."'");
146
        else
147
           $result = $db->baseExecute("SELECT sig_name FROM signature ".
×
148
                                      "WHERE sig_id = '".$signature."'");
149
        $row = $result->baseFetchRow();
×
150
        if ( $row )
151
        {
152
           $sig_name = $row[0];
×
153
           if ( $db->baseGetDBversion() >= 103 )
×
154
           {
155
              $sig_class_id = $row[1];
×
156
              $sig_priority = $row[2];
×
157
           }
158
           $result->baseFreeRows();
×
159
        } 
160
     }
161
  }
162
  else
163
  {
164
    ErrorMessage(_ERRCACHENULL);
×
165
    echo "<PRE>".$sql."</PRE>";
×
166
  }
167

168
  /* There can be events without certain attributes */
169
  if ($sig_priority=='') $sig_priority='NULL';
×
170
  if ($ip_src=='') $ip_src='NULL';
×
171
  if ($ip_dst=='') $ip_dst='NULL';
×
172
  if ($ip_proto=='') $ip_proto='NULL';
×
173
  if ($layer4_sport=='') $layer4_sport='NULL';
×
174
  if ($layer4_dport=='') $layer4_dport='NULL';
×
175
  if ($sig_class_id == '') $sig_class_id = 'NULL';
×
176
  if ($sig_priority == '') $sig_priority = 'NULL';
×
177

178
  if ( $db->baseGetDBversion() >= 100 ) {
×
179
      $sql = "INSERT INTO acid_event (sid, cid, signature, sig_name, sig_class_id, sig_priority, ";
×
180
      $sql.= "timestamp, ip_src, ip_dst, ip_proto, layer4_sport, layer4_dport) ";
×
181
      $sql.= "VALUES ($sid, $cid, $signature, '" . addslashes($sig_name) . "', $sig_class_id, $sig_priority,";
×
182
      $sql.= "'$timestamp', $ip_src, $ip_dst, $ip_proto, $layer4_sport, $layer4_dport)";
×
183
  } else {
184
      $sql = "INSERT INTO acid_event (sid, cid, signature, timestamp, ip_src, ";
×
185
      $sql.= "ip_dst, ip_proto, layer4_sport,layer4_dport) ";
×
186
      $sql.= "VALUES ($sid, $cid, '$signature', '$timestamp', $ip_src, $ip_dst, ";
×
187
      $sql.= "$ip_proto, $layer4_sport, $layer4_dport)";
×
188
  }
189

190
  $db->baseExecute($sql); 
×
191

192
  if ( $db->baseErrorMessage() != "" )
×
193
     return 0;
×
194
  else 
195
     return 1;
×
196
}
197

198
// Caches all alerts for sensor $sid newer than the event $cid
199
function CacheSensor( $sid, $cid, $db ){
200
        GLOBAL $debug_mode;
201
        $EMPfx = __FUNCTION__ . ': '; // Error Message Prefix.
×
202
        $schema_specific = array(2);
×
203
        $schema_specific[0] = '';
×
204
        $schema_specific[1] = '';
×
205
        $schema_specific[2] = '';
×
206
        if ( $db->baseGetDBversion() >= 100 ){
×
207
     $schema_specific[1] = ", sig_name"; 
×
208
     $schema_specific[2] = " INNER JOIN signature ON (signature = signature.sig_id) ";
×
209
  }
210

211
  if ( $db->baseGetDBversion() >= 103 )
×
212
  {
213
     $schema_specific[0] = $schema_specific[0].", sig_priority, sig_class_id ";
×
214
     $schema_specific[1] = $schema_specific[1].", sig_priority, sig_class_id "; 
×
215
     $schema_specific[2] = $schema_specific[2]."";
×
216
  }
217

218
  if ( $db->baseGetDBversion() < 100 )
×
219
     $schema_specific[1] = $schema_specific[1].", signature ";
×
220

221
  $update_sql = array(4);
×
222

223
        // Preprocessor events only.
224
        // The original "(sig_name LIKE '(spp_%')" is too limited. Cf.
225
        // /usr/local/src/snort-2.8.3.1_unpatched/etc/gen-msg.map
226
        // /usr/local/src/snort-2.8.3.1_unpatched/src/generators.h
227
        // Currently I have included all the names that I have found in 
228
        // these files.
229
        // Note: Do always add '%' in LIKE-statements. Otherwise the entries
230
        //       won't match.
231
  if ( $db->baseGetDBversion() >= 100 ) {
×
232
    $schema_specific[3] = " ( " . 
×
233
                          "(sig_name LIKE '(spp_%') OR " . 
234
                          "(sig_name LIKE '(spo_%') OR " . 
235
                          "(sig_name LIKE '(snort_decoder)%') OR " .
236
                          "(sig_name LIKE '(http_decode)%') OR " . 
237
                          "(sig_name LIKE '(http_inspect)%') OR " . 
238
                          "(sig_name LIKE '(portscan)%') OR " . 
239
                          "(sig_name LIKE '(flow-portscan)%') OR " . 
240
                          "(sig_name LIKE '(frag3)%') OR " . 
241
                          "(sig_name LIKE '(smtp)%') OR " .
242
                          "(sig_name LIKE '(ftp_pp)%') OR " . 
243
                          "(sig_name LIKE '(telnet_pp)%') OR " .
244
                          "(sig_name LIKE '(ssh)%') OR " .
245
                          "(sig_name LIKE '(stream5)%') OR " . 
246
                          "(sig_name LIKE '(dcerpc)%') OR " .
247
                          "(sig_name LIKE '(dns)%') OR " . 
248
                          "(sig_name LIKE '(ppm)%') " .
249
                          " ) ";
250
  }
251
  else {
252
    $schema_specific[3] = " (signature LIKE '(spp_%') ";
×
253
  }
254

255
  
256
  /* TCP events */
257
  if( $db->DB_type == 'oci8' ) {
×
258
  $update_sql[0] =
×
259
    "INSERT INTO acid_event (sid,cid,signature,timestamp,
260
                             ip_src,ip_dst,ip_proto,
261
                             layer4_sport,layer4_dport,
262
                             sig_name".
263
                             $schema_specific[0].")
×
264
     SELECT a.sid as sid, a.cid as cid, a.signature, a.timestamp,
265
            b.ip_src, ip_dst, ip_proto,
266
            tcp_sport as layer4_sport, tcp_dport as layer4_dport".
267
            $schema_specific[1]."
×
268
    FROM event a
269
    ".$schema_specific[2]." 
×
270
    INNER JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid) 
271
    LEFT JOIN tcphdr c ON (a.sid=c.sid AND a.cid=c.cid)
272
    WHERE (a.sid = $sid AND a.cid > $cid) AND ip_proto = 6
273
    AND ( NOT ".$schema_specific[3].")";
×
274
  }
275
  else {
276
  $update_sql[0] =
×
277
    "INSERT INTO acid_event (sid,cid,signature,timestamp,
278
                             ip_src,ip_dst,ip_proto,
279
                             layer4_sport,layer4_dport,
280
                             sig_name".
281
                             $schema_specific[0].")
×
282
     SELECT event.sid as sid, event.cid as cid, signature, timestamp, 
283
            ip_src, ip_dst, ip_proto,
284
            tcp_sport as layer4_sport, tcp_dport as layer4_dport".
285
            $schema_specific[1]."
×
286
    FROM event
287
    ".$schema_specific[2]." 
×
288
    INNER JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid) 
289
    LEFT JOIN tcphdr ON (event.sid=tcphdr.sid AND event.cid=tcphdr.cid)
290
    WHERE (event.sid = $sid AND event.cid > $cid) AND ip_proto = 6
291
    AND ( NOT ".$schema_specific[3].")";
×
292
  }
293

294
  /* UDP events */
295
  if( $db->DB_type == 'oci8' ) {
×
296
  $update_sql[1] = 
×
297
    "INSERT INTO acid_event (sid,cid,signature,timestamp,
298
                             ip_src,ip_dst,ip_proto,
299
                             layer4_sport,layer4_dport,
300
                             sig_name".
301
                             $schema_specific[0].")
×
302
     SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
303
            ip_src, ip_dst, ip_proto,
304
            udp_sport as layer4_sport, udp_dport as layer4_dport".
305
            $schema_specific[1]."
×
306
     FROM event a
307
     ".$schema_specific[2]."
×
308
     INNER JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
309
     LEFT JOIN udphdr c ON (a.sid=c.sid AND a.cid=c.cid)
310
     WHERE (a.sid = $sid AND a.cid > $cid) AND ip_proto = 17
311
     AND ( NOT ".$schema_specific[3].")";
×
312
  }
313
  else {
314
  $update_sql[1] = 
×
315
    "INSERT INTO acid_event (sid,cid,signature,timestamp,
316
                             ip_src,ip_dst,ip_proto,
317
                             layer4_sport,layer4_dport,
318
                             sig_name".
319
                             $schema_specific[0].")
×
320
     SELECT event.sid as sid, event.cid as cid, signature, timestamp,
321
            ip_src, ip_dst, ip_proto,
322
            udp_sport as layer4_sport, udp_dport as layer4_dport".
323
            $schema_specific[1]."
×
324
     FROM event
325
     ".$schema_specific[2]."
×
326
     INNER JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
327
     LEFT JOIN udphdr ON (event.sid=udphdr.sid AND event.cid=udphdr.cid)
328
     WHERE (event.sid = $sid AND event.cid > $cid) AND ip_proto = 17
329
     AND ( NOT ".$schema_specific[3].")";
×
330
  }
331

332
  /* ICMP events */
333
  if( $db->DB_type == 'oci8' ) {
×
334
    $update_sql[2] = 
×
335
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
336
                              ip_src,ip_dst,ip_proto,
337
                              sig_name".
338
                              $schema_specific[0].")
×
339
      SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
340
             ip_src, ip_dst, ip_proto".
341
             $schema_specific[1]."
×
342
      FROM event a
343
      ".$schema_specific[2]."
×
344
      INNER JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
345
      LEFT JOIN icmphdr c ON (a.sid=c.sid AND a.cid=c.cid)
346
      WHERE (a.sid = $sid AND a.cid > $cid) and ip_proto = 1
347
      AND ( NOT ".$schema_specific[3].")";
×
348
  }
349
  else 
350
  {
351
    $update_sql[2] = 
×
352
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
353
                              ip_src,ip_dst,ip_proto,
354
                              sig_name".
355
                              $schema_specific[0].")
×
356
      SELECT event.sid as sid, event.cid as cid, signature, timestamp,
357
             ip_src, ip_dst, ip_proto".
358
             $schema_specific[1]."
×
359
      FROM event
360
      ".$schema_specific[2]."
×
361
      INNER JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
362
      LEFT JOIN icmphdr ON (event.sid=icmphdr.sid AND event.cid=icmphdr.cid)
363
      WHERE (event.sid = $sid AND event.cid > $cid) and ip_proto = 1
364
      AND ( NOT ".$schema_specific[3].")";
×
365
  }
366

367
  /* IP based protocols that are neither ICMP nor TCP nor UDP nor
368
     preprocessor generated */
369
  if( $db->DB_type == 'oci8' ) {
×
370
    $update_sql[3] = 
×
371
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
372
                              ip_src,ip_dst,ip_proto,
373
                              sig_name".
374
                              $schema_specific[0].")
×
375
      SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
376
             ip_src, ip_dst, ip_proto".
377
             $schema_specific[1]."
×
378
      FROM event a
379
      ".$schema_specific[2]."
×
380
      LEFT JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
381
      WHERE (NOT (ip_proto IN (1, 6, 17))) AND ".
382
            " ( NOT ".$schema_specific[3].") AND
×
383
            (a.sid = $sid AND a.cid > $cid)";
384
  }
385
  else 
386
  {
387
    $update_sql[3] = 
×
388
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
389
                              ip_src,ip_dst,ip_proto,
390
                              sig_name".
391
                              $schema_specific[0].")
×
392
      SELECT event.sid as sid, event.cid as cid, signature, timestamp,
393
             ip_src, ip_dst, ip_proto".
394
             $schema_specific[1]."
×
395
      FROM event
396
      ".$schema_specific[2]."
×
397
      LEFT JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
398
      WHERE (NOT (ip_proto IN (1, 6, 17))) AND ".
399
            " ( NOT ".$schema_specific[3].") AND
×
400
            (event.sid = $sid AND event.cid > $cid)";
401
  }
402

403

404

405
  /* Event only -- pre-processor alerts */
406
  if( $db->DB_type == 'oci8' ) {
×
407
     $update_sql[4] = 
×
408
       "INSERT INTO acid_event (sid,cid,signature,timestamp,
409
                                ip_src,ip_dst,ip_proto,
410
                                sig_name".
411
                                $schema_specific[0].")
×
412
        SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
413
               ip_src, ip_dst, ip_proto".
414
               $schema_specific[1]."
×
415
        FROM event a
416
        ".$schema_specific[2]."
×
417
        LEFT JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
418
        WHERE ".$schema_specific[3]." AND 
×
419
        (a.sid = $sid AND a.cid > $cid)";
420
  }
421
  else 
422
  {
423
    $update_sql[4] = 
×
424
       "INSERT INTO acid_event (sid,cid,signature,timestamp,
425
                                ip_src,ip_dst,ip_proto,
426
                                sig_name".
427
                                $schema_specific[0].")
×
428
        SELECT event.sid as sid, event.cid as cid, signature, timestamp,
429
               ip_src, ip_dst, ip_proto".
430
               $schema_specific[1]."
×
431
        FROM event
432
        ".$schema_specific[2]."
×
433
        LEFT JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
434
        WHERE ".$schema_specific[3]." AND 
×
435
        (event.sid = $sid AND event.cid > $cid)";
436
  }
437

438

439
        // Some checks for unexpected errors
440
        $mystr = '';
×
441
  $update_cnt = count($update_sql);
×
442
  if (!isset($update_cnt)) 
×
443
  {
444
    $mystr = "\$update_cnt has not been set. sid = $sid, cid = $cid";
×
445
  }
446
  else if ((integer)$update_cnt == 0) 
×
447
  {
448
    $mystr = "\$update_cnt = 0 with sid = $sid, cid = $cid";
×
449
  }
450
  else if (!isset($update_sql[0]) && !isset($update_sql[1]) && !isset($update_sql[2]) && !isset($update_sql[3])) 
×
451
  {
452
    $mystr = "\$update_sql[] has only empty elements with sid = $sid, cid = $cid";
×
453
  } 
454
  else if ($update_sql[0] == "" && $update_sql[1] == "" && $update_sql[2] == "" && $update_sql[3] == "") 
×
455
  {
456
    $mystr = "\$update_sql[] has only empty elements with sid = $sid, cid = $cid";
×
457
  }
458

459
        if ( LoadedString($mystr) ){
×
460
                NLIO($EMPfx . "WARNING: $mystr<br/>");
×
461
        }
462
        for ( $i = 0; $i < $update_cnt; $i++ ){ // Commit all SQL commands.
×
463
                if ($debug_mode > 1 ){
×
464
                        $mystr = $EMPfx . "$update_sql[$i]<br/>";
×
465
                        NLIO($mystr);
×
466
                }
467
                $db->baseExecute($update_sql[$i]);
×
468

469
    if ( $db->baseErrorMessage() != "" )
×
470
       ErrorMessage(_ERRCACHEERROR." ["._SENSOR." #$sid]["._EVENTTYPE." $i]".
×
471
                      " "._ERRCACHEUPDATE);
472

473
        }
474
}
475

476
// This is an auxiliary function for problems with updating acid_event
477
function dump_missing_events( $db, $sid, $start_cid, $end_cid ){
478
        GLOBAL $debug_mode, $archive_exists, $DBlib_path, $DBtype,
479
        $archive_dbname, $archive_host, $archive_port, $archive_user,
480
        $archive_password;
481

482
  for ($n = (integer)$start_cid; $n <= (integer)$end_cid; $n++)
×
483
  {
484
    // Does this particular really exist in the event table?
485
    $event_list = $db->baseExecute( "SELECT count(*) FROM event WHERE sid='" . $sid . "' AND cid='" . $n. "'" );
×
486
    $event_row = $event_list->baseFetchRow();
×
487
    $event_value = $event_row[0];
×
488
    $event_list->baseFreeRows();
×
489
    if ((integer)$event_value == 1) {
×
490
      // Yes, it does.
491
      // So let's try and find it in acid event.
492
      $acid_event_list = $db->baseExecute( "SELECT count(*) FROM acid_event WHERE sid='" . $sid . "' AND cid='" . $n. "'" );
×
493
      $acid_event_row = $acid_event_list->baseFetchRow();
×
494
      $acid_event_element = $acid_event_row[0];
×
495
      $acid_event_list->baseFreeRows();
×
496
      if ((integer)$acid_event_element == 0) 
×
497
      {
498
        echo '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: Alert \"$sid - $n\" could NOT be found in acid_event.<BR>";
×
499
      }
500
    }
501
  }
502
}
503

504
function UpdateAlertCache($db, $force = 0 ){
505
        GLOBAL $debug_mode, $archive_exists, $event_cache_auto_update,
506
        $DBlib_path, $DBtype, $archive_dbname, $archive_host, $archive_port,
507
        $archive_user, $archive_password, $et;
508
        if ( $force == 0 && $event_cache_auto_update != 1 ){ // Issue #121 Fix
×
509
                return;
×
510
        }
511
        $batch_sql = '';
×
512
        $batch_cnt = 0;
×
513
        $updated_cache_cnt = 0;
×
514
        $EMPfx = __FUNCTION__ . ': ';
×
515
        // How many sensors do we have?
516
  $number_sensors_lst = $db->baseExecute("SELECT count(*) FROM sensor");
×
517
  $number_sensors_array = $number_sensors_lst->baseFetchRow();
×
518
  $number_sensors_lst->baseFreeRows();
×
519

520
  if (!isset($number_sensors_array))
×
521
  {
522
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array has not been set at all!<BR>";
×
523
    ErrorMessage($mystr);
×
524
    $number_sensors = 0;
×
525
  }
526

527
  if (!is_array($number_sensors_array))
×
528
  {
529
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array is NOT an array!<BR>";
×
530
    ErrorMessage($mystr);
×
531
    
532
    $number_sensors = 0;
×
533
  }
534

535
  if ($number_sensors_array == NULL || $number_sensors_array == "")
×
536
  {
537
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array is either NULL or empty!<BR>";
×
538
    ErrorMessage($mystr);
×
539

540
    $number_sensors = 0;
×
541
  } 
542
  else
543
  {
544
    $number_sensors = $number_sensors_array[0];
×
545
  }
546

547
  if ($debug_mode > 1)
×
548
  {
549
    echo '$number_sensors = ' . $number_sensors . '<BR><BR>';
×
550
  }
551

552
  
553
  if (($debug_mode > 0) && ($number_sensors < 1))
×
554
  {
555
    ErrorMessage("WARNING: Number of sensors = " . $number_sensors);
×
556
    echo '<HR>';
×
557
    echo '<BR>number_sensors_array:<BR>';
×
558
    echo '<PRE>';
×
559
    var_dump($number_sensors_array);
×
560
    echo '</PRE>';
×
561
    echo '<HR>';    
×
562
  }
563
  
564
  
565
  /* Iterate through all sensors in the SENSOR table */
566
  $sensor_lst = $db->baseExecute("SELECT sid FROM sensor");
×
567
  if (($debug_mode > 0) && ($number_sensors < 1))
×
568
  {
569
    echo '<HR>';
×
570
    echo '<BR>sensor_lst:<BR>';
×
571
    echo '<PRE>';
×
572
    var_dump($sensor_lst);
×
573
    echo '</PRE>';
×
574
    echo '<HR>';
×
575
  }
576

577
  for ($n = 0; $n < $number_sensors; $n++)
×
578
  {
579
    $sid_row = $sensor_lst->baseFetchRow();
×
580
    if (!isset($sid_row) || $sid_row == "" || $sid_row == NULL)
×
581
    {
582
      if ($n >= $number_sensors)
×
583
      {
584
        break;
×
585
      }
586
      else
587
      {
588
        next;
×
589
      }
590
    }
591

592
    $sid = $sid_row[0];
×
593
    /* Get highest CID for a given sensor */
594
    $cid_lst = $db->baseExecute("SELECT MAX(cid) FROM event WHERE sid='".$sid."'");
×
595
    $cid_row = $cid_lst->baseFetchRow();
×
596
    if (
597
         (!isset($cid_row)) ||
598
         ($cid_row == NULL) ||
599
         ($cid_row == "")
600
       )
601
    {
602
      /* NULL is in conflict with snort-2.8.0.1/schemas/create_mysql:
603
       * CREATE TABLE event  ( sid         INT      UNSIGNED NOT NULL,
604
                               cid         INT      UNSIGNED NOT NULL,
605
                               signature   INT      UNSIGNED NOT NULL, 
606
                               timestamp            DATETIME NOT NULL,
607
                               PRIMARY KEY (sid,cid),
608
                               INDEX       sig (signature),
609
                               INDEX       time (timestamp));
610
       */
611
      $cid = 0;
×
612
    }
613
    else
614
    {
615
      $cid = $cid_row[0];
×
616
    }
617
    if ( $cid == NULL ) $cid = 0;
×
618

619
    /* Get highest CID for a given sensor in the cache */
620
    $ccid_lst = $db->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'");
×
621
    $ccid_row = $ccid_lst->baseFetchRow();
×
622
    if (
623
         (!isset($ccid_row)) ||
624
         ($ccid_row == NULL) ||
625
         ($ccid_row == "")
626
       )
627
    {
628
      /* NULL is in conflict with base-php4/sql/create_base_tbls_mysql.sql:
629
         CREATE TABLE acid_event   ( sid                 INT UNSIGNED NOT NULL,
630
                                      cid                 INT UNSIGNED NOT NULL,     
631
         (...)
632
       */
633
      $ccid = 0;
×
634
    }
635
    else
636
    {
637
      $ccid = $ccid_row[0];
×
638
    }
639
    if ( $ccid == NULL ) $ccid = 0;
×
640

641
        if ( $debug_mode > 1 ){
×
642
                NLIO(
×
643
                        $EMPfx
644
                        . "sensor #$sid: event.cid = $cid, acid_event.cid = $ccid<br/>"
×
645
                );
646
        }
647

648
    /* if the CID in the cache < the CID in the event table 
649
     *  then there are events which have NOT been added to the cache 
650
     */
651
    if ( $cid > $ccid )
×
652
    {
653
      $expected_addition = (integer)($cid - $ccid);
×
654

655
      $before_cnt = EventCntBySensor($sid, $db);        
×
656
      CacheSensor($sid, $ccid, $db);
×
657
      $updated_cache_cnt += EventCntBySensor($sid, $db) - $before_cnt;
×
658
    }
659

660
    if ($cid_row != NULL)
×
661
    {
662
      $cid_lst->baseFreeRows();
×
663
    }
664

665
    if ($ccid_row != NULL)
×
666
    {
667
      $ccid_lst->baseFreeRows();
×
668
    }
669
 
670
        // BEGIN LOCAL FIX.
671
        // If there's an archive database, and this isn't it, get the MAX(cid)
672
        // from there.
673
        if( $archive_exists == 1 && $db->Role != 'Archive' ){
×
674
      $db2 = NewBASEDBConnection($DBlib_path, $DBtype);
×
675
      $db2->baseConnect($archive_dbname, $archive_host, $archive_port,
×
676
                        $archive_user, $archive_password);
677
      $archive_ccid_lst = $db2->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'"); 
×
678
      $archive_ccid_row = $archive_ccid_lst->baseFetchRow();
×
679
      $archive_ccid = $archive_ccid_row[0];
×
680
      $archive_ccid_lst->baseFreeRows();
×
681
      $db2->baseClose();
×
682
                if( $archive_ccid == NULL ){
×
683
                        $archive_ccid = 0;
×
684
                }
685
        }else{
686
                $archive_ccid = 0;
×
687
        }
688
        if( $archive_ccid > $ccid ){
×
689
                $max_ccid = $archive_ccid;
×
690
        }else{
691
                $max_ccid = $ccid;
×
692
        }
693
        // Fix the last_cid value for the sensor.
694
        $db->baseExecute("UPDATE sensor SET last_cid=$max_ccid WHERE sid=$sid");
×
695
        // END LOCAL FIX.
696
        // Has every alert in the event table found its way into acid_event?
697
    if (isset($ccid)) {
×
698

699
      if ($debug_mode > 1){
×
700
                ErrorMessage($EMPfx ."Sensor no. $sid:",'black',1);
×
701
                ErrorMessage($EMPfx ."Old max cid in acid_event: $ccid",'black',1);
×
702
      }
703

704
      $debug_new_ccid_lst = $db->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'");
×
705
      $debug_new_ccid_row = $debug_new_ccid_lst->baseFetchRow();
×
706
      $debug_new_ccid_lst->baseFreeRows();
×
707
      if (isset($debug_new_ccid_row[0])) 
×
708
      {
709
        $new_ccid = (integer) $debug_new_ccid_row[0];
×
710
      }else{
711
        $new_ccid = 0;
×
712
      }
713

714
      $real_addition = (integer)($new_ccid - (integer)$ccid);
×
715

716
      if ($debug_mode > 1){
×
717
                ErrorMessage($EMPfx ."New max cid in acid_event: $new_ccid",'black',1);
×
718
                ErrorMessage(
×
719
                        $EMPfx ."This many events HAVE been added to acid_event: $real_addition",
×
720
                        'black',1
721
                );
722
                ErrorMessage($EMPfx ."Max cid in event: $cid",'black',1);
×
723
      }
724

725
      if ($real_addition >= 0) 
×
726
      {
727

728
        if (!isset($expected_addition)) 
×
729
        {
730
          $expected_addition = 0;
×
731
        }
732

733
        if ($debug_mode > 1){
×
734
                ErrorMessage(
×
735
                        $EMPfx ."This many events SHOULD have been added to acid_event: $expected_addition",
×
736
                        'black',1
737
                );
738
        }
739

740
        if ($real_addition > 0 && $expected_addition > 0) 
×
741
        {
742
          if ($expected_addition - $real_addition > 0) 
×
743
          {
744
            $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: <BR>" . (integer)((integer)$expected_addition - (integer)$real_addition) . " alerts have NOT found their way into acid_event with sid = $sid<BR>";
×
745
            errorMessage($mystr);
×
746

747

748
            dump_missing_events($db, $sid, $ccid, $new_ccid);
×
749
          }
750
        }
751
      }else{
752
        if ($debug_mode > 1)
×
753
        {
754
          echo "$real_addition is negative. \$new_ccid could not be retrieved. This is apparently not a situation where this sanity check would be applicable.\n";
×
755
        }
756
      }
757

758
      if ($debug_mode > 1) 
×
759
      {
760
        echo "\n---------------<BR>\n";
×
761
      }
762
    }
763
  } // for ($n = 0; $n < $number_sensors; $n++)
764
  
765
  $sensor_lst->baseFreeRows();
×
766
        if ( $updated_cache_cnt != 0 ){
×
767
                $tmp = '';
×
768
                if ( AuthorizedPage('base_main') ){
×
769
                        $tmp = 'yellow';
×
770
                }
771
                ErrorMessage(_ADDED.$updated_cache_cnt._ALERTSCACHE, $tmp, 1);
×
772
        }
773
        if ( is_object($et) ){ // Need to TD this in Issue #11 branch.
×
774
                $et->Mark('Updated ALERT Cache.');
×
775
        }
776
}
777

778
function DropAlertCache($db)
779
{
780
  $db->baseExecute("DELETE FROM acid_event");
×
781
}
782

783
function DropDNSCache($db)
784
{
785
  $db->baseExecute("UPDATE acid_ip_cache SET ipc_fqdn = NULL, ipc_dns_timestamp = NULL");
×
786
}
787

788
function DropWhoisCache($db)
789
{
790
  $db->baseExecute("UPDATE acid_ip_cache SET ipc_whois = NULL, ipc_whois_timestamp = NULL");
×
791
}
792
?>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc