File: mlogc-batch-load.pl.in

package info (click to toggle)
libapache-mod-security 2.5.12-1%2Bsqueeze4
  • links: PTS
  • area: main
  • in suites: squeeze-lts
  • size: 6,292 kB
  • ctags: 2,537
  • sloc: ansic: 21,266; sh: 6,512; xml: 6,320; perl: 1,653; makefile: 191
file content (151 lines) | stat: -rwxr-xr-x 3,569 bytes parent folder | download | duplicates (2)
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
#!@PERL@
#
# ModSecurity for Apache 2.x, http://www.modsecurity.org/
# Copyright (c) 2004-2009 Breach Security, Inc. (http://www.breach.com/)
#
# This product is released under the terms of the General Public Licence,
# version 2 (GPLv2). Please refer to the file LICENSE (included with this
# distribution) which contains the complete text of the licence.
#
# There are special exceptions to the terms and conditions of the GPL
# as it is applied to this software. View the full text of the exception in
# file MODSECURITY_LICENSING_EXCEPTION in the directory of this software
# distribution.
#
# If any of the files related to licensing are missing or if you have any
# other questions related to licensing please contact Breach Security, Inc.
# directly using the email address support@breach.com.
#

use strict;
use File::Find qw(find);
use File::Spec::Functions qw(catfile);
use Sys::Hostname qw(hostname);
use Digest::MD5 qw(md5_hex);

my $ROOTDIR = $ARGV[0] || '';
my $MLOGC = $ARGV[1] || '';
my $MLOGCCONF = $ARGV[2] || '';
my @AUDIT = ();

if ($ROOTDIR eq '' or ! -e $MLOGC or ! -e $MLOGCCONF) {
	printf STDERR "\nUsage: $0 <rootdir> </path/to/mlogc> <mlogc_config>\n\n";
	exit 1;
}

open(MLOGC, "|$MLOGC -f $MLOGCCONF") or die "ERROR: could not open '$MLOGC' - $!\n";

find(
	{
		wanted => sub {
			my($fn,$dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size);

			(($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size) = stat($_)) &&
			-f _ &&
			/^\d{8}-\d+-\w{24}$/s
			&& (($fn = $File::Find::name) =~ s/^\Q$ROOTDIR\E//)
			&& push(@AUDIT, [$fn, $size]);
		},
		follow => 1,
	},
	$ROOTDIR
);

for my $audit (@AUDIT) {
	my $fn = $audit->[0];
	my $line = "";
	my $err = 0;
	my $ln = 0;
	my $sln = 0;
	my $sect = "";
	my $data = "";
	my %data = (
		hostname => hostname(),
		remote_addr => "-",
		remote_user => "-",
		local_user  => "-",
		logtime => "-",
		request => "-",
		response_status => "-",
		bytes_sent => "-",
		referer => "-",
		user_agent => "-",
		uniqueid => "-",
		sessionid => "-",
		audit_file => $fn,
		extra => "0",
		audit_size => $audit->[1],
		md5 => "-",
	);

	### Parse the audit file in an attempt to recreate the original log line
	open (AUDIT, "<".catfile($ROOTDIR,$fn)) or $err = 1;
	if ($err == 1) {
		print STDERR "ERROR: could not open '$fn' - $!\n";
		next;
	}

	while($line = <AUDIT>) {
		$data .= $line;
		chop $line;
		$ln++;
		$sln++;
		if ($line =~ m%^--[0-9A-Fa-f]{8}-([A-Z])--$%) {
			$sect = $1;
			$sln = 0;
			next;
		};
		if ($sect eq 'A') {
			if ($line =~ m%^(\[[-\d/: a-zA-Z]{27}\]) (\S+) (\S+) (\d+) (\S+) (\d+)%) { 
				$data{logtime} = $1;
				$data{uniqueid} = $2;
				$data{remote_addr} = $3;
			}
			next;
		}
		elsif ($sect eq 'B') {
			if ($sln == 1) {
				$data{request} = $line;
			}
			elsif ($line =~ m%^User=Agent: (.*)%i) {
				$data{user_agent} = $1;
			}
			elsif ($line =~ m%^Referer: (.*)%i) {
				$data{referer} = $1;
			}
			next;
		}
		elsif ($sect eq 'F') {
			if ($sln == 1 and $line =~ m%^\S+ (\d{3})\D?.*%) {
				$data{response_status} = $1;
			}
			elsif ($line =~ m%^Content-Length: (\d+)%i) {
				$data{bytes_sent} = $1;
			}
			next;
		}
	}
	$data{md5} = md5_hex($data);

	printf MLOGC (
		"%s %s %s %s %s \"%s\" %s %s \"%s\" \"%s\" %s \"%s\" %s %s %s md5:%s\n",
		$data{hostname},
		$data{remote_addr},
		$data{remote_user},
		$data{local_user},
		$data{logtime},
		$data{request},
		$data{response_status},
		$data{bytes_sent},
		$data{referer},
		$data{user_agent},
		$data{uniqueid},
		$data{sessionid},
		$data{audit_file},
		$data{extra},
		$data{audit_size},
		$data{md5},
	);
       
}