How to get a shell on a H2 Database, using ALIAS feature.
Today I was introduced toย H2 Database, a in-memory and pure Java Database, because itโs a in-memory database, the developers use it most to learning, unit tests and pocโs, but you can learn more about it on H2ย site.
The H2 provides a web console, where you can manage your database, and here the things starts to be more interesting, by default it does not have an password set, so you can just log in, but what can we do inside it? The first thing I tried was the same trick that everyone knows on MySQL.
And of course it didnโt work, so I decided to be more smart and google it, trying to discover if anybody already find some RCE on it, and the maximum I found was a report made by the H2 Group onย SecurityFocus, but there wasnโt an available exploit or any technical detail.
So after it I did the most obvious thing, open the H2 site, go to the documentation, and tried to find any interesting function, the first thing I found was theย FILE_READย function, where I can read files from filesystem, Ok, cool, itโs a nice thing to do, but itโs not a shell, so digging on SQL commands section, I found theย CREATE ALIAS, basically, you can create an function on H2 that calls a java code, as the example
or a more complex alias,
Now itโs game over, if I can execute Java code, I can get a shell, as Iโm not a Java expert, I searchon Google a easy way to execute system commands with java, found a link onย stackoverflow, I just adapted it inside the ALIAS, and now thereโs a function that execute arbitrary code,
After that just find if the server have any tool to do aย reverse shell, and youโll gain a interactive shell.
Attack Scenario
One scenario is a distributed database called Datomic. The free version of Datomic uses an embedded H2 storage, and older versions of Datomic enabled the H2 console with the default blank H2 password. The free version is often used locally by developers for quick prototyping, thus unauthenticated local attackers can easily compromise their machines. The issue was disclosed to the Datomic team and was quicklyย fixed.