The editor of this article wants to talk to you about PHP weak typing. PHP weak typing brings great convenience to programmers when writing code, but everything has two sides. Now let’s learn about it with the editor.
0x00 A preliminary study on weak types
No one questions the simplicity and power of PHP. It provides many features for developers to use, one of which is weak type. type mechanism.
Under the weak type mechanism, you can perform such an operation
<?php $var = 1; $var = array(); $var = "string"; ?>
php will not strictly check the type of the incoming variable, and can also freely convert the variable type.
For example, in the comparison of $a == $b
$a = null; $b = false; //为真 $a = ''; $b = 0; //同样为真
However, the developers of the PHP kernel originally wanted programmers to use this system that does not require declarations to be more efficient. Development, so a lot of loose comparisons and conversions are used in almost all built-in functions and basic structures to prevent variables in the program from frequently reporting errors due to programmers' irregularities. However, this brings security issues.
0x02 Knowledge Preparation PHP Kernel Zval Structure
Variables declared in PHP are stored in ZE using the structure zval The
zval is defined in zend/zend.h
typedef struct _zval_struct zval; struct _zval_struct { /* Variable information */ zvalue_value value; /* value */ zend_uint refcount__gc; zend_uchar type;/* active type */ zend_uchar is_ref__gc; }; typedef union _zvalue_value { long lval; /* long value */ double dval;/* double value */ struct { char *val; int len; } str; HashTable *ht; /* hash table value */ zend_object_value obj; } zvalue_value;
where PHP determines the variable type through type and stores it in value
The above is the encapsulation of weak types in the PHP kernel. It is also the principle and foundation of everything we will talk about later.
Forcing conversion of 0x03 variables
Through the previous understanding, we know that zval.type determines the type stored in zval.value.
When the source code performs some unrestricted type comparisons or mathematical operations, it may cause zval.type to change, and at the same time affect the content of zval.value to change.
When int meets string
cp.1 Mathematical operations
When PHP performs some mathematical calculations
ar_dump(0 == '0'); // true var_dump(0 == 'abcdefg'); // true var_dump(0 === 'abcdefg'); // false var_dump(1 == '1abcdef'); // true
When one comparison parameter is an integer, the other parameter will be forced to be converted to an integer.
Equivalent to comparing the string part
intval with the integer part. In fact, it changes the content of zval.type. Especially note that the converted value of '1assd' is 1 , and 'asdaf' is 0
It also means that intval will start from the first unit that is not a number
All also have
var_dump(intval('3389a'));//输出3389
This example tells us, always Don't believe the following code
if($a>1000){ mysql_query('update ... .... set value=$a') }
You think that entering the branch at this time is an integer
In fact, $a may be 1001/**/union...
cp.2 Loose judgment of statement conditions
For example, PHP's switch uses loose comparison. $which will be automatically changed to 0 by intval. If there is no break in each case, it will Execute until inclusion, and finally execute to the function we need. Here is the loose judgment of the successfully included
<?php if (isset($_GET['which'])) { $which = $_GET['which']; switch ($which) { case 0: case 1: case 2: require_once $which.'.php'; break; default: echo GWF_HTML::error('PHP-0817', 'Hacker NoNoNo!', false); break; }
cp.3 function
var_dump(in_array("abc", $array));
in_array — Check whether the array is There is a value parameter
needle for the value to be searched for.
Note: If needle is a string, the comparison is case-sensitive. haystack this array.
strict If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.
As you can see, only by adding strict will the types be strictly compared. So what if we compare ××× with strings again?
var_dump(in_array("abc", $array1));</br> var_dump(in_array("1bc", $array2));
It traverses each value of the array and performs "==" comparison ("when strict is set, use ===")
The result is obvious
If there is a value in array1 that is 0, then the first return will be true //intval('abc')=0
If there is a value in array2 that is 1, then the second one will be True//intval('1bc')=1
The same principle applies to array_search
The application here is very wide,
Many programmers will check the array value,
Then we can completely use the constructed int 0 or 1 to fool the detection function and make it return true
To summarize, enter string in all places that PHP thinks are ints. will be forced to convert, such as
$a = 'asdfgh';//字符串类型的a</br> echo $a[2]; //根据php的offset 会输出'd'</br> echo $a[x]; //根据php的预测,这里应该是int型,那么输入string,就会被intval成为0 也就是输出'a'
When the array meets string
I encountered this example in a ctf in Germany. It is very interesting. What we talked about earlier They are all comparisons between string and int
So what chemical reaction will there be when array encounters int or string?
We know from the PHP manual that
Converting Array to int/floating point type float will return the number of elements;
Converting bool returns whether there are elements in the Array; convert to string returns 'Array' and throws warning.
So what is the practical application?
if(!strcmp($c[1],$d) && $c[1]!==$d){ ... }
It can be found that this branch requires the two to be equal through strcmp function comparison and "==" requires that the two are not equal to enter.
strcmp() function compares two strings.
This function returns:
0 - if the two strings are equal
>0 - if string1 Greater than string2
The strcmp function here actually converts the two variables into ascii and then performs mathematical subtraction to return the difference of an int.
That is to say, the result of typing 'a' and 'a' to compare is 0
So what if $array is compared with ‘a'?
http://localhost:8888/1.php?a[]=1 var_dump(strcmp($_GET[a],'a'));
At this time php returned null!
In other words, we make this function error so that it will always be true, bypassing the function check.
0x04 Always beware of weak types
As a programmer, weak types do bring great convenience to programmers when writing code. But it also makes programmers forget the habit of $array =array();. It is said that all input is harmful
In fact, it can be said that the type of all input is also suspicious. Never trust any comparison function or any mathematical operation under weakly typed PHP. Otherwise, you are definitely the one betrayed by php.
Related tutorials: PHP video tutorial
The above is the detailed content of Talk about PHP weak type safety issues. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

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.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.