forked from wazuh/wazuh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ossec2mysqld.pl
executable file
·524 lines (489 loc) · 15.1 KB
/
ossec2mysqld.pl
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/usr/bin/perl -w
use strict;
use Socket;
use POSIX 'setsid';
# ---------------------------------------------------------------------------
# Author: Meir Michanie (meirm@riunx.com)
# Co-Author: J.A.Senger (jorge@br10.com.br)
# $Id$
# $Revision$
## ---------------------------------------------------------------------------
# http://www.riunx.com/
# ---------------------------------------------------------------------------
#
# ---------------------------------------------------------------------------
# About this script
# ---------------------------------------------------------------------------
#
# "Ossec to Mysql" records the OSSEC HIDS alert logs in MySQL database.
# It can run as a daemon (ossec2mysqld.pl), recording in real-time the logs in database or
# as a simple script (ossec2mysql.pl).
#
# ---------------------------------------------------------------------------
# Prerequisites
# ---------------------------------------------------------------------------
#
# MySQL Server
# Perl DBD::mysql module
# Perl DBI module
#
# ---------------------------------------------------------------------------
# Installation steps
# ---------------------------------------------------------------------------
#
# 1) Create new database
# 2a) Run ossec2mysql.sql to create MySQL tables in your database
# 2b) Create BASE tables with snort tables extention
# 3) Create a user to access the database;
# 4) Copy ossec2mysql.conf to /etc/ossec2mysql.conf with 0600 permissions
# 3) Edit /etc/ossec2mysql.conf according to your configuration:
# dbhost=localhost
# database=ossecbase
# debug=5
# dbport=3306
# dbpasswd=mypassword
# dbuser=ossecuser
# daemonize=0
# resolve=1
#
#
# ---------------------------------------------------------------------------
# License
# ---------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ---------------------------------------------------------------------------
# About OSSEC HIDS
# ---------------------------------------------------------------------------
#
# OSSEC HIDS is an Open Source Host-based Intrusion Detection System.
# It performs log analysis and correlation, integrity checking,
# rootkit detection, time-based alerting and active response.
# http://www.ossec.net
#
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Parameters
# ---------------------------------------------------------------------------
$SIG{TERM} = sub { &gracefulend('TERM')};
$SIG{INT} = sub { &gracefulend('INT')};
my ($RUNASDAEMON)=0;
my ($DAEMONLOGFILE)='/var/log/ossec2mysql.log';
my ($DAEMONLOGERRORFILE) = '/var/log/ossec2mysql.err';
my ($LOGGER)='ossec2mysql';
use ossecmysql;
my %conf;
$conf{dbhost}='localhost';
$conf{database}='snort';
$conf{debug}=5;
$conf{dbport}='3306';
$conf{dbpasswd}='password';
$conf{dbuser}='user';
$conf{daemonize}=0;
$conf{sensor}='sensor';
$conf{hids_interface}='ossec';
$conf{resolve}=1;
my($OCT) = '(?:25[012345]|2[0-4]\d|1?\d\d?)';
my($IP) = $OCT . '\.' . $OCT . '\.' . $OCT . '\.' . $OCT;
my $VERSION="0.4";
my $sig_class_id=1;
&help() unless @ARGV;
my $dump=0;
my ($hids_id,$hids,$hids_interface,$last_cid)=(undef, 'localhost', 'ossec',0);
my ($tempvar,$VERBOSE)=(0,0);
# ---------------------------------------------------------------------------
# Arguments parsing
# ---------------------------------------------------------------------------
while (@ARGV){
$_= shift @ARGV;
if (m/^-d$|^--daemon$/){
$conf{daemonize}=1;
}elsif ( m/^-h$|^--help$/){
&help();
}elsif ( m/^-n$|^--noname$/){
$conf{'resolve'}=0;
}elsif ( m/^-v$|^--verbose$/){
$VERBOSE=1;
}elsif ( m/^--interface$/){
$conf{hids_interface}= shift @ARGV if @ARGV; # ossec-rt/ossec-feed
}elsif ( m/^--sensor$/){
$conf{sensor}= shift @ARGV if @ARGV; # monitor
}elsif ( m/^--conf$/){
$conf{conf}= shift @ARGV if @ARGV; # localhost
&loadconf(\%conf);
}elsif ( m/^--dbhost$/){
$conf{dbhost}= shift @ARGV if @ARGV; # localhost
}elsif ( m/^--dbport$/){
$conf{dbport}= shift @ARGV if @ARGV; # localhost
}elsif ( m/^--dbname$/){
$conf{database}= shift @ARGV if @ARGV; # snort
}elsif ( m/^--dbuser$/){
$conf{dbuser}= shift @ARGV if @ARGV; # root
}elsif ( m/^--dbpass$/){
$conf{dbpasswd}= shift @ARGV if @ARGV; # monitor
}
}
if ($conf{dbpasswd}=~ m/^--stdin$/){
print "dbpassword:";
$conf{dbpasswd}=<>;
chomp $conf{dbpasswd};
}
$hids=$conf{sensor} if exists($conf{sensor});
$hids_interface=$conf{hids_interface} if exists($conf{hids_interface});
&daemonize() if $conf{daemonize};
my $dbi= ossecmysql->new(%conf) || die ("Could not connect to $conf{dbhost}:$conf{dbport}:$conf{database} as $conf{dbpasswd}\n");
####
# SQL vars;
my ($query,$numrows,$row_ref);
####
#get sensor id
$query= 'select sid,last_cid from sensor where hostname=? and interface=?';
$numrows= $dbi->execute($query,$hids,$hids_interface);
if (1==$numrows){
$row_ref=$dbi->{sth}->fetchrow_hashref;
$hids_id=$row_ref->{sid};
$last_cid=$row_ref->{last_cid};
}else{
$query="INSERT INTO sensor ( sid , hostname , interface , filter , detail , encoding , last_cid )
VALUES (
NULL , ?, ? , NULL , ? , ?, ?
)";
$numrows= $dbi->execute($query,$hids,$hids_interface,1,2,0);
$hids_id=$dbi->lastid();
}
$dbi->{sth}->finish;
&forceprintlog ("SENSOR:$hids; feed:$hids_interface; id:$hids_id; last cid:$last_cid");
#exit ;
my $newrecord=0;
my %stats;
my %resolv;
my ($timestamp,$sec,$mail,$date,$alerthost,$alerthostip,$datasource,$rule,$level,$description,
$srcip,$dstip,$user,$text)=();
my $lasttimestamp=0;
my $delta=0;
########################################################
my $datepath=`date "+%Y/%b/ossec-alerts-%d.log"`;
my $LOG='/var/ossec/logs/alerts/'. $datepath;
chomp $LOG;
&taillog($last_cid,$LOG);
###############################################################
sub forceprintlog(){
$tempvar=$VERBOSE;
$VERBOSE=1;
&printlog (@_);
$VERBOSE=$tempvar;
}
sub taillog {
my ($last_cid,$LOG)=@_;
my($offset, $line, $stall) = '';
$offset = (-s $LOG); # Don't start at beginning, go to end
while (1==1) {
sleep(1);
%resolv=();
$| = 1;
$stall += 1;
$datepath=`date "+%Y/%b/ossec-alerts-%d.log"`;
$LOG='/var/ossec/logs/alerts/'. $datepath;
chomp $LOG;
unless ( -f $LOG){&forceprintlog ("Error -f $LOG"); next; }
if ((-s $LOG) < $offset) {
&forceprintlog ("Log shrunk, resetting..");
$offset = 0;
}
unless (open(TAIL, $LOG)){ &forceprintlog ("Error opening $LOG: $!\n");next ;}
if (seek(TAIL, $offset, 0)) {
# found offset, log not rotated
} else {
# log reset, follow
$offset=0;
seek(TAIL, $offset, 0);
}
while (<TAIL>) {
if (m/^$/){
$newrecord=1;
next unless $timestamp;
$alerthostip=$alerthost if $alerthost=~ m/^$IP$/;
if ($alerthostip){
$dstip=$alerthostip;
$resolv{$alerthost}=$dstip;
}else{
if (exists $resolv{$alerthost}){
$dstip=$resolv{$alerthost};
}else{
if ($conf{'resolve'}){
$dstip=`host $alerthost 2>/dev/null | grep 'has address' `;
if ($dstip =~m/(\d+\.\d+\.\d+\.\d+)/ ){
$dstip=$1;
}else{
$dstip=$srcip;
}
}else{
$dstip=$alerthost;
}
$resolv{$alerthost}=$dstip;
}
}
$last_cid= &prepair2basedata(
$hids_id,
$last_cid,
$timestamp,
$sec,
$mail,
$date,
$alerthost,
$datasource,
$rule,
$level,
$description,
$srcip,
$dstip,
$user,
$text
);
($timestamp,$sec,$mail,$date,$alerthost,$alerthostip,$datasource,$rule,$level,$description,
$srcip,$dstip,$user,$text)=();
next ;
}
if (m/^\*\* Alert ([0-9]+).([0-9]+):(.*)$/){
$timestamp=$1;
if ( $timestamp == $lasttimestamp){
$delta++;
}else{
$delta=0;
$lasttimestamp=$timestamp;
}
$sec=$2;
$mail=$3;
$mail=$mail ? $mail : 'nomail';
#2006 Aug 29 17:19:52 firewall -> /var/log/messages
#2006 Aug 30 11:52:14 192.168.0.45->/var/log/secure
#2006 Sep 12 11:12:16 92382-Snort1 -> 172.16.176.132
#
}elsif ( m/^([0-9]+\s\w+\s[0-9]+\s[0-9]+:[0-9]+:[0-9]+)\s+(\S+)\s*->(.*)$/){
$date=$1;
$alerthost=$2;
$datasource=$3;
if ($datasource=~ m/(\d+\.\d+\.\d+\.\d+)/){
$alerthost=$1;
$datasource="remoted";
}
#2006 Aug 29 17:33:31 (recepcao) 10.0.3.154 -> syscheck
}elsif ( m/^([0-9]+\s\w+\s[0-9]+\s[0-9]+:[0-9]+:[0-9]+)\s+\((.*?)\)\s+(\S+)\s*->(.*)$/){
$date=$1;
$alerthost=$2;
$alerthostip=$3;
$datasource=$4;
}elsif ( m/^([0-9]+\s\w+\s[0-9]+\s[0-9]+:[0-9]+:[0-9]+)\s(.*?)$/){
$date=$1;
$alerthost='localhost';
$datasource=$2;
}elsif ( m/Rule: ([0-9]+) \(level ([0-9]+)\) -> (.*)$/ ){
$rule=$1;
$level=$2;
$description= $3;
}elsif ( m/Src IP:/){
if ( m/($IP)/){
$srcip=$1;
}else{
$srcip='0.0.0.0';
}
}elsif ( m/User: (.*)$/){
$user=$1;
}elsif( m/(.*)$/){
$text .=$1;
}
} # End while read line
$offset=tell(TAIL);
close(TAIL);
}
}
sub ossec_aton(){
my ($ip)=@_;
if ($ip=~ m/(\d+)\.(\d+)\.(\d+)\.(\d+)/){
my $num= ($1 * 256 ** 3) + ($2 * 256 ** 2)+ ($3 * 256 ** 1)+ ($4);
return "$num";
}else{
return "0";
}
}
sub prepair2basedata(){
my (
$hids_id,
$last_cid,
$timestamp,
$sec,
$mail,
$date,
$alerthost,
$datasource,
$rule,
$level,
$description,
$srcip,
$dstip,
$user,
$text
)=@_;
my ($count,$query,$row_ref,$sig_id);
###
#
# Get/Set signature id
$query = "SELECT sig_id FROM signature where sig_name=? and sig_class_id=? and sig_priority=? and sig_rev=? and sig_sid=? and sig_gid is NULL";
$dbi->execute($query,$description,1,$level,0,$rule);
$count=$dbi->{sth}->rows;
if ($count){
$row_ref=$dbi->{sth}->fetchrow_hashref;
$sig_id=$row_ref->{sig_id};
&printlog ("REUSING SIGNATURE\n");
}else{
$query="INSERT INTO signature ( sig_id , sig_name , sig_class_id , sig_priority , sig_rev , sig_sid , sig_gid )
VALUES (
NULL ,?, ? , ? , ? , ?, NULL
)";
$dbi->execute($query,$description,1,$level,0,$rule);
$sig_id = $dbi->lastid();
}
$dbi->{sth}->finish;
&printlog ("SIGNATURE: $sig_id\n");
#######
#
# Set event
$query="INSERT INTO event ( sid , cid , signature , timestamp )
VALUES (
? , ? , ? ,?
)";
$last_cid++;
$dbi->execute($query,$hids_id,$last_cid,$sig_id,&fixdate2base($date));
&printlog ("EVENT: ($query,$hids_id,$last_cid,$sig_id,&fixdate2base($date)\n");
$dbi->{sth}->finish;
#########
#
# Set acid_event
$query=" INSERT INTO acid_event ( sid , cid , signature , sig_name , sig_class_id , sig_priority , timestamp , ip_src , ip_dst , ip_proto , layer4_sport , layer4_dport )
VALUES (
? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?, ?
) ";
$dbi->execute($query,$hids_id,$last_cid,$sig_id,$description,1,$level,&fixdate2base($date),&ossec_aton($srcip),&ossec_aton($dstip),undef,undef,undef);
&printlog ("ACID_EVENT: ($query,$hids_id,$last_cid,$sig_id,$description,1,$level,&fixdate2base($date),&ossec_aton($srcip),&ossec_aton($dstip),undef,undef)\n");
$dbi->{sth}->finish;
#########
#
#
# Set data
$text = "** Alert $timestamp.$sec:\t$mail\n$date $alerthost -> $datasource\nRule: $rule (level $level) -> $description\nSrc IP: ($srcip)\nUser: $user\n$text";
$query=" INSERT INTO data ( sid , cid , data_payload )
VALUES (
?,?,?)";
$dbi->execute($query,$hids_id,$last_cid,$text);
&printlog ("DATA: ($query,$hids_id,$last_cid,$text)\n");
$dbi->{sth}->finish;
##########
#
$query="UPDATE sensor SET last_cid=? where sid=? limit 1";
$numrows= $dbi->execute($query,$last_cid,$hids_id);
# end sub
$dbi->{sth}->finish;
return $last_cid;
}
sub fixdate2base(){
my ($date)=@_;
$date=~ s/ Jan /-01-/;
$date=~ s/ Feb /-02-/;
$date=~ s/ Mar /-03-/;
$date=~ s/ Apr /-04-/;
$date=~ s/ May /-05-/;
$date=~ s/ Jun /-06-/;
$date=~ s/ Jul /-07-/;
$date=~ s/ Aug /-08-/;
$date=~ s/ Sep /-09-/;
$date=~ s/ Oct /-10-/;
$date=~ s/ Nov /-11-/;
$date=~ s/ Dec /-12-/;
$date=~ s/\s$//g;
return $date;
}
sub version(){
print "OSSEC report tool $VERSION\n";
print "Licensed under GPL\n";
print "Contributor Meir Michanie\n";
}
sub help(){
&version();
print "This tool helps you import into base the alerts generated by ossec."
. " More info in the doc directory .\n";
print "Usage:\n";
print "$0 [-h|--help] # This text you read now\n";
print "Options:\n";
print "\t--dbhost <hostname>\n";
print "\t--dbname <database>\n";
print "\t--dbport <[0-9]+>\n";
print "\t--dbpass <dbpasswd>\n";
print "\t--dbuser <dbuser>\n";
print "\t-d|--daemonize\n";
print "\t-n|--noname\n";
print "\t-v|--verbose\n";
print "\t--conf <ossec2based-config>\n";
print "\t--sensor <sensor-name>\n";
print "\t--interface <ifname>\n";
exit 0;
}
sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, ">>$DAEMONLOGFILE"
or die "Can't write to $DAEMONLOGFILE: $!";
defined(my $pid = fork) or die "Can't fork: $!";
if ($pid){
open (PIDFILE , ">/var/run/ossec2base2.pid") ;
print PIDFILE "$pid\n";
close (PIDFILE);
exit 0;
}
setsid or die "Can't start a new session: $!";
open STDERR, ">>$DAEMONLOGERRORFILE" or die "Can't write to $DAEMONLOGERRORFILE: $!";
}
sub gracefulend(){
my ($signal)=@_;
&forceprintlog ("Terminating upon signal $signal");
close TAIL;
&forceprintlog ("Daemon halted");
close STDOUT;
close STDERR;
exit 0;
}
sub printlog(){
return unless $VERBOSE;
my (@lines)=@_;
foreach my $line(@lines){
chomp $line;
my ($date)=scalar localtime;
$date=~ s/^\S+\s+(\S+.*\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}).*$/$1/;
print "$date $LOGGER: $line\n";
}
}
sub loadconf(){
my ($hash_ref)=@_;
my $conf=$hash_ref->{conf};
unless (-f $conf) { &printlog ("ERROR: I can't find config file $conf"); exit 1;}
unless (open ( CONF , "$conf")){ &printlog ("ERROR: I can't open file $conf");exit 1;}
while (<CONF>){
next if m/^$|^#/;
if ( m/^(\S+)\s?=\s?(.*?)$/) {
$hash_ref->{$1} = $2;
}
}
close CONF;
}