""" MalwareGuard — Independent 20k Benign URL Validation Suite. Loads the retrained URL HyperModel ensemble and runs a massive diagnostic test against the 20,000 completely unseen, nested benign URLs to verify dynamic robustness. """ import os import sys import time import pandas as pd # Configure python path to access backend services workspace_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) backend_path = os.path.join(workspace_root, "backend") sys.path.append(backend_path) try: from app.services.url_service import URLModelService print("[OK] Successfully imported backend URLModelService.") except ImportError as e: print(f"[ERROR] Failed to import backend services: {e}") sys.exit(1) BENIGN_CSV = os.path.join(workspace_root, "data", "benign_20k_urls.csv") def main(): print("\n" + "="*70) print(" MASSIVE 20,000 UNSEEN BENIGN URLS DYNAMIC VALIDATION") print("="*70) if not os.path.exists(BENIGN_CSV): print(f"[ERROR] Benign dataset not found at {BENIGN_CSV}. Run generate_benign_validation.py first.") sys.exit(1) print(f"[INFO] Loading 20,000 benign URLs from {BENIGN_CSV}...") df = pd.read_csv(BENIGN_CSV) urls = df["url"].tolist() print(f"[INFO] Loaded {len(urls):,} URLs.") # Initialize URL model service pointing to local urlmodel_info models_dir = os.path.join(workspace_root, "urlmodel_info") print(f"[INFO] Initializing URL HyperModel from {models_dir}...") try: service = URLModelService(models_dir=models_dir) except Exception as e: print(f"[ERROR] Failed to initialize URL Model Service: {e}") sys.exit(1) print("\n[INFO] Running predictions... (This will take a few moments for 20k samples)") start_time = time.time() benign_count = 0 malicious_count = 0 # We will track samples that trigger false positives to print them as examples false_positives = [] # Process in chunks with status prints total = len(urls) for idx, url in enumerate(urls): if idx > 0 and idx % 2000 == 0: print(f" Processed {idx:,} / {total:,} URLs...") res = service.predict(url) verdict = res["prediction"] score = res["malicious_score"] if verdict == "Benign": benign_count += 1 else: malicious_count += 1 if len(false_positives) < 15: false_positives.append((url, score)) elapsed = time.time() - start_time benign_acc = (benign_count / total) * 100 fpr = (malicious_count / total) * 100 avg_time = (elapsed / total) * 1000 print("\n" + "="*60) print(" MASSIVE DYNAMIC VALIDATION RESULTS SUMMARY") print("="*60) print(f" - Total Evaluated URLs : {total:,}") print(f" - Classified as Benign : {benign_count:,}") print(f" - Classified as Malicious: {malicious_count:,} (False Positives)") print(f" - Benign Accuracy : {benign_acc:.3f}% (No False Positives)") print(f" - False Positive Rate : {fpr:.3f}%") print(f" - Total Elapsed Time : {elapsed:.2f} seconds") print(f" - Average Inference Time : {avg_time:.2f} ms / URL") print("-"*60) if false_positives: print("\n[INFO] First 15 False Positive Examples (ordered by score):") # Sort false positives by score descending false_positives.sort(key=lambda x: x[1], reverse=True) for url, score in false_positives: print(f" - Score: {score:.5f} | URL: {url}") else: print("\n[SUCCESS] PERFECT 0.0% FALSE POSITIVE RATE ACHIEVED!") print("="*60) # Standard acceptable FPR for security platforms is < 2.0% if fpr <= 2.0: print(" DEDICATED QUALITY ASSURANCE: PASSED!") print(" The model shows exceptional dynamic path generalization.") print("="*60) sys.exit(0) else: print(" DEDICATED QUALITY ASSURANCE: FAILED (FPR > 2.0%).") print(" Further dataset adjustments needed.") print("="*60) sys.exit(1) if __name__ == "__main__": main()