search
HomeBackend DevelopmentPHP TutorialSummary of vulnerable functions in PHP

Summary of vulnerable functions in PHP

Mar 08, 2018 pm 05:41 PM
phpfunction

This article describes the existence of some PHP functions with small vulnerabilities in PHP. Those who have not understood the vulnerable functions in PHP can take a look at what to pay attention to when using these functions in actual PHP development. Let’s stop talking nonsense and read this article together!

1. Weak type comparison


Summary of vulnerable functions in PHP


##2.MD5 compare vulnerability

When PHP processes hash strings, if you use "!=" or "==" to Comparing hash values, it interprets each hash value starting with "0x" as the power of 0 in scientific notation (0), so if two different passwords are hashed, their hash value will be If the hash values ​​all start with "0e", then PHP will think that they are the same.


Common payloads include

0x01 md5(str)
    QNKCDZO
    240610708
    s878926199a
    s155964671a
    s214587387a
    s214587387a
0x02 sha1(str)
    sha1('aaroZmOk')  
    sha1('aaK1STfY')
    sha1('aaO8zKZF')
    sha1('aa3OFF9m')

At the same time, MD5 cannot process arrays. If the following judgments are made, arrays can be used to bypass

if(@md5($_GET['a']) == @md5($_GET['b']))
{
    echo "yes";
}
//http://127.0.0.1/1.php?a[]=1&b[]=2

3.ereg function vulnerability :00 truncation
ereg ("^[a-zA-Z0-9]+$", $_GET['password']) === FALSE

String comparison analysis

Here if $_GET['password'] is an array, the return value is NULL
If it is 123 || asd || 12as || 123%00&&&**, the return value is true
The rest is false

4.What is $key?

Don’t forget that the program can extract the key of the variable itself as a variable and give it to the function for processing.

<?php
    print_r(@$_GET); 
    foreach ($_GET AS $key => $value)
    {
        print $key."\n";
    }
?>

5. Variable coverage

The main function involved is the extract function. Let’s look at an example

<?php  
    $auth = &#39;0&#39;;  
    // 这里可以覆盖$auth的变量值
    print_r($_GET);
    echo "</br>";
    extract($_GET); 
    if($auth == 1){  
        echo "private!";  
    } else{  
        echo "public!";  
    }  
?>

extract can receive an array and then give it again Variable assignment, procedure page is very simple.


Summary of vulnerable functions in PHP


at the same time! PHP's feature $ can be used to assign variable names and can also cause variable overwriting!

<?php  
    $a=&#39;hi&#39;;
    foreach($_GET as $key => $value) {
        echo $key."</br>".$value;
        $$key = $value;
    }
    print "</br>".$a;
?>

Construction

http://127.0.0.1:8080/test.php?a=12 can achieve the purpose.

6.strcmp
如果 str1 小于 str2 返回  0;如果两者相等,返回 0。 
先将两个参数先转换成string类型。 
当比较数组和字符串的时候,返回是0。 
如果参数不是string类型,直接return
<?php
    $password=$_GET[&#39;password&#39;];
    if (strcmp(&#39;xd&#39;,$password)) {
     echo &#39;NO!&#39;;
    } else{
        echo &#39;YES!&#39;;
    }
?>

Construction

http://127.0.0.1:8080/test.php?password[]=

7.is_numeric

Needless to say:

<?php
echo is_numeric(233333);       # 1
echo is_numeric(&#39;233333&#39;);    # 1
echo is_numeric(0x233333);    # 1
echo is_numeric(&#39;0x233333&#39;);   # 1
echo is_numeric(&#39;233333abc&#39;);  # 0
?>

8.preg_match

If in progress Regular expressionWhen matching, if there is no restriction on the beginning and end of the string (^ and $), there may be bypass problems

<?php
$ip = &#39;asd 1.1.1.1 abcd&#39;; // 可以绕过
if(!preg_match("/(\d+)\.(\d+)\.(\d+)\.(\d+)/",$ip)) {
  die(&#39;error&#39;);
} else {
   echo(&#39;key...&#39;);
}
?>

9.parse_str

Similar functions to parse_str() include mb_parse_str(). parse_str parses the string into multiple variables. If the parameter str is the query string passed in by the URL, then It is resolved to a variable and set to the current scope.

A type of time variable coverage

<?php
    $var=&#39;init&#39;;  
    print $var."</br>";
    parse_str($_SERVER[&#39;QUERY_STRING&#39;]);  
    echo $_SERVER[&#39;QUERY_STRING&#39;]."</br>";
    print $var;
?>

10.String comparison
<?php  
    echo 0 == &#39;a&#39; ;// a 转换为数字为 0    重点注意
    // 0x 开头会被当成16进制54975581388的16进制为 0xccccccccc
    // 十六进制与整数,被转换为同一进制比较
    &#39;0xccccccccc&#39; == &#39;54975581388&#39; ;

    // 字符串在与数字比较前会自动转换为数字,如果不能转换为数字会变成0
    1 == &#39;1&#39;;
    1 == &#39;01&#39;;
    10 == &#39;1e1&#39;;
    &#39;100&#39; == &#39;1e2&#39; ;    

    // 十六进制数与带空格十六进制数,被转换为十六进制整数
    &#39;0xABCdef&#39;  == &#39;     0xABCdef&#39;;
    echo &#39;0010e2&#39; == &#39;1e3&#39;;
    // 0e 开头会被当成数字,又是等于 0*10^xxx=0
    // 如果 md5 是以 0e 开头,在做比较的时候,可以用这种方法绕过
    &#39;0e509367213418206700842008763514&#39; == &#39;0e481036490867661113260034900752&#39;;
    &#39;0e481036490867661113260034900752&#39; == &#39;0&#39; ;

    var_dump(md5(&#39;240610708&#39;) == md5(&#39;QNKCDZO&#39;));
    var_dump(md5(&#39;aabg7XSs&#39;) == md5(&#39;aabC9RqS&#39;));
    var_dump(sha1(&#39;aaroZmOk&#39;) == sha1(&#39;aaK1STfY&#39;));
    var_dump(sha1(&#39;aaO8zKZF&#39;) == sha1(&#39;aa3OFF9m&#39;));
?>

11.unset

unset(bar); is used to destroy the specified variable. If the variable bar is included in the

request parameters, some variables may be destroyed to bypass the program logic.

<?php  
$_CONFIG[&#39;extraSecure&#39;] = true;

foreach(array(&#39;_GET&#39;,&#39;_POST&#39;) as $method) {
    foreach($$method as $key=>$value) {
      // $key == _CONFIG
      // $$key == $_CONFIG
      // 这个函数会把 $_CONFIG 变量销毁
      unset($$key);
    }
}

if ($_CONFIG[&#39;extraSecure&#39;] == false) {
    echo &#39;flag {****}&#39;;
}
?>

12.intval()

int to string:

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

string to int: intval() function.

var_dump(intval(&#39;2&#39;)) //2  
var_dump(intval(&#39;3abcd&#39;)) //3  
var_dump(intval(&#39;abcd&#39;)) //0 
// 可以使用字符串-0转换,来自于wechall的方法

Explains that when converting intval(), it will convert from the beginning of the string until it encounters a non-numeric character. Even if a string that cannot be converted appears, intval() will not report an error but return 0

By the way, intval can be truncated by %00

if($req[&#39;number&#39;]!=strval(intval($req[&#39;number&#39;]))){
     $info = "number must be equal to it&#39;s integer!! ";  
}

If $req['number']=0% 00 can bypass

13.switch()

If switch is a case of numeric type, switch will convert the parameters into int type. The effect is equivalent to the intval function. As follows:

<?php
    $i ="abc";  
    switch ($i) {  
    case 0:  
    case 1:  
    case 2:  
    echo "i is less than 3 but not negative";  
    break;  
    case 3:  
    echo "i is 3";  
    } 
?>

14.in_array()
$array=[0,1,2,&#39;3&#39;];  
var_dump(in_array(&#39;abc&#39;, $array)); //true  
var_dump(in_array(&#39;1bc&#39;, $array)); //true

Entering a string in any place where PHP considers it to be an int will be

forced conversion

15.serialize and unserialize vulnerabilities
这里我们先简单介绍一下php中的魔术方法(这里如果对于类、对象、方法不熟的先去学学吧),即Magic方法,php类可能会包含一些特殊的函数叫magic函数,magic函数命名是以符号开头的,比如 construct, destruct,toString,sleep,wakeup等等。这些函数都会在某些特殊时候被自动调用。 
例如construct()方法会在一个对象被创建时自动调用,对应的destruct则会在一个对象被销毁时调用等等。 
这里有两个比较特别的Magic方法,sleep 方法会在一个对象被序列化的时候调用。 wakeup方法会在一个对象被反序列化的时候调用。
<?php
class test
{
    public $username = &#39;&#39;;
    public $password = &#39;&#39;;
    public $file = &#39;&#39;;
    public function out(){
        echo "username: ".$this->username."<br>"."password: ".$this->password ;
    }
     public function toString() {
        return file_get_contents($this->file);
    }
}
$a = new test();
$a->file = &#39;C:\Users\YZ\Desktop\plan.txt&#39;;
echo serialize($a);
?>
//tostring方法会在输出实例的时候执行,如果实例路径是隐秘文件就可以读取了

echo unserialize triggers the tostring function, and the C:\Users\YZ\Desktop\plan.txt file can be read below

<?php
class test
{
    public $username = &#39;&#39;;
    public $password = &#39;&#39;;
    public $file = &#39;&#39;;
    public function out(){
        echo "username: ".$this->username."<br>"."password: ".$this->password ;
    }
     public function toString() {
        return file_get_contents($this->file);
    }
}
$a = &#39;O:4:"test":3:{s:8:"username";s:0:"";s:8:"password";s:0:"";s:4:"file";s:28:"C:\Users\YZ\Desktop\plan.txt";}&#39;;
echo unserialize($a);
?>

16.session deserialization vulnerability

The main reason is

ini_set('session.serialize_handler', 'php_serialize');
ini_set( 'session.serialize_handler', 'php');
The two methods of handling sessions are different
I don't understand this thing very well, I will write a solution later!
There is a question here! This is a
topic

Related recommendations:

The basic structure of PHP functions

The above is the detailed content of Summary of vulnerable functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 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.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 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.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 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.

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.

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software