File size: 2,743 Bytes
0baf9d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Location: chimera_engine/evaluate.py
import os
import time
import xgboost as xgb
import numpy as np
from features import extract_url_features

def main():
    model_path = os.path.join("models", "final_model.json")
    
    if not os.path.exists(model_path):
        print("[-] Final model not found! Please run 'python train.py' first.")
        return

    print("[*] Loading high-speed XGBoost engine...")
    bst = xgb.Booster()
    bst.load_model(model_path)
    print("[+] Engine operational. Enter a URL to evaluate.")
    print("-" * 55)

    while True:
        try:
            user_url = input("\nEnter Target URL (or type 'quit' to exit): ").strip()
            
            if user_url.lower() in ['quit', 'exit', 'q']:
                print("[*] Exiting evaluation engine.")
                break
            
            if not user_url:
                continue

            # --- FIX: Parser Architecture Blind Spot Sanitization ---
            # If the user enters a raw domain variant without a scheme (e.g., 'youtube@evil-site.com'),
            # we force a default prefix so the underlying regex and string split parsers don't 
            # mistake the entire token for a local relative file path.
            processed_url = user_url
            if not processed_url.lower().startswith(('http://', 'https://')):
                processed_url = "http://" + processed_url

            # Start Microsecond Timer
            start_time = time.perf_counter()

            # 1. Extract Features using the sanitized structural URL
            features = extract_url_features(processed_url)
            
            # 2. Convert to DMatrix format required by XGBoost
            dmatrix_payload = xgb.DMatrix(np.array([features]))
            
            # 3. Predict Probability
            probability = bst.predict(dmatrix_payload)[0]

            # Stop Timer
            end_time = time.perf_counter()
            latency_ms = (end_time - start_time) * 1000

            # Formatting the Output
            if probability >= 0.50:
                verdict = "🚨 PHISHING DETECTED"
                confidence = probability * 100
            else:
                verdict = "✅ LEGITIMATE SAFE"
                confidence = (1 - probability) * 100

            print(f"Verdict    : {verdict}")
            print(f"Confidence : {confidence:.2f}%")
            print(f"Latency    : {latency_ms:.3f} ms")

        except KeyboardInterrupt:
            print("\n[*] Exiting evaluation engine.")
            break
        except Exception as e:
            print(f"[-] An error occurred during evaluation: {e}")

if __name__ == "__main__":
    main()