Summary 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. 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";
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
0=='0'//true0 == 'abcdefg'//true0 === 'abcdefg'//false1 == '1abcdef'//true
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
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
Type conversion
Common conversions are mainly converting int to string and string to int. int to string:$var = 5;
Method 2: $item = strval($var);
var_dump(intval('2'))//2 var_dump(intval('3abcd'))//3 var_dump(intval('abcd'))//0
if(intval($a)>1000) { mysql_query("select * from news where id=".$a) }
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
$array=[1,2,3]; var_dump(strcmp($array,'123')); //null,在某种意义上null也就是相当于false。
$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"; }
在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
可以看到上面的情况返回的都是true,因为’abc’会转换为0,’1bc’转换为1。
array_search()与in_array()也是一样的问题。

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)
