#!/usr/bin/perl -w #!C:\Perl\bin\perl.exe -w ################################################################ ################################################################ # # # Copyright 2002 Rootsecure.net # # # # You may use / modified this code as long as you leave this # # message in the source. # # By using this code you agree to indemnify rootsecure.net # # from any liability that might arise from its use. # # # ################################################################ ################################################################ # # # This script can be used to view Apache log files. # # You must change the “log_file” variable to point to your log # # files. # # # # Before running the script you should either set the "my_ip" # # variable, or comment out that section of the script if you # # have an alternate method of security. # # # # The script is designed to only show you new hits since you # # last viewed the log page. # # # # The text input box is provided to allow you to view # # previously looked at logs. # # # ################################################################ ########## Variables########## $log_file = "/usr/local/apache/log.txt"; $config_file = "log_config.txt"; $my_lan_ip = "192.168.0.1"; $my_ip = "000.000.000.000"; $site_name = "Rootsecure.net"; ########## Variables########## #####Standard line print "Content-type: text/html\n\n"; #####Only work from owners IP if (defined $ENV{'REMOTE_ADDR'}) { unless (($ENV{'REMOTE_ADDR'} eq $my_ip) || ($ENV{'REMOTE_ADDR'} eq $my_lan_ip)) { print "Error: Wrong Access Credentials"; exit; } } if (-e $config_file) { open(DAT, $config_file); $config_log_line=; close(DAT); } else { open(DAT,">>$config_file"); print DAT "0"; close(DAT); $config_log_line=1; } #$lines=''; #foreach $line (@config) { $lines=$lines.$line } &get_post_data; &html_head_display; if (-e "$log_file") { open(DAT, $log_file); @log=; close(DAT); $num_log_lines=@log; if ($config_log_line > $num_log_lines) { open(DAT,">$config_file"); print DAT "0"; close(DAT); } else { unless ($in{'config_num'}) { open(DAT,">$config_file"); print DAT "$num_log_lines"; close(DAT); } } } else { print "Error log file does not exist"; exit; } if ($in{'config_num'}) { $config_log_line = $in{'config_num'}; } for ($count=$config_log_line; $count < $num_log_lines;) { for $log_item ($log[$count]) { $log_item =~ s//>/g; ($log_line{'ip'},$null,$null,$log_line{'date_time'},$null,$log_line{'type'},$log_line{'requested_file'},$log_line{'protocol'},$log_line{'status'},$null,$log_line{'referer'},@user_agent)=split(/\ /o,$log_item); foreach $key (%log_line) { unless (defined @user_agent) { @user_agent='-'; }; unless (defined $log_line{$key}) { $log_line{$key}='-'; }; } $log_line{'date_time'} =~ s/\[|\]//; ($log_line_date_time{'p1'},$log_line_date_time{'p2'},$log_line_date_time{'p3'})=split(/\//,$log_line{'date_time'}); ($log_line_date_time{'p4'},$log_line_date_time{'p5'},$log_line_date_time{'p6'},$log_line_date_time{'p7'})=split(/:/,$log_line_date_time{'p3'}); $log_line{'date_time'} = "$log_line_date_time{'p1'} # $log_line_date_time{'p5'}:$log_line_date_time{'p6'}:$log_line_date_time{'p7'}"; $log_line{'user_agent'}=''; foreach $part (@user_agent) { $log_line{'user_agent'}=$log_line{'user_agent'}.$part.' '; } chop($log_line{'user_agent'}); chop($log_line{'user_agent'}); $log_line{'type'} =~ s/"//; $log_line{'protocol'} =~ s/"//; $log_line{'referer'} =~ s/"//g; $log_line{'user_agent'} =~ s/"//g; $user_agent[0] =~ s/"//g; $font_start=''; $font_stop=''; if (($log_line{'status'} =~ /404/) || ($log_line{'status'} =~ /403/) || ($log_line{'status'} =~ /500/) || ($log_line{'type'} =~ /POST/) || ($log_line{'requested_file'} =~ /admin/) || ($log_line{'requested_file'} =~ /http\:\/\//) || ($log_line{'requested_file'} =~ /\.\./)) { $font_start = ''; $font_stop = ''; } if (($log_line{'requested_file'} =~ /index/)) { $font_start = ''; $font_stop = ''; } $log_line{'all_referer'} = $string = $log_line{'referer'}; $string = $log_line{'date_time'}; $avail_chars = 13; &chop_excess; &sort_lengths; $log_line{'date_time'} = $string; $string = $log_line{'ip'}; $avail_chars = 13; &chop_excess; &sort_lengths; $log_line{'ip'} = $string; $string = $user_agent[0]; $avail_chars = 7; &chop_excess_start; &sort_lengths; $user_agent[0] = $string; $string = $log_line{'type'}; $avail_chars = 4; &chop_excess; &sort_lengths; $log_line{'type'} = $string; $string = $log_line{'status'}; $avail_chars = 3; &chop_excess; &sort_lengths; $log_line{'status'} = $string; $string = $log_line{'referer'}; $avail_chars = 30; &chop_excess_start; &sort_lengths; $log_line{'referer'} = $string; $string = $log_line{'requested_file'}; $avail_chars = 70; &sort_lengths; $log_line{'requested_file'} = $string; $string = $log_line{'user_agent'}; $avail_chars = 90; &sort_lengths; $log_line{'user_agent'} = $string; $string = $log_line{'all_referer'}; $avail_chars = 0; &sort_lengths; $log_line{'all_referer'} = $string; print "$font_start$log_line{'date_time'} # $log_line{'ip'} # @user_agent[0] # $log_line{'type'} # $log_line{'status'} # $log_line{'referer'} # $log_line{'requested_file'} # $log_line{'user_agent'} # $log_line{'all_referer'}$font_stop\n"; } $count++ } &html_end_display; sub get_post_data { my $method = $ENV{'REQUEST_METHOD'}; if ($method eq "POST") { read(STDIN, $buffer_comments, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer_comments); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $in{$name} = $value; } } } # end get_post_data sub chop_excess_start { $chars = length ($string); if ( $chars > $avail_chars) { $string = substr($string, 0, $avail_chars); } } sub chop_excess { $chars = length ($string); if ( $chars > $avail_chars) { $over_by = $avail_chars - $chars; $string = substr($string, -$over_by); } } sub sort_lengths { $chars = length ($string); $chars = $avail_chars - $chars; $space = " " x $chars; $string = $string . $space; } # end sort_lengths sub html_head_display { unless ($ENV{'REQUEST_URI'}) { $ENV{'REQUEST_URI'} = '?'; } if ($in{'config_num'}) { $config_log_line = $in{'config_num'}; } print < $site_name - Logs
$site_name Referer Logs

ENDHTML
} # end html_head_display

sub html_end_display {
    print <



ENDHTML

} # end html_end_display