#!/usr/bin/env bash
#=============================================================================
# Sentinela ? Intelligent Linux Security Framework
# ids/sqli.sh ? Detección avanzada de SQL Injection
#=============================================================================
#
# Detecta patrones de SQL Injection en:
# - URI (GET parameters)
# - POST body (via logs)
# - User-Agent
# - Referer
# - Cookies (via logs de Apache)
#
# Patrones cubiertos:
# - SQL keywords (union, select, insert, drop, etc.)
# - Time-based (sleep, benchmark, waitfor delay)
# - Error-based (extractvalue, updatexml, convert)
# - Blind (comparison operators, ascii, substr)
# - Hex encoding (0x...)
# - URL encoding (%27, %22, etc.)
# - Double URL encoding (%2527, %2522)
# - Unicode bypasses (%c0%ae, %c0%bc)
# - Comments (--, /**/, #, --+)
# - Stacked queries (;)
# - Out-of-band (xp_cmdshell, sp_configure)
#=============================================================================
# shellcheck source=lib/common.sh
source "${SENTINELA_DIR}/lib/common.sh"
#=============================================================================
# Patrones de SQL Injection
#=============================================================================
SQLI_PATTERNS=(
# Keywords SQL básicos
'union.*select'
'select.*from'
'insert.*into'
'update.*set'
'delete.*from'
'drop.*table'
'drop.*database'
'alter.*table'
'create.*table'
'truncate.*table'
'exec.*xp_cmdshell'
'exec.*sp_configure'
'information_schema'
'mysql\.user'
'mysql\.db'
'pg_catalog'
'sqlite_master'
# Time-based
'sleep\([0-9]'
'benchmark\([0-9]'
'waitfor.*delay'
'pg_sleep'
# Error-based
'extractvalue\('
'updatexml\('
'convert\('
'cast\('
'floor\(rand'
'count\(\*\)'
# Blind SQLi
'ascii\('
'substr\('
'substring\('
'ord\('
'char\('
'if\('
'case when'
'case.*when.*then'
# Hex encoding
'0x[0-9a-f]{8,}'
'0x[0-9a-f]{4,}'
# Comments SQL
'--[^-]'
'/\*!.*\*/'
'#'
# URL encoding malicioso
'%27'
'%22'
'%2527'
'%2522'
'%%27'
'%%22'
# Unicode bypass
'%c0%ae'
'%c0%bc'
'%c0%af'
'%c1%9c'
# Stacked queries
';\s*drop'
';\s*insert'
';\s*delete'
';\s*update'
';\s*exec'
';\s*create'
';\s*alter'
';\s*truncate'
# Out-of-band
'xp_cmdshell'
'sp_configure'
'sp_oacreate'
'sp_oamethod'
# Information gathering
'@@version'
'@@datadir'
'@@basedir'
'user\(\)'
'database\(\)'
'current_user'
'system_user'
)
#=============================================================================
# Verificar si una línea contiene SQL Injection
#=============================================================================
sqli_check() {
local text="$1"
for pattern in "${SQLI_PATTERNS[@]}"; do
if echo "${text}" | grep -qiP "${pattern}"; then
echo "${pattern}"
return 0
fi
done
return 1
}
#=============================================================================
# Analizar URI en busca de SQL Injection
#=============================================================================
sqli_analyze_uri() {
local uri="$1"
local decoded_uri
# Intentar doble decodificación
decoded_uri=$(echo "${uri}" | python3 -c "import sys,urllib.parse; print(urllib.parse.unquote(urllib.parse.unquote(sys.stdin.read())))" 2>/dev/null || echo "${uri}")
sqli_check "${decoded_uri}"
return $?
}
#=============================================================================
# Analizar POST body (si está disponible en logs)
#=============================================================================
sqli_analyze_body() {
local body="$1"
sqli_check "${body}"
return $?
}
#=============================================================================
# Analizar User-Agent
#=============================================================================
sqli_analyze_ua() {
local ua="$1"
sqli_check "${ua}"
return $?
}
#=============================================================================
# Analizar Cookie
#=============================================================================
sqli_analyze_cookie() {
local cookie="$1"
sqli_check "${cookie}"
return $?
}
|