search
HomeBackend DevelopmentPHP TutorialSummary of PHP weak type safety issues

Some time ago, I did a question on the Network Attack and Defense Platform of Nanjing University of Posts and Telecommunications. After writing a writeup, I still need to summarize it. Since the questions are all web-type and all questions are written using PHP, many questions do not examine traditional vulnerabilities such as SQL injection and XSS. Many of them are problems with the syntax of PHP itself. Given that PHP is currently the best language in the world, problems with PHP itself can also be counted as an aspect of web security. Features in PHP training are weak typing and the loose handling of incoming parameters by built-in functions. This article is mainly to record the problems in PHP functions that I encountered when building an offensive and defensive platform, as well as the problems caused by PHP's weak types.
Introduction to PHP weak types
In PHP, you can perform the following operations.
$param = 1;
$param = array();
$param = "stringg";
Weakly typed languages ​​have no restrictions on the data type of variables. You can assign variables to any other type at any time. Variables can also be converted into any other type of data.
Type conversion problem
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 operator
Type conversion
In the comparison of $a==$b
$a=null;$b=flase; //true
$a='';$b=null; //true
Like this There are many examples, and this kind of comparison is 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
When different types of variables are compared, there will be a problem of variable conversion, and there may be problems after the conversion.
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
When performing comparison operations, if you encounter 0ed+this string, the 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 0ed+ is not satisfied, this pattern will not be equal. This question is tested in the md5 collision in the offensive and defensive platform.
Hex conversion
There is also a problem when comparing hexadecimal remainder strings. Examples are as follows:
"0x1e240"=="123456" //true
"0x1e240"==123456 //true
"0x1e240"=="1e240" //false
When one of the strings starts with 0x, PHP will parse this string into decimal and then compare it. When 0×1240 is parsed into decimal, it is 123456, so the comparison with 123456 of int type and string type is 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;
Method 1: $item = (string)$var;
Method 2: $item = strval($var);
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
Explain that when intval() is converted, it will Conversion is performed from the beginning of the string until a non-numeric character is encountered. Even if there is a string that cannot be converted, intval() will not report an error but return 0.
This feature of intval() 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)
}
At this time, the value of $a may be 1002 union...
The looseness of parameters of built-in functions
The looseness of built-in functions means that when calling a function, the function is passed parameter types that the function cannot accept. 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
The description of the md5() function in the PHP manual is string md5 ( string $str [, bool $raw_output = false ] ), and the required parameter in md5() is 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. This will cause the md5 values ​​of any two arrays to be equal. This feature of md5() is also considered in bypass again in the attack and defense platform.
strcmp()
strcmp() function is described in the PHP official manual as int strcmp ( string $str1 , string $str2 ), and two string type parameters need to be passed to strcmp(). If str1 is less than str2, -1 is returned, 0 is returned if equal, otherwise 1 is returned. The essence of the strcmp function comparing strings is to convert two variables into ascii, then perform a subtraction operation, and then determine the return value based on the operation result.
What if the parameter passed in to strcmp() is a number?
$array=[1,2,3];
var_dump(strcmp($array,'123')); //null, in a sense, null is equivalent to false.
This feature of strcmp has been tested in the pass check in the attack and defense platform.
switch()
If switch is a case judgment of numeric type, switch will convert the parameters into int type. As follows:
$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";
}
At this time, the program output is i is less than 3 but not negative. This is because the switch() function performs type conversion on $i, and the conversion result is 2.
in_array()
In the PHP manual, the explanation of the in_array() function is bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ). If the strict parameter is not provided, then in_array will use loose comparison To determine whether $needle is in $haystack. When the value of stringe is true, in_array() will compare the type of needsls and the type in haystack to see whether they are the same.
$array=[0,1,2,'3'];
var_dump(in_array('abc', $array)); //true
var_dump(in_array('1bc', $array)); //true
You can see that the above situation returns true, because 'abc' will be converted to 0 and '1bc' will be converted to 1.
array_search() and in_array() have the same problem.

Statement
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
What are some common problems that can cause PHP sessions to fail?What are some common problems that can cause PHP sessions to fail?Apr 25, 2025 am 12:16 AM

Reasons for PHPSession failure include configuration errors, cookie issues, and session expiration. 1. Configuration error: Check and set the correct session.save_path. 2.Cookie problem: Make sure the cookie is set correctly. 3.Session expires: Adjust session.gc_maxlifetime value to extend session time.

How do you debug session-related issues in PHP?How do you debug session-related issues in PHP?Apr 25, 2025 am 12:12 AM

Methods to debug session problems in PHP include: 1. Check whether the session is started correctly; 2. Verify the delivery of the session ID; 3. Check the storage and reading of session data; 4. Check the server configuration. By outputting session ID and data, viewing session file content, etc., you can effectively diagnose and solve session-related problems.

What happens if session_start() is called multiple times?What happens if session_start() is called multiple times?Apr 25, 2025 am 12:06 AM

Multiple calls to session_start() will result in warning messages and possible data overwrites. 1) PHP will issue a warning, prompting that the session has been started. 2) It may cause unexpected overwriting of session data. 3) Use session_status() to check the session status to avoid repeated calls.

How do you configure the session lifetime in PHP?How do you configure the session lifetime in PHP?Apr 25, 2025 am 12:05 AM

Configuring the session lifecycle in PHP can be achieved by setting session.gc_maxlifetime and session.cookie_lifetime. 1) session.gc_maxlifetime controls the survival time of server-side session data, 2) session.cookie_lifetime controls the life cycle of client cookies. When set to 0, the cookie expires when the browser is closed.

What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

See all articles

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 Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function