Commit Graph

607 Commits

Author SHA1 Message Date
cschantz 849a112b5c Add Nginx + Varnish Cache Manager with complete cPanel integration
New Features:
- Full Varnish 6.6+ installation and configuration for cPanel servers
- 99.5% stock compliance using settings.json approach (RPM-safe)
- Complete HTTPS caching via SSL termination and config-script automation
- Two-tier revert system (partial/full stack removal)
- Enhanced status display with mode detection and color-coded port status
- Self-healing diagnostics with 8 automatic fixes
- Host header preservation fix for multi-domain WordPress compatibility

Technical Details:
- Supports ea-nginx + Varnish + Apache stack on AlmaLinux 9+
- Caches 93 static file types with smart bypasses for cPanel services
- Config-script ensures HTTPS traffic uses HTTP backend to Varnish
- Adaptive detection handles partial states and manual interventions

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-21 18:53:04 -05:00
cschantz 5b7253c1ff Fix HARDCODED-PATH check for array elements
Skip array element lines that are part of multi-panel path arrays
Checks previous 10 lines for array declaration pattern
2026-01-09 18:12:47 -05:00
cschantz 52770efb1b Fix HARDCODED-PATH false positives
Skip these safe multi-panel patterns:
- Fallback patterns: ${VAR:-/path}
- if/elif path existence checks
- Array definitions with multiple panel paths

These patterns are proper multi-panel implementations.
2026-01-09 18:10:12 -05:00
cschantz b61d16dc7e Fix DEP check false positives for detect_control_panel
detect_control_panel is in system-detect.sh, not domain-discovery.sh
Check now properly validates that system-detect.sh is sourced
2026-01-09 18:09:18 -05:00
cschantz 4ab211fd26 Fix false positives in QA checks
SUBSHELL-VAR (CHECK 69):
- Skip variables only used for writing to files (echo ... >> pattern)
- File writes persist even in subshells, so these are safe

NULL (CHECK 47):
- Skip echo/print_info/print_warning/print_error/printf statements
- These are displaying example commands, not executing them

ESCAPE (CHECK 66):
- Skip filename variables after redirection operators (>, >>, 2>)
- Example: grep ... > "$output_file" is writing TO file, not reading FROM it

These improvements reduce false positive rate significantly.
2026-01-09 18:06:27 -05:00
cschantz dea6f27b4d Fix ESCAPE issues in multiple library files
- lib/domain-discovery.sh: Added -- to grep command (1 fix)
- lib/reference-db.sh: Added -- to grep command (1 fix)
- lib/user-manager.sh: Added -- to grep command (1 fix)
- lib/email-functions.sh: Added -- to awk and grep commands (2 fixes)
- lib/php-config-manager.sh: Added -- to grep commands (3 fixes)
- lib/php-detector.sh: Added -- to grep command (1 fix)
Total: 9 ESCAPE fixes

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:38:55 -05:00
cschantz 9a98f4b251 Fix remaining ESCAPE issues in rate anomaly detector
- Added -- separator to awk commands (3 more fixes at lines 76, 101, 185)
- Total of 6 ESCAPE fixes in this file

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:28:28 -05:00
cschantz 886a1af35e Fix ESCAPE issues in rate anomaly detector
- Added -- separator to awk commands (3 fixes at lines 36-38)
- Prevents filename injection attacks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:26:04 -05:00
cschantz 630cea7cb7 Fix ESCAPE issues in IP reputation and user manager
- Added -- separator to grep/awk commands in lib/ip-reputation.sh (4 fixes)
- Added -- separator to grep commands in lib/user-manager.sh (2 fixes)
- Prevents filename injection attacks

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:23:17 -05:00
cschantz c6d5affbee Fix ESCAPE issues in threat intelligence and reference DB
- Added -- separator to grep commands in lib/threat-intelligence.sh (5 fixes)
- Added -- separator to grep commands in lib/reference-db.sh (3 fixes)
- Prevents filename injection attacks where filenames starting with - could be misinterpreted as command options

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:20:23 -05:00
cschantz b6c0ec0e9b Fix security issues and QA false positives
Security fixes in lib/mysql-analyzer.sh:
- Added -- separator to grep/sed/awk/wc commands to prevent filename injection
- Fixed 10 ESCAPE issues (lines 130, 153, 180, 208, 210, 320, 324, 405, 507, 513)

QA script improvements in tools/toolkit-qa-check.sh:
- Updated ESCAPE check (CHECK 66) to recognize -- as safe pattern
- Updated HARDCODED-PATH check (CHECK 81) to skip control panel abstraction libraries
- Now correctly excludes domain-discovery.sh, plesk-helpers.sh, user-manager.sh from hardcoded path warnings
- Reduced false positives by ~23 issues

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-09 16:17:23 -05:00
cschantz 0c25f15c89 Fix major false positives in QA script (33 HIGH issues eliminated)
Reduced false positives from 104 to 71 HIGH issues by improving detection logic:

1. SOURCE Detection (CHECK 44):
   - Skip lines with error handling (|| or 2>/dev/null)
   - Better extraction: handle quotes, skip special chars
   - Skip empty/variable/absolute paths
   - More precise grep pattern (only ^\s*source lines)
   - Validates existence checks more accurately

2. IFS Detection (CHECK 68):
   - Skip safe pattern: 'IFS= read' (only affects read command)
   - Skip IFS in while/for conditions (locally scoped)
   - Only flag standalone IFS assignments without reset
   - Changed grep to only match ^\s*IFS= (not inline usage)

3. WORDSPLIT Detection (CHECK 51):
   - Downgraded from HIGH to MEDIUM severity
   - Skip intentional patterns: $disks, $ips, $users, $dbs, etc.
   - Skip variables ending in _list, _array, _items
   - Added guidance: suppress if intentional, quote if bug
   - Recognizes common bash idiom for space-separated lists

Results:
- Before: 104 HIGH, 223 MEDIUM, 390 TOTAL
- After:  71 HIGH (-33), 231 MEDIUM (+8), 365 TOTAL (-25)
- Eliminated: 10 IFS false positives, ~15 SOURCE, ~8 WORDSPLIT
- Accuracy improvement: ~32% reduction in false HIGH issues

Impact: QA scan now focuses on real issues, not common bash patterns.
2026-01-09 00:42:03 -05:00
cschantz 8f3b764e26 Fix NULL check issues (5 HIGH issues resolved)
Added proper null/empty checks and variable quoting in 3 files:

1. wordpress-cron-manager.sh (2 issues):
   - Added validation for $site_path before use
   - Quoted variable in cron command to prevent word splitting
   - Lines 446-449: Check if path is empty or invalid before processing

2. malware-scanner.sh (1 issue):
   - Added safety check for $SCAN_DIR before suggesting rm -rf command
   - Prevents dangerous rm operations if variable is empty or root
   - Line 1583-1585: Guard against accidental deletions

3. mysql-restore-to-sql.sh (2 issues):
   - Quoted $datadir in echo statements showing manual commands
   - Lines 426, 441, 444, 447: Proper quoting in examples

Impact: Prevents potential issues from empty/undefined variables
2026-01-09 00:33:02 -05:00
cschantz 2ccbdc530b Add machine-readable summary and actionable recommendations
Major improvements for AI/automated parsing:

1. MACHINE-READABLE SUMMARY:
   SCAN_STATUS=WARNING CRITICAL=0 HIGH=104 MEDIUM=223 LOW=63 TOTAL=390
   - Easily parseable key-value format
   - No need to parse colored ANSI text
   - Perfect for scripts/automation

2. RECOMMENDED ACTIONS (new section):
   [1] Fix tools/toolkit-qa-check.sh - 25 issues (fix DISK-SPACE issues)
   [2] Fix lib/mysql-analyzer.sh - 14 issues (fix ESCAPE issues)
   [3] Add source existence checks across codebase (15 issues in 4 files)
   - Numbered action list (top 5 tasks)
   - Shows what to fix, not just where
   - Identifies dominant issue type per file
   - Includes quick-win patterns

3. HIGH ISSUES - COMPACT FORMAT:
   ● tools/toolkit-qa-check.sh (25 issues: 6× DISK-SPACE, starting at line 481)
   - Shows dominant pattern + count
   - Provides starting line for investigation
   - 80% less verbose than before
   - Still provides all key information

4. PATTERN SUMMARY (simplified):
   SOURCE              15 occurrences
   TEMP                15 occurrences
   - Simple two-column format
   - No redundant descriptions (already in RECOMMENDED ACTIONS)

Benefits:
- Answers "what should I do?" immediately
- Machine-parseable status line
- 60% less output to read
- Every line is actionable
- Perfect for automated workflows
- Clear visual hierarchy with separators

This format is optimized for rapid AI parsing and decision-making.
2026-01-09 00:26:25 -05:00
cschantz 5096b0f4cc Restructure QA output for maximum actionability
Complete rewrite of output format:

1. PRIORITY FILES section:
   - Shows files with CRITICAL/HIGH issues sorted by count
   - Breaks down severity per file: "file.sh (CRITICAL: 2, HIGH: 5)"
   - Calculates coverage: "Fix top 3 files = 50% of issues"
   - Immediately answers: "Which files should I fix first?"

2. HIGH ISSUES grouped BY FILE:
   - Shows first 3 issues per file with line numbers
   - Displays total count: "file.sh (12 issues)"
   - Groups related issues together for batch fixing
   - Much easier to work through file-by-file

3. QUICK WINS section:
   - Shows patterns appearing 10+ times
   - Provides fix description for each pattern
   - Example: "15 × SOURCE - Add existence checks before sourcing"
   - Identifies opportunities to fix many issues at once

4. MEDIUM/LOW collapsed:
   - Single summary line (not pages of low-priority detail)
   - Provides grep command to view when needed

Benefits for AI/human readers:
- Answers "where do I start?" immediately
- Groups issues by file (actionable context)
- Shows impact (% coverage of top files)
- Identifies patterns (fix 15 issues with one approach)
- Reduces noise (no pages of MEDIUM/LOW details)
- Clear hierarchy: PRIORITY → CRITICAL → HIGH → QUICK WINS

Output is now optimized for taking action, not just reporting.
2026-01-08 23:17:19 -05:00
cschantz 97b91ba5f6 Improve QA output format for better readability
Changes to output format:
- Clear PASS/FAIL status at top (✓ PASSED, ⚠ WARNINGS, ✗ FAILED)
- Show ALL critical issues (no truncation)
- HIGH issues: Show top 20 instead of 15
- MEDIUM/LOW: Group by file with counts (not individual issues)
- Compact category breakdown (top 10 only)
- Concise action summary (removed verbose next steps)
- Single-line completion status

Benefits:
- Immediately see pass/fail status
- Critical issues never truncated
- Less noise from minor issues
- File-grouped view shows problem areas
- Faster to scan and understand
- More structured for AI parsing

Output is now optimized for both human and AI readability.
2026-01-08 23:02:51 -05:00
cschantz 021e3229e0 Optimize QA script to eliminate timeout issues
Critical optimizations:
- CHECK 31: Rewrite with AWK for 10-100x speedup (50k+ lines processed)
  * Replaced bash loops with multiple greps per line
  * Single-pass AWK processing with brace tracking
  * Reduced processing time from 120s+ timeout to ~15s

- CHECK 32: Add quick/summary mode skip (LOW severity)
  * Expensive nested loop for menu validation
  * Properly skipped in --quick and --summary modes

Results:
- Full scan: 114s (was timing out at 120s)
- Quick mode: 109s
- All 88 checks now complete successfully

Technical details:
- Old: 81 files × 50,630 lines with 4-5 greps each = 2M+ operations
- New: Single AWK pass = 50k operations (40-100x faster)
2026-01-08 21:40:32 -05:00
cschantz e4611b994f Update README with new security features (v2.2)
Added comprehensive documentation for:
- Auto-Mitigation Engine (Score >= 80/100 blocking)
- Distributed attack detection and blocking (5+ IPs)
- Subnet-level blocking (25+ IPs from same /24)
- IPset kernel-level blocking with batching
- 24 attack signatures with improved accuracy
- Bot classification system
- Multi-source monitoring (HTTP, SSH, Email, FTP, DB, Network)
- No system pollution design (/tmp storage)

Updated version to 2.2.0 with January 2026 highlights.
Enhanced security module documentation in usage examples.
2026-01-08 17:24:19 -05:00
cschantz 9b47187399 Clean up session notes and temporary files
Removed:
- Session planning docs (CODING_GUIDELINES, AUDIT summaries, etc)
- docs/ directory (PHP planning notes, session summaries)
- tmp/bot_analysis_report_*.txt (old analysis files)
- backups/php/test_* (test backup directories)

Kept:
- REFDB_FORMAT.txt (memory/reference file)
- README.md (project documentation)
- config/whitelist-*.txt (functional configs)
- modules/*/README.md (module documentation)

Total cleanup: ~133KB of session artifacts
2026-01-08 17:18:34 -05:00
cschantz 17cde51bcb Export functions for subshell access (CRITICAL FIX)
HTTP monitoring runs in subshells (from tail pipe) but functions
were not exported, making them unavailable in those subshells.

Exported functions:
- write_ip_data_to_file (writes scores to file)
- update_ip_intelligence (updates IP scores)
- get_ip_intelligence (reads IP data)
- get_threat_level (calculates threat level)
- get_threat_color (gets display color)

This fixes the critical bug where HTTP attacks reached Score:100
but were never blocked because scores weren't written to ip_data file.

Without exports: function called in subshell = command not found
With exports: function available in all child processes
2026-01-06 22:11:21 -05:00
cschantz 3a3b8dbda7 Move all persistent data to /tmp (no system pollution)
Moved from /var/lib/server-toolkit/ to /tmp/:
- Threat intelligence cache
- Whitelist IPs
- Attack pattern logs
- Incident reports
- Shared threat coordination logs
- Live monitor snapshots

Philosophy: Deleting toolkit directory should remove ALL data.
System directories (/var/lib/) caused stale data to persist.
Using /tmp/ ensures auto-cleanup on reboot and complete removal.
2026-01-06 22:03:18 -05:00
cschantz 2391ded8e4 Move IP reputation database to /tmp
Changed from /var/lib/server-toolkit/ to /tmp/server-toolkit-reputation/

Reasons:
- No system pollution - deleting toolkit removes all data
- Auto-cleanup on reboot (no stale scores)
- Self-contained design

Old location (/var/lib/) caused stale Score:100 entries to persist
after code fixes were deployed.
2026-01-06 22:02:28 -05:00
cschantz 24363a1713 Add auto-blocking for distributed attacks
When 5+ IPs perform same attack type (RCE, SQL_INJECTION, XSS, PATH_TRAVERSAL, BRUTEFORCE) within 2 minutes:
- Block all individual attacking IPs immediately via IPset
- If 25+ IPs from same /24 subnet, block entire subnet

Uses batch_block_ips() for efficient IPset operations.
All blocking is kernel-level via IPset (no CSF commands).
2026-01-06 21:55:58 -05:00
cschantz 02a42a98cb CRITICAL: Fix massive false positives causing Score:100 on legitimate traffic
Problem:
- Normal URLs like /contactus.aspx reaching Score:100
- Legitimate browser traffic being flagged as attacks
- Auto-blocking legitimate users

Root Cause #1: HTTP_SMUGGLING Detection
- Regex pattern \n matched literal letter 'n' in URLs
- ANY URL with 'n' triggered +22 point penalty
- /index.html, /contactus.aspx, /admin/login all false positives

Root Cause #2: SUSPICIOUS_UA Detection
- Pattern ^mozilla/[45]\.0 matched ALL modern browsers
- Every Chrome/Firefox/Safari user flagged as suspicious
- Added +15 points to every request
- Combined with 'suspicious' bot classification: +30 total

Impact:
Before fix:
  /contactus.aspx with Chrome = 52 points (3 false attack types)
  After 2-3 requests = Score:100 = auto-blocked

After fix:
  /contactus.aspx with Chrome = 0 points (correct)
  /contactus.aspx with curl = 15 points (correct - is suspicious)

Changes:
1. HTTP_SMUGGLING: Only check URL-encoded CRLF (%0d%0a)
   - Removed literal \r\n and \n patterns (match letters!)
   - Real attacks still detected correctly

2. SUSPICIOUS_UA: Only flag incomplete Mozilla UAs
   - Changed ^mozilla/[45]\.0 to ^mozilla/[45]\.0$
   - Now only matches bare 'Mozilla/5.0' without browser info
   - Real browsers with full UA strings are safe

Testing:
✓ /index.html with Chrome: 0 points (was 52)
✓ /contactus.aspx with Chrome: 0 points (was 52)
✓ /path%0d%0aHeader: Still detected (real attack)
✓ curl/wget UAs: Still detected (automation tools)
2026-01-06 18:47:35 -05:00
cschantz 4b6e655123 CRITICAL FIX: Prevent main loop from overwriting subprocess updates
Problem:
- IPs reaching Score:100 but STILL not being auto-blocked
- write_ip_data_to_file was working correctly in subprocesses
- BUT main loop was OVERWRITING entire ip_data file every 2 seconds
- Line 3539 used ">" which truncates the file
- Auto-mitigation engine reads stale data from parent's IP_DATA array
- Parent's IP_DATA doesn't have subprocess updates (subshell isolation)

Example:
1. HTTP subprocess: IP reaches score=100, writes to file
2. 2 seconds later: Main loop OVERWRITES file with parent's IP_DATA
3. Auto-mitigation reads file: Score shows 0 or old value
4. IP never blocked!

Root Cause:
The original fix (write_ip_data_to_file) was correct, but the main
loop's periodic file write was destroying those updates.

Solution:
- Main loop now MERGES data instead of overwriting
- Reads existing file (contains fresh subprocess updates)
- Adds only NEW IPs from parent process
- Writes back existing entries (subprocess data takes priority)
- Uses flock to prevent race conditions
- Atomic replacement with .new file

This preserves subprocess updates while still allowing parent
process to add IPs it discovers.

Result:
- Subprocess updates (Score:100) now PERSIST
- Auto-mitigation engine sees correct scores
- IPs with score >= 80 will be blocked within 10 seconds

Testing:
Before: Score:100 shown but IP never blocked
After:  Score:100 → INSTANT_BLOCK within 10 seconds
2026-01-06 18:25:41 -05:00
cschantz 49b0bf3a90 Improve attack signature scoring for faster blocking
Issues Fixed:
1. SUSPICIOUS_UA under-valued (+10 → +15)
   - Automation tools now block in 6 hits instead of 8
   - Matches severity of SQL injection and path traversal

2. BOT_FINGERPRINT under-valued (+8 → +15)
   - Headless browsers now properly scored as HIGH risk
   - Blocks in 6 hits instead of 10

3. Suspicious bot penalty increased (+10 → +15)
   - Consistent with new SUSPICIOUS_UA scoring
   - Faster blocking of malicious automation

4. Legit bot penalty exploit fixed
   - Score reduction (-5) now ONLY applies if NO attacks detected
   - Prevents spoofed Googlebot/legitimate UAs from avoiding blocks
   - Attack detection overrides bot classification

Impact:
Before:
- SUSPICIOUS_UA: 8 hits to auto-block (score 80)
- BOT_FINGERPRINT: 10 hits to auto-block
- Spoofed Googlebot with attacks: Could avoid blocking

After:
- SUSPICIOUS_UA: 6 hits to auto-block (score 90)
- BOT_FINGERPRINT: 6 hits to auto-block (score 90)
- Spoofed legitimate UAs: No penalty if attacks present
- Faster response to automation attacks

Real-World Example:
IP with python-requests UA making SQL injection attempts:
- Old: +10 (SUSPICIOUS_UA) +10 (suspicious bot) = 20 per hit
- New: +15 (SUSPICIOUS_UA) +15 (suspicious bot) = 30 per hit
- Result: Blocks in 3 hits instead of 4
2026-01-06 17:28:35 -05:00
cschantz 4a9f40ce53 CRITICAL FIX: Resolve subshell data loss preventing auto-blocking
Problem:
- Scores showing 100 in display but IPs NOT being auto-blocked
- HTTP/SSH/network monitoring run in subshells (pipe/background processes)
- IP_DATA array updates in subshells invisible to parent process
- Auto-mitigation engine reading stale ip_data file with score=0
- Result: SUSPICIOUS_UA and other attacks never triggering blocks

Root Cause:
```bash
tail -F logs | while read line; do
    IP_DATA[$ip]=100  # Updates in SUBSHELL - parent never sees it!
done
```

Solution:
1. Added write_ip_data_to_file() with flock-based locking
2. Every IP_DATA update now writes directly to ip_data file
3. Auto-mitigation engine can now see real-time scores
4. Fixed in 8 locations:
   - update_ip_intelligence (main scoring)
   - HTTP log monitoring (ET attacks)
   - AbuseIPDB reputation boost (3 levels)
   - cPHulk monitoring
   - SYN flood detection
   - Port scan detection

Testing:
- SUSPICIOUS_UA reaching score 100 will now auto-block
- All attack types properly trigger mitigation
- File locking prevents race conditions
- Background writes prevent blocking main loop

This fixes the #1 reported issue where attacks showed critical
scores but were never blocked.
2026-01-06 17:27:04 -05:00
cschantz 72047b4098 Fix Maldet directory detection after extraction
Problem:
- cd maldetect-* was failing because glob expansion doesn't work
  reliably in this context
- Error: "Cannot find extracted directory"

Solution:
- Use find command to locate extracted directory explicitly
- Store directory path in variable before cd
- Add diagnostic output showing available directories on failure
- More robust error handling with explicit directory checks
2026-01-02 21:29:37 -05:00
cschantz da041b22b0 Improve Maldet installation error handling and diagnostics
Problem:
- Maldet installation was failing silently on Plesk servers
- No error output to diagnose issues (./install.sh &>/dev/null)
- Users only saw "✗ Maldet installation failed" with no context

Changes:
- Add comprehensive error capture to /tmp/maldet-install-$$.log
- Show last 10 lines of installation output on failure
- Add step-by-step progress indicators (download, extract, install)
- Check each operation and fail fast with clear error messages
- Add Plesk-specific diagnostics:
  • Detect Plesk installation
  • Check cron directory permissions
  • Verify /usr/local/sbin exists
- Preserve full log file for detailed investigation
- Return proper exit codes for error handling

This enables users to diagnose and fix Plesk-specific installation
issues instead of being stuck with a generic failure message.
2026-01-02 20:51:21 -05:00
cschantz 33ade14188 Improve functional test accuracy - reduce false positives
Enhanced function call validation to be much more accurate:

Improvements:
1. Function definitions must have opening brace { to avoid matching
   function names in comments
2. Function calls exclude comment lines (lines starting with #)
3. Better handling of 'function name {' syntax
4. Exclude lines with { from call detection (catches definitions)

Results:
- Before: 14 false positive warnings
- After: 2 false positives (both in echo/documentation strings)
- 85% reduction in false positives

Remaining 2 warnings are in toolkit-qa-check.sh in echo statements
showing users how to use functions - not actual undefined calls.

The test now accurately identifies real function call issues while
minimizing noise from comments and documentation.
2026-01-02 20:44:59 -05:00
cschantz 491d56bd74 Add comprehensive functional testing framework
Created qa-functional-tests.sh to verify scripts actually work,
not just pass static analysis.

5 Types of Functional Tests:

1. Bash Syntax Validation
   - Uses 'bash -n' to check syntax without execution
   - Validates all 81 scripts
   - Result: 100% pass rate

2. Function Call Validation
   - Verifies called functions are defined
   - Checks sourced files for function definitions
   - Detects potential undefined functions

3. Dependency Validation
   - Verifies all sourced files exist
   - Resolves common variable patterns ($SCRIPT_DIR, $LIB_DIR, etc.)
   - Distinguishes between missing files and dynamic paths

4. Library Function Unit Tests
   - Tests core functions with sample data
   - Validates email, IP, and formatting functions
   - Expandable framework for more tests

5. Script Execution Smoke Tests
   - Tries to run scripts with --help
   - Ensures scripts don't crash on startup
   - Validates basic executability

Usage:
  bash tools/qa-functional-tests.sh

Benefits:
- Catches runtime errors static analysis misses
- Verifies dependencies are properly set up
- Tests actual function behavior
- Provides confidence code will run in production

Overall pass rate: 97% (82 passed, 2 failed, 1 skipped)
2026-01-02 20:38:38 -05:00
cschantz bad5955d41 Fix WORDSPLIT issues in for loops (HIGH priority)
Converted unsafe 'for var in $list' loops to 'while read' loops
to properly handle items with spaces in names.

reference-db.sh (4 fixes):
- Line 172: Database iteration (SHOW DATABASES)
- Line 330: Server alias iteration (space-separated aliases)
- Line 345: Domain iteration (get_user_domains)
- Line 414: WordPress config file paths (find results)

user-manager.sh (4 fixes):
- Line 396: Domain iteration in cPanel log paths
- Line 404: Domain iteration in Plesk log paths
- Line 410: Domain iteration in InterWorx log paths
- Line 632: User iteration (list_all_users)

Pattern changes:
- for item in $list → while IFS= read -r item
- Added [ -z "$item" ] && continue for safety
- Used echo "$list" | while or piped commands directly

This prevents word splitting on spaces in database names,
domain names, file paths, and usernames.
2026-01-02 17:34:56 -05:00
cschantz 5a2d51d496 Fix NULL check issues (HIGH priority)
Added validation checks for potentially empty variables before use
to prevent errors and unsafe operations.

WordPress Cron Manager (5 fixes):
- Added site_path validation after dirname operations
- Prevents using empty paths in cd commands and file operations
- Pattern: Check [ -z "$site_path" ] before use

Bot Analyzer:
- Quoted TEMP_DIR in trap command for safety

Hardware Health Check:
- Quoted MESSAGES_CACHE in trap command for safety

Note: 5 issues flagged in toolkit-qa-check.sh were false positives
(echo statements demonstrating bad patterns, not actual code issues)
2026-01-02 17:32:15 -05:00
cschantz 45e115ec4b Fix SOURCE command safety issues (HIGH priority)
Added existence checks and error handling for all source commands
to prevent silent failures when dependencies are missing.

Library files (use 'return' for error):
- reference-db.sh: Added checks for 3 dependencies
- mysql-analyzer.sh: Added checks for 3 dependencies
- domain-discovery.sh: Added checks for 2 dependencies
- system-detect.sh: Added check for common-functions.sh
- plesk-helpers.sh: Added check for common-functions.sh
- user-manager.sh: Added checks for 2 dependencies

Executable scripts (use 'exit' for error):
- wordpress-cron-manager.sh: Added checks for 2 dependencies
- website-error-analyzer.sh: Added checks for 4 dependencies

Pattern: [ -f "file" ] && source "file" || { echo "ERROR" >&2; return/exit 1; }

This ensures scripts fail fast with clear error messages when
required dependencies are missing, rather than continuing with
undefined functions.
2026-01-02 17:26:21 -05:00
cschantz 51b4dbde1e Fix integer comparison safety issues (6 HIGH priority)
Added parameter expansion with defaults to prevent comparison errors
on potentially empty variables:

- live-attack-monitor-v2.sh: IPSET_CREATE_EXIT, IPTABLES_EXIT
- live-attack-monitor.sh: IPSET_CREATE_EXIT, IPTABLES_EXIT
- malware-scanner.sh: START_EXIT
- email-diagnostics.sh: check_type, account_found

Pattern: Changed "$VAR" to "${VAR:-default}" in integer comparisons
to ensure safe comparisons even if variable is unexpectedly empty.
2026-01-02 17:23:02 -05:00
cschantz cd079bd7b6 Fix HIGH priority issues: paths, globs, deps, wordsplit
- Fixed 3 unquoted path expansions in cleanup-toolkit-data.sh
  (lines 175, 192-193: quoted $pattern in ls/rm commands)

- Fixed 3 unquoted globs in erase/malware-scanner scripts
  (erase-toolkit-traces.sh lines 103-104, malware-scanner.sh line 229)

- Added system-detect.sh sourcing to email-functions.sh
  (fixes 5 HIGH priority DEP warnings for detect_control_panel)

- Fixed 2 WORDSPLIT issues in mysql-analyzer.sh
  (lines 137, 362: changed from for loops to while read loops
   to safely handle database/table names with spaces)
2026-01-02 17:21:19 -05:00
cschantz 8f6cb6e91c Fix HIGH priority issues: library exit, unquoted paths, and globs
Fixed multiple HIGH severity issues found by QA scan:

1. Library exit usage (lib/http-attack-analyzer.sh):
   - Changed exit 1 to return 1
   - Libraries should return, not exit (would terminate caller)

2. Unquoted path expansions (9 fixes):
   - cleanup-toolkit-data.sh: Quoted $pattern in ls/rm commands
   - hardware-health-check.sh: Quoted /sys/block/$disk/queue paths
   - plesk-helpers.sh: Quoted /var/qmail/mailnames/$domain path
   - Prevents breakage with paths containing spaces

3. Unquoted globs in rm commands (3 fixes):
   - erase-toolkit-traces.sh: Quoted glob patterns
   - Prevents unintended file deletion from glob expansion

All changes improve robustness and prevent edge case failures.
2026-01-02 16:39:57 -05:00
cschantz a5d61ea7d8 Fix false positives in QA script checks
Refined two checks that were generating false positive warnings:

1. SCRIPT_DIR check (was HIGH, now MEDIUM):
   - Previously flagged ALL 59 files that define SCRIPT_DIR
   - Now only flags library files (which shouldn't define paths)
   - Executable scripts CORRECTLY define their own SCRIPT_DIR
   - Added note explaining this is not a collision

2. USERDATA-ACCESS check (was CRITICAL, now MEDIUM):
   - Reduced severity from CRITICAL to MEDIUM (code quality, not security)
   - Added exclusions for legitimate use cases:
     - QA script itself (searches for this pattern)
     - Diagnostic/analysis tools (malware-scanner, error-analyzer, etc.)
     - These tools need direct access by design
   - Changed message to suggest abstractions rather than demand them

This eliminates 7 false CRITICAL warnings and 1 false HIGH warning,
making the QA report more actionable.
2026-01-02 16:27:47 -05:00
cschantz fdce4ccd07 Remove duplicate show_progress function
QA scan found duplicate show_progress function in analyze-historical-attacks.sh
that's already available in lib/common-functions.sh.

Changes:
- Added source for lib/common-functions.sh
- Removed local show_progress() definition
- Added comment noting function is now sourced

This reduces code duplication and ensures consistent progress display
across all toolkit scripts.
2026-01-02 16:24:56 -05:00
cschantz 682bd69cf8 Add missing function exports to library files
QA scan found 4 library files with functions that weren't exported,
making them unavailable in subshells and nested calls.

Added export statements for:
- lib/attack-signatures.sh: 3 functions
- lib/http-attack-analyzer.sh: 5 functions
- lib/email-functions.sh: 18 functions
- lib/rate-anomaly-detector.sh: 9 functions

Total: 35 functions now properly exported

This ensures functions are available when libraries are sourced by
scripts that spawn subshells or use process substitution.
2026-01-02 16:23:17 -05:00
cschantz 87c69cd59b Fix integer expression errors in QA script category counts
Same newline sanitization issue as email diagnostics - grep -c output
can contain newlines causing "integer expression expected" errors.

Fixed by sanitizing count variables:
- Line 3367: Category count comparison
- Line 2533: Performance cache occurrence count

Added: echo "$count" | head -1 | tr -d '\n\r'

Prevents errors like: [: 0\n0: integer expression expected
2026-01-02 16:19:11 -05:00
cschantz c3868db8e2 Fix bot blocking recommendations to use cPanel mod_rewrite format
Changed User-Agent blocking output from old .htaccess SetEnvIfNoCase
format to modern mod_rewrite format suitable for cPanel global config.

New format:
- File: /etc/apache2/conf.d/includes/pre_main_global.conf
- Uses <IfModule mod_rewrite.c> with RewriteCond/RewriteRule
- Returns 403 Forbidden [F,L] for bad bots
- Case-insensitive matching [NC]
- Properly formatted for cPanel best practices

Also updated SEO bot blocking section to match format.
2026-01-02 15:56:31 -05:00
cschantz 65d26ba95e Massive performance improvement: use awk mktime instead of date command
Previous implementation called external date command for EVERY log entry,
causing 30+ minute hangs on servers with hundreds of thousands of entries.

New implementation:
- Uses awk built-in mktime() function (native, no external process)
- Month lookup table built once in BEGIN block
- Simple string parsing with split()
- Thousands of times faster (no process spawning per entry)

Performance comparison:
- Before: ~1000 entries/second (calling date each time)
- After: ~100,000+ entries/second (native awk)

Should complete in seconds instead of 30+ minutes.
2025-12-31 23:26:24 -05:00
cschantz 1a2f5cb116 Fix bash syntax error caused by apostrophe in awk comment
The comment "it's too old" contained an apostrophe (single quote) which
broke the bash single-quote enclosure of the awk script, causing:
  "syntax error near unexpected token '}'"

Changed to "too old" to avoid the apostrophe.

In bash, single-quoted strings cannot contain single quotes/apostrophes.
2025-12-31 22:24:55 -05:00
cschantz 3730f8bd0c Fix timestamp comparison to use epoch seconds for accurate filtering
Previous commit used string comparison which failed across month/year
boundaries (e.g., "01/Jan/2026" < "31/Dec/2025" due to day comparison).

Now converts timestamps to epoch seconds for proper numerical comparison:
- Cutoff calculated as epoch seconds (date +%s)
- Apache log timestamps converted from "dd/mmm/yyyy:HH:MM:SS" format
- Format conversion: replace slashes and first colon with spaces
- Numerical comparison ensures correct ordering across all boundaries

Tested with dates spanning year/month changes - works correctly.
2025-12-31 22:21:01 -05:00
cschantz de3e95bcb7 Fix bot analyzer to filter log entries by timestamp, not just files
Previously, the script filtered log FILES by modification time but read
ALL entries from those files, causing "Last 1 hour" to show entries from
weeks/months ago if they were in recently-modified files.

Now filters individual log entries by parsing their timestamps and
comparing to the selected time range (1 hour, 6 hours, 24 hours, etc.).

Changes:
- Added cutoff timestamp calculation in awk BEGIN block
- Extract timestamp from each Apache log entry
- Skip entries older than cutoff with timestamp comparison
- Works with both GNU date and BSD date for portability
2025-12-31 22:15:00 -05:00
cschantz f4c921bea0 Reduce false positives in integer comparison check
Improvements:
- Added more common integer variable patterns (crit, high, med, low, severity, line_num, port, pid, uid, gid, attempt, tries)
- Skip variables with default value syntax ${var:-0}
- Reduces false positives for counters, IDs, severity levels, and line numbers

This significantly reduces noise in QA output while maintaining detection
of genuinely unsafe integer comparisons.
2025-12-31 21:57:31 -05:00
cschantz 37fea20ba5 Add progress indicator function to QA script
- Added show_progress() helper function
- Shows real-time progress during scan [X/88] Check name...
- Only displays when running in terminal (not in summary mode)
- First step towards more performance improvements
2025-12-31 21:52:52 -05:00
cschantz b5f11dcfdb Enhance QA script output with colors and better formatting
Improvements to output/reporting:
- Color-coded severity levels (red=CRITICAL, yellow=HIGH, blue=MEDIUM, cyan=LOW)
- Progress indicators during scan
- Relative file paths (easier to read)
- Scan duration timing
- Smart category breakdown (only shows categories with issues, sorted by count)
- Better visual hierarchy with bold headers and separators
- Helpful next steps based on results
- Improved footer with useful command examples
- Zero issues now shows green success message

Terminal output is now much easier to scan and understand at a glance
while maintaining plain text format in the report file.
2025-12-31 21:47:03 -05:00
cschantz dcf2ccd414 Fix integer expression errors in failure categorization
Sanitize all grep counts to remove newlines that cause
'integer expression required' errors
2025-12-31 19:24:00 -05:00