Advertisement
RaFaeLs

RaFaeL Security v0.2

Jul 30th, 2012
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2.     //=======================================================================
  3.     //==                        RaFaeL Security                            ==
  4.     //=======================================================================\
  5.     class Security {
  6.         private $functions = array(
  7.             "Xss" => array(
  8.                 "htmlspecialchars" => true,
  9.                 "htmlentities" => true, //Don't use if strip_tags enabled
  10.                 "strip_tags" => false //Don't use if htmlentities enabled
  11.             ), "Sql" => array(
  12.                 "stripslashes" => true,
  13.                 "mysql_real_escape_string" => true
  14.             )
  15.         );
  16.         private $settings = array(
  17.             "REQUESTS_ENCTYPE" => true,
  18.             "DOUBLE_ENCTYPE"  => true
  19.         );
  20.         private $servervars = array("GET","POST","REQUEST");
  21.         private $badwords=array('declare','char','set','cast','convert','drop','exec','meta','script','select','truncate','insert','delete','union','update','create','where','join','information_schema','table_schema','into');
  22.        
  23.         function __construct(array $settings = array(), array $enabled = array()) {
  24.             foreach($enabled as $enable=>$value)
  25.                 $this->SetFunc($enable, $value);
  26.            
  27.             foreach($settings as $setting=>$value)
  28.                 if(isset($this->settings[$setting]) && is_bool($value)) $this->settings[$setting] = $value;
  29.            
  30.             if($this->settings["REQUESTS_ENCTYPE"]) {
  31.                 foreach($this->servervars as $svar)
  32.                     eval('foreach($_'.$svar.' as $key=>$value) $_'.$svar.'[$key] = $this->Sql($value);');
  33.             }
  34.         }
  35.         public function Xss($str) {
  36.             foreach($this->functions["Xss"] as $func=>$state)
  37.                 if($state === true) $str = $func($str);
  38.            
  39.             return $str;
  40.         }
  41.         private function BadWords($str) {
  42.             foreach($this->badwords as $word)
  43.                 $str=str_replace($word,'\\'.$word.'\\',$str);
  44.              
  45.             return $str;
  46.         }
  47.         public function Sql($str) {
  48.             foreach($this->functions["Sql"] as $func=>$state)
  49.                 if($state === true) $str = $func($str);
  50.            
  51.             return $this->BadWords($str);
  52.         }
  53.         public function SetFunc($enable, $value = true) {
  54.             if(isset($this->functions["Xss"][$enable]) && is_bool($value))
  55.                 $this->functions["Xss"][$enable] = $value;
  56.             else if(isset($this->functions["Sql"][$enable]) && is_bool($value))
  57.                 $this->functions["Sql"][$enable] = $value;
  58.             return true;
  59.         }
  60.         public function Enctype($str) {
  61.             $str = hash('sha256', md5($str));
  62.             return $this->settings["DOUBLE_ENCTYPE"]? (substr(hash('sha512', $str), strlen($str), strlen($str)*0.8)):($str);
  63.         }
  64.     }
  65.  
  66.     //Usage Example's:
  67.     $Security = new Security(array("REQUESTS_ENCTYPE" => true)); // $Security = new Security([Settings], [Disabled]);
  68.     $Security->SetFunc("htmlentities", true);
  69.     echo $Security->Xss("<html>'RaFaeL's Security class'</html>"); //&lt;html&gt;'RaFaeL's Security class'&lt;/html&gt;
  70.     echo "<br />";
  71.     echo $Security->Sql("'RaFaeL's Security class'"); //\'RaFaeL\'s Security class\'
  72.     echo "<br />";
  73.     echo $_REQUEST["test"]; // ?set=update%20`test`%20set%20`i`=1 = \update\ `test` \set\ `i`=1
  74.     echo "<br />";
  75.     echo $_GET["set"]; // ?set='rafael' = \'rafael\'
  76.     echo "<br />";
  77.     echo $Security->Enctype("RaFaeL"); //e980a7582c517886f2d80f88fd9a5a533a6fab71f3b4b04c8dcb1b59e4f2f033
  78. ?>
  79. <br />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement