Skip to content
RCE Write-up Intermediate

Jasper Reports Library Code Injection

An API that accepted user-supplied Jasper Report templates allowed Java code injection through template expressions, leading to remote code execution. The library compiles expressions by string concatenation without sanitisation, so an attacker could break out of the assignment and execute arbitrary Java code.

Dennis Heinze Follow

Published on insinuator.net

During the past year we had several projects where our target application used Jasper Reports in some way. In a few of the cases we found an API that offered to render a template along with some arguments into a PDF file.

Dennis Heinze, on insinuator.net

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The library generates Java code by concatenating expression text directly into a 'value = ...' assignment without any sanitisation, allowing an attacker to break out and inject arbitrary statements.

Impact

An attacker could execute arbitrary code on the server, leading to full compromise, and potentially establish a reverse shell.

What the attacker needed

API accepts user-supplied Jasper templates; Attacker can send POST requests to the endpoint

The fix

Never allow user-controlled Jasper Report templates; use a fixed set of reviewed templates instead.

How it was found

  1. Send a POST request to the template rendering endpoint with a JSON body containing a base64-encoded Jasper template and optional parameters.
  2. In the template, define a parameter (e.g., 'cmd') and a text field expression that executes a system command using Runtime.getRuntime().exec().
  3. The expression returns a string, so the command output is displayed in the generated PDF.
  4. To execute arbitrary Java statements, break out of the assignment by starting the expression with a string literal and a semicolon, then add the desired code.
  5. For a reverse shell, include a full Java reverse shell payload in the expression, using parameters for host and port.
  6. Start a netcat listener on the specified port to catch the reverse shell.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Jasper Reports is a tool that turns templates into PDFs. The templates can contain small pieces of Java code, and the library builds a Java program by gluing those pieces together as text. If an attacker can supply the template, they can include their own Java code, which then runs on the server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.