Skydata001 commited on
Commit
2c71d6e
·
verified ·
1 Parent(s): 53f3678

Update app.py

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