Skip to content
RCE Write-up Advanced

Exploiting H2 Database with native libraries and JNI

The H2 Database Engine's CSVWRITE function could be abused to write arbitrary binary files, including native libraries, to the server. Combined with the ability to create an alias for System.load, an attacker could load a malicious native library and use JNI to inject and execute arbitrary Java code without needing a Java compiler.

Markus Wulftange Follow

Published on codewhitesec.blogspot.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…

Markus Wulftange, on codewhitesec.blogspot.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

Impact

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

What the attacker needed

H2 Database Engine version 1.2.141; Web console exposed; No Java compiler available

How it was found

  1. Use CSVWRITE with crafted column names to write a native library file to disk.
  2. Create an alias for System.load(String) to load the written native library.
  3. The native library uses JNI to attach to the JVM and call ClassLoader.defineClass to inject a custom Java class.
  4. Create an alias for the injected class's static method to execute arbitrary JavaScript from SQL.

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 Java programs. The report shows that a built-in database function could be tricked into writing any file to the server's disk, and another feature let the attacker load that file as a native library. The native library then used a bridge called JNI to run custom Java code, giving the attacker full control over the database server.

What is RCE?

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

More by Markus Wulftange

View all

11 more disclosed reports, best first.

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.