Star Cereal
PHP insecure deserialisation vulnerability
Last updated
PHP insecure deserialisation vulnerability
Last updated
Have you heard of Star Cereal? It's a new brand of cereal that's been rapidly gaining popularity amongst astronauts - so much so that their devs had to scramble to piece together a website for their business! The stress must have really gotten to them though, because a junior dev accidentally leaked part of the source code...
http://20.198.209.142:55043
The flag is in the flag format: STC{...}
Author: zeyu2001
The goal of this challenge is to perform an authentication bypass through a PHP object injection vulnerability. There are three classes involved, and each one of them needs to be examined to construct a "POP chain" for successful exploitation.
We are given the following page:
Going over to the login page, we see the following 3 fields.
At the bottom of the provided source code, we see the logic behind the application's authentication.
The login
cookie is deserialized into a Login
object. This should already sound some alarm bells!
The Login
object consists of a User
object and an MFA token. The $mfa_token
is checked against an integer $_correctValue
randomly generated at runtime. If the check passes, the user credentials are then checked.
Interestingly, the User
class instantiates a SQL
object, and uses it to execute SQL queries to authenticate the user. If results are returned and consist of the email
and password
columns, then the authentication is successful.
The SQL
class contains a $query
attribute that is used to generate a prepared statement. Note that if the bind_param()
call returns false
, the authentication fails. This can happen if, for example, the number of parameters in the prepared statement and the number of variables to bind do not match.
When user data is deserialized into objects, we can inject custom objects to e.g. modify protected attributes, bypass authentication, etc. We can bypass the above checks by using a "POP chain" of custom objects.
The MFA token check can be bypassed if we set $mfa_token
as a reference to the $_correctValue
attribute using the ampersand (&). Note that in PHP, a reference is simply another variable that points to the same data (unlike pointers in C).
Thus, this will ensure that the two values are always equal.
The custom object can be generated as follows:
Note that the SQL
class has a $query
attribute that is used in the prepared statement. By simply modifying the $query
, we can perform an SQL injection.
To bypass the authentication we simply need a valid result set with email
and password
columns.
We can use something like
which will return one row with email
and password
columns.
Remember the bind_param()
check? We still need to make sure that there are two parameters in the prepared statement, so we will do something like this:
any other valid query that makes use of two parameters would work too.
Using the previously discussed knowledge, it is now trivial to create a solver script that gives us the required base-64 encoded serialized data.
Running the above script gives us the required cookie value.
Plugging this into the login
cookie on our browser, we can login and get the flag.