text
stringlengths
0
1.99k
["a"]=>
string(1) "b"
}
researcher@venus:~/sentinel$ php -r 'var_dump(json_decode("false"));'
bool(false)
...........................................................................
The attacker can control the return type as well as value from the cookie
grab, nice! After that, findUserByPersistenceCode() is called at (6) from
within the Sentinel class:
...........................................................................
public function findByPersistenceCode($code)
{
$persistence = $this->createModel()
->newQuery()
->where('code', $code)
->first(); // 8
return $persistence ? $persistence : false;
}
public function findUserByPersistenceCode($code)
{
$persistence = $this->findByPersistenceCode($code); // 7
return $persistence ? $persistence->user : false;
}
...........................................................................
At (7) the call to findByPersistenceCode() is triggered with the
attacker-controlled cookie (type/value). At (8) a query is built using
Illuminate's query builder API and it takes into consideration the code
*type* during construction. If it's a string then the following query is
built:
...........................................................................
select * from persistences where code='1337';
...........................................................................
However, if it's an int then the following query is built:
...........................................................................
select * from persistences where code=1337;
...........................................................................
Lucky for us the code is generated with str_random(32) which includes
numeric values!
--[ 4. Mitigations
I mean really, there is a lot you can do to prevent this. Right off the
bat, if you use Laravel + Sentinel then you are not affected assuming you
use the service provider SentinelServiceProvider which is the default.
This is because the code uses the IlluminateCookie class for cookie
management vs the NativeCookie class for a Native deployment. The
IlluminateCookie class does not use json_decode():
...........................................................................
/**
* Registers the cookie.
*
* @return void
*/
protected function registerCookie()
{
$this->app->singleton('sentinel.cookie', function ($app) {
return new IlluminateCookie(
$app['request'],
$app['cookie'],
$app['config']->get('cartalyst.sentinel.cookie')
);
});
}
...........................................................................
The other mitigating factor is that in newer versions of Cartalyst Sentinel
(> v2.0) the code uses type hinting. So, assuming that your target is using
a *newer* version of PHP (> php 7.0), I know big assumption right? then
they are safe because >= v3 specifies the return type for the check()
method, so we can't return an int. If that isn't enough then you will
see that findUserByPersistenceCode() hints at a string type for the
code argument:
...........................................................................
public function check(): ?string
{
if ($code = $this->session->get()) {
return $code;
}
if ($code = $this->cookie->get()) {
return $code;
}
return null;
}
public function findByPersistenceCode(string $code):
?PersistenceInterface
{
return $this->createModel()->newQuery()->where('code',
$code)->first();
}