php的比较操作符有==(等于)松散比较,===(完全等于)严格比较,这里面就会引入很多有意思的问题。
在松散比较的时候,php会将他们的类型统一,比如说字符到数字,非bool类型转换成bool类型,为了避免意想不到的运行效果,应该使用严格比较。如下是php manual上的比较运算符表:
例子 名称 结果 $a == $b 等于 TRUE,如果类型转换后 $a 等于 $b。 $a === $b 全等 TRUE,如果 $a 等于 $b,并且它们的类型也相同。 $a != $b 不等 TRUE,如果类型转换后 $a 不等于 $b。 $a <> $b 不等 TRUE,如果类型转换后 $a 不等于 $b。 $a !== $b 不全等 TRUE,如果 $a 不等于 $b,或者它们的类型不同。 $a < $b 小与 TRUE,如果 $a 严格小于 $b。 $a > $b 大于 TRUE,如果 $a 严格大于 $b。 $a <= $b 小于等于 TRUE,如果 $a 小于或者等于 $b。 $a >= $b 大于等于 TRUE,如果 $a 大于或者等于 $b。
0x01 安全问题
1 hash比较缺陷
php在处理hash字符串的时候会用到!=,==来进行hash比较,如果hash值以0e开头,后边都是数字,再与数字比较,就会被解释成0*10^n还是为0,就会被判断相等,绕过登录环节。
root@kali:~/tool# php -r 'var_dump("00e0345" == "0");var_dump("0e123456789"=="0");var_dump("0e1234abc"=="0");'
bool(true)
bool(true)
bool(false)
当全是数字的时候,宽松的比较会执行尽力模式,如0e12345678会被解释成0*10^12345678,除了e不全是数字的时候就不会相等,这能从var_dump("0e1234abc"=="0")可以看出来。
2 bool 欺骗
当存在json_decode和unserialize的时候,部分结构会被解释成bool类型,也会造成欺骗。json_decode示例代码:
$json_str = '{"user":true,"pass":true}'; $data = json_decode($json_str,true); if ($data['user'] == 'admin' && $data['pass']=='secirity') { print_r('logined in as bool'."\n"); }
运行结果:
root@kali:/var/www# php /root/php/hash.php
logined in as bool
unserialize示例代码:
$unserialize_str = 'a:2:{s:4:"user";b:1;s:4:"pass";b:1;}'; $data_unserialize = unserialize($unserialize_str); if ($data_unserialize['user'] == 'admin' && $data_unserialize['pass']=='secirity') { print_r('logined in unserialize'."\n"); }
运行结果如下:
root@kali:/var/www# php /root/php/hash.php
logined in unserialize
3 数字转换欺骗
$user_id = ($_POST['user_id']); if ($user_id == "1") { $user_id = (int)($user_id); #$user_id = intval($user_id); $qry = "SELECT * FROM `users` WHERE user_id='$user_id';"; } $result = mysql_query($qry) or die('<pre class="brush:php;toolbar:false">' . mysql_error() . '' ); print_r(mysql_fetch_row($result));
将user_id=0.999999999999999999999发送出去得到结果如下:
Array
(
[0] => 0
[1] => lxx'
[2] =>
[3] =>
[4] =>
[5] =>
)
本来是要查询user_id的数据,结果却是user_id=0的数据。int和intval在转换数字的时候都是就低的,再如下代码:
if ($_POST['uid'] != 1) { $res = $db->query("SELECT * FROM user WHERE uid=%d", (int)$_POST['uid']); mail(...); } else { die("Cannot reset password of admin"); }
假如传入1.1,就绕过了$_POST['uid']!=1的判断,就能对uid=1的用户进行操作了。另外intval还有个尽力模式,就是转换所有数字直到遇到非数字为止,如果采用:
if (intval($qq) === '123456') { $db->query("select * from user where qq = $qq") }
攻击者传入123456 union select version()进行攻击。
4 PHP5.4.4 特殊情况
这个版本的php的一个修改导致两个数字型字符溢出导致比较相等
$ php -r 'var_dump("61529519452809720693702583126814" == "61529519452809720000000000000000");'
bool(true)
3 题外话:
同样有类似问题的还有php strcmp函数,manual上是这么解释的,int strcmp ( string $str1 , string $str2 ),str1是第一个字符串,str2是第二个字符串,如果str1小于str2,返回str2,返回>0,两者相等返回0,假如str2为一个array呢?
$_GET['key'] = array(); $key = "llocdpocuzion5dcp2bindhspiccy"; $flag = strcmp($key, $_GET['key']); if ($flag == 0) { print "Welcome!"; } else { print "Bad key!"; }
运行结果:
root@kali:~/php# php strcmp.php
PHP Warning: strcmp() expects parameter 2 to be string, array given in /root/php/strcmp.php on line 13
Welcome!
比较多种类型
运算数 1 类型 | 运算数 1 类型 | 结果 |
---|---|---|
null 或 string | string | 将 NULL 转换为 "",进行数字或词汇比较 |
bool 或 null | 任何其它类型 | 转换为 bool,FALSE TRUE |
object | object | 内置类可以定义自己的比较,不同类不能比较,相同类和数组同样方式比较属性(PHP 4 中),PHP 5 有其自己的说明 |
string,resource或 number | string,resource或 number | 将字符串和资源转换成数字,按普通数学比较 |
array | array | 具有较少成员的数组较小,如果运算数 1 中的键不存在于运算数 2 中则数组无法比较,否则挨个值比较(见下例) |
array | 任何其它类型 | array 总是更大 |
object | 任何其它类型 | object 总是更大 |

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.

Fibers was introduced in PHP8.1, improving concurrent processing capabilities. 1) Fibers is a lightweight concurrency model similar to coroutines. 2) They allow developers to manually control the execution flow of tasks and are suitable for handling I/O-intensive tasks. 3) Using Fibers can write more efficient and responsive code.

The PHP community provides rich resources and support to help developers grow. 1) Resources include official documentation, tutorials, blogs and open source projects such as Laravel and Symfony. 2) Support can be obtained through StackOverflow, Reddit and Slack channels. 3) Development trends can be learned by following RFC. 4) Integration into the community can be achieved through active participation, contribution to code and learning sharing.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use