PHP yaml_parse/yaml_parse_file/yaml_parse_url Unsafe Deserialization
The yaml_parse family of PHP functions was documented as safe, but the undocumented !php/object tag causes the parser to call unserialize() on attacker-supplied data. An attacker who controls YAML input could therefore instantiate arbitrary PHP classes and trigger code in destructors.
Originally disclosed on HackerOne
- Fixed in 51 days
- Paid in 51 days
- Disclosed after 8 days
https://bugs.php.net/bug.php?id=69617
Description:
The PHP unserialize() function is considered unsafe due to its behavior regarding class instantiation; in cases where serialized data is attacker controlled, it can be tampered with, allowing for the instantiation of arbitrary PHP classes and thus code execution via destructor.
Inversely, as per the documentation, the yaml_parse*() functions carry no such risk. In fact, among the YAML functions is yaml_parse_url, which retrieves and parses a remote YAML file. Deserialization of built-in YAML types appears safe, as the serializer utilizes arrays and scalar types, prohibiting control of instantiation. However, this limitation can be overcome with the largely undocumented !php/object extension type, which in turn invokes unserialize(), effectively making the yaml_parse* functions just as dangerous. An example follows:
<?php
class A {
function __destruct() {
echo 'destructor invoked';
}
}
yaml_parse('x: !php/object O:1:"A":0:{}');
?>
As mentioned previously, the yaml_parse_url function, which encourages the retrieval of YAML from remote endpoints, exhibits the same behavior:
<?php
class A {
function __destruct() {
echo 'destructor invoked';
}
}
yaml_parse_url('http://autosectools.com/yaml.txt');
?>
To mitigate this, it is recommended that the YAML serializer handle the !php/object type in a safe manner, such as prohibiting the deserialization of types that have destructors defined. If this is not possible, it is recommended that yaml_parse_url be removed and the documentation for the remaining yaml_* functions be updated with warnings akin to that of unserialize().
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The YAML parser supports a !php/object tag that passes the tagged value to unserialize(), so the documented safety of yaml_parse* does not hold.
Impact
An attacker who can supply YAML to these functions could instantiate arbitrary classes and execute code through destructors; yaml_parse_url widens this because it fetches YAML from a remote endpoint.
What the attacker needed
Attacker controls YAML input; target uses yaml_parse* functions
The fix
The YAML serialiser should handle the !php/object type safely, for example by refusing to deserialise types that define destructors; otherwise yaml_parse_url should be removed and the remaining functions documented with warnings like unserialize().
How it was found
- Define a PHP class with a destructor that performs an action.
- Pass YAML containing the !php/object tag with a serialised object of that class to yaml_parse.
- Observe the destructor being invoked when the object is destroyed.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When PHP reads serialised data it can be tricked into creating objects of classes the programmer never intended, and those objects can run code when they are cleaned up. The YAML parsing functions were believed to avoid this, but a special tag lets YAML input reach the same dangerous serialisation routine, so untrusted YAML becomes as risky as untrusted serialised data.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.