#!/usr/bin/perl # You may have to change the line above to refer to where perl # is located on your system. At the Unix prompt, type whereis perl # and a list of locations of perl on your server will be shown. # Change the line above to match one of those locations. ##KloakItPAGE.CGI##################################################### # # (C) Copyright 2000 Volatile Graphix, Inc. All rights reserved. # This script may not be copied or modified in any way without # permission in writing from Volatile Graphix, Inc. # # Limitation of Liability: KloakIt is to be used at your own risk # Volatile Graphix, Inc. d.b.a. KloakIt expressly disclaims any and # all liability from any and all consequences arising from the use # of this and all scripts that Volatile Graphix, Inc. d.b.a. KloakIt # have written. By using this script in whole or in part, the user # hereby agrees to abide by this statement. # ###################################################################### ###################################################################### # OPTIONAL # If you change the name of the data directory, make $writedir equal # to the new name and path of the directory. $writedir = "data"; ###################################################################### ############# NO EDITING NECESSARY BELOW HERE ######################## ############# NO EDITING NECESSARY BELOW HERE ######################## ############# NO EDITING NECESSARY BELOW HERE ######################## ###################################################################### #open (STDERR, ">>pageerrorlog.txt"); if ($ENV{'SERVER_SOFTWARE'} =~ /Win/i) {$winserver = 1;} use LWP::Simple; use LWP::UserAgent; use HTTP::Date; open (R, "$writedir/config.daq"); flock (R, 2) unless($winserver); $config = ; flock (R, 8) unless($winserver); close (R); chomp($config); ($redir, $timeModifier, $suckerURL) = split (/\t/, $config); $temp=""; $keywordarray=""; $tobedisplayed=""; $ifMail=0; $notify=0; $autoIPupdateRequired=0; $IPupdateURL = "http://www.iplists.com/nw"; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time+ (3600*$timeModifier)); # set time $sec;$yday;$isdst;$wday; # second use of vars to stop errors $mon++; if ($min !~ /^..$/) {$min= '0'.$min;} if ($hour !~ /^..$/) {$hour= '0'.$hour;} if ($mday !~ /^..$/) {$mday= '0'.$mday;} if ($mon !~ /^..$/) {$mon= '0'.$mon;} if ($year !~ /^..$/) {$year= '0'.$year;} $year =~ s/.+(..)$/$1/; if ($year < 90) { $year = "20" . $year; } $date="$mon/$mday/$year, $hour:$min"; $nowdate = "$year-$mon-$mday"; $loc = "http://" . $ENV{'SERVER_NAME'}; $myname = $ENV{'QUERY_STRING'}; if ($myname eq "") { exit; } $myname =~ s/^\/.*\///; $myname =~ s/^\///; $gethost = $ENV{'REMOTE_HOST'}; $getaddr = $ENV{'REMOTE_ADDR'}; $ref = $ENV{'HTTP_REFERER'}; $agent = $ENV{'HTTP_USER_AGENT'}; $localdirectory = $ENV{'SCRIPT_NAME'}; $localdirectory =~ s/^(.+)\/page\.cgi$/$1/; $localdirectory =~ s/\/page\.cgi//; $ext = $myname; $ext =~ s/.*(\..*)$/$1/; if (-e("$writedir/customloc.daq")){ open (CUSTLOC, "<$writedir/customloc.daq"); $customloc = ; close (CUSTLOC); } else { $customloc = ""; } if (-e "$writedir/mail.daq"){ open (MAIL, "<$writedir/mail.daq"); flock (MAIL, 2) unless $winserver; $mailinfo = ; flock (MAIL, 8) unless $winserver; chomp($mailinfo); ($mailProgram, $email) = split(/\t/,$mailinfo); if (($mailProgram ne "") and ($email =~ /.+@.+\..+/)){ $ifMail = 1; } } open (SEP, "$writedir/sep.daq"); flock (SEP, 2) unless $winserver; $sep = ; flock (SEP, 8) unless $winserver; close (SEP); chomp($sep); if (-e "$writedir/mailsettings.daq"){ open (MAILSETTINGS, "<$writedir/mailsettings.daq"); flock (MAILSETTINGS, 2) unless $winserver; $mailsettings= ; flock (MAILSETTINGS, 8) unless $winserver; close (MAILSETTINGS); chomp($mailsettings); ($unknownUA, $unknownIP, $noRef) = split(/\t/,$mailsettings); } @messagebody = (); if (-e "$writedir/uaredirect.daq"){ open (UA, "<$writedir/uaredirect.daq"); flock (UA, 2) unless $winserver; @UAredirect=; flock (UA, 8) unless $winserver; close (UA); if (@UAredirect>0){ $UARedirection=1; } } if (-e "$writedir/uaredirectbot.daq"){ open (UA, "<$writedir/uaredirectbot.daq"); flock (UA, 2) unless $winserver; @UAredirectbot=; flock (UA, 8) unless $winserver; close (UA); if (@UAredirectbot>0){ $UARedirectionbot=1; } } if (-e "$writedir/refererredirect.daq"){ open (REFERER, "<$writedir/refererredirect.daq"); flock (REFERER, 2) unless $winserver; @refererRedirects=; flock (REFERER, 8) unless $winserver; close (REFERER); if (@refererRedirects>0){ $rrd=1;} } if (-e "$writedir/aipufc.daq"){ open (IPUPDATE, "<$writedir/lastipupdate.txt"); $lastupdate = ; close (IPUPDATE); ($lastYear, $lastMonth, $lastDay) = split ($lastupdate, /-/); if ($lastupdate ne $nowdate) {$autoIPupdateRequired = 1;} } if (-e "$writedir/randompages.txt"){ open (RA, "$writedir/randompages.txt"); @randompages = ; @randompagesInUse = @randompages; close (RA); } if (-e "$writedir/nolistlinks.daq") { $listLinks = "0";} else { $listLinks = "1";} open (REDIR, "$writedir/altredir.txt"); @redirlist=; close (REDIR); $stop=0; foreach (@redirlist){ chomp; ($pageName,$altRedir,$customTemplate,$randomSeed) = split (/#/, $_); if ($myname eq $pageName) { if ($altRedir ne "") { $redir = $altRedir; $altRedirection = 1;} if ($customTemplate ne "") { $altTempl = 1; } $randseed = $randomSeed; last; } } foreach (@redirlist){ chomp; ($linkName,$tempfoo,$tempfoo2,$tempfoo3) = split (/#/, $_); push (@cloakedURLs, $linkName); } @cloakedURLsInUse = @cloakedURLs; open (K, "$writedir/gr.daq"); flock (K, 2) unless($winserver); $c = ; flock (K, 8) unless($winserver); close (K); if (-e "$writedir/nocache.daq") { $nocache = 1; } unless (($c =~ /$ENV{'SERVER_NAME'}/) or ($c =~ /kk.+ .+/)) { open (A, "$writedir/gra.daq"); flock (A, 2) unless($winserver); @gra = ; flock (A, 8) unless($winserver); close (A); } if ($localdirectory =~ /\.s?html?$/) { $localdirectory =~ s/.+.s?html?$//; } if (-e "$writedir/nocloak.daq"){ $nocloakcheck=1; open (NOCLOAK, "$writedir/nocloak.daq"); @nocl = ; close (NOCLOAK); foreach (@nocl){ chomp; ($enginename, $switch) = split(/\t/, $_); $nocloak{$enginename} = $switch; } } @exclude = ("204.123.9.65", # @exclude is for certain applications "204.123.9.66", # sponsored by search engines such "204.123.9.67", # as Babelfish that translate pages into "204.123.9.68", # other languages and display them. You "204.123.9.106", # don't want humans seeing this, so I "204.123.9.107", # made an exclude list. "204.152.191.27", "204.152.191.28", "204.152.191.29", "204.152.190.27", "204.152.190.28", "204.152.190.29", "204.152.190.37", "204.152.190.154", "204.162.96.104", "204.162.96.154", "204.162.96.176", "209.247.194.35", "209.247.194.100", "64.208.35.5"); if (-e "$writedir/customexclude.daq"){ open (CUSTOMEXCLUDE, "$writedir/customexclude.daq"); @customexclude = ; close (CUSTOMEXCLUDE); } else { open (CUSTOMEXCLUDE, "+>$writedir/customexclude.daq"); close (CUSTOMEXCLUDE); chmod (0777, "$writedir/customexclude.daq"); } open (SUCKERLIST, "$writedir/suckerlist.txt"); flock(SUCKERLIST, 2) unless($winserver); @suckerlist = ; flock(SUCKERLIST, 8) unless($winserver); close(SUCKERLIST); if ((-e "$writedir/langredirect.daq") and ($ENV{'HTTP_ACCEPT_LANGUAGE'} ne "")){ @languagecodes = split (/,/,$ENV{'HTTP_ACCEPT_LANGUAGE'}); open (LANGREDIRECT, "<$writedir/langredirect.daq"); flock (LANGREDIRECT, 2) unless $winserver; @langredirection = ; flock (LANGREDIRECT, 8) unless $winserver; close(LANGREDIRECT); foreach (@langredirection){ chomp; ($langcode, $langurl) = split(/###/, $_); if ($languagecodes[0] eq $langcode){ $languageRedirectURL = $langurl; last; } } } if ($gethost eq "") { $gethost = $getaddr; } for ($count=1; $count<=10; $count++){ open (ENGINELIST, "$writedir\/$count" . ".list"); flock(ENGINELIST, 2) unless($winserver); @enginelist1= if $count eq 1; @enginelist2= if $count eq 2; @enginelist3= if $count eq 3; @enginelist4= if $count eq 4; @enginelist5= if $count eq 5; @enginelist6= if $count eq 6; @enginelist7= if $count eq 7; @enginelist8= if $count eq 8; @enginelist9= if $count eq 9; @enginelist10= if $count eq 10; flock(ENGINELIST, 8) unless($winserver); close (ENGINELIST); } open (UAS, "<$writedir/uas.daq"); flock (UAS, 2) unless $winserver; @UA = ; flock (UAS, 8) unless $winserver; close (UAS); # MAIN PROGRAM ###################################################################### ###################################################################### # Do not cloak if cloaking is turned off. open (IFCL, "<$writedir/ifcl.daq"); flock (IFCL, 2) unless($winserver); $ifcl = ; flock (IFCL, 8) unless($winserver); close (IFCL); if (!$ifcl) { &detect; &robot; exit; } # Detect if requestor is a robot or a normal person &detect; # Detect if on Sucker List &sucker; # Display normal page to normal person or entrance page to robot if (($isRobot ne "0") and ($isSucker eq "0")) { &robot; } else { &normal; } # Do an automatic IP update if required &autoipupdate if ($autoIPupdateRequired); exit; ###################################################################### # DETECT IF REQUESTOR IS ROBOT OR NORMAL ###################################################################### ###################################################################### sub detect{ $isRobot = 0; $classCclient = $getaddr; $classCclient =~ s/\.\d+$//; for ($count=1;$count<=11;$count++){ @list = @enginelist1 if $count eq 1; # MSN $SE = "MSN" if $count eq 1; @list = @enginelist2 if $count eq 2; # Alta Vista $SE = "Alta Vista" if $count eq 2; @list = @enginelist3 if $count eq 3; # Lycos $SE = "Lycos" if $count eq 3; @list = @enginelist4 if $count eq 4; # Inktomi $SE = "Inktomi" if $count eq 4; @list = @enginelist5 if $count eq 5; # WiseNut $SE = "WiseNut" if $count eq 5; @list = @enginelist6 if $count eq 6; # Google $SE = "Google" if $count eq 6; @list = @enginelist7 if $count eq 7; # Ask Jeeves/Teoma $SE = "Ask Jeeves/Teoma" if $count eq 7; @list = @enginelist8 if $count eq 8; # Misc $SE = "Misc" if $count eq 8; @list = @enginelist9 if $count eq 9; # Custom List 1 $SE = "Custom List 1" if $count eq 9; @list = @enginelist10 if $count eq 10; # Custom List 2 $SE = "Custom List 2" if $count eq 10; $SE = "" if $count eq 11; foreach $line (@list) { chomp $line; next if $line eq ""; next if $line =~ /^\#/; $line =~ s/ //g; if (($gethost =~ /$line/i) and ($gethost =~ /[abcdefghijklmnopqrstuvwxyz\-]+/i)) { $isRobot = $count; } if ($getaddr eq $line){ $isRobot = $count; } if ($line =~ /^\d+\.\d+\.\d+$/){ if ($classCclient eq $line){ $isRobot = $count; } } last if $isRobot eq $count; } last if $isRobot eq $count; } # check for User Agent Bot cloaking if (($UARedirectionbot) and (!$isRobot)){ foreach (@UAredirectbot) { $t = $_; chomp $t; if ($t !~ /##/) {$t = $t . "##8";} ($ua, $templateUA) = split(/##/, $t); if ($ENV{'HTTP_USER_AGENT'} =~ /$ua/i){ $isRobot = $templateUA; chomp($isRobot); $UACloak = " -- User Agent Cloaking Engaged"; $SE = "MSN" if $isRobot eq 1; $SE = "Alta Vista" if $isRobot eq 2; $SE = "Lycos" if $isRobot eq 3; $SE = "Inktomi" if $isRobot eq 4; $SE = "WiseNut" if $isRobot eq 5; $SE = "Google" if $isRobot eq 6; $SE = "Ask Jeeves/Teoma" if $isRobot eq 7; $SE = "Misc" if $isRobot eq 8; $SE = "Custom List 1" if $isRobot eq 9; $SE = "Custom List 2" if $isRobot eq 10; if ($SE eq "") { $isRobot = 8; $SE = "Misc";} $count = $isRobot; } } } # protect against translators if ($gethost =~ /babelfish/i) { $translator = " -- Translator"; $SE = ""; $isRobot = 0; } # protect against BabelFish using Scooter IPs if ($getaddr eq "209.73.164.50"){ unless ($agent =~ /scooter/i){ $translator = " -- Translator"; $SE = ""; $isRobot = 0; } } foreach $ip (@exclude) { if ($getaddr eq $ip) { $isRobot = 0; $translator = " -- Translator"; $SE = ""; } } # check for custom excludes foreach $ip (@customexclude) { chomp($ip); if ($getaddr eq $ip) { $isRobot = 0; $NoCloak = " -- Exclude on for $getaddr "; $SE = ""; } } # check for nocloak if ($nocloak{$SE}) { $NoCloak = " -- Cloaking off for $SE "; $isRobot = 0; } } ###################################################################### # DETECT IF ON SUCKER LIST ###################################################################### ###################################################################### sub sucker{ $isSucker=0; foreach $line (@suckerlist) { chomp $line; next if $line eq ""; next if $line =~ /^\#/; $line =~ s/ //g; if (($gethost =~ /$line/i) and ($gethost =~ /[abcdefghijklmnopqrstuvwxyz\-]+/i)) { $isSucker = 1; } if ($getaddr eq $line){ $isSucker = 1; } if ($line =~ /^\d+\.\d+\.\d+$/){ if ($classCclient eq $line){ $isSucker = 1; } } last if($isSucker); } @suckerlist = (); } ###################################################################### # SERVE NORMAL PAGE TO NORMAL REQUESTOR ###################################################################### ###################################################################### sub normal{ if ($isSucker) {$redir = $suckerURL;} # User Agent Redirection if ($UARedirection){ foreach (@UAredirect){ chomp; ($thisagent, $thisUAredir) = split(/###/, $_); if ($agent =~ /$thisagent/i){ if (!$thisUAredir) { $thisUAredir = $redir; } $redir = $thisUAredir; $UARredirectionNotice =" -- UA Redirect"; last; } } @UAredirect=(); } # Referer Redirection -- overrides user agent redirection if ($rrd){ foreach $line (@refererRedirects){ chomp($line); ($regex, $rrdurl) = split(/###/, $line); $rrd_ref = $ref; $rrd_ref =~ s/\+/ /g; $rrd_ref =~ s/\%20/ /g; if ($rrd_ref =~ /$regex/i){ $redir = $rrdurl; $rrdnotice = " -- Referrer Redirect"; last; } } } # sucker list redirection -- overrides user agent redirection, referer redirection and $altRedirection if ($isSucker) { $redir = $suckerURL; $sucker = " -- Sucker!"; $UARredirectionNotice = ""; } # language redirection -- overridden by altRedirection, referrer redirection and sucker list redirection, overrides user agent redirection if ($languageRedirectURL){ unless (($altRedirection) or ($sucker) or ($rrdnotice)){ $redir = $languageRedirectURL; $LanguageRedirectMessage = "-- Language Redirect ($langcode)"; $UARredirectionNotice = ""; } } # Log the access open (ACCESSLOG, ">>$writedir/access.log"); flock(ACCESSLOG, 2) unless($winserver); $logline = " $date -- $gethost -- \"$agent\" -- $myname -- $NoCloak$ref$translator$sucker$UARredirectionNotice$rrdnotice$LanguageRedirectMessage\n"; print ACCESSLOG $logline; flock(ACCESSLOG, 8) unless($winserver); close (ACCESSLOG); # log suspect IPs if (!$NoCloak){ foreach $userAgent (@UA) { chomp($userAgent); if ($agent =~ /$userAgent/){ open (SUS, ">>$writedir/suspected.log"); flock(SUS, 2) unless($winserver); $logline = " $date -- $gethost -- \"$agent\" -- $myname -- $NoCloak$ref$translator$sucker$UARredirectionNotice$rrdnotice$LanguageRedirectMessage\n"; print SUS $logline; flock(SUS, 8) unless($winserver); close (SUS); if ($unknownIP) { push(@messagebody, "Unknown IP Address\n$logline\n\n"); $notify=1; } last; } } if (($agent !~ /mozilla/i) and (!$notify)){ $unknownUA = 1; if (-e "$writedir/uasx.daq"){ open (UASx, "<$writedir/uasx.daq"); flock (UASx, 2) unless $winserver; @uasx = ; flock (UASx, 8) unless $winserver; close (UASx); foreach $uasxi (@uasx){ chomp ($uasxi); if ($agent =~ /$uasxi/i){ $notify = 0; $unknownUA = 0; last; } } } if ($unknownUA){ open (SUS, ">>$writedir/suspected.log"); flock(SUS, 2) unless($winserver); $logline = " $date -- $gethost -- \"$agent\" -- $myname -- $NoCloak$ref$translator$sucker$UARredirectionNotice$rrdnotice$LanguageRedirectMessage\n"; print SUS $logline; flock(SUS, 8) unless($winserver); close (SUS); if ($unknownUA){ push(@messagebody, "Unknown User Agent\n$logline\n\n"); $notify=1; } } } if (($ref eq "") and ($noRef)){ push(@messagebody, "No Referrer\n$logline\n\n"); $notify=1; } } if ($redir ne "") { #$redirectedpage = get "$redir"; # replaced older code (above) to pass on original user agent and referer strings if ($redir !~ /^redirect: */i){ # handles useragent requests $uatopasson = $ENV{"HTTP_USER_AGENT"}; $referertopasson = $ENV{"HTTP_REFERER"}; $ua = LWP::UserAgent->new; $ua->agent($uatopasson); $req = HTTP::Request->new (GET => "$redir"); $req->header('referer' => $referertopasson); $res = $ua->request($req); $redirectedpage = $res->content; print STDOUT "Content-type: text/html\n\n"; print STDOUT @gra unless $gra[0] eq ""; if ($redir =~ /\.htm.*$/i){ $redir =~ s/^(.+\/).+\.htm.*$/$1/i; } if ($redir !~ /\/$/) { $redir = $redir . "/";} # check for custom base href tag if (-e "$writedir/bhref.txt"){ open (BHREF, "<$writedir/bhref.txt"); flock(BHREF, 2) unless $winserver; $bhref = ; flock(BHREF, 8) unless $winserver; close (BHREF); chomp($bhref); } else { $BASE_HREF = $redir; @href_array = split(/\//, $BASE_HREF); if (($href_array[@href_array-1] =~ /\./) and (@href_array > 3)){ $foo = pop(@href_array); $foo = ""; } $BASE_HREF = join ("/",@href_array); if ($BASE_HREF !~ /\/$/){ $BASE_HREF .= "/"; } $bhref = $BASE_HREF; } @htmlout = split (/\n/, $redirectedpage); if (-e "$writedir/marker.daq"){ $insertText = 1; open (MARKER, "<$writedir/marker.daq"); flock (MARKER, 2) unless $winserver; $tempmarker = ; flock (MARKER, 8) unless $winserver; close (MARKER); chomp($tempmarker); ($insertMarker,$insertType) = split(/###/,$tempmarker); if (-e "$writedir/insert.daq"){ open (INSERT, "<$writedir/insert.daq"); flock (INSERT, 2) unless $winserver; @insert = ; flock (INSERT, 8) unless $winserver; close (INSERT); } else { $insertText = 0; $insert[0]= ""; } } else { $insertText = 0; } if ($insertText){ $TextToInsert = ""; # handle normal page text insertion foreach $ins (@insert){ $ins =~ s/#filename#/$myname/g; $TextToInsert = $TextToInsert . $ins; } } $count=0; foreach $li (@htmlout){ if ($li =~ /\/i){ $li =~ s/^(.*)\<([hH][eE][aA][dD])\>(.*)$/$1\<$2\>\n$3/; $htmlout[$count] = $li; if (!$insertText) { last; } } if ($insertText){ if ($insertType eq "replace"){ $li =~ s/$insertMarker/$TextToInsert/g; } else { $li =~ s/($insertMarker)/$1$TextToInsert/g; } } $count++; } #print STDOUT "\n"; #print STDOUT $redirectedpage; foreach (@htmlout){ print STDOUT "$_\n"; } } else { # handles redirection requests $redir =~ s/^redirect: *//i; print STDOUT "Content-type: text/html\n\n"; print STDOUT @gra unless $gra[0] eq ""; print STDOUT <<"_end_"; _end_ } if (($ifMail) and ($notify)){ &mailNotify("Automatic eMail Notification", @messagebody); } } } ###################################################################### # SERVE ROBOT PAGE TO ROBOT REQUESTOR ###################################################################### ###################################################################### sub robot{ # determine primary keyword $keyword = $myname; $keyword =~ s/\.s?html?$//; $keyword =~ s/[\-\_\.]/ /g; $keyword =~ s/^\///; open (KEY, "<$writedir/indexkeywords.daq"); flock (KEY, 2) unless($winserver); $indexOrSitemapkeyword = ; flock (KEY, 8) unless($winserver); close (KEY); if (($keyword eq "index") or ($keyword eq "sitemap")){ if ($keyword eq "sitemap") {$IsSitemap=1;} $keyword = $indexOrSitemapkeyword; $IsindexORsitemap = 1; } @keywordarray = split (/ /, $keyword); @keyword = split (/ /, $keyword); $lckey = lc($keyword); $uckey = uc($keyword); $ucfirstkey = ""; foreach $circ (@keyword){ $circ = ucfirst($circ); $ucfirstkey = $ucfirstkey . " " . $circ; } $ucfirstkey =~ s/^ //; $hyphenkeyword = &hyphenate($keyword); $hyphenLC = &hyphenate($lckey); $hyphenUC = &hyphenate($uckey); $hyphenTC = &hyphenate($ucfirstkey); $underkeyword = &underscore($keyword); $underLC = &underscore($lckey); $underUC = &underscore($uckey); $underTC = &underscore($ucfirstkey); open (RANDOM, "$writedir/random.txt"); flock (RANDOM, 2) unless($winserver); @random = ; flock (RANDOM, 8) unless($winserver); close (RANDOM); # initialize random stuff $randomset = 0; $randomdyn = 0; srand($randseed); $nothing = rand(10); @randomInUse = @random; @randomdynamic = @random; @randomdynamicInUse = @random; $SEsymbol = "# "; if (!$ifcl) { if ($SE eq "") { $SE = "web surfer"; $SEsymbol = " "; } $SE = $SE . " (cloaking disabled)"; $count = 9; } # access secondary keyword list open (SECONDARYKEYWORDS, "$writedir/keywords.txt"); flock(SECONDARYKEYWORDS, 2) unless($winserver); @secondarykeywords = ; flock(SECONDARYKEYWORDS, 8) unless($winserver); @secondarykeywordsInUse = @secondarykeywords; # access tertiary keyword list open (TERTIARYKEYWORDS, "$writedir/tertiarykeywords.txt"); flock(TERTIARYKEYWORDS, 2) unless($winserver); @tertiarykeywords = ; flock(TERTIARYKEYWORDS, 8) unless($winserver); @tertiarykeywordsInUse = @tertiarykeywords; # open template unless ($altTempl){ open (TEMPLATE, "$writedir/template" . $count . ".txt"); flock(TEMPLATE, 2) unless($winserver); @template =