We have an Application called 'Tripwire' to monitor Security Compliance & Change. It was set up by another Deptmt. It is based on RegEx's, and my job is to ensure the RegEx's that are in it are working correctly. I've made progress, but have run into an issue I could use help with.

I have a script feed a RegEx from the command line which works.

My test passwd file (To test only root has UID=0)

$ cat passwd root:x:0:0:ifeuu1 root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

The Script

#!/usr/bin/perl # perl-grep4.pl my $pattern = shift @ARGV; my $regex = eval { qr/$pattern/ }; die "Check your pattern! $@" if $@; while( <> ) { if( m/$regex/ ) { print "$_"; print "\t\t\$&: ", substr( $_, $-[0], $+[0] - $-[0] ), "\n"; foreach my $i ( 1 .. $#- ) { print "\t\t\$$i: ", substr( $_, $-[$i], $+[$i] - $-[$i] ), "\n"; } } }

Usage & Output

$ Test5.pl '(?<!\broot:)x:0:' passwd noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash $&: x:0:

However, trying to get a script which is feed the RegEx from a file is failing

My RegEx-File

PW:root:passwd:(?<!\broot:)x:0:

The script

#!/usr/bin/perl #use strict; #use warnings; use Text::ParseWords; my ($RECORD, @FIELDS, $RE); my $DFile="/home/infosec/data/RegEx"; open (DATA, "<$DFile") || die ("Cannot open DATA file \n"); my @LINES=<DATA>; my $tLINES=@LINES; foreach $RECORD (@LINES[0..$tLINES-1]) { @FIELDS=split(/:/, "$RECORD"); my $TNAME=$FIELDS[0]; my $TVALU=$FIELDS[1]; my $TFILE=$FIELDS[2]; my $REGEX=$FIELDS[3]; chomp($REGEX); my $regex = eval { qr/$REGEX/ }; print "$regex \n"; open (TFILE, "<$TFILE") || die ("Cannot open Test file \n"); while (<TFILE>) { if( m/$regex/ ) { print "$_"; print "\t\t\$&: ", substr( $_, $-[0], $+[0] - $-[0] ), "\n"; foreach my $i ( 1 .. $#- ) { print "\t\t\$$i: ", substr( $_, $-[$i], $+[$i] - $-[$i] ), "\n"; } } } print "\n"; } print "\n";

The output

$ Test6.pl root:x:0:0:ifeuu1 root:/root:/bin/bash $&: bin:x:1:1:bin:/bin:/sbin/nologin $&: noncom:x:0:0:cause RegEx Fail:/noncom:/bin/bash $&: daemon:x:2:2:daemon:/sbin:/sbin/nologin $&: adm:x:3:4:adm:/var/adm:/sbin/nologin $&: lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin $&: sync:x:5:0:sync:/sbin:/bin/sync $&: shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown $&:

This matches all lines instead of only the third, and I had trouble getting strict & warning to run clean. Could anyone help?


In reply to Test RegEx by Saved

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.