LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2003, 10:44 PM   #1
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Rep: Reputation: 15
parse iptables logs - Perl Question


Hello all - Perl question:

I would love to see the output of this if statement:

if (/^(\w+)\s+([0-9]+) ([0-9]+):([0-9]+):([0-9]+) (\w+) kernel: INPUT packet died: IN=(\w+[0-9]+) OUT= MAC=(\w+) SRC=([0-9\.]+) DST=([0-9\.]+) LEN=([0-9]+) TOS=(\w+) PREC=(\w+) TTL=([0-9]+) ID=([0-9]+) DF PROTO=(\w+) SPT=([0-9]+) DPT=([0-9]+) WINDOW=([0-9]+) RES=(\w+) ([\w+\s]+)URGP=([1-9]+)/) {
#If we audited an incoming TCP packet
print"I have been trying to get this to work for 10 hours";
send to mysql
}

my syslog entry looks like:

Oct 29 23:31:34 noc kernel: INPUT packet died: IN=eth0 OUT= MAC=00:20:78:1c:fe:b3:00:60:0f:4f:d3:e2:08:00 SRC=xxx.xxx.xxx.xxx DST=xxx.xxx.xxx.xxx LEN=48 TOS=0x00 PREC=0x00 TTL=121 ID=56152 DF PROTO=TCP SPT=3119 DPT=135 WINDOW=64240 RES=0x00 SYN URGP=0


Anybody know whats wrong with my parse?
 
Old 10-30-2003, 06:04 AM   #2
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

Rep: Reputation: 15
1. You've got: URGP=([1-9]+)
But the snippet of syslog you provide has: URGP=0

2. You've got: MAC=(\w+)
But that doesn't match a ":", so you need: MAC=([\w:]+)

Incidentally, that has got to be one of the ugliest bits of code I've ever seen. There are dozens of better ways to do what you want. Give me three examples for homework :-)

Working code:

Code:
#!/usr/bin/perl

use warnings;
use diagnostics;

my $log = "Oct 29 23:31:34 noc kernel: INPUT packet died: IN=eth0 OUT= MAC=00:20:78:1c:fe:b3:00:60:0f:4f:d3:e2:08:00 SRC=000.000.000.000 DST=000.000.000.000 LEN=48 TOS=0x00 PREC=0x00 TTL=121 ID=56152 DF PROTO=TCP SPT=3119 DPT=135 WINDOW=64240 RES=0x00 SYN URGP=0";

if ( $log =~ /^(\w+)\s+([0-9]+) ([0-9]+):([0-9]+):([0-9]+) (\w+) kernel: INPUT packet died: IN=(\w+[0-9]+) OUT= MAC=([\w:]+) SRC=([0-9\.]+) DST=([0-9\.]+) LEN=([0-9]+) TOS=(\w+) PREC=(\w+) TTL=([0-9]+) ID=([0-9]+) DF PROTO=(\w+) SPT=([0-9]+) DPT=([0-9]+) WINDOW=([0-9]+) RES=(\w+) ([\w+\s]+) URGP=([0-9]+)/ )
{
	print "Worked!\n";
}

Last edited by Nightblade_oz; 10-30-2003 at 06:06 AM.
 
Old 10-30-2003, 04:22 PM   #3
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Original Poster
Rep: Reputation: 15
Yea - the URGP was the culprit - Figured it out late last night crawling through perldoc-

Wasnt my code - there is a script given out with snort to parse iptables logs and drop them into the snort db - it did not parse for MAC or DF. My problem now is that some packets logged will not show DF? How do I make it so that "DF" might be there and might not.

there are six if statements like that - this is what I did for MAC, but I guess yours will work too

if (/^(\w+)\s+([0-9]+) ([0-9]+)[0-9]+)[0-9]+) ([^\s]+) kernel: INPUT packet died: IN=(\w+[0-9]+) OUT= MAC=([a-f,0-9,:]+) SRC=([0-9\.]+) DST=([0-9\.]+) LEN=([0-9]+) TOS=(\w+) PREC=(\w+) TTL=([0-9]+) ID=([0-9]+) DF PROTO=(\w+) SPT=([0-9]+) DPT=([0-9]+) WINDOW=([0-9]+) RES=(\w+) ([\w+\s]+)URGP=([0-9]+)/) {
print"this code is a load of ....";
$month=$1;
$day=$2;
$hour=$3;
$min=$4;
$sec=$5;
$linux=$6;
$ruleset=IN;
$auth=audit;
$interface=$7;
# MAC=$8;
$src_addr=$9;
$dst_addr=$10;
$ip_len=$11;
$ip_tos=$12;
# PREC=$13
$ip_ttl=$14;
$ip_id=$15;
$proto=$16;
$src_port=$17;
$dst_port=$18;
# WINDOW=$19;
# RES=$20;
# FLAGS=$21
# URGP=$22
$therest=$23;
# $frag_off=$;

drop into database;

print"how would you do it? thanks";
 
Old 10-30-2003, 07:45 PM   #4
Nightblade_oz
LQ Newbie
 
Registered: Aug 2003
Location: Australia
Distribution: Fedora Core 2
Posts: 24

Rep: Reputation: 15
"+" = one or more things
"*" = zero or more things

so you could use: ([\w+\s]*)

Code:
#!/usr/bin/perl

use warnings;
use strict;

my $log = 'Oct 29 23:31:34 noc kernel: INPUT packet died: IN=eth0 OUT= '
        . 'MAC=00:20:78:1c:fe:b3:00:60:0f:4f:d3:e2:08:00 SRC=000.000.000.000 ' 
        . 'DST=000.000.000.000 LEN=48 TOS=0x00 PREC=0x00 TTL=121 ID=56152 DF ' 
        . 'PROTO=TCP SPT=3119 DPT=135 WINDOW=64240 RES=0x00 SYN URGP=0';

my $re	= '^(\w+)\s+([0-9]+) ([0-9]+):([0-9]+):([0-9]+) (\w+) kernel: INPUT '
        . 'packet died: IN=(\w+[0-9]+) OUT= MAC=([\w:]+) SRC=([0-9\.]+) '
        . 'DST=([0-9\.]+) LEN=([0-9]+) TOS=(\w+) PREC=(\w+) TTL=([0-9]+) '
        . 'ID=([0-9]+) ([\w+\s]*) PROTO=(\w+) SPT=([0-9]+) DPT=([0-9]+) '
        . 'WINDOW=([0-9]+) RES=(\w+) ([\w+\s]+) URGP=([0-9]+)';

if ( $log =~ /$re/o )
{
    print "Worked!\n";
}
 
Old 10-30-2003, 10:56 PM   #5
toovato
Member
 
Registered: Jul 2003
Location: Ft Lauderdale, FL
Distribution: debian
Posts: 48

Original Poster
Rep: Reputation: 15
I like that - thanks nightblade
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
optimizing perl parse file. eastsuse Programming 1 12-22-2004 02:49 AM
Need help with perl/bash script to parse PicBasic file cmfarley19 Programming 13 11-18-2004 05:06 PM
Perl Script To Check Logs Crashed_Again Programming 0 11-13-2004 03:13 PM
Parse a perl string djgerbavore Programming 3 10-31-2004 07:23 AM
PERL error logs...? vous Programming 1 02-06-2004 01:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration