Home Backend Development PHP Tutorial A small feature of php deserialization unserialize_PHP tutorial

A small feature of php deserialization unserialize_PHP tutorial

Jul 13, 2016 pm 05:19 PM
php unserialize wordpress Small sequence Compare loopholes characteristic of

The desequence vulnerability in WordPress has become quite popular these days. I will not analyze the specific vulnerability. Please read this article http://drops.wooyun.org/papers/596. You can also read the original English text http:/ /vagosec.org/2013/09/wordpress-php-object-injection/.

WP official website has a patch, I tried to bypass the patch, but when I thought I was successful, I found that I was naive and did not successfully bypass the patch of WP, but I discovered a small feature of unserialize, here Share it with everyone.
1.unserialize() function related source code:
if ((YYLIMIT - YYCURSOR) < 7) YYFILL(7);
        yych = *YYCURSOR;
        switch (yych) {
        case &#39;C&#39;:
        case &#39;O&#39;:        goto yy13;
        case &#39;N&#39;:        goto yy5;
        case &#39;R&#39;:        goto yy2;
        case &#39;S&#39;:        goto yy10;
        case &#39;a&#39;:        goto yy11;
        case &#39;b&#39;:        goto yy6;
        case &#39;d&#39;:        goto yy8;
        case &#39;i&#39;:        goto yy7;
        case &#39;o&#39;:        goto yy12;
        case &#39;r&#39;:        goto yy4;
        case &#39;s&#39;:        goto yy9;
        case &#39;}&#39;:        goto yy14;
        default:        goto yy16;
        }
Copy after login

The above code is the processing method of judging the sequence string, such as the sequence string O:4:"test":1:{s:1:"a";s:3:"aaa";}, processing this sequence String, first get the first character of the string as O, then case 'O': goto yy13
yy13:
yych = *(YYMARKER = ++YYCURSOR);
if (yych == ':') goto yy17;
goto yy3;
As can be seen from the above code, the pointer moves one position to point to the second character, determines whether the character is:, and then goto yy17
yy17:
        yych = *++YYCURSOR;
        if (yybm[0+yych] & 128) {
                goto yy20;
        }
        if (yych == &#39;+&#39;) goto yy19;

 .......

yy19:
        yych = *++YYCURSOR;
        if (yybm[0+yych] & 128) {
                goto yy20;
        }
        goto yy18;
Copy after login

From
As can be seen from the above code, the pointer moves to determine the next character. If the character is a number, goto yy20 directly. If it is '+', goto
yy19, and yy19 is to judge the next character. If the next character is a number goto yy20, if not, goto
yy18, yy18 is to exit the sequence processing directly, yy20 is to process the object sequence, so it can be seen from the above:
O:+4:"test":1:{s:1:"a";s:3:"aaa";}
O:4:"test":1:{s:1:"a";s:3:"aaa";}
can all be deserialized by unserialize, and the results are the same.
2. Actual test:
<?php
var_dump(unserialize(&#39;O:+4:"test":1:{s:1:"a";s:3:"aaa";}&#39;));
var_dump(unserialize(&#39;O:4:"test":1:{s:1:"a";s:3:"aaa";}&#39;));
?>
输出:
object(__PHP_Incomplete_Class)#1 (2) { ["__PHP_Incomplete_Class_Name"]=> string(4) "test" ["a"]=> string(3) "aaa" } 
object(__PHP_Incomplete_Class)#1 (2) { ["__PHP_Incomplete_Class_Name"]=> string(4) "test" ["a"]=> string(3) "aaa" }
Copy after login

Actually, not only the object type can be processed with an extra '+', but also other types. The specific test will not be described too much.
3. Let’s take a look at the wp patch:
function is_serialized( $data, $strict = true ) {
        // if it isn&#39;t a string, it isn&#39;t serialized
        if ( ! is_string( $data ) )
                return false;
        $data = trim( $data );
         if ( &#39;N;&#39; == $data )
                return true;
        $length = strlen( $data );
        if ( $length < 4 )
                return false;
        if ( &#39;:&#39; !== $data[1] )
                return false;
        if ( $strict ) {//output
                $lastc = $data[ $length - 1 ];
                if ( &#39;;&#39; !== $lastc && &#39;}&#39; !== $lastc )
                        return false;
        } else {//input
                $semicolon = strpos( $data, &#39;;&#39; );
                $brace     = strpos( $data, &#39;}&#39; );
                // Either ; or } must exist.
                if ( false === $semicolon && false === $brace )
                        return false;
                // But neither must be in the first X characters.
                if ( false !== $semicolon && $semicolon < 3 )
                        return false;
                if ( false !== $brace && $brace < 4 )
                        return false;
        }
        $token = $data[0];
        switch ( $token ) {
                case &#39;s&#39; :
                        if ( $strict ) {
                                if ( &#39;"&#39; !== $data[ $length - 2 ] )
                                        return false;
                        } elseif ( false === strpos( $data, &#39;"&#39; ) ) {
                                return false;
                        }
                case &#39;a&#39; :
                case &#39;O&#39; :
                        echo "a";
                        return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
                case &#39;b&#39; :
                case &#39;i&#39; :
Copy after login

in patch
return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
It can be bypassed by adding an extra '+'. Although we wrote the sequence value into the database through this method, it cannot be bypassed when extracting the data from the database and verifying it again. My plus sign failed. To make any changes in the data in and out of the database, I personally think that the focus of this patch is to bypass the changes in the data in and out of the data.
4. Summary
Although the wp patch has not been bypassed, this small feature of unserialize() may be ignored by many developers, resulting in security flaws in the program.
Please leave a message to point out any errors in the above analysis.
5. Reference
《WordPress < 3.6.1 PHP Object Injection》
http://vagosec.org/2013/09/wordpress-php-object-injection/
《var_unserializer.c source code》
https://github.com/php/php-src/b ... /var_unserializer.c
"Security risks caused by inconsistent syntax parsing between PHP string serialization and deserialization" Reprinted from
http://zone.wooyun.org/content/1664
Reprinted from: https://forum.90sec.org/thread-6694-1-1.html
Author: L.N.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532682.htmlTechArticleThe desequence vulnerability in WordPress has become quite popular these days. I will not analyze the specific vulnerability. Read this article http://drops.wooyun.org/papers/596, you can also read the original English text http://va...
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles