Hello,
I recently posted a questions about Snort and MikroTik:
http://forum.mikrotik.com/t/snort-ids-on-ubuntu-with-packet-sniffer-mikrotik/98046/1
And my system is almost running except the PHP/API script part, where the script searches priority 1 alerts in syslog and connects to MikroTik via SSH and puts the attackers IP address to a blacklist.
This is the script modified for my network (I am not sure if I modified it correctly):
<?php
$blocked=array();
exec('cat /var/log/syslog | grep "`date -d "-1 minute" "+%b %e %H:%M"`"',$lastMin); foreach($lastMin as $line) {
if (strpos($line,"Priority: 1")!==FALSE || strpos($line,"portscan")!==FALSE)
{
preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $line, $matches);
$filter=$matches[0];
if (!in_array($filter, $blocked))
{
$blocked[]=$filter;
if (strpos($filter,"192.168.80.")!==FALSE) continue; //Protected space
sendMikrotik('192.168.80.50', 'ips', 'snort',$filter); // Mikrotik 1
}
}
}
function sendMikrotik($mt,$user,$pass,$filter) { $connection = ssh2_connect($mt); ssh2_auth_password($connection,$user,$pass); sleep(1); $stream = ssh2_exec($connection, ':global ip '.$filter); $stream = ssh2_exec($connection, '/system script run filter'); $stream = ssh2_exec($connection, 'quit'); } ?>
Do I have a mistake in this PHP script? I am new to programming and scripting and kind of lost in this script.
Attacking PC and the one who is being attacked are in different network than 192.168.80.X. Now, when i produce a priority 1 alert, and run the php script, I get an error about “ssh2_connect();” and being something wrong with it. I made a new MikroTik user for this named “ips” with password “snort”. I have the scripts from this post made in MikroTik:
http://wiki.mikrotik.com/wiki/Mikrotik_IPS_IDS
Do they have to have a specific name? Do I have to name the first script on MikroTiks side “filter”?
boen_robot said:
Use the API protocol for the PHP part. It’s much easier, and far less error prone than SSH.
(I suggest the API client from my signature in particular)
I have no clue on how to use this protocol. Do you have any advice?
Best regards,
Filip