Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,10 +23,7 @@ except ImportError:
|
|
| 23 |
# ==========================================
|
| 24 |
# ๐ ๏ธ DYNAMIC PATHS FOR CLOUD DEPLOYMENT
|
| 25 |
# ==========================================
|
| 26 |
-
# Automatically finds the directory where app.py is located
|
| 27 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 28 |
-
|
| 29 |
-
# Assumes your Web files and Models are structured relative to app.py
|
| 30 |
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')
|
| 31 |
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')
|
| 32 |
|
|
@@ -45,10 +42,32 @@ def score_password(password: str):
|
|
| 45 |
if not password:
|
| 46 |
return 0, 'Too Short'
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
order, text = 2, '<^^' + password + '<'
|
| 53 |
log_prob, count, vocab = 0.0, 0, 150
|
| 54 |
|
|
@@ -66,10 +85,10 @@ def score_password(password: str):
|
|
| 66 |
|
| 67 |
avg_ll = log_prob / count
|
| 68 |
score = (avg_ll - (-3.5)) / ((-8.0) - (-3.5)) * 100
|
| 69 |
-
score = max(0, min(score, 100))
|
| 70 |
labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')]
|
| 71 |
label = next((l for s, l in labels if score < s), 'Very Strong')
|
| 72 |
-
return score, label
|
| 73 |
|
| 74 |
def load_model_file(filename):
|
| 75 |
path = os.path.join(MODEL_PATH, filename)
|
|
@@ -78,16 +97,16 @@ def load_model_file(filename):
|
|
| 78 |
return None
|
| 79 |
try:
|
| 80 |
return joblib.load(path)
|
| 81 |
-
except:
|
|
|
|
| 82 |
try:
|
| 83 |
with open(path, "rb") as f:
|
| 84 |
return pickle.load(f)
|
| 85 |
-
except Exception as
|
| 86 |
-
print(f"[ERROR]
|
| 87 |
return None
|
| 88 |
|
| 89 |
# --- MODELS LOADING ---
|
| 90 |
-
# ๐ FIXED: Pointed to the clean .joblib file to bypass Hugging Face security blocking
|
| 91 |
password_model = load_model_file("markov_password_model.joblib")
|
| 92 |
markov_model = password_model
|
| 93 |
phishing_svm_model = load_model_file("phishing_svm_model.pkl")
|
|
@@ -116,16 +135,13 @@ def similarity(a, b):
|
|
| 116 |
def detect_typo(domain):
|
| 117 |
best_match = None
|
| 118 |
best_score = 0
|
| 119 |
-
|
| 120 |
for legit in TOP_TARGET_DOMAINS:
|
| 121 |
sim = similarity(domain, legit)
|
| 122 |
if sim > best_score:
|
| 123 |
best_score = sim
|
| 124 |
best_match = legit
|
| 125 |
-
|
| 126 |
return best_match, best_score
|
| 127 |
|
| 128 |
-
# --- Feature Extraction ---
|
| 129 |
def extract_url_features(url: str):
|
| 130 |
parsed = urlparse(url if url.startswith("http") else "http://" + url)
|
| 131 |
domain = parsed.netloc.lower().replace("www.", "")
|
|
@@ -135,7 +151,7 @@ def extract_url_features(url: str):
|
|
| 135 |
return feats
|
| 136 |
|
| 137 |
# ==========================================
|
| 138 |
-
# ๐ ๏ธ ROBUST FEATURE EXTRACTION
|
| 139 |
# ==========================================
|
| 140 |
def extract_real_features(url):
|
| 141 |
try:
|
|
@@ -149,14 +165,12 @@ def extract_real_features(url):
|
|
| 149 |
num_special = len(re.findall(r"[!@#$%^&*(),?\":{}|<>]", url))
|
| 150 |
url_depth = parsed.path.count("/")
|
| 151 |
|
| 152 |
-
# DNS Check
|
| 153 |
try:
|
| 154 |
socket.gethostbyname(domain)
|
| 155 |
dns_exists = 1
|
| 156 |
except:
|
| 157 |
-
return None
|
| 158 |
|
| 159 |
-
# Safe WHOIS check
|
| 160 |
age_days = 0
|
| 161 |
if whois:
|
| 162 |
try:
|
|
@@ -169,7 +183,6 @@ def extract_real_features(url):
|
|
| 169 |
except:
|
| 170 |
age_days = 0
|
| 171 |
|
| 172 |
-
# Live Web Scraping Checklist
|
| 173 |
try:
|
| 174 |
response = requests.get(url, timeout=4)
|
| 175 |
html = response.text
|
|
@@ -217,31 +230,25 @@ def check_blacklist(url):
|
|
| 217 |
return 0
|
| 218 |
|
| 219 |
# ==========================================
|
| 220 |
-
# ๐ฃ๏ธ ROUTES
|
| 221 |
# ==========================================
|
| 222 |
@app.route('/')
|
| 223 |
-
def index():
|
| 224 |
-
return render_template('Index.html')
|
| 225 |
|
| 226 |
@app.route('/url-checker')
|
| 227 |
-
def url_page():
|
| 228 |
-
return render_template('url-checker.html')
|
| 229 |
|
| 230 |
@app.route('/phishing')
|
| 231 |
-
def phishing_page():
|
| 232 |
-
return render_template('phishing.html')
|
| 233 |
|
| 234 |
@app.route('/password-checker')
|
| 235 |
-
def password_checker_page():
|
| 236 |
-
return render_template('password-checker.html')
|
| 237 |
|
| 238 |
@app.route('/hash')
|
| 239 |
-
def hash_page():
|
| 240 |
-
return render_template('hash.html')
|
| 241 |
|
| 242 |
@app.route('/analysis')
|
| 243 |
-
def analysis_page():
|
| 244 |
-
return render_template('Analysis.html')
|
| 245 |
|
| 246 |
# ==========================================
|
| 247 |
# ๐ API ENDPOINTS
|
|
@@ -249,7 +256,7 @@ def analysis_page():
|
|
| 249 |
@app.route('/check_url', methods=['POST'])
|
| 250 |
def check_url():
|
| 251 |
if not url_legitimacy_model:
|
| 252 |
-
return jsonify({"error": "URL legitimacy model missing
|
| 253 |
|
| 254 |
url = request.get_json().get("url", "")
|
| 255 |
domain = get_domain(url)
|
|
@@ -279,7 +286,7 @@ def check_url():
|
|
| 279 |
@app.route('/check_phishing', methods=['POST'])
|
| 280 |
def check_phishing():
|
| 281 |
if not phishing_svm_model:
|
| 282 |
-
return jsonify({"error": "Phishing SVM model missing
|
| 283 |
|
| 284 |
url = request.get_json().get("url", "")
|
| 285 |
model = phishing_svm_model['pipeline']
|
|
@@ -312,9 +319,6 @@ def route_score():
|
|
| 312 |
s, lbl = score_password(data.get('password', ''))
|
| 313 |
return jsonify({'score': s, 'label': lbl})
|
| 314 |
|
| 315 |
-
# ==========================================
|
| 316 |
-
# ๐ ENVIRONMENT ENFORCED PORT BINDING
|
| 317 |
-
# ==========================================
|
| 318 |
if __name__ == '__main__':
|
| 319 |
port = int(os.environ.get("PORT", 5000))
|
| 320 |
app.run(host='0.0.0.0', port=port)
|
|
|
|
| 23 |
# ==========================================
|
| 24 |
# ๐ ๏ธ DYNAMIC PATHS FOR CLOUD DEPLOYMENT
|
| 25 |
# ==========================================
|
|
|
|
| 26 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
|
|
|
|
|
| 27 |
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')
|
| 28 |
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')
|
| 29 |
|
|
|
|
| 42 |
if not password:
|
| 43 |
return 0, 'Too Short'
|
| 44 |
|
| 45 |
+
# ๐ ๏ธ HIGH-FIDELITY HYBRID FALLBACK ALGORITHM
|
| 46 |
+
# If file loader fails due to structural environment guards, calculate real complexity dynamically
|
| 47 |
+
if not markov_model or not hasattr(markov_model, 'transitions') or len(markov_model.transitions) == 0:
|
| 48 |
+
length = len(password)
|
| 49 |
+
pool = 0
|
| 50 |
+
if re.search(r'[a-z]', password): pool += 26
|
| 51 |
+
if re.search(r'[A-Z]', password): pool += 26
|
| 52 |
+
if re.search(r'[0-9]', password): pool += 10
|
| 53 |
+
if re.search(r'[^a-zA-Z0-9]', password): pool += 32
|
| 54 |
+
|
| 55 |
+
if pool == 0: pool = 1
|
| 56 |
+
entropy = length * math.log2(pool)
|
| 57 |
+
|
| 58 |
+
score = (entropy / 80) * 100
|
| 59 |
+
score = max(5, min(score, 100))
|
| 60 |
+
|
| 61 |
+
if length < 6:
|
| 62 |
+
score = min(score, 25)
|
| 63 |
+
elif length < 8:
|
| 64 |
+
score = min(score, 45)
|
| 65 |
+
|
| 66 |
+
labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')]
|
| 67 |
+
label = next((l for s, l in labels if score < s), 'Very Strong')
|
| 68 |
+
return round(score, 2), label
|
| 69 |
+
|
| 70 |
+
# Standard Markov Calculation if loaded successfully
|
| 71 |
order, text = 2, '<^^' + password + '<'
|
| 72 |
log_prob, count, vocab = 0.0, 0, 150
|
| 73 |
|
|
|
|
| 85 |
|
| 86 |
avg_ll = log_prob / count
|
| 87 |
score = (avg_ll - (-3.5)) / ((-8.0) - (-3.5)) * 100
|
| 88 |
+
score = max(0, min(score, 100))
|
| 89 |
labels = [(20, 'Very Weak'), (40, 'Weak'), (60, 'Moderate'), (80, 'Strong')]
|
| 90 |
label = next((l for s, l in labels if score < s), 'Very Strong')
|
| 91 |
+
return round(score, 2), label
|
| 92 |
|
| 93 |
def load_model_file(filename):
|
| 94 |
path = os.path.join(MODEL_PATH, filename)
|
|
|
|
| 97 |
return None
|
| 98 |
try:
|
| 99 |
return joblib.load(path)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"[ERROR/JOBLIB] Direct load failed: {e}")
|
| 102 |
try:
|
| 103 |
with open(path, "rb") as f:
|
| 104 |
return pickle.load(f)
|
| 105 |
+
except Exception as e2:
|
| 106 |
+
print(f"[ERROR/PICKLE] Fallback read failed: {e2}")
|
| 107 |
return None
|
| 108 |
|
| 109 |
# --- MODELS LOADING ---
|
|
|
|
| 110 |
password_model = load_model_file("markov_password_model.joblib")
|
| 111 |
markov_model = password_model
|
| 112 |
phishing_svm_model = load_model_file("phishing_svm_model.pkl")
|
|
|
|
| 135 |
def detect_typo(domain):
|
| 136 |
best_match = None
|
| 137 |
best_score = 0
|
|
|
|
| 138 |
for legit in TOP_TARGET_DOMAINS:
|
| 139 |
sim = similarity(domain, legit)
|
| 140 |
if sim > best_score:
|
| 141 |
best_score = sim
|
| 142 |
best_match = legit
|
|
|
|
| 143 |
return best_match, best_score
|
| 144 |
|
|
|
|
| 145 |
def extract_url_features(url: str):
|
| 146 |
parsed = urlparse(url if url.startswith("http") else "http://" + url)
|
| 147 |
domain = parsed.netloc.lower().replace("www.", "")
|
|
|
|
| 151 |
return feats
|
| 152 |
|
| 153 |
# ==========================================
|
| 154 |
+
# ๐ ๏ธ ROBUST FEATURE EXTRACTION
|
| 155 |
# ==========================================
|
| 156 |
def extract_real_features(url):
|
| 157 |
try:
|
|
|
|
| 165 |
num_special = len(re.findall(r"[!@#$%^&*(),?\":{}|<>]", url))
|
| 166 |
url_depth = parsed.path.count("/")
|
| 167 |
|
|
|
|
| 168 |
try:
|
| 169 |
socket.gethostbyname(domain)
|
| 170 |
dns_exists = 1
|
| 171 |
except:
|
| 172 |
+
return None
|
| 173 |
|
|
|
|
| 174 |
age_days = 0
|
| 175 |
if whois:
|
| 176 |
try:
|
|
|
|
| 183 |
except:
|
| 184 |
age_days = 0
|
| 185 |
|
|
|
|
| 186 |
try:
|
| 187 |
response = requests.get(url, timeout=4)
|
| 188 |
html = response.text
|
|
|
|
| 230 |
return 0
|
| 231 |
|
| 232 |
# ==========================================
|
| 233 |
+
# ๐ฃ๏ธ ROUTES
|
| 234 |
# ==========================================
|
| 235 |
@app.route('/')
|
| 236 |
+
def index(): return render_template('Index.html')
|
|
|
|
| 237 |
|
| 238 |
@app.route('/url-checker')
|
| 239 |
+
def url_page(): return render_template('url-checker.html')
|
|
|
|
| 240 |
|
| 241 |
@app.route('/phishing')
|
| 242 |
+
def phishing_page(): return render_template('phishing.html')
|
|
|
|
| 243 |
|
| 244 |
@app.route('/password-checker')
|
| 245 |
+
def password_checker_page(): return render_template('password-checker.html')
|
|
|
|
| 246 |
|
| 247 |
@app.route('/hash')
|
| 248 |
+
def hash_page(): return render_template('hash.html')
|
|
|
|
| 249 |
|
| 250 |
@app.route('/analysis')
|
| 251 |
+
def analysis_page(): return render_template('Analysis.html')
|
|
|
|
| 252 |
|
| 253 |
# ==========================================
|
| 254 |
# ๐ API ENDPOINTS
|
|
|
|
| 256 |
@app.route('/check_url', methods=['POST'])
|
| 257 |
def check_url():
|
| 258 |
if not url_legitimacy_model:
|
| 259 |
+
return jsonify({"error": "URL legitimacy model missing"}), 500
|
| 260 |
|
| 261 |
url = request.get_json().get("url", "")
|
| 262 |
domain = get_domain(url)
|
|
|
|
| 286 |
@app.route('/check_phishing', methods=['POST'])
|
| 287 |
def check_phishing():
|
| 288 |
if not phishing_svm_model:
|
| 289 |
+
return jsonify({"error": "Phishing SVM model missing"}), 500
|
| 290 |
|
| 291 |
url = request.get_json().get("url", "")
|
| 292 |
model = phishing_svm_model['pipeline']
|
|
|
|
| 319 |
s, lbl = score_password(data.get('password', ''))
|
| 320 |
return jsonify({'score': s, 'label': lbl})
|
| 321 |
|
|
|
|
|
|
|
|
|
|
| 322 |
if __name__ == '__main__':
|
| 323 |
port = int(os.environ.get("PORT", 5000))
|
| 324 |
app.run(host='0.0.0.0', port=port)
|