Home > php教程 > PHP源码 > body text

Summary of PHP weak type safety issues

大家讲道理
Release: 2016-11-12 11:01:54
Original
2687 people have browsed it

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. The features in PHP 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";
Copy after login

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

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 operators


Type conversion

In the comparison of $a==$b

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

There are many such examples, and these comparisons are all equal.

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

0=='0'//true0 == 'abcdefg'//true0 === 'abcdefg'//false1 == '1abcdef'//true
Copy after login

When different types of variables are compared, there will be variable conversion problems, 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
Copy after login

When performing comparison operations, if a string like 0ed+ 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 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. The example is 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. 0×1240 parsed into decimal is 123456, so it is the same as 123456 of int type and string type. All comparisons are equal. The difficulty in naming in the offensive and defensive platform is due to the characteristics of the investigation.

Type conversion

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

int to string:

$var = 5;
Copy after login

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
Copy after login

Explains that when converting intval(), it will convert from the beginning of the string until it encounters a non-numeric character. 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)
}
Copy after login

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

The looseness of the parameters of the built-in function


The looseness of built-in functions means that when calling a function, you pass a parameter type 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
Copy after login

The description of the md5() function in the PHP manual is string md5 ( string $str [, bool $raw_output = false ] ), and the requirement 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,在某种意义上null也就是相当于false。
Copy after login

strcmp This feature is tested in the pass check in the offensive and defensive platform.

switch()

If switch is a case 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";
}
Copy after login

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()

在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。

array_search()与in_array()也是一样的问题。


source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template