Archiving the intermediate analysis result and sharing with other researchers is a common practice in the field of binary analysis. Popular disassemblers such as IDA and Ghidra also provide the project archiving functionality to help researchers communicating their results. However, it turns out that such project sharing is not secure (at all). For example, researchers have found that an XXE vulnerability existed in the project parsing logic of Ghidra, which can be abused to achieve arbitrary file read or even command execution on a particular platform(i.e., Windows). In this post, we are presenting an even more powerful exploit chain discovered in Ghidra, which can be exploited via a malicious project archive to execute arbitrary code in the victim machine no matter what platform he is using.
Vulnerablity Description
A path traversal vulnerability exists in RestoreTask.java from package ghidra.app.plugin.core.archive. This vulnerability allows attackers to overwrite arbitrary files on the system. To achieve arbitrary code execution, one of the solutions is to overwrite some critical ghidra modules, e.g., decompile module (In this case we need to know the installation path of ghidra).
Exploit Analysis
The arbitrary code execution exploit chain consists of two nodes. The first node overwrites arbitrary files in the system, and the second node executes the malware containing in the crafted project file.
Overwrite Arbitrary File
In Ghidra, every project is a jar archive file. When restoring a project, Ghidra will extract the project files and store them in the file system. However, Ghidra doesn’t check whether the file name of every archive entity is legal or not. By inserting a malicious binary with a file name starting with ../, attackers are able to let ghidra overwrite arbitrary files on the system.
The vulnerability locates at Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/archive/RestoreTask.java
The unjarArchive method iterates every file entity in the project archive and stores it on the file system directly using its original filename recording in the archive.
1 | private void unjarArchive(TaskMonitor monitor) throws IOException { |
Code Execution
As demonstrated by the following code, the DecompileProcess.java locating in the ghidra.app.decompiler package will executes the decompile under GHIDRA_INSTALL_DIR/Ghidra/Features/Decompiler/os/YOUR_ARCH/ folder. This method will be called when ghidra is initiated and start to decompile the binary.
1 | private void setup() throws IOException { |
Attack Demo
Here is a demo of the attack behavior. Youtube