Sql Injection in Cockroachdb

How Sql Injection Manifests in Cockroachdb

SQL Injection in CockroachDB exploits the database's SQL interface to execute malicious queries. While CockroachDB shares many SQL injection patterns with other databases, its distributed architecture and specific query patterns create unique attack vectors.

CockroachDB's support for PostgreSQL wire protocol and SQL syntax means it inherits classic injection patterns. However, its distributed nature introduces specific concerns:

  • Cross-node query propagation: Malicious queries can traverse multiple nodes in a cluster, potentially exposing data across the entire distributed system
  • Time-based blind injection: CockroachDB's distributed clock implementation can be exploited for timing attacks that reveal data through query execution delays
  • JSONB injection: CockroachDB's native JSONB support enables injection attacks that manipulate JSON structures within queries

Common injection patterns in CockroachDB applications include:

// VULNERABLE: String concatenation creates injection point
func getUserByUsername(db *pgx.Conn, username string) (*User, error) {
query := fmt.Sprintf(`SELECT * FROM users WHERE username = '%s'`, username)
row := db.QueryRow(query)
// ...
}

An attacker could supply admin' OR '1'='1 as the username, bypassing authentication entirely.

Another CockroachDB-specific pattern involves the crdb_internal schema:

SELECT * FROM crdb_internal.node_build_info;
SELECT * FROM crdb_internal.node_metadata;

Unauthenticated access to these system tables can leak cluster information, including node IDs, build versions, and deployment details useful for further attacks.

Time-based blind injection in CockroachDB might look like:

SELECT * FROM users WHERE username = 'admin' AND (SELECT count(*) FROM crdb_internal.node_build_info) > 0 OR SLEEP(5)='

This exploits the distributed nature of system queries to create measurable delays across nodes.

Cockroachdb-Specific Detection

Detecting SQL Injection in CockroachDB requires both runtime monitoring and static analysis. middleBrick's black-box scanning approach tests the unauthenticated attack surface by submitting crafted payloads to API endpoints that interact with CockroachDB.

middleBrick's SQL Injection detection for CockroachDB specifically tests:

  • Authentication bypass: Attempting to log in with injection payloads like ' OR '1'='1
  • Data exfiltration: Testing for UNION-based attacks that append system table queries
  • Time-based detection: Measuring response delays from sleep functions
  • Error-based detection: Analyzing error messages for SQL syntax details

For CockroachDB-specific detection, middleBrick scans for:

{

Related CWEs: inputValidation

CWE IDNameSeverity
CWE-20Improper Input Validation HIGH
CWE-22Path Traversal HIGH
CWE-74Injection CRITICAL
CWE-77Command Injection CRITICAL
CWE-78OS Command Injection CRITICAL
CWE-79Cross-site Scripting (XSS) HIGH
CWE-89SQL Injection CRITICAL
CWE-90LDAP Injection HIGH
CWE-91XML Injection HIGH
CWE-94Code Injection CRITICAL