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

NathanGibbs3 / BASE / 590

pending completion
590

push

travis-ci-com

NathanGibbs3
20230420 Fix CI build breakage. 2

2755 of 16977 relevant lines covered (16.23%)

21.61 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
/** The below check is to make sure that the conf file has been loaded before this one....
22
 **  This should prevent someone from accessing the page directly. -- Kevin
23
 **/
24
defined( '_BASE_INC' ) or die( 'Accessing this file directly is not allowed.' );
25

26
include_once("$BASE_path/base_stat_common.php");
27
include_once("$BASE_path/includes/base_log_error.inc.php");
28

29
function UpdateDNSCache($db)
30
{
31
  GLOBAL $debug_mode, $dns_cache_lifetime;
32

33
  $cnt = 0;
×
34

35
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_src FROM acid_event ".
×
36
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_src ".
37
                                "WHERE ipc_fqdn IS NULL");
38

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

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

60
  ErrorMessage(_ADDED.$cnt._HOSTNAMESDNS);
×
61
}
62

63
function UpdateWhoisCache($db)
64
{
65
  GLOBAL $debug_mode, $whois_cache_lifetime;
66

67
  $cnt = 0;
×
68

69
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_src FROM acid_event ".
×
70
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_src ".
71
                                "WHERE ipc_whois IS NULL");
72

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

81
  $ip_result = $db->baseExecute("SELECT DISTINCT ip_dst FROM acid_event ".
×
82
                                "LEFT JOIN acid_ip_cache ON ipc_ip = ip_dst ".
83
                                "WHERE ipc_whois IS NULL");
84

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

93
  ErrorMessage(_ADDED.$cnt._HOSTNAMESWHOIS);
×
94
}
95

96
function CacheAlert($sid, $cid, $db)
97
{
98
  $signature = $timestamp = $ip_src = $ip_dst = null;
×
99
  $ip_proto = $layer4_sport = $layer4_dport = $sig_name = null;
×
100
  $sig_class_id = $sig_priority = null;
×
101

102
  $sql = "SELECT signature, timestamp, ip_src, ip_dst, ip_proto FROM event ".
103
         "LEFT JOIN iphdr ON (event.sid=iphdr.sid AND event.cid = iphdr.cid) ".
104
         "WHERE (event.sid='".$sid."' AND event.cid='".$cid."') ORDER BY event.cid";
105

106
  $result = $db->baseExecute($sql);
×
107

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

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

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

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

172
  /* There can be events without certain attributes */
173
  if ($sig_priority=='') $sig_priority='NULL';
×
174
  if ($ip_src=='') $ip_src='NULL';
×
175
  if ($ip_dst=='') $ip_dst='NULL';
×
176
  if ($ip_proto=='') $ip_proto='NULL';
×
177
  if ($layer4_sport=='') $layer4_sport='NULL';
×
178
  if ($layer4_dport=='') $layer4_dport='NULL';
×
179
  if ($sig_class_id == '') $sig_class_id = 'NULL';
×
180
  if ($sig_priority == '') $sig_priority = 'NULL';
×
181

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

194
  $db->baseExecute($sql); 
×
195

196
  if ( $db->baseErrorMessage() != "" )
×
197
     return 0;
×
198
  else 
199
     return 1;
×
200
}
201

202
function CacheSensor($sid, $cid, $db)
203
/*
204
  Caches all alerts for sensor $sid newer than the event $cid
205
 */
206
{
207
  GLOBAL $debug_mode;
208

209

210
  $schema_specific = array(2);
×
211

212
  $schema_specific[0] = "";
×
213
  $schema_specific[1] = "";
×
214
  $schema_specific[2] = "";
×
215

216
  if ( $db->baseGetDBversion() >= 100 ) 
×
217
  {
218
     $schema_specific[1] = ", sig_name"; 
×
219
     $schema_specific[2] = " INNER JOIN signature ON (signature = signature.sig_id) ";
×
220
  }
221

222
  if ( $db->baseGetDBversion() >= 103 )
×
223
  {
224
     $schema_specific[0] = $schema_specific[0].", sig_priority, sig_class_id ";
×
225
     $schema_specific[1] = $schema_specific[1].", sig_priority, sig_class_id "; 
×
226
     $schema_specific[2] = $schema_specific[2]."";
×
227
  }
228

229
  if ( $db->baseGetDBversion() < 100 )
×
230
     $schema_specific[1] = $schema_specific[1].", signature ";
×
231

232
  $update_sql = array(4);
×
233

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

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

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

343
  /* ICMP events */
344
  if( $db->DB_type == 'oci8' ) {
×
345
    $update_sql[2] = 
×
346
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
347
                              ip_src,ip_dst,ip_proto,
348
                              sig_name".
349
                              $schema_specific[0].")
×
350
      SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
351
             ip_src, ip_dst, ip_proto".
352
             $schema_specific[1]."
×
353
      FROM event a
354
      ".$schema_specific[2]."
×
355
      INNER JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
356
      LEFT JOIN icmphdr c ON (a.sid=c.sid AND a.cid=c.cid)
357
      WHERE (a.sid = $sid AND a.cid > $cid) and ip_proto = 1
358
      AND ( NOT ".$schema_specific[3].")";
×
359
  }
360
  else 
361
  {
362
    $update_sql[2] = 
×
363
     "INSERT INTO acid_event (sid,cid,signature,timestamp,
364
                              ip_src,ip_dst,ip_proto,
365
                              sig_name".
366
                              $schema_specific[0].")
×
367
      SELECT event.sid as sid, event.cid as cid, signature, timestamp,
368
             ip_src, ip_dst, ip_proto".
369
             $schema_specific[1]."
×
370
      FROM event
371
      ".$schema_specific[2]."
×
372
      INNER JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
373
      LEFT JOIN icmphdr ON (event.sid=icmphdr.sid AND event.cid=icmphdr.cid)
374
      WHERE (event.sid = $sid AND event.cid > $cid) and ip_proto = 1
375
      AND ( NOT ".$schema_specific[3].")";
×
376
  }
377

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

414

415

416
  /* Event only -- pre-processor alerts */
417
  if( $db->DB_type == 'oci8' ) {
×
418
     $update_sql[4] = 
×
419
       "INSERT INTO acid_event (sid,cid,signature,timestamp,
420
                                ip_src,ip_dst,ip_proto,
421
                                sig_name".
422
                                $schema_specific[0].")
×
423
        SELECT a.sid as sid, a.cid as cid, signature, a.timestamp,
424
               ip_src, ip_dst, ip_proto".
425
               $schema_specific[1]."
×
426
        FROM event a
427
        ".$schema_specific[2]."
×
428
        LEFT JOIN iphdr b ON (a.sid=b.sid AND a.cid=b.cid)
429
        WHERE ".$schema_specific[3]." AND 
×
430
        (a.sid = $sid AND a.cid > $cid)";
431
  }
432
  else 
433
  {
434
    $update_sql[4] = 
×
435
       "INSERT INTO acid_event (sid,cid,signature,timestamp,
436
                                ip_src,ip_dst,ip_proto,
437
                                sig_name".
438
                                $schema_specific[0].")
×
439
        SELECT event.sid as sid, event.cid as cid, signature, timestamp,
440
               ip_src, ip_dst, ip_proto".
441
               $schema_specific[1]."
×
442
        FROM event
443
        ".$schema_specific[2]."
×
444
        LEFT JOIN iphdr ON (event.sid=iphdr.sid AND event.cid=iphdr.cid)
445
        WHERE ".$schema_specific[3]." AND 
×
446
        (event.sid = $sid AND event.cid > $cid)";
447
  }
448

449

450
  // Some checks for unexpected errors
451
  $update_cnt = count($update_sql);
×
452
  if (!isset($update_cnt)) 
×
453
  {
454
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": WARNING: \$update_cnt has not been set. sid = $sid, cid = $cid<BR>";
×
455
    echo $mystr; 
×
456
  }
457
  else if ((integer)$update_cnt == 0) 
×
458
  {
459
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": WARNING: \$update_cnt = 0 with sid = $sid, cid = $cid<BR>";
×
460
    echo $mystr; 
×
461
  }
462
  else if (!isset($update_sql[0]) && !isset($update_sql[1]) && !isset($update_sql[2]) && !isset($update_sql[3])) 
×
463
  {
464
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": WARNING: \$update_sql[] has only empty elements with sid = $sid, cid = $cid<BR>";
×
465
    echo $mystr;
×
466
  } 
467
  else if ($update_sql[0] == "" && $update_sql[1] == "" && $update_sql[2] == "" && $update_sql[3] == "") 
×
468
  {
469
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": WARNING: \$update_sql[] has only empty elements with sid = $sid, cid = $cid<BR>";
×
470
    echo $mystr;
×
471
  }
472

473
        for ( $i = 0; $i < $update_cnt; $i++ ){
×
474
                // Now commit all those SQL commands
475
                if ($debug_mode > 0 ){
×
476
                        $mystr = '<BR>' . __FUNCTION__ . ": <BR>\n$update_sql[$i] <BR><BR>\n\n";
×
477
      echo $mystr;
×
478
                }
479
                $db->baseExecute($update_sql[$i]);
×
480

481
    if ( $db->baseErrorMessage() != "" )
×
482
       ErrorMessage(_ERRCACHEERROR." ["._SENSOR." #$sid]["._EVENTTYPE." $i]".
×
483
                      " "._ERRCACHEUPDATE);
484

485
        }
486
}
487

488
// This is an auxiliary function for problems with updating acid_event
489
function dump_missing_events( $db, $sid, $start_cid, $end_cid ){
490
        GLOBAL $debug_mode, $archive_exists, $DBlib_path, $DBtype,
491
        $archive_dbname, $archive_host, $archive_port, $archive_user,
492
        $archive_password;
493

494
  for ($n = (integer)$start_cid; $n <= (integer)$end_cid; $n++)
×
495
  {
496
    // Does this particular really exist in the event table?
497
    $event_list = $db->baseExecute( "SELECT count(*) FROM event WHERE sid='" . $sid . "' AND cid='" . $n. "'" );
×
498
    $event_row = $event_list->baseFetchRow();
×
499
    $event_value = $event_row[0];
×
500
    $event_list->baseFreeRows();
×
501
    if ((integer)$event_value == 1) {
×
502
      // Yes, it does.
503
      // So let's try and find it in acid event.
504
      $acid_event_list = $db->baseExecute( "SELECT count(*) FROM acid_event WHERE sid='" . $sid . "' AND cid='" . $n. "'" );
×
505
      $acid_event_row = $acid_event_list->baseFetchRow();
×
506
      $acid_event_element = $acid_event_row[0];
×
507
      $acid_event_list->baseFreeRows();
×
508
      if ((integer)$acid_event_element == 0) 
×
509
      {
510
        echo '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: Alert \"$sid - $n\" could NOT be found in acid_event.<BR>";
×
511
      }
512
    }
513
  }
514
}
515

516
function UpdateAlertCache($db, $force = 0 ){
517
        GLOBAL $debug_mode, $archive_exists, $event_cache_auto_update,
518
        $DBlib_path, $DBtype, $archive_dbname, $archive_host, $archive_port,
519
        $archive_user, $archive_password, $et;
520
        if ( $force == 0 && $event_cache_auto_update != 1 ){ // Issue #121 Fix
×
521
                return;
×
522
        }
523
        $batch_sql = '';
×
524
        $batch_cnt = 0;
×
525
        $updated_cache_cnt = 0;
×
526
        $EMPfx = __FUNCTION__ . ': ';
×
527
        // How many sensors do we have?
528
  $number_sensors_lst = $db->baseExecute("SELECT count(*) FROM sensor");
×
529
  $number_sensors_array = $number_sensors_lst->baseFetchRow();
×
530
  $number_sensors_lst->baseFreeRows();
×
531

532
  if (!isset($number_sensors_array))
×
533
  {
534
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array has not been set at all!<BR>";
×
535
    ErrorMessage($mystr);
×
536
    $number_sensors = 0;
×
537
  }
538

539
  if (!is_array($number_sensors_array))
×
540
  {
541
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array is NOT an array!<BR>";
×
542
    ErrorMessage($mystr);
×
543
    
544
    $number_sensors = 0;
×
545
  }
546

547
  if ($number_sensors_array == NULL || $number_sensors_array == "")
×
548
  {
549
    $mystr = '<BR>' . __FILE__ . ':' . __LINE__ . ": ERROR: \$number_sensors_array is either NULL or empty!<BR>";
×
550
    ErrorMessage($mystr);
×
551

552
    $number_sensors = 0;
×
553
  } 
554
  else
555
  {
556
    $number_sensors = $number_sensors_array[0];
×
557
  }
558

559
  if ($debug_mode > 1)
×
560
  {
561
    echo '$number_sensors = ' . $number_sensors . '<BR><BR>';
×
562
  }
563

564
  
565
  if (($debug_mode > 0) && ($number_sensors < 1))
×
566
  {
567
    ErrorMessage("WARNING: Number of sensors = " . $number_sensors);
×
568
    echo '<HR>';
×
569
    echo '<BR>number_sensors_array:<BR>';
×
570
    echo '<PRE>';
×
571
    var_dump($number_sensors_array);
×
572
    echo '</PRE>';
×
573
    echo '<HR>';    
×
574
  }
575
  
576
  
577
  /* Iterate through all sensors in the SENSOR table */
578
  $sensor_lst = $db->baseExecute("SELECT sid FROM sensor");
×
579
  if (($debug_mode > 0) && ($number_sensors < 1))
×
580
  {
581
    echo '<HR>';
×
582
    echo '<BR>sensor_lst:<BR>';
×
583
    echo '<PRE>';
×
584
    var_dump($sensor_lst);
×
585
    echo '</PRE>';
×
586
    echo '<HR>';
×
587
  }
588

589
  for ($n = 0; $n < $number_sensors; $n++)
×
590
  {
591
    $sid_row = $sensor_lst->baseFetchRow();
×
592
    if (!isset($sid_row) || $sid_row == "" || $sid_row == NULL)
×
593
    {
594
      if ($n >= $number_sensors)
×
595
      {
596
        break;
×
597
      }
598
      else
599
      {
600
        next;
×
601
      }
602
    }
603

604
    $sid = $sid_row[0];
×
605
    /* Get highest CID for a given sensor */
606
    $cid_lst = $db->baseExecute("SELECT MAX(cid) FROM event WHERE sid='".$sid."'");
×
607
    $cid_row = $cid_lst->baseFetchRow();
×
608
    if (
609
         (!isset($cid_row)) ||
610
         ($cid_row == NULL) ||
611
         ($cid_row == "")
612
       )
613
    {
614
      /* NULL is in conflict with snort-2.8.0.1/schemas/create_mysql:
615
       * CREATE TABLE event  ( sid         INT      UNSIGNED NOT NULL,
616
                               cid         INT      UNSIGNED NOT NULL,
617
                               signature   INT      UNSIGNED NOT NULL, 
618
                               timestamp            DATETIME NOT NULL,
619
                               PRIMARY KEY (sid,cid),
620
                               INDEX       sig (signature),
621
                               INDEX       time (timestamp));
622
       */
623
      $cid = 0;
×
624
    }
625
    else
626
    {
627
      $cid = $cid_row[0];
×
628
    }
629
    if ( $cid == NULL ) $cid = 0;
×
630

631
    /* Get highest CID for a given sensor in the cache */
632
    $ccid_lst = $db->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'");
×
633
    $ccid_row = $ccid_lst->baseFetchRow();
×
634
    if (
635
         (!isset($ccid_row)) ||
636
         ($ccid_row == NULL) ||
637
         ($ccid_row == "")
638
       )
639
    {
640
      /* NULL is in conflict with base-php4/sql/create_base_tbls_mysql.sql:
641
         CREATE TABLE acid_event   ( sid                 INT UNSIGNED NOT NULL,
642
                                      cid                 INT UNSIGNED NOT NULL,     
643
         (...)
644
       */
645
      $ccid = 0;
×
646
    }
647
    else
648
    {
649
      $ccid = $ccid_row[0];
×
650
    }
651
    if ( $ccid == NULL ) $ccid = 0;
×
652

653
    if ( $debug_mode > 0 )
×
654
      echo "sensor #$sid: event.cid = $cid, acid_event.cid = $ccid";
×
655

656
    /* if the CID in the cache < the CID in the event table 
657
     *  then there are events which have NOT been added to the cache 
658
     */
659
    if ( $cid > $ccid )
×
660
    {
661
      $expected_addition = (integer)($cid - $ccid);
×
662

663
      $before_cnt = EventCntBySensor($sid, $db);        
×
664
      CacheSensor($sid, $ccid, $db);
×
665
      $updated_cache_cnt += EventCntBySensor($sid, $db) - $before_cnt;
×
666
    }
667

668
    if ( $debug_mode > 0 )
×
669
      echo "<BR>";
×
670

671
    if ($cid_row != NULL)
×
672
    {
673
      $cid_lst->baseFreeRows();
×
674
    }
675

676
    if ($ccid_row != NULL)
×
677
    {
678
      $ccid_lst->baseFreeRows();
×
679
    }
680
 
681
    /* BEGIN LOCAL FIX */
682
 
683
    /* If there's an archive database, and this isn't it, get the MAX(cid) from there */
684
        if ( $archive_exists == 1 && !ChkCookie ('archive', 1) ){
×
685
      $db2 = NewBASEDBConnection($DBlib_path, $DBtype);
×
686
      $db2->baseConnect($archive_dbname, $archive_host, $archive_port,
×
687
                        $archive_user, $archive_password);
688
      $archive_ccid_lst = $db2->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'"); 
×
689
      $archive_ccid_row = $archive_ccid_lst->baseFetchRow();
×
690
      $archive_ccid = $archive_ccid_row[0];
×
691
      $archive_ccid_lst->baseFreeRows();
×
692
      $db2->baseClose();
×
693
      if ( $archive_ccid == NULL ) $archive_ccid = 0;
×
694
    } else {
695
      $archive_ccid = 0; 
×
696
    }
697
 
698
    if ( $archive_ccid > $ccid ) {
×
699
      $max_ccid = $archive_ccid;
×
700
    } else {
701
      $max_ccid = $ccid;
×
702
    }
703
 
704
    /* Fix the last_cid value for the sensor */
705
    $db->baseExecute("UPDATE sensor SET last_cid=$max_ccid WHERE sid=$sid"); 
×
706

707
    /* END LOCAL FIX */
708

709

710
    ####### Has every alert in the event table found its way into
711
    ####### acid_event?
712
    if (isset($ccid)) {
×
713

714
      if ($debug_mode > 1){
×
715
                ErrorMessage($EMPfx ."Sensor no. $sid:",'black',1);
×
716
                ErrorMessage($EMPfx ."Old max cid in acid_event: $ccid",'black',1);
×
717
      }
718

719
      $debug_new_ccid_lst = $db->baseExecute("SELECT MAX(cid) FROM acid_event WHERE sid='".$sid."'");
×
720
      $debug_new_ccid_row = $debug_new_ccid_lst->baseFetchRow();
×
721
      $debug_new_ccid_lst->baseFreeRows();
×
722
      if (isset($debug_new_ccid_row[0])) 
×
723
      {
724
        $new_ccid = (integer) $debug_new_ccid_row[0];
×
725
      }
726
      else
727
      {
728
        $new_ccid = 0;
×
729
      }
730

731
      
732
      $real_addition = (integer)($new_ccid - (integer)$ccid);
×
733

734
      if ($debug_mode > 1){
×
735
                ErrorMessage($EMPfx ."New max cid in acid_event: $new_ccid",'black',1);
×
736
                ErrorMessage(
×
737
                        $EMPfx ."This many events HAVE been added to acid_event: $real_addition",
×
738
                        'black',1
739
                );
740
                ErrorMessage($EMPfx ."Max cid in event: $cid",'black',1);
×
741
      }
742

743
      if ($real_addition >= 0) 
×
744
      {
745

746

747
        if (!isset($expected_addition)) 
×
748
        {
749
          $expected_addition = 0;
×
750
        }
751

752
        if ($debug_mode > 1){
×
753
                ErrorMessage(
×
754
                        $EMPfx ."This many events SHOULD have been added to acid_event: $expected_addition",
×
755
                        'black',1
756
                );
757
        }
758

759
        if ($real_addition > 0 && $expected_addition > 0) 
×
760
        {
761
          if ($expected_addition - $real_addition > 0) 
×
762
          {
763
            $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>";
×
764
            errorMessage($mystr);
×
765

766

767
            dump_missing_events($db, $sid, $ccid, $new_ccid);
×
768
          }
769
        }
770
      }
771
      else
772
      {
773
        if ($debug_mode > 1)
×
774
        {
775
          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";
×
776
        }
777
      }
778

779
      if ($debug_mode > 1) 
×
780
      {
781
        echo "\n---------------<BR>\n";
×
782
      }
783
    }
784
  } // for ($n = 0; $n < $number_sensors; $n++)
785
  
786
  $sensor_lst->baseFreeRows();
×
787
        if ( $updated_cache_cnt != 0 ){
×
788
                if ( AuthorizedPage('base_main') ){
×
789
                        ErrorMessage(_ADDED.$updated_cache_cnt._ALERTSCACHE, "yellow", 1);
×
790
                }else{
791
                        ErrorMessage(_ADDED.$updated_cache_cnt._ALERTSCACHE);
×
792
                }
793
        }
794
        if ( is_object($et) ){ // Need to TD this in Issue #11 branch.
×
795
                $et->Mark('Updated ALERT Cache.');
×
796
        }
797
}
798

799
function DropAlertCache($db)
800
{
801
  $db->baseExecute("DELETE FROM acid_event");
×
802
}
803

804
function DropDNSCache($db)
805
{
806
  $db->baseExecute("UPDATE acid_ip_cache SET ipc_fqdn = NULL, ipc_dns_timestamp = NULL");
×
807
}
808

809
function DropWhoisCache($db)
810
{
811
  $db->baseExecute("UPDATE acid_ip_cache SET ipc_whois = NULL, ipc_whois_timestamp = NULL");
×
812
}
813
?>
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