Home Backend Development PHP Tutorial Summary of security issues that need to be paid attention to when using PHP weak types

Summary of security issues that need to be paid attention to when using PHP weak types

Jun 29, 2017 pm 01:31 PM
php Safety question

Weakly typed language is also called weakly typed language. The opposite of strongly typed definition. Languages ​​such as VB and PHP are weakly typed. This article will give you a detailed introduction to the security issues of PHP's weak types. You can refer to them if necessary. Let's take a look together.

Preface

I believe everyone knows that PHP is the best language in the world, and the problems of PHP itself can also be regarded as web security. One aspect. The characteristics in PHP are weak types and built-in functions loose handling of incoming parameters.

This article is mainly to record the problems in PHP functions that I encountered on the offensive and defensive platform, as well as the problems caused by PHP's weak types. It has certain reference value for everyone when learning or using PHP. Let’s take a look together.

Introduction to PHP weak types

The reason why the editor thinks that PHP is very powerful is because PHP provides many unique features for developers to use , one of which is the PHP weak type mechanism.

In PHP, you can perform the following operations.

$param = 1;
$param = array();
$param = "stringg";
Copy after login

Weakly typed languages ​​have no restrictions on the data type of variables. You can assign variables to any other types of variables at any time, and the variables can also be converted to any Other types of data.

Type conversionProblem

Type conversion is an unavoidable problem. For example, when you need to convert GET or POST parameters into int type, or when the two variables do not match, PHP will automatically convert the variables. However, PHP is a weakly typed language, which leads to many unexpected problems when performing type conversion.

Comparison operators

Type conversion

at$a==$ There are many examples of

$a=null;$b=flase ; //true
$a='';$b=null; //true
Copy after login

in the comparison of b

, and these comparisons are all equal.

There are also type conversion problems when using comparison operators, as follows:

0=='0' //true
0 == 'abcdefg' //true
0 === 'abcdefg' //false
1 == '1abcdef' //true
Copy after login

When variables of different types are compared, there will be variable conversion problems. During conversion There may be problems later.

Hash comparison

In addition to the above method, there are also problems when performing hash comparison. As follows:

"0e132456789"=="0e7124511451155" //true
"0e123456abc"=="0e1dddada" //false
"0e1abc"=="0"  //true
Copy after login

When performing comparison operations, if a string like 0e\d+ is encountered, this string will be parsed into scientific notation. Therefore, the values ​​of the two numbers in the above example are both 0 and they are equal. If 0e\d+ is not satisfied, this pattern will not be equal. This question is tested in the md5 collision in the offensive and defensive platform.

HexadecimalConversion

There is also a problem when comparing hexadecimal remainder strings.

Examples are as follows:

"0x1e240"=="123456" //true
"0x1e240"==123456 //true
"0x1e240"=="1e240" //false
Copy after login

When one of the strings starts with 0x, PHP will parse the string into decimal and then compare it. When 0x1240 is parsed into decimal, it is 123456, so Comparisons with 123456 of int type and string type are all equal. The difficulty in naming in the offensive and defensive platform is due to this characteristic of inspection.

Type conversion

Common conversions are mainly converting int to string and string to int.

int to string:

$var = 5;
方式1:$item = (string)$var;
方式2:$item = strval($var);
Copy after login

string to int: intval() function.

For this function, you can look at 2 examples first.

var_dump(intval('2')) //2
var_dump(intval('3abcd')) //3
var_dump(intval('abcd')) //0
Copy after login

Explanationintval()When converting, the conversion will be performed from the beginning of the string until a non-numeric character is encountered. Even if a string that cannot be converted appears, intval() will not report an error but return 0.

intval() This feature is tested in the question of MYSQL in the offensive and defensive platform.

At the same time, programmers should not use the following code when programming:

if(intval($a)>1000) {
 mysql_query("select * from news where id=".$a)
}
Copy after login

At this time, the value of $a may be 1002 union…..

Built-inParameters of functionsThe looseness of

The looseness of built-in functions means that when calling a function, it is passed to the function Function cannot accept parameter type. The explanation is a bit confusing, so let’s illustrate the problem directly through practical examples. Below we will focus on a few such functions.

md5()

$array1[] = array(
 "foo" => "bar",
 "bar" => "foo",
);
$array2 = array("foo", "bar", "hello", "world");
var_dump(md5($array1)==var_dump($array2)); //true
Copy after login

The description of the md5() function in the PHP manual is string md5 ( string $str [, bool $raw_output = false ] ) , md5() needs to be a string type parameter. But when you pass an array, md5() will not report an error, and knowledge will not be able to correctly calculate the md5 value of the array, which will cause the md5 values ​​of any two arrays to be equal. This md5() feature is also considered in bypass again in the attack and defense platform.

strcmp()

strcmp()函数在PHP官方手册中的描述是int strcmp ( string $str1 , string $str2 ) ,需要给strcmp()传递2个string类型的参数。如果str1小于str2,返回-1,相等返回0,否则返回1。strcmp函数比较字符串的本质是将两个变量转换为ascii,然后进行减法运算,然后根据运算结果来决定返回值。

如果传入给出strcmp()的参数是数字呢?

$array=[1,2,3];
var_dump(strcmp($array,'123')); //null,在某种意义上null也就是相当于false。
Copy after login

strcmp这种特性在攻防平台中的pass check有考到。

switch()

如果switch是数字类型的case的判断时,switch会将其中的参数转换为int类型。如下:

$i ="2abc";
switch ($i) {
case 0:
case 1:
case 2:
 echo "i is less than 3 but not negative";
 break;
case 3:
 echo "i is 3";
}
Copy after login

这个时候程序输出的是i is less than 3 but not negative,是由于switch()函数将$i进行了类型转换,转换结果为2。

in_array()

在PHP手册中,in_array()函数的解释是bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) ,如果strict参数没有提供,那么in_array就会使用松散比较来判断$needle是否在$haystack中。当strince的值为true时,in_array()会比较needls的类型和haystack中的类型是否相同。

$array=[0,1,2,'3'];
var_dump(in_array('abc', $array)); //true
var_dump(in_array('1bc', $array)); //true
Copy after login

可以看到上面的情况返回的都是true,因为'abc'会转换为0,'1bc'转换为1。

<a href="http://www.php.cn/wiki/1007.html" target="_blank">array_search</a>()in_array()也是一样的问题。

The above is the detailed content of Summary of security issues that need to be paid attention to when using PHP weak types. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

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 in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles