#!/usr/bin/perl
print "Content-type: text/html \n\n";

##################################################################
## Script to fill file for the top10 attackers presentation	##
## The data is taken from the file /attacks/ids/<ban_ip.txt>	##
## The FORMAT:							##
## IP-address attack type Date-Time				##
##################################################################

use IO::File;
use Socket;
use File::Copy;
use strict;

my ($i,$j,$p,$pp,$ppp,$N,$M,$k,$m,$y,$line,$ref,$ref0,$where,$string,$str,$str1,$new_max,$n,$lin,$symb,$datime);
my (@lines,@words,@word0,@word1,@word2,@word3,@records,@templ);
$j=0;
$y=0;		# records index

$p="/home/httpd/htdocs/saturn/attacks/ids/ban_ip.txt";
$pp ="/home/httpd/htdocs/saturn/safe/top10_new_0.htm";		# file-template
$ppp="/home/httpd/htdocs/saturn/safe/top10_new.htm";		# file-new
open(TMP,$pp) or die "Can't open template : $!";
sysopen(Mnm, $ppp, O_RDWR|O_TRUNC) or die "Can't open output : $!";
#print "Open\n";
for($n=0; $n<22; $n++)	{
		$templ[$n]=readline(TMP);			# read file beginning
			}

for($n=0; $n<22; $n++)	{
print Mnm $templ[$n] or die "can not do anything $!";	# Write file-top
			}
#print "R=",$templ[1],"\n";
$lin=readline(TMP);					# read first parameter line

print "F_line=", $lin,"\n";

open(LN,$p) or die "Can't open : $!";	# Open file ban_ip.txt
while(!eof(LN))		{	#########################
$line=readline(LN);					# Read file lines
$records[$y]=$line;					# Write records array
chomp($line);						#
$y++;						 	# $y - record number in the file ban_ip.txt
			}	#########################
#print "N=",$y," ref=",$ref, "\n";
close(LN);

$j=0;
for($i=($y-10);$i<$y;$i++)	{		#########################
		$line=$records[$i];		# Data lines		#
#print "L=",$line,"\n";
		@words=split / /,$line;					#
		chomp($words[3]);					#
		$word0[$j]=$words[0];					#
		$word1[$j]=$words[1];					#
		$datime=$word0[0]." ".$word1[0];			#
		$word2[$j]=$words[2];					#
		$word3[$j]=$words[3];					#
#	print "FL=",$lines[$j],"\n";					#
		$j++;							#
					}	#########################
#print "word0=",$word0[0],"\n";
#print "word1=",$word1[0],"\n";
#print "word2=",$word2[0],"\n";
#print "word3=",$word3[0],"\n";

# First record

$lin=~s/75\.126\.24\.9/$word3[0]/;		# Rewrite IP
my $sub="ID='N1'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF1=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym1=",$symb,"\n";
$string=substr($lin,0,$ref+30);			# copy line up to the symbol
$str=$string.$word2[0];				# add new symbol
$str1=substr($lin,$ref+31);			# Select tail of line
#print "str1=",$str1,"\n";
$str=$str.$str1;				# Write tail og line
#print "LINE=",$str,"\n";
#print "Datime=",$datime,"\n";
#print "line1=",$lines[0],"\n";
$str=~s/9-8-2013\s13:40:21/$datime/;
#print "final1=",$str,"\n";
print Mnm $str;					# Write first line

# Second record

$lin=readline(TMP);				# read next parameter line
$lin=~s/177\.139\.218\.216/$word3[1]/;		# change IP
#print "LINE=",$lin,"\n";
my $sub="ID='N2'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF2=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym2=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[1];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[1]." ".$word1[1];
$str=~s/9-8-2013\s7:0:15/$datime/;
#print "final2=", $str,"\n";
print Mnm $str;					# Write second line

# Record 3

$lin=readline(TMP);				# read next parameter line
$lin=~s/66\.197\.253\.121/$word3[2]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N3'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF3=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym3=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[2];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[2]." ".$word1[2];
$str=~s/8-8-2013\s15:20:6/$datime/;
#print "final3=", $str,"\n";
print Mnm $str;					# Write 3-th line

#record 4

$lin=readline(TMP);				# read next parameter line
$lin=~s/208\.109\.207\.184/$word3[3]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N4'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF4=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym4=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[3];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[3]." ".$word1[3];
$str=~s/7-8-2013\s15:21:45/$datime/;
#print "final4=", $str,"\n";
print Mnm $str;					# Write 4-th line

# Recorg 5

$lin=readline(TMP);				# read next parameter line
$lin=~s/213\.165\.68\.77/$word3[4]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N5'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF5=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym5=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[4];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[4]." ".$word1[4];
$str=~s/7-8-2013\s15:1:46/$datime/;
#print "final5=", $str,"\n";
print Mnm $str;					# Write 5-th line

# Record 6

$lin=readline(TMP);				# read next parameter line
$lin=~s/64\.34\.178\.78/$word3[5]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N6'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF6=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym6=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[5];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[5]." ".$word1[5];
$str=~s/7-8-2013\s14:41:46/$datime/;
#print "final6=", $str,"\n";
print Mnm $str;					# Write 6-th line

# Record 7

$lin=readline(TMP);				# read next parameter line
$lin=~s/218\.25\.83\.197/$word3[6]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N7'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF7=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym7=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[6];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[6]." ".$word1[6];
$str=~s/7-8-2013\s7:1:42/$datime/;
#print "final7=", $str,"\n";
print Mnm $str;					# Write 7-th line


# Record 8

$lin=readline(TMP);				# read next parameter line
$lin=~s/41\.141\.249\.251/$word3[7]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N8'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF8=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym8=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[7];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[7]." ".$word1[7];
$str=~s/6-8-2013\s7:1:21/$datime/;
#print "final8=", $str,"\n";
print Mnm $str;					# Write 8-th line

# Record 9

$lin=readline(TMP);				# read next parameter line
$lin=~s/178\.19\.251\.121/$word3[8]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N9'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF9=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym9=",$symb,"\n";
$string=substr($lin,0,$ref+30);
$str=$string.$word2[8];
$str1=substr($lin,$ref+31);
$str=$str.$str1;
$datime=$word0[8]." ".$word1[8];
$str=~s/6-8-2013\s0:21:20/$datime/;
#print "final9=", $str,"\n";
print Mnm $str;					# Write 9-th line

# Record 10

$lin=readline(TMP);				# read next parameter line
$lin=~s/5\.10\.83\.11/$word3[9]/;		# change IP
#print "LINE=",$lin,"\n";
$sub="ID='N10'><font color=lightblue>";
$ref=index($lin,$sub);
#print "REF10=",$ref,"\n";
$symb=substr($lin,$ref+30,1);
#print "sym10=",$symb,"\n";
$string=substr($lin,0,$ref+31);
$str=$string.$word2[9];
$str1=substr($lin,$ref+32);
$str=$str.$str1;
$datime=$word0[9]." ".$word1[9];
$str=~s/2-8-2013\s8:40:14/$datime/;
#print "final10=", $str,"\n";
print Mnm $str;						# Write 10-th line

for($n=0; $n<13; $n++)	{
			$templ[$n]=readline(TMP);	# read file tail
			}
for($n=0; $n<13; $n++)	{
			print Mnm $templ[$n];		# write file tail
			}


close(TMP);
close (Mnm);
print "END\n";
