Guest User

Untitled

a guest
May 14th, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.17 KB | None | 0 0
  1. <?php
  2.  
  3. class csrf {
  4.     public $status;
  5.  
  6.     /* Cookies backend */
  7.     private function store_in_cookies($key,$value) {
  8.         setcookie($key, $value, time()+300);
  9.     }
  10.  
  11.     private function unset_cookies($key) {
  12.         unset($_COOKIE[$key]);
  13.         setcookie($key, 'expired', time()-3600);
  14.     }
  15.  
  16.     private function get_from_cookies($key) {
  17.         if (isset($_COOKIE[$key])) return $_COOKIE[$key];
  18.         return false;
  19.     }
  20.  
  21.     private function csrf_generate_token($unique_form_name) {
  22.         if (function_exists("hash_algos") and in_array("sha512",hash_algos())) {
  23.             $token=hash("sha512",mt_rand(0,mt_getrandmax()));
  24.         } else {
  25.             $token=' ';
  26.             for ($i=0;$i<128;++$i) {
  27.                 $r=mt_rand(0,35);
  28.                 $c = ($r < 26) ? chr(ord('a')+$r) : hr(ord('0')+$r-26);
  29.                 $token .= $c;
  30.             }
  31.         }
  32.         $this->store_in_cookies($unique_form_name,$token);
  33.         return $token;
  34.     }
  35.  
  36.     private function csrf_validate_token($unique_form_name,$token_value) {
  37.         $token=$this->get_from_cookies($unique_form_name);
  38.         $this->unset_cookies($unique_form_name);
  39.         if($token === $token_value) return true;
  40.         return false;
  41.     }
  42.  
  43.     private function csrf_replace_forms($form_data_html) {
  44.         $count=preg_match_all("/<form(.*?)>(.*?)<\\/form>/is",$form_data_html,$matches,PREG_SET_ORDER);
  45.         if (is_array($matches)) {
  46.             foreach ($matches as $m) {
  47.                 if (strpos($m[1],"nocsrf")!==false) { continue; }
  48.                 $name="X_CSRF_".mt_rand(0,mt_getrandmax());
  49.                 $token=$this->csrf_generate_token($name);
  50.                 $form_data_html=str_replace($m[0], "<form{$m[1]}><input type='hidden' name='CSRFName' value='{$name}' /><input type='hidden' name='CSRFToken' value='{$token}' />{$m[2]}</form>",$form_data_html);
  51.             }
  52.         }
  53.         return $form_data_html;
  54.     }
  55.  
  56.     public function csrf_inject() {
  57.         $data=ob_get_clean();
  58.         $data=$this->csrf_replace_forms($data);
  59.         echo $data;
  60.     }
  61.  
  62.     public function __construct() {
  63.         $this->status = true;
  64.         if (count($_POST)) {
  65.             $name =$_POST['CSRFName'];
  66.             $token=$_POST['CSRFToken'];
  67.             if ( !isset($_POST['CSRFName']) or !isset($_POST['CSRFToken']) ) $this->status = false;
  68.             if (!$this->csrf_validate_token($name, $token)) $this->status = false;
  69.         }
  70.         ob_start();
  71.         register_shutdown_function(array($this, "csrf_inject"));   
  72.     }
  73.  
  74. }
  75. ?>
Advertisement
Add Comment
Please, Sign In to add comment