Forum Moderators: phranque

Message Too Old, No Replies

Good Cloaking - Content Protection Using Ajax

         

3zero

1:35 am on Nov 24, 2015 (gmt 0)



Hello about to upload this to github , its a simple script that prevents automated scraping of content, I think it also breaks proxies too, the content remains accessible in the browser though and the script is search engine friendly. Looking for suggestions on how to improve it and if anyone believes its a worthwhile project. In this example it protects all content but thinking it could just be used to protect portions of code such as forms and avoid captchas (that I hate).


<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); ?>
<!DOCTYPE html>
<html>
<head>
<?php

$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);

if(
preg_match("/\.googlebot\.com$/", $hostname) ||
preg_match("/search\.msn\.com$/", $hostname)

) {
$host= "search";
} else {
$host = "notsearch";
}




$ip = $_SERVER['REMOTE_ADDR'];
$current_date = date('dmY');
$referer = $_GET['cypher'];

function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = '$ip';
$secret_iv = '$current_date';
$key = hash('sha256', $secret_key);
$iv = substr(hash('sha256', $secret_iv), 0, 16);

if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}

return $output;
}

$plain_txt = "Content Protection";

$encrypted = encrypt_decrypt('encrypt', $plain_txt);

$decrypted = encrypt_decrypt('decrypt', $encrypted_txt);

if ($referer == '' && $host == "notsearch"){ ?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("html").load("<?php echo $_SERVER['REQUEST_URI']?>?cypher=<?php echo $encrypted ?>");
});
</script>
</head>
<?php
$File = "$encrypted.txt";
$Handle = fopen($File, 'w');
$Data = "$ip\n".PHP_EOL;;
fwrite($Handle, $Data);
fclose($Handle);
}
else if ($encrypted == $referer && file_exists("$encrypted.txt") || $host == "search") {
?>
<title>Protected Content</title>
</head>
<body>
<p>Protected Content goes here</p>
</body>
<?php

if ($host != "search") {
$File = "$encrypted.txt";
unlink($File);
}
} else {
?>
<title>This page has expired</title>
</head><body>
<h1>This page has expired</h1>
<p>The page has expired</p>
</body>
<?php
}
?>
</html>

3zero

2:49 am on Dec 7, 2015 (gmt 0)



OH well this script might serve someone one day, I put an earlier version on github if you want to contribute

[github.com...]

tangor

7:47 am on Dec 7, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This might not be effective against those running script blockers.... which at present is more than 25% of web users world wide.