Talk about PHP weak type safety issues
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 is less than string2
>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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
