Skip to content

Commit

Permalink
9/8の記事を投稿
Browse files Browse the repository at this point in the history
  • Loading branch information
YusukeKato committed Sep 8, 2024
1 parent c5ffddf commit 739c54c
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
Binary file added images/2024/20240908_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2024/20240908_1.jpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2024/20240908_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2024/20240908_2.jpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2024/20240908_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/2024/20240908_3.jpg.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions markdown/2024/0819.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ keymoonさんのXのポスト
初めてリアルタイムでCTFに参加しました。
とても楽しかったので今後もまた参加したいと思います。
それでは、また。

## 次の記事
2024年9月8日の記事です。

https://yusukekato.jp/html/2024/0908.html

AlpacaHackで始めるCTF入門4:AlpacaHack Round 2 - Simple Loginに挑戦

162 changes: 162 additions & 0 deletions markdown/2024/0908.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# メタデータ
- title=AlpacaHackで始めるCTF入門4:AlpacaHack Round 2 - Simple Loginに挑戦
- description=個人戦CTFを開催するプラットフォーム「AlpacaHack」を通じてCTFに入門してみようと思います。今回はAlpacaHack Round 2に参加しました。
- date=2024年9月8日(日)
- update=2024年9月8日(日)
- math=false
- tag=alpacahack

## はじめに

### はじめに
リアルタイムではないですが、AlpacaHack Round 2の問題に挑戦してみます。
今回は最初の問題の「Simple Login」を解いてみます。
ジャンルとしては「Web」とのことです。
CTFの問題はまだ数問しか解いたことがないので初心者も初心者ですが、
調べながらやっていこうと思います。

https://alpacahack.com/ctfs/round-2

AlpacaHack Round 2のトップページ

### AlpacaHack
AlpacaHackへのリンクはこちら

https://alpacahack.com/

AlpacaHackのサイトへのリンク

### 私のCTF環境
- Windows 11/WSL2
- Ubuntu 22.04 LTS

## 前回の記事
2024年8月19日の記事です。

https://yusukekato.jp/html/2024/0819.html

AlpacaHackで始めるCTF入門3:初めてのCTFに参加

## Simple Login

### 問題を確認
問題はこんな感じです。
ログイン処理があるウェブサイトが題材のようです。
これまでCTFに参加してこなかったのでCTFの普通というものが分かっていないですが、基本的にはCTFでは問題文的なものはないっぽいです(もちろん問題文があるコンテストもあるとは思います)。
flagを回収するというルールは変わらないので問題文は必要ないのかもしれません。

![](../../images/2024/20240908_1.jpg)

問題情報

### 問題用サーバにアクセスしてみる

「simple-login.tar.gz」をクリックすると問題のデータをダウンロードできます。
「.tar.gz」は圧縮ファイルなので展開します。
中身を確認するとウェブサイトのトップページとログインページのHTMLファイル、サーバ側の処理のPythonスクリプト、データベースのファイルが存在しています。

次に「Spawn Challenge Server」ボタンをクリックして問題用サーバを起動します。
とりあえずアクセスしてみるとログイン画面が表示されます。
データベースのファイルを読んでみるとユーザ名が`admin`でパスワードが`pass`のユーザデータがあるっぽいので入力してみます
`hacker`というユーザもいる)。

```
INSERT INTO users (username, password) VALUES ('admin', 'pass');
INSERT INTO users (username, password) VALUES ('hacker', '1337');
```

ログイン画面↓

![](../../images/2024/20240908_2.jpg)

ログイン画面

ログインできるとこのような表示になります↓

![](../../images/2024/20240908_3.jpg)

ログインできた

### flagの場所を確認
データベースのファイルを読んでみると「flag」が挿入されている箇所が見つかるので、おそらくデータベースからflagを奪取できれば良いのだろうと分かります。

```
INSERT INTO flag (value) VALUES ('Alpaca{REDACTED}');
```

### flagを奪取する方針
ウェブ上での動作は確認できたのでサーバ側の処理も確認します。
Pythonスクリプトを読んでみるとログインページにPOSTされた時に、
データベースからユーザ情報を照合しているようです。
つまりここでSQLインジェクションを行える可能性があります。

該当箇所は下記の一行です。ここではPOSTされた時の`username``password`を使ってデータベースでユーザ情報を照合しているようです。
処理としては`users`というテーブルの中に`username``password`が一致するデータがあるかどうか調べています。

```
f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
```

ここの処理で何とかして`flag`テーブルからflagの文字列を奪取したいです。

### POSTしてみる
下記のPythonスクリプトをログインページへPOSTすると`200`が返ってきて、ログイン後のトップページのHTMLも取得できます。

```py
import requests
response = requests.post("http://<Server-Address>/login", data={'username': 'admin', 'password': 'pass'})
print(response.status_code)
print(response.text)
```

返ってきた内容↓
```
200
<!DOCTYPE html>
<html>
<head>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css"
/>
<title>Simple Login</title>
</head>
<body>
<main>
<h1>Simple Login</h1>
<p>Hello, admin</p>
<marquee scrollamount="16" direction="right">
Logged in successfully🎉
</marquee>
</main>
</body>
</html>
```

### SQLインジェクションの試み
SQLへの操作を行う一文では`username``password`をそのまま入れ込んでいるので、ここでいい感じにSQLインジェクションを行いたいです。
ChatGPTにSQLインジェクションについて質問したところ、シングルクウォートを`username``password`に入れて予期せぬ操作を行うのが基本らしいです。
ただし、app.pyではシングルクウォートが入らないようにif文で予防しているのでそのままでは無理そうです。とはいえ`\'`でシングルクウォートをエスケープはできそうです。

`username``\`を入力するとエラーが出ます↓

```
500
Error: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''' at line 1")
```

ChatGPTが`OR 1=1`と書けば条件が常に`true`になると教えてくれたのえ、POSTするデータを下記のように変更したところ、無事ログインできました。

```
data={'username': '\\', 'password': 'OR 1=1 #'}
```

あとはflagのテーブルからflagの文字列を取得すれば良さそうですが、SQLの知識が無くて現状flagは取得できていません。
SQLインジェクションについては何となく理解できたので、
続きは他の方のwrite upなどを参考にしてみようと思います。

## おわりに
今回はAlpacaHack Round 2の問題「Simple login」に挑戦してみました。
結果的には解けていませんが、SQLインジェクションについて理解が深まったので良かったです。
またAlpacaHackには挑戦していきたいと思います。
それでは、また。

0 comments on commit 739c54c

Please sign in to comment.