Private Messages Options Search Blogs Images Chat Cam Portals Calendar FAQ's Join  
Asylum Forums : Powered by vBulletin version 2.2.8 Asylum Forums > WIT - Whore Institute of Technology > sed & awk question
  Last Thread   Next Thread
Author
Thread [new thread]    [post reply]
squee
the amen break

Registered: Jul 2001
Location: Norfolk, VA
Posts: 4678

sed & awk question

So, I've got a tasker at work where I'm looking over Raptor (Symantec) firewall logs. These are content-filtering firewalls with several (20 or so) daemons running, like httpd which (oddly enough) examines http/https traffic.

Typically I work with router logs, like Cisco netflow logs, which are delimited. That is, the third column is always going to be (e.g.) the source IP, in the entire file, so it's really easy to mine with fp trees or apriori.

These raptor logs, on the other hand...
So far as I can tell, they were never meant to be analyzed. Instead of delimited text you have an entry like this:
httpd src=192.168.1.1/4675 dst=192.168.2.1/80 sent=192 rcvd=3000

This is sort of typical of what you might see for a web session for an intranet web server.

But if the traffic transit the firewall you also see information about the source & destination interfaces:

httpd src=192.168.1.1/4675 dst=195.224.113.251/80 srcif=192.168.100.1 dstif=34.80.92.1 sent=176 rcvd=4650

I'm trying to parse these out using grep, sed, and awk to get a plain delimited file.

There is a utility called 'flatten' which can be used to do this, but Symantec won't send me one because WE don't have a license. I tried using sawmill and the result was just utter trash--it only pays attention to a few of the daemons, like httpd, ignoring or mangling dns and icmp traffic in the process.

So, at first I was just figuring, well, all the httpd traffic must be similar. Then I noticed that there were extra fields depending on whether or not the traffic transited the firewall, so I grepped those out separately and used awk to add the missing fields to the others.

Then I noticed that the order of the fields is not constant. Sometimes "srcif" will precede "dstif" and sometimes it won't. Sometimes the firewall records sent and received bytes and sometimes it doesn't. Sometimes the firewall rule that was triggered is noted--and sometimes not. When it is, sometimes it's listed before, say, the protocol, and sometimes after.

So now what I need to do is figure out some way to find, on a line, the field beginning with "src=" and place that as field $2 in awk, then find the one matching "dst=" and place that as $3, and so on and so forth. Is this possible using grep/sed/awk or am I barking up the wrong tree?

__________________
What does polite society know of the secret hearts of men?
What shows the shuttered window but all the evil you can imagine?

Report this post to a moderator | IP: Logged

Old Post 12-14-2005 03:48 PM
squee is offline Click Here to See the Profile for squee Click here to Send squee a Private Message Find more posts by squee Add squee to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

As I said in chat(but I'm not sure if you saw it), this isn't actually all that easy to implement as a oneliner. This is probably best off as a perl script.

You did miss one example though, and that is a line containing for a protocol and a firewall ruleset column.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-14-2005 05:51 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
squee
the amen break

Registered: Jul 2001
Location: Norfolk, VA
Posts: 4678

Well, I didn't get what I initially wanted, but

grep -i 'httpd' input.txt | awk 'BEGIN {FS=" arg="};{print $2}' | awk '{print $1}' > output.txt

gave me a list of all urls requested, which is what I needed today.

__________________
What does polite society know of the secret hearts of men?
What shows the shuttered window but all the evil you can imagine?

Report this post to a moderator | IP: Logged

Old Post 12-14-2005 09:44 PM
squee is offline Click Here to See the Profile for squee Click here to Send squee a Private Message Find more posts by squee Add squee to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

code:
#!/usr/bin/perl # # Simple script to attempt to remunge raptor firewall logs # # The only sample I have of the log format: # httpd src=192.168.1.1/4675 dst=192.168.2.1/80 sent=192 rcvd=3000 # httpd src=192.168.1.1/4675 dst=195.224.113.251/80 srcif=192.168.100.1 dstif=34.80.92.1 sent=176 rcvd=4650 # # There are apparently other variations, but hopefully the script will be simple # enough to alter to compensate for them use strict; # I like pedantry while(<STDIN> ) { my($protocol,$src,$dst,$srcif,$dstif,$sent,$rcvd); chomp; my @linesplit = split(/ /, $_); $protocol = $linesplit[0]; # assuming the first value is always the protocol foreach (@linesplit) { $src = $_ if s/src=//; $dst = $_ if s/dst=//; $srcif = $_ if s/srcif=//; $dstif = $_ if s/dstif=//; $sent = $_ if s/sent=//; $rcvd = $_ if s/rcvd=//; } # Print it print "$protocol,$src,$dst,$srcif,$dstif,$sent,$rcvd\n"; }


Should be simple enough to figure out. You can run it with "cat logile | ./raptor.pl"

The output is a comma deliminated set of values(naturally if the value isn't in the log line, you'll just get two or more commas in a row). It's not perfect though, as I suspect the switched srcif/dstif values indicate incoming or outgoing traffic. It also makes an assumption about where the protocol bit is which may be wrong.

But hey, it's what you asked for.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Last edited by macker on 12-17-2005 at 04:03 PM

Report this post to a moderator | IP: Logged

Old Post 12-17-2005 03:59 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
squee
the amen break

Registered: Jul 2001
Location: Norfolk, VA
Posts: 4678

Thanks.

You'll have to PM me your name if you want credit when this is used. Of course, that means it'll be circulated among certain places you might not want. Let me know.

__________________
What does polite society know of the secret hearts of men?
What shows the shuttered window but all the evil you can imagine?

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 01:40 AM
squee is offline Click Here to See the Profile for squee Click here to Send squee a Private Message Find more posts by squee Add squee to your buddy list [P] Edit/Delete Message Reply w/Quote
Large Filipino
Fuck me hard in my arse.

Registered: Feb 2004
Location: in colorado somewhere!
Posts: 25592

You lost me with "So."

__________________
EEEEEEEEEEEEE!!!!!

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 02:17 AM
Large Filipino is offline Click Here to See the Profile for Large Filipino Click here to Send Large Filipino a Private Message Visit Large Filipino's homepage! Find more posts by Large Filipino Add Large Filipino to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

Don't worry about credit and circulate as you wish.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 09:21 AM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
zim
-

Registered: Dec 2002
Location:
Posts: 3063

another possibility:

quote:

#!/usr/bin/perl -w
use strict;

my $inputString = shift;
my @fields = split /,/, $inputString;
print "Printing fields: " . join(', ', @fields). "\n";

while(<STDIN> )
{
chomp;
my %data = split /[ =]/, "protocol=$_";
my @output;
foreach my $field (@fields)
{
push(@output, $data{$field}) if(defined $data{$field});
}
next unless(@output);
print join(',', @output) . "\n";
}



use:
cat log | ./parse.pl protocol,src,dst

output:
Printing fields: protocol, src, dst
httpd,192.168.1.1/4675,195.224.113.251/80
httpd,192.168.1.1/4675,192.168.2.1/80

if you ask for something that doesnt exist, you get nothing.
Printing fields: protocol, joe, src, dst
httpd,192.168.1.1/4675,195.224.113.251/80
httpd,192.168.1.1/4675,192.168.2.1/80


if you want a null field in that case, you can change the foreach loop to include:
quote:

if (defined $data{$field})
{
push(@output, $data{$field});
}
else
{
push(@output, q{});
}

instead.

resulting output would be:
Printing fields: protocol, joe, src, dst
httpd,,192.168.1.1/4675,195.224.113.251/80
httpd,,192.168.1.1/4675,192.168.2.1/80

__________________
insert witty remark

Last edited by CHiPsJr on 11-09-2006 at 08:23 AM

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 01:32 PM
zim is offline Click Here to See the Profile for zim Click here to Send zim a Private Message Find more posts by zim Add zim to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

quote:
Originally posted by zim
my $inputString = shift;


Java slut

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 01:40 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

perlvert.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 12-19-2005 02:13 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
zim
-

Registered: Dec 2002
Location:
Posts: 3063

actually, i don't know a bit of java, was just trying to make it easier to digest.

__________________
insert witty remark

Last edited by CHiPsJr on 11-09-2006 at 08:23 AM

Report this post to a moderator | IP: Logged

Old Post 12-21-2005 05:13 AM
zim is offline Click Here to See the Profile for zim Click here to Send zim a Private Message Find more posts by zim Add zim to your buddy list [P] Edit/Delete Message Reply w/Quote
zim
-

Registered: Dec 2002
Location:
Posts: 3063

code:
#!/usr/bin/perl # New RFC 3092 Compliant Code! my $foo = shift; my @bar = split /,/, $foo; print join(',', @bar). "\n"; while(chomp ( my $foobar = <> )) { my (%baz, @quux); %baz = split /[ =]/, 'protocol=' . $foobar; foreach my $qux (@bar) { push @quux, $baz{$qux} if defined $baz{$qux}; push @quux, q{} unless defined $baz{$qux}; } print join(',', @quux) . "\n"; }
Satisfied?

__________________
insert witty remark

Last edited by CHiPsJr on 11-09-2006 at 08:23 AM

Last edited by zim on 12-21-2005 at 05:38 AM

Report this post to a moderator | IP: Logged

Old Post 12-21-2005 05:34 AM
zim is offline Click Here to See the Profile for zim Click here to Send zim a Private Message Find more posts by zim Add zim to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

It's better, but your bracket spacing scheme is inconsistant now . You do get points for using a hash though(as hashes are great).

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-21-2005 02:17 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
Smug Git
Arrogance Personified

Registered: Aug 2001
Location: Hilbert Space
Posts: 35561

And it's on more than one line.

__________________
I want to live and I want to love
I want to catch something that I might be ashamed of

Report this post to a moderator | IP: Logged

Old Post 12-21-2005 02:41 PM
Smug Git is offline Click Here to See the Profile for Smug Git Click here to Send Smug Git a Private Message Find more posts by Smug Git Add Smug Git to your buddy list [P] Edit/Delete Message Reply w/Quote
Large Filipino
Fuck me hard in my arse.

Registered: Feb 2004
Location: in colorado somewhere!
Posts: 25592

I feel this is fitting here.

Attachment: nerds.jpg
This has been downloaded 43 time(s).

__________________
EEEEEEEEEEEEE!!!!!

Report this post to a moderator | IP: Logged

Old Post 12-22-2005 01:42 AM
Large Filipino is offline Click Here to See the Profile for Large Filipino Click here to Send Large Filipino a Private Message Visit Large Filipino's homepage! Find more posts by Large Filipino Add Large Filipino to your buddy list [P] Edit/Delete Message Reply w/Quote
zim
-

Registered: Dec 2002
Location:
Posts: 3063

quote:
Originally posted by macker
It's better, but your bracket spacing scheme is inconsistant now . You do get points for using a hash though(as hashes are great).
how is it inconsistant? The first was a paste into a block quote with no formatting. the second was with code tags and actual formatting.

__________________
insert witty remark

Last edited by CHiPsJr on 11-09-2006 at 08:23 AM

Report this post to a moderator | IP: Logged

Old Post 12-23-2005 04:41 AM
zim is offline Click Here to See the Profile for zim Click here to Send zim a Private Message Find more posts by zim Add zim to your buddy list [P] Edit/Delete Message Reply w/Quote
squee
the amen break

Registered: Jul 2001
Location: Norfolk, VA
Posts: 4678

I've been playing around with macker's script. It is pretty easy to modify. I think I will have it processing the bulk logs by this evening.

One thing however--

As it turns out, the first few fields in every entry are the same:

quote:
Month Day Time Firewall_Name Daemon Entry_Type

So, right now I have
quote:
$date1 = $linesplit[0]
$date2 = $linesplit[1]
$time = $linesplit[2]

and so on. I am wondering however if I can't just use a regular expression:
quote:
$cruft = $linesplit[0-5]

or something. I tried it and it didn't seem to work. Anyone want to clue me in?

__________________
What does polite society know of the secret hearts of men?
What shows the shuttered window but all the evil you can imagine?

Report this post to a moderator | IP: Logged

Old Post 12-28-2005 09:21 PM
squee is offline Click Here to See the Profile for squee Click here to Send squee a Private Message Find more posts by squee Add squee to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

$cruft = join(' ', @linesplit[0..5]);

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-28-2005 11:10 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
macker
Holy Me-el

Registered: Nov 2000
Location: UK
Posts: 4736

quote:
Originally posted by zim
how is it inconsistant?


You switch between cuddled and non-cuddled form. And yes, before you point it out I have a space after the second bracket on the while loop.

__________________
Expecting people to be smart team players is like looking for double Ds in an oriental brothel.

Report this post to a moderator | IP: Logged

Old Post 12-28-2005 11:19 PM
macker is offline Click Here to See the Profile for macker Click here to Send macker a Private Message Visit macker's homepage! Find more posts by macker Add macker to your buddy list [P] Edit/Delete Message Reply w/Quote
squee
the amen break

Registered: Jul 2001
Location: Norfolk, VA
Posts: 4678

Danke!

__________________
What does polite society know of the secret hearts of men?
What shows the shuttered window but all the evil you can imagine?

Report this post to a moderator | IP: Logged

Old Post 12-29-2005 12:20 AM
squee is offline Click Here to See the Profile for squee Click here to Send squee a Private Message Find more posts by squee Add squee to your buddy list [P] Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 02:48 PM. Post New Thread    Post A Reply
  Last Thread   Next Thread
Show Printable Version | Email this Page | Subscribe to this Thread

Forum Jump:
 

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
 

< Contact Us - The Asylum >

Powered by: vBulletin Version 3.0.6
Copyright ©2000 - 2002, Jelsoft Enterprises Limited.
Copyright © 2000- Imaginet Inc.
[Legal Notice] | [Privacy Policy] | [Site Index]