PHP Session Data Injection Vulnerability
PHP's session 'php' and 'php_binary' handlers incorrectly skip disallowed session variable names during deserialisation, allowing an attacker who controls the session name to inject arbitrary serialised data. This can lead to object injection and use-after-free vulnerabilities similar to unsafe unserialize().
- Fixed in 34 days
- Paid in 34 days
- Disclosed after 3.2 years
Summary by the researcher
PHP's session php/php_binary handlers wrongly handles the session name cause arbitrarily session data injection.
The report
#PHP Session Data Injection Vulnerability
bug report at: https://bugs.php.net/bug.php?id=72681 fix commit at: https://github.com/php/php-src/commit/8763c6090d627d8bb0ee1d030c30e58f406be9ce
Affected Versions
Affected is PHP 5 < 5.6.25<br /> Affected is PHP 7 < 7.0.10
Credits
This vulnerability was disclosed by Taoguang Chen.
Description
PS_SERIALIZER_DECODE_FUNC(php) /* {{{ */
{
...
while (p < endptr) {
zval **tmp;
q = p;
while (*q != PS_DELIMITER) {
if (++q >= endptr) goto break_outer_loop;
}
if (p[0] == PS_UNDEF_MARKER) {
p++;
has_value = 0;
} else {
has_value = 1;
}
namelen = q - p;
name = estrndup(p, namelen);
q++;
if (zend_hash_find(&EG(symbol_table), name, namelen + 1, (void **) &tmp) == SUCCESS) {
if ((Z_TYPE_PP(tmp) == IS_ARRAY && Z_ARRVAL_PP(tmp) == &EG(symbol_table)) || *tmp == PS(http_session_vars)) {
goto skip;
}
}
...
skip:
efree(name);
p = q;
}
If the seesion name is not allowed, then the seesion php handler will ignore and skip the name, and continue to parsing. Therefore, if an attacker can control the session name, then he will be able to inject arbitrarily serialized data into the session. This means that such as the following code from real world&apps can be used to inject arbitrarily session data.
i)
$_SESSION = array_merge($_SESSION, $_POST);
ii)
if (isset($_GET['id']) && $_GET['result']) {
$_SESSION[$_GET['id']] = $_GET['result'];
This also means allow user input into session deserialization. This will lead to vulnerabilities that are similar to input into unserialize(), for example use-after-free, object injection, and etc.
The similar issue also exist in the session php_binary handler.
Proof of Concept Exploit
<?php
ini_set('session.serialize_handler', 'php');
session_start();
$_SESSION['_SESSION'] = 'ryat|O:8:"stdClass":0:{}';
session_write_close();
session_start();
var_dump($_SESSION);
?>
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 session deserialisation loop skips disallowed variable names but continues parsing, so an attacker-controlled session name can inject serialised data.
Impact
An attacker could inject arbitrary serialised data into the session, leading to vulnerabilities similar to unsafe unserialize(), such as use-after-free and object injection.
What the attacker needed
Attacker controls session name; PHP version before 5.6.25 or 7.0.10
The fix
The affected versions are PHP below 5.6.25 and 7.0.10.
How it was found
- Set the session serialisation handler to 'php'.
- Start a session and set a session variable with a crafted name and serialised value.
- Write and close the session.
- Start the session again and observe the injected data.
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.
PHP stores session data in a file or memory and later reads it back. The code that reads it back has a flaw: if a variable name is not allowed, it skips that name but keeps reading the rest of the data. An attacker who can choose the session name can sneak in extra data that gets treated as trusted session content, potentially leading to serious bugs like object injection.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.