Skip to main content

Arbitrary Code Execution Through Uploading a Malicious JSP File – Vulnerability Analysis

Illustration of Arbitrary Code Execution Through Uploading a Malicious JSP File – Vulnerability Analysis
MIKOŁAJ PUDLICKI

Introduction

During security tests, a critical vulnerability was discovered in the tested application. This issue allows an attacker to execute any code on the server by uploading a malicious JSP file. The problem arises from a lack of proper validation of uploaded files, which can be exploited by attackers.

Exploiting improper file validation

An attacker can upload a malicious JSP file to the server due to weak file validation. This ZIP file contains a web page structure that is converted into a JSP file. The attacker places harmful code in the file, such as in index.html. After the ZIP file is uploaded and extracted, the attacker can access the path to the JSP file and execute any system commands on the server using the permissions of the application user (e.g., Tomcat).

Proof of concept

The attack requires logging into an administrator account, which lowers the risk level to high. Below are examples of information obtained by executing system commands on the server:

  1. “pwd”: /home/tomcat-[REDACTED]
  2. “id”: uid=1003(tomcat-[REDACTED]) gid=1003(tomcat-[REDACTED]) groups=1003(tomcat-[REDACTED])

Importantly, the tests showed that no antivirus software was running on the server to detect the malicious code in the uploaded files.

How the attack works

  1. Creating a malicious file - the attacker crafts an index.html with harmful JSP code that allows executing system commands.

Malicious file creation

  1. Preparing the ZIP file - the attacker places the index.html file in a specific folder structure and compresses it into a ZIP file:

ZIP file preparation

  1. Uploading the file to the server - after logging into the administrator account, the attacker:

    a) Navigates to: Administration → Web Templates → New Template. b) Fills out the form and select the prepared malicious ZIP file as the template. c) Submits the template and opening its preview.

  2. Executing system commands - once the file is uploaded and previewed, the attacker tests the vulnerability by running commands such as uname -a, gaining detailed system information.

Command execution

Recommendations

To defend the application from such attacks, it is crucial to implement several protective measures.

Firstly, the use of an allowlist for uploaded files is highly recommended. By restricting the types of files that can be uploaded, such as permitting only specific extensions and verifying MIME types, the risk of undetected malicious files can be minimized. Additionally, checking file headers and limiting file size can add further layers of security.

Secondly, every file uploaded to the server should undergo rigorous validation and scanning. This ensures that harmful code embedded within files can be detected and neutralized. It is essential to apply these checks to all areas of the application that facilitate file uploads, leaving no potential entry points unprotected.

Finally, adding extra layers of protection can significantly enhance security. Installing antivirus software on the server provides a frontline defense against malicious files. Moreover, restricting user permissions related to file handling and keeping a close watch on administrative activities through detailed monitoring can prevent unauthorized actions and quickly identify suspicious behavior.

Other Insights

Illustration of Exploiting the Password Reset Vulnerability: A Real-World Case Study.

Exploiting the Password Reset Vulnerability: A Real-World Case Study.

Securitum

Modern web applications need to prioritize user security. However, even well-designed systems can have hidden flaws that make them vulnerable to attacks. During a recent security test, a serious issue was found in the password reset feature of an application. This vulnerability made it possible for attackers to gain access to any user account, including the super administrators. Here's what went wrong and why it's such a big problem.

READ article
Illustration of Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Hacking - Man-in-the-Middle and Brute-force in Action. Part 2 of 2.

Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Hacking - Man-in-the-Middle and Brute-force in Action. Part 2 of 2.

Dariusz Tytko

In first part of this article, I described how do we analyze protocols during mobile applications testing. During this analysis, I noticed that the Diffie–Hellman protocol is used to exchange encryption keys. The protocol implementation was audited, and I discovered that it is prone to two attacks: Man-in-the-Middle and brute-force. Each of these attacks compromise the security of the protocol, allowing attackers to view and modify the data sent between the mobile applications and the servers.

READ article
Illustration of Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Analysis and Toolkit Setup. Part 1 of 2.

Exploring Vulnerabilities in Mobile Applications: Key Exchange Protocol Analysis and Toolkit Setup. Part 1 of 2.

Dariusz Tytko

During one of latest pentests I tested mobile application. To perform analysis of the communication protocol and prepare a toolkit for testing network communication, the Android version of the application was used. Protocol analysis The protocol is implemented using C++ language, the implementation is included in the native library lib/arm64-v8a/lib[…].so. Wireshark, Frida (the instrumentation toolkit) and Ghidra (a software reverse engineering framework) were used to analyze the protocol.

READ article
A professional cybersecurity consultant ready to assist with your inquiry.

Any questions?

Happy to get a call or email
and help!