text
stringlengths
0
1.99k
php > $user = 'orange';
php > $pass = ';s:8:"password";O:4:"Evil":0:{}s:8:"realname";s:5:"pwned';
php > $name = 'Orange Tsai' . str_repeat('..', 25);
php > $obj = new User($user, $pass, $name);
php > $data = serialize($obj);
|-------------------------------- [ next ] -------------------------------|
=> [2] developers attempt to block path traversal :)
php > $data = str_replace("..", "", $data);
|-------------------------------- [ next ] -------------------------------|
=> [3] the length of `realname` field has been corrupted ;)
php > print_r($data);
O:4:"User":3:{
s:8:"realname";s:61:"Orange Tsai";s:8:"username";[...]
^^ <--- corrupted length: [61 bytes]
|---------------------------------|
|"Orange Tsai";s:8:"username";s:6:|
|"orange";s:8:"password";s:56:" |
|---------------------------------|
;s:8:"password";O:4:"Evil":0:{}
s:8:"realname";s:5:"pwned";
}
|-------------------------------- [ next ] -------------------------------|
=> [4] We have smuggled our own *Evil* object!
php > print_r(unserialize($data));
User Object (
[realname] => pwned
[password] => Evil Object ()
)
+-------------------------------------------------------------------------+
Though the above case sounds specific - developers ideally shouldn't mess
around with serialized content in the first place - the real world is
usually way more complex than expected. As architectures start stacking
layers upon layers, it's easy to make mistakes. A perfect showcase is how
WordPress introduced a "dirty fix" to address its infamous "Double
Preparing" problem [27].
Basically, Double Preparing is simply a bad developer practice. It stems
from the mistaken assumption that anything returned by one `prepare()` is
inherently safe - so passing it to another `prepare()` must be safe, too.
Though WordPress does block dangerous characters from SQL injection, it
can't really prevent developers from misusing format strings like `%s`.
This bad practice ultimately breaks the whole prepared statement
mechanism, reviving SQL Injection once again!
----------------------[ Prepare Twice, Inject Once! ]----------------------
php > $value = "%1$%s OR 1=1--#";
php > $clause = $wpdb->prepare(" AND value = %s", $value);
php > $query = $wpdb->prepare(
"SELECT col FROM table WHERE key = %s $clause", $key);
php > $wpdb->get_row($query);
// SELECT col FROM table WHERE key='****' AND value = '****' OR 1=1--#'
---------------------------------------------------------------------------
And to keep developers from stepping on this landmine, WordPress
introduced a workaround - it temporarily replaces all formatting
characters processed by `$wpdb->prepare()` with placeholders, and only
restores all right before executing the actual query. Though this approach
effectively stops developers from introducing unexpected formatting
characters while constructing queries, WordPress didn't count on one
special case - the query itself might also contain placeholders!
=> The WordPress Way: Hiding every single `%`!
---------------------------------------------------------------------------
public function prepare( $query, $args ) {
/* [...] formatting the $query with $args */
// [!!!] replace `%` with a *random* placeholder.
return str_replace( '%', $this->placeholder_escape(), $query );
}
---------------------------------------------------------------------------
So when this mechanism is combined with the serialization process we
mentioned earlier, developers could unintentionally create a "serialized
string containing placeholders," causing WordPress to mistakenly restore
extra placeholders, storing serialized data with mismatched lengths. Then,
the next time WordPress retrieves the data, it gets parsed incorrectly -
turning a previously legitimate string into a malicious object.
Since this behavior is considered just an unintended side effect, the
issue still exists even in the latest version. For plugin developers
relying on the WordPress ecosystem, the best they can do is try to avoid
stepping on this landmine as much as possible - or they'll end up like
WooCommerce, becoming yet another victim of deserialization
vulnerabilities [28].