CSRF (Cross-site request forgery cross-site solicitation forgery, also known as "one click Attack" or Session Riding, usually abbreviated as CSRF or XSRF, is a malicious use of the site. Although it sounds like a cross-site script (XSS), it is very different from XSS and is almost at odds with the way it is attacked. XSS leverages trusted users within the site, while CSRF leverages trusted sites by disguising requests from trusted users. Compared to XSS attacks, csrf attacks are often less prevalent (and therefore have very few resources to protect against them) and are difficult to guard against, so they are considered more dangerous than XSS.
Http://baike.baidu.com/view/1609487.htm
CSRF defense can be from both the server and the client side, the defense effect is from the service side of the effect is better, and now the general CSRF Defense is also on the service side.
client.php
1<?PHP2 functionAuthcode ($string,$operation= ' DECODE ',$key= ",$expiry= 0) {3 4 $ckey _length= 4;5 $key=MD5($key?$key: Time());6 $keya=MD5(substr($key, 0, 16));7 $keyb=MD5(substr($key, 16, 16));8 $KEYC=$ckey _length? ($operation= = ' DECODE '?substr($string, 0,$ckey _length):substr(MD5(Microtime()), -$ckey _length)) : ‘‘;9 Ten $cryptkey=$keya.MD5($keya.$KEYC); One $key _length=strlen($cryptkey); A - $string=$operation= = ' DECODE '?Base64_decode(substr($string,$ckey _length)) :sprintf('%010d ',$expiry?$expiry+ Time(): 0).substr(MD5($string.$keyb), 0, 16).$string; - $string _length=strlen($string); the - $result= ' '; - $box=Range(0, 255); - + $rndkey=Array(); - for($i= 0;$i<= 255;$i++) { + $rndkey[$i] =Ord($cryptkey[$i%$key _length]); A } at - for($j=$i= 0;$i< 256;$i++) { - $j= ($j+$box[$i] +$rndkey[$i])% 256; - $tmp=$box[$i]; - $box[$i] =$box[$j]; - $box[$j] =$tmp; in } - to for($a=$j=$i= 0;$i<$string _length;$i++) { + $a= ($a+ 1)% 256; - $j= ($j+$box[$a])% 256; the $tmp=$box[$a]; * $box[$a] =$box[$j]; $ $box[$j] =$tmp;Panax Notoginseng $result.=CHR(Ord($string[$i]) ^ ($box[($box[$a] +$box[$j])% 256])); - } the + if($operation= = ' DECODE ') { A if((substr($result, 0, 10) = = 0 | |substr($result, 0, 10)- Time() > 0) &&substr($result, 10, 16) = =substr(MD5(substr($result, 26).$keyb), 0, 16)) { the return substr($result, 26); +}Else { - return‘‘; $ } $}Else { - return $KEYC.Str_replace(' = ', ',Base64_encode($result)); - } the - }Wuyi the functionFormhash ($specialadd= ' ') { - $username=$_cookie[' username ']; Wu $uid=$_cookie[' UID '];
$auth _code = Authcode ($string, $operation = ' DECODE ', $key = ', $expiry = 0);
- return substr(MD5(substr( Time(), 0,-7).$username.$uid.$auth _code.$specialadd), 8, 8); About } $ - $hash=Formhash (); -?> -<form method= "POST" action= "transfer.php" > A<input type= "text" name= "Tobankid" > +<input type= "text" name= "Money" > the<input type= "hidden" name= "hash" value= "<?=$hash;? > "> -<input type= "Submit" name= "submit" value= "Submit" > $</form>
Hash authentication on the server side
server.php
1<?PHP2 if(isset($_post[' Submit '])) {3 $hash=MD5($_cookie[' token ']);4 if($_post[' hash '] = =$hash) {5 dojob ();6}Else {7 //...8 //Illegal request9 }Ten}Else { One //... A } -?>
Extended reading:
Http://www.baidu.com/s?wd=csrf
Http://www.sogou.com/web?query=csrf
Http://www.so.com/s?q=csrf
http://www.baidu.com/s?wd=csrf%20php
http://www.sogou.com/web?query=csrf%20php
http://www.so.com/s?q=csrf%20php
Http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html
CSRF Cross-site request forgery