In opinia mea e una dintre cele mai bune metode de protectie impotriva spammerilor. Lipsa acestei masuri de securitate lasa foarte multe posibilitati pentru un programator de a trimite requesturi false. In acest tutorial va voi arata metoda de pastrare a codului in cookie si nu in $_SESSION deoarece aceasta functie nu este implementata peste tot. In plus, majoritatea browserelor moderne, inclusiv cele mobile au 'prajiturelele' activate
In ce consta: Se genereaza random un cod in php(un numar e de ajuns), se genereaza imaginea, se seteaza headerele pentru a nu pastra urme si a accepta browserul imaginea si se salveaza codul hash(de obicei md5) al numarului generat. De ce hashuit? Ganditi-va ca daca ati pastra numarul in cookie, ar fi foarte simplu pentru oricine sa il ia si sa-l refoloseasca. Unde ar fi protectia? Asa, avand in vedere ca gradul de injectivitate al functiei md5 este destul de sporit, coliziunile de hash fiind foarte intamplatoare, e de ajuns sa pastram hashul.
Punem codul de generare a imaginii, hashului, si storage intr-un php, numit sa spunem cap.php
<?php
// generate 5 digit random number
$rand = rand(10000, 99999);
// create the hash for the random number and put it in the cookie
setcookie("int",md5($rand));
// create the image
$image = imagecreate(60, 30);
// use white as the background image
$bgColor = imagecolorallocate ($image, 255, 255, 255);
// the text color is black
$textColor = imagecolorallocate ($image, 0, 0, 0);
// write the random number
imagestring ($image, 5, 5, 8, $rand, $textColor);
// send several headers to make sure the image is not cached
// taken directly from the PHP Manual
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');
// send the image to the browser
imagejpeg($image);
// destroy the image to free up the memory
imagedestroy($image);
?>
Presupun ca va dati seama ca acest cod nu a fost creat de mine, deci nu imi asum drepturile asupra lui.
Dupa cum vedeti, am salvat hashul generat in Cookieul int.
Acum, trebuie sa apelam scriptul in form-ul nostru. O sa dau exemplu cel de la evidenta powergods.
<form name="input" action="submit.php" method="post">
<input type="text" name="nume" value="Nume Licenta/Server/DNS" /></br>
<input type="text" name="ip" value="IP Server"/></br>
<input type="text" name="port" value="Port Server" /></br>
<input type="text" name="email" value="E-Mail" /></br>
<input type="text" name="forum" value="Forum/Website"/></br>
            
<input type="text" name="captcha" value="Codul de alaturi"/><img src="verificationimage.php?<?php echo rand(0,9999); echo $_COOKIE['int'];?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br />
Acest <img src="cap.php?<?php echo rand(0,9999); echo $_COOKIE['int'];?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /> defapt afiseaza imaginea cu codul de securitate. Ii puteti adauga un background modificand cap.php pentru a face mai greu de ghicit in mod automat codul.
Ne mai ramane decat sa verificam hashul salvat in cookie cu cel generat de parametrul din form, pentru a vedea daca numerele corespund. Acest lucru il faceti doar in scriptul de action, in cazul nostru submit.php , altfel s-ar putea sa lasati o mica portita pentru injectarea hashului.
In submit.php punem:
if (md5($_POST['captcha']) != $_COOKIE['int'])
{
echo "<font color='red'><b> Cod de securitate(imagine verificare) invalid</br></b></font>";
$ok = 0;
}
Cu acest sfat, va puteti apara de majoritatea Spamurilor.
In ce consta: Se genereaza random un cod in php(un numar e de ajuns), se genereaza imaginea, se seteaza headerele pentru a nu pastra urme si a accepta browserul imaginea si se salveaza codul hash(de obicei md5) al numarului generat. De ce hashuit? Ganditi-va ca daca ati pastra numarul in cookie, ar fi foarte simplu pentru oricine sa il ia si sa-l refoloseasca. Unde ar fi protectia? Asa, avand in vedere ca gradul de injectivitate al functiei md5 este destul de sporit, coliziunile de hash fiind foarte intamplatoare, e de ajuns sa pastram hashul.
Punem codul de generare a imaginii, hashului, si storage intr-un php, numit sa spunem cap.php
<?php
// generate 5 digit random number
$rand = rand(10000, 99999);
// create the hash for the random number and put it in the cookie
setcookie("int",md5($rand));
// create the image
$image = imagecreate(60, 30);
// use white as the background image
$bgColor = imagecolorallocate ($image, 255, 255, 255);
// the text color is black
$textColor = imagecolorallocate ($image, 0, 0, 0);
// write the random number
imagestring ($image, 5, 5, 8, $rand, $textColor);
// send several headers to make sure the image is not cached
// taken directly from the PHP Manual
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
// send the content type header so the image is displayed properly
header('Content-type: image/jpeg');
// send the image to the browser
imagejpeg($image);
// destroy the image to free up the memory
imagedestroy($image);
?>
Presupun ca va dati seama ca acest cod nu a fost creat de mine, deci nu imi asum drepturile asupra lui.
Dupa cum vedeti, am salvat hashul generat in Cookieul int.
Acum, trebuie sa apelam scriptul in form-ul nostru. O sa dau exemplu cel de la evidenta powergods.
<form name="input" action="submit.php" method="post">
<input type="text" name="nume" value="Nume Licenta/Server/DNS" /></br>
<input type="text" name="ip" value="IP Server"/></br>
<input type="text" name="port" value="Port Server" /></br>
<input type="text" name="email" value="E-Mail" /></br>
<input type="text" name="forum" value="Forum/Website"/></br>
            
<input type="text" name="captcha" value="Codul de alaturi"/><img src="verificationimage.php?<?php echo rand(0,9999); echo $_COOKIE['int'];?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /><br />
Acest <img src="cap.php?<?php echo rand(0,9999); echo $_COOKIE['int'];?>" alt="verification image, type it in the box" width="50" height="24" align="absbottom" /> defapt afiseaza imaginea cu codul de securitate. Ii puteti adauga un background modificand cap.php pentru a face mai greu de ghicit in mod automat codul.
Ne mai ramane decat sa verificam hashul salvat in cookie cu cel generat de parametrul din form, pentru a vedea daca numerele corespund. Acest lucru il faceti doar in scriptul de action, in cazul nostru submit.php , altfel s-ar putea sa lasati o mica portita pentru injectarea hashului.
In submit.php punem:
if (md5($_POST['captcha']) != $_COOKIE['int'])
{
echo "<font color='red'><b> Cod de securitate(imagine verificare) invalid</br></b></font>";
$ok = 0;
}
Cu acest sfat, va puteti apara de majoritatea Spamurilor.
Niciun comentariu:
Trimiteți un comentariu