Skip to content
RCE Write-up Advanced

[Blog] Exploiting H2 Database with native libraries and JNI

The H2 Database Engine's web console allowed an attacker to create aliases for public static Java methods and write arbitrary files via CSVWRITE, enabling loading of a native library that uses JNI to inject and execute arbitrary Java code without needing a Java compiler.

code-white.com

Published on code-white.com

Techniques to gain code execution in an H2 Database Engine are already well known but require H2 being able to compile Java code on the fly. This blog post will show a previously undisclosed way of exploiting H2 without the need of the Java compiler being available, a way that leads us through the native world just to…

The author, on code-white.com

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

Likely the H2 web console allowed execution of SQL statements that could create aliases to arbitrary public static Java methods and use CSVWRITE to write files, without sufficient restrictions.

Impact

An attacker could execute arbitrary code on the server hosting the H2 database, leading to full compromise.

What the attacker needed

H2 Database Engine web console exposed; H2 version 1.2.141 (or similar); No Java compiler available (JRE only)

How it was found

  1. Use CSVWRITE with a crafted column name to write a native library file to disk.
  2. Create an alias for System.load and call it to load the native library.
  3. The native library uses JNI to define a custom Java class in the running JVM.
  4. Create an alias for the custom class's eval method and call it to execute arbitrary code.

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.

H2 is a database that can run inside a Java program. The report shows that if its web console is exposed, an attacker can use built-in database commands to write a file and then load it as a native library, which lets them run their own code on the server. This is dangerous because it bypasses the usual requirement for a Java compiler.

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.