File size: 10,706 Bytes
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
feb33bc
 
7fe8d20
feb33bc
 
 
 
 
 
 
 
7fe8d20
 
feb33bc
 
 
7fe8d20
 
feb33bc
 
 
e6b1e68
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
feb33bc
 
7fe8d20
feb33bc
 
7fe8d20
feb33bc
 
7fe8d20
feb33bc
6a908c0
7fe8d20
6a908c0
 
7fe8d20
6a908c0
 
7fe8d20
6a908c0
 
 
 
feb33bc
 
 
7fe8d20
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7fe8d20
feb33bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import os
import re
import math
import pickle
import joblib
from urllib.parse import urlparse
from collections import defaultdict, Counter
from difflib import SequenceMatcher   
# Socket fallback for local DNS checking
import socket
import datetime
import requests
from bs4 import BeautifulSoup
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS

# --- SAFE WHOIS IMPORT ---
try:
    import whois
except ImportError:
    whois = None

# ==========================================
# ๐Ÿ› ๏ธ DYNAMIC PATHS FOR CLOUD DEPLOYMENT
# ==========================================
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'Web') if os.path.exists(os.path.join(BASE_DIR, 'Web')) else os.path.join(BASE_DIR, 'templates')
MODEL_PATH = os.path.join(BASE_DIR, 'Web', 'Models') if os.path.exists(os.path.join(BASE_DIR, 'Web', 'Models')) else os.path.join(BASE_DIR, 'Models')

app = Flask(__name__, template_folder=TEMPLATE_DIR)
CORS(app)

# ==========================================
# ๐Ÿง  MODEL CLASSES & DEFINITIONS
# ==========================================
class MarkovPasswordModel:
    def __init__(self):
        self.n = 3
        self.transitions = defaultdict(Counter)

def score_password(password: str):
    if not password:
        return 0, 'Too Short'

    # ๐Ÿ› ๏ธ HIGH-FIDELITY HYBRID FALLBACK ALGORITHM
    # If file loader fails due to structural environment guards, calculate real complexity dynamically
    if not markov_model or not hasattr(markov_model, 'transitions') or len(markov_model.transitions) == 0:
        length = len(password)
        pool = 0
        if re.search(r'[a-z]', password): pool += 26
        if re.search(r'[A-Z]', password): pool += 26
        if re.search(r'[0-9]', password): pool += 10
        if re.search(r'[^a-zA-Z0-9]', password): pool += 32
        
        if pool == 0: pool = 1
        entropy = length * math.log2(pool)
        
        score = (entropy / 80) * 100
        score = max(5, min(score, 100))
        
        if length < 6:
            score = min(score, 25)
        elif length < 8:
            score = min(score, 45)
            
        labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')]
        label = next((l for s, l in labels if score < s), 'Very Strong')
        return round(score, 2), label

    # Standard Markov Calculation if loaded successfully
    order, text = 2, '<^^' + password + '<'
    log_prob, count, vocab = 0.0, 0, 150

    for i in range(order, len(text)):
        ctx  = text[i - order:i]
        nx   = text[i]
        tot  = sum(markov_model.transitions[ctx].values())
        freq = markov_model.transitions[ctx].get(nx, 0)
        prob = (freq + 1) / (tot + vocab) if tot > 0 else 1 / vocab
        log_prob += math.log2(prob)
        count += 1

    if count == 0:
        return 0, 'Too Short'

    avg_ll = log_prob / count
    score  = (avg_ll - (-3.5)) / ((-8.0) - (-3.5)) * 100
    score = max(0, min(score, 100))  
    labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')]
    label  = next((l for s, l in labels if score < s), 'Very Strong')
    return round(score, 2), label  

def load_model_file(filename):
    path = os.path.join(MODEL_PATH, filename)
    if not os.path.exists(path):
        print(f"[WARNING] Cloud model file not found at: {path}")
        return None
    try:
        return joblib.load(path)
    except Exception as e:
        print(f"[ERROR/JOBLIB] Direct load failed: {e}")
        try:
            with open(path, "rb") as f:
                return pickle.load(f)
        except Exception as e2:
            print(f"[ERROR/PICKLE] Fallback read failed: {e2}")
            return None

# --- MODELS LOADING ---
password_model = load_model_file("markov_password_model.joblib")
markov_model = password_model
phishing_svm_model = load_model_file("phishing_svm_model.pkl")

url_data = load_model_file("phishing_model_url.pkl")
url_legitimacy_model = url_data["model"] if isinstance(url_data, dict) else url_data

print("[SYSTEM] Cloud-ready configuration loaded.")

# ==========================================
# ๐Ÿ” TYPO DETECTION
# ==========================================
TOP_TARGET_DOMAINS = [
    "google.com", "facebook.com", "amazon.com", "paypal.com",
    "microsoft.com", "apple.com", "netflix.com", "instagram.com",
    "twitter.com", "linkedin.com", "github.com", "yahoo.com"
]

def get_domain(url):
    parsed = urlparse(url if url.startswith("http") else "http://" + url)
    return parsed.netloc.lower().replace("www.", "")

def similarity(a, b):
    return SequenceMatcher(None, a, b).ratio()

def detect_typo(domain):
    best_match = None
    best_score = 0
    for legit in TOP_TARGET_DOMAINS:
        sim = similarity(domain, legit)
        if sim > best_score:
            best_score = sim
            best_match = legit
    return best_match, best_score

def extract_url_features(url: str):
    parsed = urlparse(url if url.startswith("http") else "http://" + url)
    domain = parsed.netloc.lower().replace("www.", "")
    feats = [len(url), len(re.findall(r"\d", url)), url.count('.'), domain.count('-'), 
             1 if parsed.scheme == "https" else 0]
    while len(feats) < 18: feats.append(0) 
    return feats

# ==========================================
# ๐Ÿ› ๏ธ ROBUST FEATURE EXTRACTION
# ==========================================
def extract_real_features(url):
    try:
        parsed = urlparse(url if url.startswith("http") else "http://" + url)
        domain = parsed.netloc

        url_length = len(url)
        num_subdomains = domain.count(".")
        has_ip = 1 if re.match(r"\d+\.\d+\.\d+\.\d+", domain) else 0
        has_https = 1 if parsed.scheme == "https" else 0
        num_special = len(re.findall(r"[!@#$%^&*(),?\":{}|<>]", url))
        url_depth = parsed.path.count("/")

        try:
            socket.gethostbyname(domain)
            dns_exists = 1
        except:
            return None  

        age_days = 0
        if whois:
            try:
                domain_info = whois.whois(domain)
                creation_date = domain_info.creation_date
                if isinstance(creation_date, list):
                    creation_date = creation_date[0]
                if creation_date:
                    age_days = (datetime.datetime.now() - creation_date).days
            except:
                age_days = 0 

        try:
            response = requests.get(url, timeout=4)
            html = response.text
            soup = BeautifulSoup(html, "html.parser")
        except:
            return None

        forms = soup.find_all("form")
        iframes = soup.find_all("iframe")
        scripts = soup.find_all("script", src=True)

        has_login_form = 0
        for form in forms:
            if form.find("input", {"type": "password"}):
                has_login_form = 1
                break

        iframe_count = len(iframes)

        external_scripts = 0
        for script in scripts:
            src = script.get("src")
            if src and domain not in src:
                external_scripts += 1

        ssl_valid = 1 if parsed.scheme == "https" else 0

        features = [
            url_length, num_subdomains, has_ip, 0, num_special, has_https, url_depth,
            0, 0, len(domain), age_days, ssl_valid, dns_exists, 1, 1,
            external_scripts, has_login_form, 0, iframe_count, 0, 0
        ]
        return features
    except:
        return None

def check_blacklist(url):
    try:
        test_url = f"https://transparencyreport.google.com/transparencyreport/api/v3/safebrowsing/status?site={url}"
        r = requests.get(test_url, timeout=4)
        if "unsafe" in r.text.lower():
            return 1
        return 0
    except:
        return 0

# ==========================================
# ๐Ÿ›ฃ๏ธ ROUTES
# ==========================================
@app.route('/')
def index(): return render_template('Index.html')

@app.route('/url-checker')
def url_page(): return render_template('url-checker.html')

@app.route('/phishing')
def phishing_page(): return render_template('phishing.html')

@app.route('/password-checker')
def password_checker_page(): return render_template('password-checker.html')

@app.route('/hash')
def hash_page(): return render_template('hash.html')

@app.route('/analysis')
def analysis_page(): return render_template('Analysis.html')

# ==========================================
# ๐Ÿ”Œ API ENDPOINTS
# ==========================================
@app.route('/check_url', methods=['POST'])
def check_url():
    if not url_legitimacy_model:
        return jsonify({"error": "URL legitimacy model missing"}), 500

    url = request.get_json().get("url", "")
    domain = get_domain(url)

    prob = url_legitimacy_model.predict_proba([extract_url_features(url)])[0][1]
    ml_score = (1 - prob) * 100

    matched_domain, sim_score = detect_typo(domain)
    typo_penalty = 0
    is_typo = False

    if sim_score > 0.75 and domain != matched_domain:
        is_typo = True
        typo_penalty = sim_score * 50  

    final_score = min(ml_score + typo_penalty, 100)
    status = "Not Safe" if is_typo or final_score < 75 else "Safe"

    return jsonify({
        "score": round(final_score, 2),
        "status": status,
        "typo_detected": is_typo,
        "matched_domain": matched_domain if is_typo else None,
        "similarity": round(sim_score, 2)
    })

@app.route('/check_phishing', methods=['POST'])
def check_phishing():
    if not phishing_svm_model:
        return jsonify({"error": "Phishing SVM model missing"}), 500

    url = request.get_json().get("url", "")
    model = phishing_svm_model['pipeline']
    features = extract_real_features(url)

    if features is None:
        return jsonify({"score": 0, "status": "Website Not Found"})

    blacklist_flag = check_blacklist(url)
    probs = model.predict_proba([features])[0]
    phishing_prob = probs[1] * 100

    final_score = 100 if blacklist_flag else phishing_prob

    domain = get_domain(url)
    matched_domain, sim_score = detect_typo(domain)
    if sim_score > 0.75 and domain != matched_domain:
        final_score += 30  

    final_score = min(final_score, 100)

    return jsonify({
        "score": round(final_score, 2),
        "status": "Not Safe" if final_score > 50 else "Safe"
    })

@app.route('/check_password', methods=['POST'])
def route_score():
    data = request.get_json()
    s, lbl = score_password(data.get('password', ''))
    return jsonify({'score': s, 'label': lbl})

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 5000))
    app.run(host='0.0.0.0', port=port)