Skydata001 commited on
Commit
cb9516b
·
verified ·
1 Parent(s): 5dd7666

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -54
app.py CHANGED
@@ -1,95 +1,84 @@
1
  # app.py
2
 
3
  from flask import Flask, render_template, request, jsonify
4
- import re
 
5
  import os
6
 
7
  app = Flask(__name__)
8
 
9
  # ----------------------------------------------------------------------
10
- # تحميل قائمة كلمات المرور المسربة (من ملف leaked_passwords.txt)
11
  # ----------------------------------------------------------------------
12
 
13
- # استخدام مجموعة (Set) لسرعة البحث العالية جداً في قائمة ضخمة
14
  LEAKED_PASSWORDS = set()
15
 
16
  def load_leaked_passwords(file_path='leaked_passwords.txt'):
17
  """تقوم بتحميل كلمات المرور المسربة من ملف نصي كبير."""
18
 
19
- # ⚠️ إذا لم يتم العثور على الملف، نستخدم قائمة صغيرة للاختبار
20
  if not os.path.exists(file_path):
21
  print(f"⚠️ الملف {file_path} غير موجود. سيتم استخدام قائمة صغيرة للاختبار.")
22
- # هذه القائمة تحاكي الكلمات الأكثر شيوعاً
23
- return {"123456", "password", "كلمةسر", "محمد", "qwerty", "admin", "تيكتوك", "مرحبا123"}
24
 
25
  try:
26
- # قراءة الملف وإضافة الكلمات بعد تحويلها لأحرف صغيرة
27
  with open(file_path, 'r', encoding='utf-8') as f:
28
  for line in f:
29
  LEAKED_PASSWORDS.add(line.strip().lower())
30
  print(f"✅ تم تحميل {len(LEAKED_PASSWORDS)} كلمة مرور مسربة بنجاح.")
31
  except Exception as e:
32
- print(f"❌ حدث خطأ أثناء تحميل الملف: {e}")
33
- # في حالة فشل القراءة، نرجع قائمة صغيرة لعدم توقيف التطبيق
34
  return {"123456", "password"}
35
 
36
- # تنفيذ دالة التحميل عند بدء التطبيق
37
- # سيتم تخزين جميع الكلمات في متغير LEAKED_PASSWORDS
38
  LEAKED_PASSWORDS = load_leaked_passwords()
39
 
40
 
41
  # ----------------------------------------------------------------------
42
- # 1. منطق تحليل قوة كلمة المرور والتحقق من التسريب
43
  # ----------------------------------------------------------------------
44
 
45
  def check_password_strength(password):
46
- """تحليل قوة كلمة المرور وحساب النسبة المئوية."""
47
- score = 0
48
- feedback = []
49
- MAX_SCORE = 6 # (طول + صغير + كبير + رقم + رمز/عربي + نقطة إضافية للطول الجيد)
50
 
51
- # أ. قواعد قوة كلمة المرور
52
- if len(password) >= 12:
53
- score += 3
54
- feedback.append("✔️ طول ممتاز (12 حرفًا أو أكثر).")
55
- elif len(password) >= 8:
56
- score += 2
57
- feedback.append("⭐ طول جيد (8-11 حرفًا).")
58
- else:
59
- feedback.append("❌ كلمة المرور قصيرة جداً (يجب أن تكون 8 أحرف على الأقل).")
60
-
61
- # تنوع الأحرف (نقاط إضافية)
62
- if re.search(r"[a-z]", password): score += 1
63
- if re.search(r"[A-Z]", password): score += 1
64
- if re.search(r"\d", password): score += 1
65
- if re.search(r"[^a-zA-Z0-9\s]", password): score += 1
66
-
67
- # ب. التحقق من التسريب
68
  is_leaked = password.lower() in LEAKED_PASSWORDS
69
 
70
- # ------------------
71
- # ج. تحديد الحالة وحساب النسبة المئوية
72
- # ------------------
73
-
74
  if is_leaked:
75
  status = "ضعيفة جداً"
76
  color = "red"
77
  percentage = 0
78
- feedback.append("🚨 **خطر!** تم العثور عليها ضمن كلمات المرور المسربة أو الشائعة جداً.")
79
- else:
80
- # حساب النسبة المئوية
81
- percentage = round((score / MAX_SCORE) * 100)
82
- if percentage > 100: percentage = 100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
- if score >= 5:
85
- status = "آمنة"
86
- color = "green"
87
- elif score >= 3:
88
- status = "جيدة"
89
- color = "orange"
90
- else:
91
- status = "ضعيفة"
92
- color = "red"
 
93
 
94
  return status, color, feedback, percentage
95
 
@@ -97,10 +86,10 @@ def check_password_strength(password):
97
  # 2. مسارات تطبيق Flask
98
  # ----------------------------------------------------------------------
99
 
100
- # ⚠️ تم تعديل هذا المسار لاستخدام ملف HTML الجديد
101
  @app.route('/')
102
  def index():
103
- # عند الوصول للرابط الرئيسي، يتم عرض أداة فحص كلمة المرور مباشرة
 
104
  return render_template('password-strength.html')
105
 
106
  @app.route('/check', methods=['POST'])
@@ -118,8 +107,9 @@ def check():
118
  })
119
 
120
  # ----------------------------------------------------------------------
121
- # 3. تشغيل التطبيق (على المنفذ 7860 للتوافق مع Hugging Face)
122
  # ----------------------------------------------------------------------
123
 
124
  if __name__ == '__main__':
 
125
  app.run(host='0.0.0.0', port=7860, debug=True)
 
1
  # app.py
2
 
3
  from flask import Flask, render_template, request, jsonify
4
+ # استيراد مكتبة الفحص الذكي الجديدة
5
+ from zxcvbn import zxcvbn
6
  import os
7
 
8
  app = Flask(__name__)
9
 
10
  # ----------------------------------------------------------------------
11
+ # تحميل قائمة كلمات المرور المسربة
12
  # ----------------------------------------------------------------------
13
 
 
14
  LEAKED_PASSWORDS = set()
15
 
16
  def load_leaked_passwords(file_path='leaked_passwords.txt'):
17
  """تقوم بتحميل كلمات المرور المسربة من ملف نصي كبير."""
18
 
19
+ # ⚠️ في حال عدم وجود الملف، يتم استخدام قائمة صغيرة للاختبار
20
  if not os.path.exists(file_path):
21
  print(f"⚠️ الملف {file_path} غير موجود. سيتم استخدام قائمة صغيرة للاختبار.")
22
+ # قائمة تحاكي الكلمات الأكثر شيوعاً
23
+ return {"123456", "password", "كلمةسر", "محمد", "qwerty", "admin", "تيكتوك", "مرحبا123", "iloveyou"}
24
 
25
  try:
26
+ # قراءة الملف بالكامل وتحويل الكلمات إلى أحرف صغيرة
27
  with open(file_path, 'r', encoding='utf-8') as f:
28
  for line in f:
29
  LEAKED_PASSWORDS.add(line.strip().lower())
30
  print(f"✅ تم تحميل {len(LEAKED_PASSWORDS)} كلمة مرور مسربة بنجاح.")
31
  except Exception as e:
32
+ print(f"❌ حدث خطأ أثناء تحميل الملف: {e}. يتم استخدام قائمة الاختبار.")
 
33
  return {"123456", "password"}
34
 
 
 
35
  LEAKED_PASSWORDS = load_leaked_passwords()
36
 
37
 
38
  # ----------------------------------------------------------------------
39
+ # 1. منطق تحليل قوة كلمة المرور الذكي (باستخدام zxcvbn)
40
  # ----------------------------------------------------------------------
41
 
42
  def check_password_strength(password):
43
+ """تحليل القوة الذكي والتحقق من التسريب."""
 
 
 
44
 
45
+ # 1. التحقق من التسريب (الأولوية القصوى)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  is_leaked = password.lower() in LEAKED_PASSWORDS
47
 
 
 
 
 
48
  if is_leaked:
49
  status = "ضعيفة جداً"
50
  color = "red"
51
  percentage = 0
52
+ feedback = ["🚨 **خطر!** تم العثور عليها ضمن كلمات المرور المسربة أو الشائعة جداً."]
53
+ return status, color, feedback, percentage
54
+
55
+ # 2. تحليل القوة ��استخدام zxcvbn (النتيجة من 0 إلى 4)
56
+ results = zxcvbn(password)
57
+ zxcvbn_score = results['score']
58
+
59
+ # 3. تحويل النقاط إلى نسبة مئوية وحالة
60
+
61
+ percentage = int((zxcvbn_score / 4) * 100)
62
+
63
+ feedback = []
64
+
65
+ # إضافة تحذيرات ونصائح النموذج إلى قائمة النصائح
66
+ if results['feedback']['warning']:
67
+ feedback.append(f"💡 تحذير: {results['feedback']['warning']}")
68
+
69
+ if results['feedback']['suggestions']:
70
+ feedback.extend([f"🌟 نصيحة: {s}" for s in results['feedback']['suggestions']])
71
 
72
+ # تحديد الحالة بناءً على النتيجة
73
+ if zxcvbn_score >= 3:
74
+ status = "آمنة"
75
+ color = "green"
76
+ elif zxcvbn_score == 2:
77
+ status = "جيدة"
78
+ color = "orange"
79
+ else: # Score 0 or 1
80
+ status = "ضعيفة"
81
+ color = "red"
82
 
83
  return status, color, feedback, percentage
84
 
 
86
  # 2. مسارات تطبيق Flask
87
  # ----------------------------------------------------------------------
88
 
 
89
  @app.route('/')
90
  def index():
91
+ """مسار عرض الواجهة الأمامية (HTML)."""
92
+ # يجب أن يكون الملف 'password-strength.html' داخل مجلد 'templates'
93
  return render_template('password-strength.html')
94
 
95
  @app.route('/check', methods=['POST'])
 
107
  })
108
 
109
  # ----------------------------------------------------------------------
110
+ # 3. تشغيل التطبيق
111
  # ----------------------------------------------------------------------
112
 
113
  if __name__ == '__main__':
114
+ # ضروري للتشغيل على Hugging Face
115
  app.run(host='0.0.0.0', port=7860, debug=True)