#!/bin/bash
# =============================================================================
# Script de Test - ModSecurity Rules (XSS, SQL Injection, RCE, LFI, RFI)
# =============================================================================
# Description: Teste les règles WAF pour XSS, SQL, RCE, LFI et RFI
# Usage: ./test-rules.sh
# =============================================================================
# Couleurs pour l'affichage
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
BOLD='\033[1m'
# Configuration
API_BASE_URL="http://172.20.167.237"
# Credentials Client
CLIENT_USERNAME="salut"
CLIENT_PASSWORD="salut1234_"
CLIENT_TOKEN=""
# Credentials Admin
ADMIN_USERNAME="admin_1768505094"
ADMIN_PASSWORD="AdminPass123!"
ADMIN_TOKEN=""
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
LOG_FILE="modsec_test_$(date +%Y%m%d_%H%M%S).log"
# =============================================================================
# Fonctions Utilitaires
# =============================================================================
print_header() {
echo -e "\n${BOLD}${CYAN}========================================${NC}"
echo -e "${BOLD}${CYAN}$1${NC}"
echo -e "${BOLD}${CYAN}========================================${NC}\n"
}
print_section() {
echo -e "\n${BOLD}${BLUE}>>> $1${NC}\n"
}
print_test() {
echo -e "${YELLOW}[TEST] $1${NC}"
}
print_success() {
((PASSED_TESTS++))
((TOTAL_TESTS++))
echo -e "${GREEN}✓ PASS${NC} - $1" | tee -a "$LOG_FILE"
}
print_fail() {
((FAILED_TESTS++))
((TOTAL_TESTS++))
echo -e "${RED}✗ FAIL${NC} - $1" | tee -a "$LOG_FILE"
}
print_info() {
echo -e "${CYAN}ℹ INFO${NC} - $1"
}
print_warning() {
echo -e "${YELLOW}⚠ WARNING${NC} - $1"
}
print_response() {
echo -e "${PURPLE}📄 Response:${NC} $1"
}
# Fonction pour effectuer une requête HTTP avec token client
http_test_client() {
local method=$1
local endpoint=$2
local data=$3
local expected_code=$4
local description=$5
local extra_headers=$6
print_test "$description"
if [ -z "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Authorization: Bearer $CLIENT_TOKEN" \
-H "Content-Type: application/json" \
$extra_headers \
"${API_BASE_URL}${endpoint}" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Authorization: Bearer $CLIENT_TOKEN" \
-H "Content-Type: application/json" \
$extra_headers \
-d "$data" \
"${API_BASE_URL}${endpoint}" 2>&1)
fi
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq "$expected_code" ]; then
print_success "$description (HTTP $http_code)"
else
print_fail "$description - Expected: $expected_code, Got: $http_code"
print_response "$body"
echo "$description - Expected: $expected_code, Got: $http_code" >> "$LOG_FILE"
echo "Response: $body" >> "$LOG_FILE"
fi
sleep 0.5
}
# Fonction pour effectuer une requête HTTP avec token admin
http_test_admin() {
local method=$1
local endpoint=$2
local data=$3
local expected_code=$4
local description=$5
local extra_headers=$6
print_test "$description"
if [ -z "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
$extra_headers \
"${API_BASE_URL}${endpoint}" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
$extra_headers \
-d "$data" \
"${API_BASE_URL}${endpoint}" 2>&1)
fi
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq "$expected_code" ]; then
print_success "$description (HTTP $http_code)"
echo "$body"
else
print_fail "$description - Expected: $expected_code, Got: $http_code"
print_response "$body"
echo "$description - Expected: $expected_code, Got: $http_code" >> "$LOG_FILE"
echo "Response: $body" >> "$LOG_FILE"
fi
sleep 0.5
}
# Fonction pour effectuer une requête HTTP sans authentification
http_test_no_auth() {
local method=$1
local endpoint=$2
local data=$3
local expected_code=$4
local description=$5
print_test "$description"
if [ -z "$data" ]; then
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Content-Type: application/json" \
"${API_BASE_URL}${endpoint}" 2>&1)
else
response=$(curl -s -w "\n%{http_code}" -X "$method" \
-H "Content-Type: application/json" \
-d "$data" \
"${API_BASE_URL}${endpoint}" 2>&1)
fi
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq "$expected_code" ]; then
print_success "$description (HTTP $http_code)"
else
print_fail "$description - Expected: $expected_code, Got: $http_code"
print_response "$body"
echo "$description - Expected: $expected_code, Got: $http_code" >> "$LOG_FILE"
echo "Response: $body" >> "$LOG_FILE"
fi
sleep 0.5
}
# =============================================================================
# Authentification
# =============================================================================
authenticate() {
print_header "AUTHENTIFICATION"
# ==================== CLIENT LOGIN ====================
print_section "1. Login Client"
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Content-Type: application/json" \
-d "{\"username\":\"$CLIENT_USERNAME\",\"password\":\"$CLIENT_PASSWORD\"}" \
"${API_BASE_URL}/api/v1/auth/login")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
CLIENT_TOKEN=$(echo "$body" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
if [ -n "$CLIENT_TOKEN" ]; then
print_success "Login Client réussi - Token obtenu"
print_info "Token Client: ${CLIENT_TOKEN:0:50}..."
else
print_fail "Login Client réussi mais token non trouvé"
print_response "$body"
exit 1
fi
else
print_fail "Échec du login Client (HTTP $http_code)"
print_response "$body"
exit 1
fi
# ==================== ADMIN LOGIN ====================
print_section "2. Login Admin"
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Content-Type: application/json" \
-d "{\"username\":\"$ADMIN_USERNAME\",\"password\":\"$ADMIN_PASSWORD\"}" \
"${API_BASE_URL}/api/v2/admin/auth/login")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
ADMIN_TOKEN=$(echo "$body" | grep -o '"access_token":"[^"]*' | cut -d'"' -f4)
if [ -n "$ADMIN_TOKEN" ]; then
print_success "Login Admin réussi - Token obtenu"
print_info "Token Admin: ${ADMIN_TOKEN:0:50}..."
else
print_fail "Login Admin réussi mais token non trouvé"
print_response "$body"
exit 1
fi
else
print_fail "Échec du login Admin (HTTP $http_code)"
print_response "$body"
exit 1
fi
}
# =============================================================================
# Tests SQL Injection
# =============================================================================
test_sql_injection() {
print_header "TESTS SQL INJECTION"
print_section "1. SQL Injection - Login"
# Test 1: SQL Injection classique dans login client
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' OR '\''1'\''='\''1","password":"test"}' \
403 "SQLi - Login Client OR 1=1"
# Test 2: SQL Injection dans login admin
http_test_no_auth "POST" "/api/v2/admin/auth/login" \
'{"username":"admin'\'' OR '\''1'\''='\''1","password":"test"}' \
403 "SQLi - Login Admin OR 1=1"
# Test 3: SQL Injection avec UNION
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' UNION SELECT * FROM users--","password":"test"}' \
403 "SQLi - UNION SELECT"
# Test 4: SQL Injection avec DROP TABLE
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\''; DROP TABLE users;--","password":"test"}' \
403 "SQLi - DROP TABLE"
# Test 5: SQL Injection avec commentaire
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\''--","password":"test"}' \
403 "SQLi - Commentaire SQL --"
print_section "2. SQL Injection - Panier"
# Test 6: SQL Injection dans name_product
http_test_client "POST" "/api/v1/panier/add" \
'{"name_product":"Pizza'\'' OR 1=1--","category":"pizza","quantity":1}' \
403 "SQLi - Panier name_product"
# Test 7: SQL Injection dans category
http_test_client "POST" "/api/v1/panier/add" \
'{"name_product":"Pizza","category":"pizza'\'' OR '\''1'\''='\''1","quantity":1}' \
403 "SQLi - Panier category"
print_section "3. SQL Injection - Admin"
# Test 8: SQL Injection dans username pénalité
http_test_admin "POST" "/api/v2/admin/protected/penalty" \
"{\"username\":\"admin' OR '1'='1\",\"amount\":50.0,\"reason\":\"Test\"}" \
403 "SQLi - Username pénalité"
# Test 9: SQL Injection dans paramètres commandes
http_test_admin "GET" "/api/v2/admin/protected/orders?status=pending' OR '1'='1" \
"" \
403 "SQLi - Paramètres commandes"
# Test 10: SQL Injection dans ID commande
http_test_admin "POST" "/api/v2/admin/protected/orders/1' OR '1'='1/auto-assign" \
"" \
403 "SQLi - ID commande"
# Test 11: SQL Injection dans username livreur
http_test_admin "GET" "/api/v2/admin/protected/delivery-persons/john' OR '1'='1/location" \
"" \
403 "SQLi - Username livreur"
print_section "4. SQL Injection - Commandes Client"
# Test 12: SQL Injection dans adresse checkout
http_test_client "POST" "/api/v1/checkout" \
'{"delivery_address":"1'\'' OR '\''1'\''='\''1"}' \
403 "SQLi - Adresse checkout"
# Test 13: SQL Injection nom produit admin
http_test_admin "POST" "/api/v2/admin/protected/products" \
'{"nom":"Pizza'\'' OR '\''1'\''='\''1","category":"pizza","stock":10,"prix":12.99}' \
403 "SQLi - Nom produit admin"
print_section "5. SQL Injection - Variantes avancées"
# Test 14: SQL Injection avec AND
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' AND '\''1'\''='\''1","password":"test"}' \
403 "SQLi - AND condition"
# Test 15: SQL Injection avec encodage hex
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' OR 0x31=0x31--","password":"test"}' \
403 "SQLi - Encodage hex"
# Test 16: SQL Injection avec SLEEP (Time-based)
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' AND SLEEP(5)--","password":"test"}' \
403 "SQLi - Time-based SLEEP"
# Test 17: SQL Injection avec BENCHMARK
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' AND BENCHMARK(10000000,SHA1('\''test'\''))--","password":"test"}' \
403 "SQLi - BENCHMARK"
# Test 18: SQL Injection avec sous-requête
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"admin'\'' AND (SELECT COUNT(*) FROM users)>0--","password":"test"}' \
403 "SQLi - Sous-requête"
}
# =============================================================================
# Tests XSS (Cross-Site Scripting)
# =============================================================================
test_xss() {
print_header "TESTS XSS (CROSS-SITE SCRIPTING)"
print_section "1. XSS - Login"
# Test 1: XSS basique avec script tag
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"","password":"test"}' \
403 "XSS - Script tag basique"
# Test 2: XSS avec event handler
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"
","password":"test"}' \
403 "XSS - Event handler onerror"
# Test 3: XSS avec SVG
http_test_no_auth "POST" "/api/v1/auth/login" \
'{"username":"