So I was bored, and ripped pieces from the snort alert log to fit into the snort_alerts widget.
This is to my snort logging which is set to Full, I didn't throw in the checks to verify what type of logging you're doing, I figured the author of the widget could finish it up. I didn't add anything new, just modified this so that it will function. I hate when things don't function.
You can change the $logent=10; for however many lines of alerts you want I would recommend keeping this small, it's just a widget. You can see some of the functions I grabbed from /usr/local/www/snort/snort_alerts.php
The below code should replace the widget located in "/usr/local/www/widgets/widgets/snort_alerts.widget.php".
<?php
/*
snort_alerts.widget.php
Copyright (C) 2009 Jim Pingle
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INClUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
global $config, $g;
$logent=10;
function get_snort_alert_class($fileline)
{
/* class */
if (preg_match('/\[Classification:\s.+[^\d]\]/', $fileline, $matches2))
$alert_class = "$matches2[0]";
return $alert_class;
}
function get_snort_alert_priority($fileline)
{
/* Priority */
if (preg_match('/Priority:\s\d/', $fileline, $matches3))
$alert_priority = "$matches3[0]";
return $alert_priority;
}
function get_snort_alert_disc($fileline)
{
/* disc */
if (preg_match("/\[\*\*\] (\[.*\]) (.*) (\[\*\*\])/", $fileline, $matches))
$alert_disc = "$matches[2]";
return $alert_disc;
}
function get_snort_alert_ip_src($fileline)
{
/* SRC IP */
$re1='.*?'; # Non-greedy match on filler
$re2='((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[0
1]?[0-9][0-9]?))(?![\\d])'; # IPv4 IP Address 1
if ($c=preg_match_all ("/".$re1.$re2."/is", $fileline, $matches4))
$alert_ip_src = $matches4[1][0];
return $alert_ip_src;
}
function get_snort_alert_ip_dst($fileline)
{
/* DST IP */
$re1dp='.*?'; # Non-greedy match on filler
$re2dp='(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?![\\d])'; # Uninteresting: ipaddress
$re3dp='.*?'; # Non-greedy match on filler
$re4dp='((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])'; # IPv4 IP Address 1
if ($c=preg_match_all ("/".$re1dp.$re2dp.$re3dp.$re4dp."/is", $fileline, $matches6
))
$alert_ip_dst = $matches6[1][0];
return $alert_ip_dst;
}
function get_snort_alert_date($fileline)
{
/* date full date \d+\/\d+-\d+:\d+:\d+\.\d+\s */
if (preg_match("/\d+\/\d+-\d+:\d+:\d\d/", $fileline, $matches1))
$alert_date = "$matches1[0]";
return $alert_date;
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr class="snort-alert-header">
<td width="30%" class="widgetsubheader" >Date-Time</td>
<td width="40%" class="widgetsubheader">Src/Dst</td>
<td width="40%" class="widgetsubheader">Details</td>
</tr>
<?php
$alerts_array = array_reverse(array_filter(explode("\n\n", file_get_contents('/var/log/snort/alert'))));
if (is_array($alerts_array)) {
$counter = 0;
foreach($alerts_array as $fileline)
{
if($logent <= $counter)
continue;
$counter++;
/* Date */
$alert_date_str = get_snort_alert_date($fileline);
if($alert_date_str != '')
{
$alert_date = $alert_date_str;
}else{
$alert_date = '';
}
/* Discription */
$alert_disc_str = get_snort_alert_disc($fileline);
if($alert_disc_str != '')
{
$alert_disc = $alert_disc_str;
}else{
$alert_disc = 'empty';
}
/* Classification */
$alert_class_str = get_snort_alert_class($fileline);
if($alert_class_str != '')
{
$alert_class_match = array('[Classification:',']');
$alert_class = str_replace($alert_class_match, '', "$alert_class_str");
}else{
$alert_class = 'Prep';
}
/* Priority */
$alert_priority_str = get_snort_alert_priority($fileline);
if($alert_priority_str != '')
{
$alert_priority_match = array('Priority: ',']');
$alert_priority = str_replace($alert_priority_match, '', "$alert_priority_str");
}else{
$alert_priority = '';
}
/* IP SRC */
$alert_ip_src_str = get_snort_alert_ip_src($fileline);
if($alert_ip_src_str != '')
{
$alert_ip_src = $alert_ip_src_str;
}else{
$alert_ip_src = '';
}
/* IP Destination */
$alert_ip_dst_str = get_snort_alert_ip_dst($fileline);
if($alert_ip_dst_str != '')
{
$alert_ip_dst = $alert_ip_dst_str;
}else{
$alert_ip_dst = 'unk';
}
// echo $activerow;
if ($alert_disc != 'empty')
{
echo "<tr class=\"snort-alert-entry\" id=\"snort-firstrow\"> \n";
echo "<td width=\"30%\" class=\"listr\">{$alert_date}<br></td>
<td width=\"40%\" class=\"listr\">{$alert_ip_src}<br>{$alert_ip_dst}</td>
<td width=\"40%\" class=\"listr\">Pri: {$alert_priority}<br>Cat: {$alert_class}</td>
</tr>";
}
}
}
?>
</tbody>
</table>