LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-08-2010, 10:09 PM   #1
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Rep: Reputation: 31
Parse log file with bash


I'm not a programmer and barely a scripter (compared to many of the God here) so I'm having a hard time deciding what I should do in parsing the snare.log file. I only need a bit of information from certain lines. For instance when someone logs in with SSH, I want to know when, who, successful/unsuccessful and from where.

So one entry from the snare log looks like this:

Code:
vm-rhel5        LinuxKAudit     criticality,0   event,login_start,20100907 22:22:43     uid,0,root      id,     gid,0,root      euid,,root      egid,,root   process,32519,ssh        return,0,yes    acct,mijohnst   addr,127.0.0.1  auid,500,mijohnst       exe,/usr/sbin/sshd      hostname,vm-rhel5       msg,PAM session open  subj,system_u:system_r:unconfined_t:s0-s0:c0.c1023      terminal,ssh
Of coarse everything I need is in this entry, but how do I pick through what I don't need. I've written a short command to help.

Code:
egrep "return\,0\,yes" /var/log/audit/audit.log | awk '{print "Date = " $5" "$4"\t\tUID = "$13"\t\tFrom = "$14}'
The problem with this command is that the output data isn't always located in the same column. So UID might be $21 for one entry and $22 for the next. An example would be like:

Code:
Date = 22:22:43 event login_start 20100907              UID = acct mijohnst             From = addr 127.0.0.1
Date = 22:22:43 event login_auth 20100907               UID = addr 127.0.0.1            From = auid 500 mijohnst
So I guess my question is, what's the best way to proceed? I'm thinking somehow I should be able to write a function that assigns the beginning of certain columns with a veritable, ie. any line beginning with "acct" would automatically be written into a variable called $ACCOUNT. Does that make sense?

I don't expect anyone to know this, I guess I'm still trying to solve it out in my head and it's helping to write it down, think about it and hope someone might have a direction to point me. As always, thanks again!
 
Old 09-08-2010, 10:16 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
There should be facilities already created to parse snare log files. Have you check tools like sawmill etc ?
 
1 members found this post helpful.
Old 09-08-2010, 10:46 PM   #3
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Well, you have to buy sawmill and I was looking at something to work with that I already have... Also, I audit and roll my logs every week. Thanks for the suggestion though.

Last edited by mijohnst; 09-08-2010 at 10:49 PM.
 
Old 09-09-2010, 02:16 AM   #4
quanta
Member
 
Registered: Aug 2007
Location: Vietnam
Distribution: RedHat based, Debian based, Slackware, Gentoo
Posts: 724

Rep: Reputation: 101Reputation: 101
Quote:
Originally Posted by mijohnst View Post
For instance when someone logs in with SSH, I want to know when, who, successful/unsuccessful and from where.
Because of this purpose, I suggest you use OSSEC.
 
1 members found this post helpful.
Old 09-09-2010, 09:29 AM   #5
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Thanks for the suggestion Quanta. OSSEC looks good but I don't want to have to install agents, enable httpd or anything like that. I have a whole security process that I would have to go through in order to allow use of something like OSSEC or Sawmill. I want to keep it simple...which means just parse the snare.log file for the week and then roll them off the machines. Anyway, thanks for the suggestion.
 
Old 09-09-2010, 11:48 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
I'm reasonably fluent at parsing text strings with bash but would choose awk for this task; it could be done in bash but would be difficult to write in a transparent way and so difficult to maintain. Please say if using awk is not acceptable for you and I'll see if anything half-comprehensible can be written in bash but I'll need an example of every type of line from the log.
 
0 members found this post helpful.
Old 09-09-2010, 12:37 PM   #7
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
in that case i would probably use cut with a for loop and an if statement to check for the word UID, then base the rest of the parsing for the line from the results of that

in pseudo code
Code:
for [c=1;c<=(a large number);c=c+1]
do
   if [`cut (delimeter = space colum=$c)` == 'UID']
   then
        uidcolum=c
        break
   fi
done
this might not be the best idea but it should work note the backtics (`) tell the shell that the part between them is a shell command

Last edited by frieza; 09-09-2010 at 12:39 PM.
 
1 members found this post helpful.
Old 09-09-2010, 06:08 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python
Code:
tags=['acct','addr']
for line in open("file"):
    s=line.split()
    print s[3],s[4],
    for item in s:
        if item[:4] in tags:
            print item,
 
1 members found this post helpful.
Old 09-09-2010, 10:46 PM   #9
mijohnst
Member
 
Registered: Nov 2003
Location: Huntsville, AL
Distribution: RHEL, Solaris, OSX, SuSE
Posts: 419

Original Poster
Rep: Reputation: 31
Thanks very much for the responses guys! Some very good ideas here. I wish I knew more about python because I've heard it's powerful. I think however I'm going to go with the awk script...only because I know more about it and I have the sed/awk books on hand. When I figure it out I'll post it here In hopes it will help others.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help w/ script to read file and parse log message shyork2001 Linux - General 4 04-06-2010 11:48 AM
Parse log file for some error lovelysandu Programming 10 05-26-2009 05:56 AM
Tool to parse squid log file. linuxlover.chaitanya Linux - Software 3 02-08-2009 11:40 PM
Simple parse of html file using bash ericcarlson Linux - Software 2 05-07-2008 09:44 AM
parse log file hourly onewave Programming 2 03-28-2005 01:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:18 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