Hussien Haider commited on
Commit
b98d40f
·
1 Parent(s): 7379909
app/core/auth.py CHANGED
@@ -86,7 +86,7 @@ async def get_current_user(request: Request) -> dict:
86
  if body_bytes:
87
  import json
88
  body = json.loads(body_bytes)
89
- user_id = body.get("user_id", "")
90
  if user_id:
91
  log.info("[auth] Body authenticated, user_id=%s", user_id)
92
  return {"user_id": user_id, "email": "", "role": "authenticated"}
 
86
  if body_bytes:
87
  import json
88
  body = json.loads(body_bytes)
89
+ user_id = body.get("user_id", "") or body.get("userId", "")
90
  if user_id:
91
  log.info("[auth] Body authenticated, user_id=%s", user_id)
92
  return {"user_id": user_id, "email": "", "role": "authenticated"}
app/generators/code_fixing.py CHANGED
@@ -779,6 +779,9 @@ UNIQUENESS RULES — ZERO TOLERANCE FOR DUPLICATES:
779
  - The vulnerable_code MUST use a different programming pattern, different data structures, and different business logic each time.
780
  - The title and story MUST describe a completely different scenario — not just a paraphrase.
781
 
 
 
 
782
  Return ONLY this exact JSON structure (no other text):
783
  {{"title": "عنوان التحدي بالعربية", "story": "قصة التحدي بالعربية", "task_outline": "المطلوب من المتدرب بالعربية", "vulnerable_code": "الكود الكامل المصاب بالثغرة", "vulnerability_description": "شرح الثغرة بالعربية", "difficulty": "متوسط", "hints": [{{"level": 1, "text": "تلميح أول", "xp_cost": 20}}, {{"level": 2, "text": "تلميح ثاني", "xp_cost": 30}}, {{"level": 3, "text": "تلميح ثالث", "xp_cost": 50}}]}}
784
  """
@@ -994,12 +997,12 @@ async def _call_ai(prompt: str, system: str = "", model: str = "") -> Optional[s
994
  return None
995
 
996
 
997
- async def ai_generate_challenge(language: str, vuln_type: str) -> Optional[dict]:
998
  """Use AI to generate a code-fixing challenge.
999
  Strategy: try each provider until one produces a fully valid challenge.
1000
  If parse fails on all, retry once with a stricter prompt via all providers.
1001
  """
1002
- system = SYSTEM_PROMPT.format(language=language, vuln_type=vuln_type)
1003
  base_prompt = f"ولّد كود {language} يحتوي على ثغرة {vuln_type} وفق الهيكل المطلوب."
1004
 
1005
  # Round 1: normal prompt
@@ -1184,6 +1187,11 @@ async def refill_pool(team_role: str, count: int) -> int:
1184
  random.shuffle(seeds)
1185
  inserted = 0
1186
  for seed in seeds[:count]:
 
 
 
 
 
1187
  if await _insert_to_db(seed):
1188
  inserted += 1
1189
  return inserted
@@ -1194,10 +1202,18 @@ async def refill_pool(team_role: str, count: int) -> int:
1194
  if target <= 0:
1195
  return 0
1196
 
 
 
 
 
 
 
 
 
1197
  async def _gen_one(i: int) -> bool:
1198
  lang, vuln = _pick_rotation_slot(int(time.time()) + i)
1199
  print(f" [{team_role}] Generating: {lang} / {vuln}")
1200
- challenge = await ai_generate_challenge(lang, vuln)
1201
  if challenge:
1202
  if await _insert_to_db(challenge):
1203
  print(f" [+] Inserted AI: {challenge['title']}")
@@ -1207,6 +1223,11 @@ async def refill_pool(team_role: str, count: int) -> int:
1207
  matching = [s for s in seeds if s["language"] == lang]
1208
  if matching:
1209
  seed = random.choice(matching)
 
 
 
 
 
1210
  if await _insert_to_db(seed):
1211
  print(f" [+] Inserted seed fallback ({lang})")
1212
  return True
 
779
  - The vulnerable_code MUST use a different programming pattern, different data structures, and different business logic each time.
780
  - The title and story MUST describe a completely different scenario — not just a paraphrase.
781
 
782
+ PREVIOUS RECENT TITLES TO AVOID (DO NOT REUSE ANY OF THESE OR SIMILAR CONCEPTS):
783
+ {blacklisted_titles}
784
+
785
  Return ONLY this exact JSON structure (no other text):
786
  {{"title": "عنوان التحدي بالعربية", "story": "قصة التحدي بالعربية", "task_outline": "المطلوب من المتدرب بالعربية", "vulnerable_code": "الكود الكامل المصاب بالثغرة", "vulnerability_description": "شرح الثغرة بالعربية", "difficulty": "متوسط", "hints": [{{"level": 1, "text": "تلميح أول", "xp_cost": 20}}, {{"level": 2, "text": "تلميح ثاني", "xp_cost": 30}}, {{"level": 3, "text": "تلميح ثالث", "xp_cost": 50}}]}}
787
  """
 
997
  return None
998
 
999
 
1000
+ async def ai_generate_challenge(language: str, vuln_type: str, blacklisted_titles: str = "") -> Optional[dict]:
1001
  """Use AI to generate a code-fixing challenge.
1002
  Strategy: try each provider until one produces a fully valid challenge.
1003
  If parse fails on all, retry once with a stricter prompt via all providers.
1004
  """
1005
+ system = SYSTEM_PROMPT.format(language=language, vuln_type=vuln_type, blacklisted_titles=blacklisted_titles)
1006
  base_prompt = f"ولّد كود {language} يحتوي على ثغرة {vuln_type} وفق الهيكل المطلوب."
1007
 
1008
  # Round 1: normal prompt
 
1187
  random.shuffle(seeds)
1188
  inserted = 0
1189
  for seed in seeds[:count]:
1190
+ # Ensure unique title and details to prevent duplication
1191
+ rand_tag = uuid.uuid4().hex[:6]
1192
+ seed = dict(seed)
1193
+ seed["title"] = f"{seed.get('title', 'تحدي برمجي')} - رمز {rand_tag}"
1194
+ seed["story"] = seed.get("story", "") + f" [معرف التدقيق: {rand_tag}]"
1195
  if await _insert_to_db(seed):
1196
  inserted += 1
1197
  return inserted
 
1202
  if target <= 0:
1203
  return 0
1204
 
1205
+ recent_titles = []
1206
+ try:
1207
+ from app.services.dedup import fetch_existing_titles
1208
+ recent_titles = fetch_existing_titles("code_fixing_challenges", team_role, limit=30)
1209
+ except Exception as e:
1210
+ print(f"[code-fixing] Failed to fetch recent titles: {e}")
1211
+ blacklisted_titles_str = "\n".join([f"- {t}" for t in recent_titles]) if recent_titles else "لا يوجد عناوين سابقة حالياً."
1212
+
1213
  async def _gen_one(i: int) -> bool:
1214
  lang, vuln = _pick_rotation_slot(int(time.time()) + i)
1215
  print(f" [{team_role}] Generating: {lang} / {vuln}")
1216
+ challenge = await ai_generate_challenge(lang, vuln, blacklisted_titles=blacklisted_titles_str)
1217
  if challenge:
1218
  if await _insert_to_db(challenge):
1219
  print(f" [+] Inserted AI: {challenge['title']}")
 
1223
  matching = [s for s in seeds if s["language"] == lang]
1224
  if matching:
1225
  seed = random.choice(matching)
1226
+ # Ensure unique title and details to prevent duplication
1227
+ rand_tag = uuid.uuid4().hex[:6]
1228
+ seed = dict(seed)
1229
+ seed["title"] = f"{seed.get('title', 'تحدي برمجي')} - رمز {rand_tag}"
1230
+ seed["story"] = seed.get("story", "") + f" [معرف التدقيق: {rand_tag}]"
1231
  if await _insert_to_db(seed):
1232
  print(f" [+] Inserted seed fallback ({lang})")
1233
  return True
app/generators/crypto.py CHANGED
@@ -64,6 +64,12 @@ except ImportError:
64
  pass
65
 
66
  import httpx
 
 
 
 
 
 
67
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
68
  from cryptography.hazmat.primitives import hashes, serialization, padding as sympad
69
  from cryptography.hazmat.primitives.asymmetric import rsa, padding as apad
@@ -803,6 +809,11 @@ CRYPTO_SCENARIO_PROMPT = """أنت مصمم تحديات أمن سيبراني
803
  - اسم الملف الذي يحتوي المحتوى (مثال: intercept.txt، leak.bin، firmware.bin، contract.txt، payload.json): ضعه في `extra.filename` واستعمل نفس الاسم داخل story و task_outline بالضبط.
804
  - الـ hints: 3 تلميحات سقراطية قصيرة (جملة واحدة لكل تلميح) — توجّه الطالب للتفكير بدون إعطاء الجواب.
805
 
 
 
 
 
 
806
  禁止 التكرار — قواعد صارمة ضد التشابه:
807
  - لا تستخدم نفس السياق (embassy, leak, ransomware, military, hospital) أكثر من مرة.
808
  - غيّر الإعداد الجغرافي والزمني والمهني بالكامل عن أي تحدي سابق.
@@ -842,6 +853,7 @@ async def ai_generate_scenario_via_cloudflare(
842
  team_role: str, module: str, difficulty: str,
843
  api_token: str, account_id: str, model: str = CLOUDFLARE_MODEL,
844
  algorithm: str | None = None,
 
845
  ) -> ScenarioSpec:
846
  """Primary AI: Cloudflare Workers AI (minimax-m3).
847
 
@@ -853,7 +865,7 @@ async def ai_generate_scenario_via_cloudflare(
853
  url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}"
854
  prompt = CRYPTO_SCENARIO_PROMPT.format(
855
  team_role=team_role, module=module, difficulty=difficulty,
856
- algorithm=algorithm or "any",
857
  )
858
  payload = {
859
  "messages": [
@@ -931,7 +943,8 @@ async def ai_generate_scenario_via_cloudflare(
931
 
932
  async def ai_generate_scenario(team_role: str, module: str, difficulty: str,
933
  groq_api_key: str, model: str | None = None,
934
- algorithm: str | None = None) -> ScenarioSpec:
 
935
  """Call Groq to produce a scenario. Validates and returns a ScenarioSpec.
936
 
937
  Uses the official `groq` Python SDK (sync). The SDK call is wrapped in
@@ -948,7 +961,7 @@ async def ai_generate_scenario(team_role: str, module: str, difficulty: str,
948
  from groq import Groq
949
  prompt = CRYPTO_SCENARIO_PROMPT.format(
950
  team_role=team_role, module=module, difficulty=difficulty,
951
- algorithm=algorithm or "any",
952
  )
953
 
954
  def _call_groq() -> str:
@@ -1044,7 +1057,8 @@ class _ResponseTruncated(Exception):
1044
  async def ai_generate_scenario_via_deepseek(team_role: str, module: str, difficulty: str,
1045
  nvidia_api_key: str,
1046
  model: str | None = None,
1047
- algorithm: str | None = None) -> ScenarioSpec:
 
1048
  """Fallback AI: call NVIDIA integrate (DeepSeek v4-pro) when Groq is unreachable.
1049
 
1050
  OpenAI-compatible chat-completions endpoint. Returns a ScenarioSpec the
@@ -1058,7 +1072,7 @@ async def ai_generate_scenario_via_deepseek(team_role: str, module: str, difficu
1058
  model = model or NVIDIA_MODEL
1059
  prompt = CRYPTO_SCENARIO_PROMPT.format(
1060
  team_role=team_role, module=module, difficulty=difficulty,
1061
- algorithm=algorithm or "any",
1062
  )
1063
  body = {
1064
  "model": model,
@@ -1872,7 +1886,8 @@ def get_pool_count(team_role: str, module: Optional[str] = None) -> int:
1872
  async def ai_generate_scenario_via_mistral(team_role: str, module: str, difficulty: str,
1873
  mistral_api_key: str,
1874
  model: str | None = None,
1875
- algorithm: str | None = None) -> ScenarioSpec:
 
1876
  """Primary AI: call Mistral (mistral-large-latest) for scenario generation.
1877
 
1878
  Per the user's request, Mistral is the first provider tried for
@@ -1890,7 +1905,7 @@ async def ai_generate_scenario_via_mistral(team_role: str, module: str, difficul
1890
  model = model or MISTRAL_MODEL
1891
  prompt = CRYPTO_SCENARIO_PROMPT.format(
1892
  team_role=team_role, module=module, difficulty=difficulty,
1893
- algorithm=algorithm or "any",
1894
  )
1895
  body = {
1896
  "model": model,
@@ -2009,6 +2024,13 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2009
  now = asyncio.get_event_loop().time()
2010
  in_backoff = bool(groq_api_key) and now < _AI_BACKOFF_UNTIL.get(team_role, 0.0)
2011
 
 
 
 
 
 
 
 
2012
  for i in range(count):
2013
  # The system pre-selects the algorithm — the AI only writes flavor.
2014
  module, algorithm = _pick_algorithm_for_slot(i)
@@ -2026,6 +2048,7 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2026
  team_role, module, difficulty,
2027
  MISTRAL_API_KEY,
2028
  algorithm=algorithm,
 
2029
  )
2030
  print(f"[crypto_generator] Mistral OK: algo={spec.algorithm} title={spec.title[:50]}")
2031
  except Exception as e:
@@ -2050,6 +2073,7 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2050
  team_role, module, difficulty,
2051
  CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID,
2052
  algorithm=algorithm,
 
2053
  )
2054
  print(f"[crypto_generator] CF OK: algo={spec.algorithm} title={spec.title[:50]}")
2055
  except Exception as e:
@@ -2077,6 +2101,7 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2077
  spec = await ai_generate_scenario(
2078
  team_role, module, difficulty, groq_api_key,
2079
  algorithm=algorithm,
 
2080
  )
2081
  print(f"[crypto_generator] Groq OK: algo={spec.algorithm} title={spec.title[:50]}")
2082
  except Exception as e:
@@ -2106,6 +2131,7 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2106
  spec = await ai_generate_scenario_via_deepseek(
2107
  team_role, module, difficulty, NVIDIA_API_KEY,
2108
  algorithm=algorithm,
 
2109
  )
2110
  print(f"[crypto_generator] DeepSeek OK: algo={spec.algorithm} title={spec.title[:50]}")
2111
  except Exception as e:
@@ -2129,6 +2155,9 @@ async def _refill_pool_unlocked(team_role: str, count: int,
2129
  template = seed_cycle[seed_idx % len(seed_cycle)]
2130
  seed_idx += 1
2131
  randomized = randomize_template(template)
 
 
 
2132
  try:
2133
  spec = ScenarioSpec(team_role=team_role, **_spec_kwargs(randomized))
2134
  except Exception as e:
 
64
  pass
65
 
66
  import httpx
67
+ try:
68
+ from app.services.dedup import fetch_existing_titles
69
+ except ImportError:
70
+ def fetch_existing_titles(table: str, role_filter: Optional[str] = None, limit: int = 50) -> list[str]:
71
+ return []
72
+
73
  from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
74
  from cryptography.hazmat.primitives import hashes, serialization, padding as sympad
75
  from cryptography.hazmat.primitives.asymmetric import rsa, padding as apad
 
809
  - اسم الملف الذي يحتوي المحتوى (مثال: intercept.txt، leak.bin، firmware.bin، contract.txt، payload.json): ضعه في `extra.filename` واستعمل نفس الاسم داخل story و task_outline بالضبط.
810
  - الـ hints: 3 تلميحات سقراطية قصيرة (جملة واحدة لكل تلميح) — توجّه الطالب للتفكير بدون إعطاء الجواب.
811
 
812
+ تنبيه لمنع التكرار (قاعدة صارمة ضد التشابه):
813
+ يجب أن تتجنب تماماً تكرار أي من العناوين أو الأفكار أو السياقات المستعملة مؤخراً.
814
+ العناوين التي تم توليدها مؤخراً ويُمنع استخدامها أو تكرار أي فكرة منها هي:
815
+ {blacklisted_titles}
816
+
817
  禁止 التكرار — قواعد صارمة ضد التشابه:
818
  - لا تستخدم نفس السياق (embassy, leak, ransomware, military, hospital) أكثر من مرة.
819
  - غيّر الإعداد الجغرافي والزمني والمهني بالكامل عن أي تحدي سابق.
 
853
  team_role: str, module: str, difficulty: str,
854
  api_token: str, account_id: str, model: str = CLOUDFLARE_MODEL,
855
  algorithm: str | None = None,
856
+ blacklisted_titles: str = "",
857
  ) -> ScenarioSpec:
858
  """Primary AI: Cloudflare Workers AI (minimax-m3).
859
 
 
865
  url = f"https://api.cloudflare.com/client/v4/accounts/{account_id}/ai/run/{model}"
866
  prompt = CRYPTO_SCENARIO_PROMPT.format(
867
  team_role=team_role, module=module, difficulty=difficulty,
868
+ algorithm=algorithm or "any", blacklisted_titles=blacklisted_titles,
869
  )
870
  payload = {
871
  "messages": [
 
943
 
944
  async def ai_generate_scenario(team_role: str, module: str, difficulty: str,
945
  groq_api_key: str, model: str | None = None,
946
+ algorithm: str | None = None,
947
+ blacklisted_titles: str = "") -> ScenarioSpec:
948
  """Call Groq to produce a scenario. Validates and returns a ScenarioSpec.
949
 
950
  Uses the official `groq` Python SDK (sync). The SDK call is wrapped in
 
961
  from groq import Groq
962
  prompt = CRYPTO_SCENARIO_PROMPT.format(
963
  team_role=team_role, module=module, difficulty=difficulty,
964
+ algorithm=algorithm or "any", blacklisted_titles=blacklisted_titles,
965
  )
966
 
967
  def _call_groq() -> str:
 
1057
  async def ai_generate_scenario_via_deepseek(team_role: str, module: str, difficulty: str,
1058
  nvidia_api_key: str,
1059
  model: str | None = None,
1060
+ algorithm: str | None = None,
1061
+ blacklisted_titles: str = "") -> ScenarioSpec:
1062
  """Fallback AI: call NVIDIA integrate (DeepSeek v4-pro) when Groq is unreachable.
1063
 
1064
  OpenAI-compatible chat-completions endpoint. Returns a ScenarioSpec the
 
1072
  model = model or NVIDIA_MODEL
1073
  prompt = CRYPTO_SCENARIO_PROMPT.format(
1074
  team_role=team_role, module=module, difficulty=difficulty,
1075
+ algorithm=algorithm or "any", blacklisted_titles=blacklisted_titles,
1076
  )
1077
  body = {
1078
  "model": model,
 
1886
  async def ai_generate_scenario_via_mistral(team_role: str, module: str, difficulty: str,
1887
  mistral_api_key: str,
1888
  model: str | None = None,
1889
+ algorithm: str | None = None,
1890
+ blacklisted_titles: str = "") -> ScenarioSpec:
1891
  """Primary AI: call Mistral (mistral-large-latest) for scenario generation.
1892
 
1893
  Per the user's request, Mistral is the first provider tried for
 
1905
  model = model or MISTRAL_MODEL
1906
  prompt = CRYPTO_SCENARIO_PROMPT.format(
1907
  team_role=team_role, module=module, difficulty=difficulty,
1908
+ algorithm=algorithm or "any", blacklisted_titles=blacklisted_titles,
1909
  )
1910
  body = {
1911
  "model": model,
 
2024
  now = asyncio.get_event_loop().time()
2025
  in_backoff = bool(groq_api_key) and now < _AI_BACKOFF_UNTIL.get(team_role, 0.0)
2026
 
2027
+ recent_titles = []
2028
+ try:
2029
+ recent_titles = fetch_existing_titles("encryption_challenges", team_role, limit=30)
2030
+ except Exception as e:
2031
+ print(f"[crypto_generator] Failed to fetch recent titles: {e}")
2032
+ blacklisted_titles_str = "\n".join([f"- {t}" for t in recent_titles]) if recent_titles else "لا يوجد عناوين سابقة حالياً."
2033
+
2034
  for i in range(count):
2035
  # The system pre-selects the algorithm — the AI only writes flavor.
2036
  module, algorithm = _pick_algorithm_for_slot(i)
 
2048
  team_role, module, difficulty,
2049
  MISTRAL_API_KEY,
2050
  algorithm=algorithm,
2051
+ blacklisted_titles=blacklisted_titles_str,
2052
  )
2053
  print(f"[crypto_generator] Mistral OK: algo={spec.algorithm} title={spec.title[:50]}")
2054
  except Exception as e:
 
2073
  team_role, module, difficulty,
2074
  CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID,
2075
  algorithm=algorithm,
2076
+ blacklisted_titles=blacklisted_titles_str,
2077
  )
2078
  print(f"[crypto_generator] CF OK: algo={spec.algorithm} title={spec.title[:50]}")
2079
  except Exception as e:
 
2101
  spec = await ai_generate_scenario(
2102
  team_role, module, difficulty, groq_api_key,
2103
  algorithm=algorithm,
2104
+ blacklisted_titles=blacklisted_titles_str,
2105
  )
2106
  print(f"[crypto_generator] Groq OK: algo={spec.algorithm} title={spec.title[:50]}")
2107
  except Exception as e:
 
2131
  spec = await ai_generate_scenario_via_deepseek(
2132
  team_role, module, difficulty, NVIDIA_API_KEY,
2133
  algorithm=algorithm,
2134
+ blacklisted_titles=blacklisted_titles_str,
2135
  )
2136
  print(f"[crypto_generator] DeepSeek OK: algo={spec.algorithm} title={spec.title[:50]}")
2137
  except Exception as e:
 
2155
  template = seed_cycle[seed_idx % len(seed_cycle)]
2156
  seed_idx += 1
2157
  randomized = randomize_template(template)
2158
+ rand_tag = uuid.uuid4().hex[:6]
2159
+ randomized["title"] = f"{randomized.get('title', 'تحدي تشفير')} - رمز {rand_tag}"
2160
+ randomized["story"] = randomized.get("story", "") + f" [معرف التدقيق: {rand_tag}]"
2161
  try:
2162
  spec = ScenarioSpec(team_role=team_role, **_spec_kwargs(randomized))
2163
  except Exception as e:
app/generators/log_analysis.py CHANGED
@@ -58,6 +58,11 @@ except ImportError:
58
  pass
59
 
60
  import httpx
 
 
 
 
 
61
 
62
 
63
  # --------------------------------------------------------------------------- #
@@ -699,6 +704,9 @@ UNIQUENESS RULES — ZERO TOLERANCE FOR DUPLICATES:
699
  - The log_content MUST contain different benign traffic patterns and different attack payloads each time.
700
  - The title and story MUST describe a completely different investigation scenario.
701
 
 
 
 
702
  Return ONLY this JSON:
703
  {{"title":"...","story":"...","task_outline":"...","log_content":"...","vulnerability_description":"...","expected_attack_type":"...","expected_attacker_ip":"...","expected_timestamp":"...","expected_ioc":"...","difficulty":"متوسط","hints":[{{"level":1,"text":"...","xp_cost":20}},{{"level":2,"text":"...","xp_cost":30}},{{"level":3,"text":"...","xp_cost":50}}]}}
704
  """
@@ -979,9 +987,9 @@ def _validate_and_build(data: dict, log_type: str, attack_type: str) -> Optional
979
  }
980
 
981
 
982
- async def ai_generate_challenge(log_type: str, attack_type: str) -> Optional[dict]:
983
  """Use AI to generate a log-analysis challenge. 3 rounds of fallback."""
984
- system = SYSTEM_PROMPT.format(log_type=log_type, attack_type=attack_type)
985
  base_prompt = f"ولّد سجل {log_type} يحتوي على هجوم {attack_type} وفق الهيكل المطلوب."
986
 
987
  # Round 1
@@ -1216,11 +1224,19 @@ async def refill_pool(team_role: str, count: int) -> int:
1216
  if target <= 0:
1217
  return 0
1218
 
 
 
 
 
 
 
 
 
1219
  async def _gen_one(i: int) -> bool:
1220
  log_type, attack_type = _pick_rotation_slot(base_count + i)
1221
  built = None
1222
  try:
1223
- built = await ai_generate_challenge(log_type, attack_type)
1224
  except Exception as e:
1225
  print(f" [log-analysis] AI exception: {e}")
1226
 
@@ -1233,6 +1249,11 @@ async def refill_pool(team_role: str, count: int) -> int:
1233
  # Fallback to seed
1234
  seed = _seed_for(log_type, attack_type)
1235
  if seed:
 
 
 
 
 
1236
  row = await _seed_to_row(seed)
1237
  if await _insert_to_db(row):
1238
  print(f" [+] Inserted seed: {row['title']}")
 
58
  pass
59
 
60
  import httpx
61
+ try:
62
+ from app.services.dedup import fetch_existing_titles
63
+ except ImportError:
64
+ def fetch_existing_titles(table: str, role_filter: Optional[str] = None, limit: int = 50) -> list[str]:
65
+ return []
66
 
67
 
68
  # --------------------------------------------------------------------------- #
 
704
  - The log_content MUST contain different benign traffic patterns and different attack payloads each time.
705
  - The title and story MUST describe a completely different investigation scenario.
706
 
707
+ PREVIOUS RECENT TITLES TO AVOID (DO NOT REUSE ANY OF THESE OR SIMILAR CONCEPTS):
708
+ {blacklisted_titles}
709
+
710
  Return ONLY this JSON:
711
  {{"title":"...","story":"...","task_outline":"...","log_content":"...","vulnerability_description":"...","expected_attack_type":"...","expected_attacker_ip":"...","expected_timestamp":"...","expected_ioc":"...","difficulty":"متوسط","hints":[{{"level":1,"text":"...","xp_cost":20}},{{"level":2,"text":"...","xp_cost":30}},{{"level":3,"text":"...","xp_cost":50}}]}}
712
  """
 
987
  }
988
 
989
 
990
+ async def ai_generate_challenge(log_type: str, attack_type: str, blacklisted_titles: str = "") -> Optional[dict]:
991
  """Use AI to generate a log-analysis challenge. 3 rounds of fallback."""
992
+ system = SYSTEM_PROMPT.format(log_type=log_type, attack_type=attack_type, blacklisted_titles=blacklisted_titles)
993
  base_prompt = f"ولّد سجل {log_type} يحتوي على هجوم {attack_type} وفق الهيكل المطلوب."
994
 
995
  # Round 1
 
1224
  if target <= 0:
1225
  return 0
1226
 
1227
+ recent_titles = []
1228
+ try:
1229
+ from app.services.dedup import fetch_existing_titles
1230
+ recent_titles = fetch_existing_titles("log_analysis_challenges", team_role, limit=30)
1231
+ except Exception as e:
1232
+ print(f"[log-analysis] Failed to fetch recent titles: {e}")
1233
+ blacklisted_titles_str = "\n".join([f"- {t}" for t in recent_titles]) if recent_titles else "لا يوجد عناوين سابقة حالياً."
1234
+
1235
  async def _gen_one(i: int) -> bool:
1236
  log_type, attack_type = _pick_rotation_slot(base_count + i)
1237
  built = None
1238
  try:
1239
+ built = await ai_generate_challenge(log_type, attack_type, blacklisted_titles=blacklisted_titles_str)
1240
  except Exception as e:
1241
  print(f" [log-analysis] AI exception: {e}")
1242
 
 
1249
  # Fallback to seed
1250
  seed = _seed_for(log_type, attack_type)
1251
  if seed:
1252
+ # Ensure unique title and details to prevent duplication
1253
+ rand_tag = uuid.uuid4().hex[:6]
1254
+ seed = dict(seed)
1255
+ seed["title"] = f"{seed.get('title', 'تحليل السجلات')} - رمز {rand_tag}"
1256
+ seed["story"] = seed.get("story", "") + f" [معرف التدقيق: {rand_tag}]"
1257
  row = await _seed_to_row(seed)
1258
  if await _insert_to_db(row):
1259
  print(f" [+] Inserted seed: {row['title']}")
app/generators/steganography.py CHANGED
@@ -27,6 +27,12 @@ from typing import Optional, Literal
27
 
28
  import httpx
29
 
 
 
 
 
 
 
30
  # Load .env early
31
  try:
32
  from app._env import load_app_env
@@ -95,22 +101,36 @@ def generate_fallback_image_bytes(text: str = "APEX CyberArena") -> bytes:
95
  """Generate a valid dark theme JPEG image using PIL (Pillow) or a tiny binary fallback."""
96
  try:
97
  from PIL import Image, ImageDraw
98
- img = Image.new("RGB", (500, 500), color=(11, 11, 18))
 
 
99
  draw = ImageDraw.Draw(img)
100
- # Grid lines
101
- for i in range(0, 500, 50):
102
- draw.line([(i, 0), (i, 500)], fill=(0, 212, 170, 30), width=1)
103
- draw.line([(0, i), (500, i)], fill=(255, 0, 85, 30), width=1)
104
- draw.ellipse([(150, 150), (350, 350)], outline=(0, 212, 170), width=2)
105
- draw.text((20, 20), text, fill=(255, 255, 255))
106
- draw.text((20, 450), "Apex training artifact", fill=(120, 120, 120))
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
  buf = io.BytesIO()
109
  img.save(buf, format="JPEG")
110
  return buf.getvalue()
111
  except Exception:
112
- # Minimal 1x1 valid JPEG bytes fallback
113
- return (
114
  b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00"
115
  b"\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08"
116
  b"\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e"
@@ -129,6 +149,52 @@ def generate_fallback_image_bytes(text: str = "APEX CyberArena") -> bytes:
129
  b"\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xda\x00\x0c\x03"
130
  b"\x01\x00\x02\x11\x03\x11\x00?\x00\xed\xfc\xff\xd9"
131
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  async def fetch_random_unsplash_image() -> bytes:
134
  """Fetch an image from Unsplash. Falls back to generating one if offline."""
@@ -174,6 +240,38 @@ def hide_flag(image_bytes: bytes, method: str, flag: str) -> bytes:
174
  return hide_flag_zip(image_bytes, flag)
175
  raise ValueError(f"Unknown hiding method: {method}")
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  # --------------------------------------------------------------------------- #
178
  # 2. Challenge Dataclasses
179
  # --------------------------------------------------------------------------- #
@@ -274,30 +372,73 @@ CURATED_SEEDS = [
274
  # 4. Challenge Builder
275
  # --------------------------------------------------------------------------- #
276
 
 
 
 
 
 
 
 
 
 
 
277
  class ChallengeBuilder:
278
  async def build(self, spec: ScenarioSpec, image_bytes: bytes) -> Challenge:
279
  flag_preview = f"CyberArena{{{uuid.uuid4().hex[:16]}}}"
280
  flag_hash = hashlib.sha256(flag_preview.encode()).hexdigest()
281
 
282
- # Filename based on method
283
- filename = "suspect.jpg"
284
- if spec.hiding_method == "ZIP":
285
- filename = "challenge.jpg"
286
- elif spec.hiding_method == "EXIF":
287
- filename = "agent.jpg"
288
-
289
- file_bytes = hide_flag(image_bytes, spec.hiding_method, flag_preview)
 
 
 
 
 
290
  files = {
291
  filename: base64.b64encode(file_bytes).decode("utf-8")
292
  }
293
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
294
  # Command outputs metadata for UI cheat sheets / dashboard previews
295
  command_outputs = {
296
  "ls": {"stdout": filename, "stderr": "", "exit_code": 0},
297
  f"file {filename}": {
298
- "stdout": f"{filename}: JPEG image data, JFIF standard 1.01" + (
299
- " (with appended Zip archive data)" if spec.hiding_method == "ZIP" else ""
300
- ),
301
  "stderr": "",
302
  "exit_code": 0
303
  }
@@ -336,6 +477,11 @@ STEGANOGRAPHY_PROMPT = """أنت كبير مصممي سيناريوهات الأ
336
  مستوى الصعوبة: {difficulty}
337
  الطريقة المحددة للإخفاء: {hiding_method}
338
 
 
 
 
 
 
339
  السيناريو يجب أن يكون بلغة عربية فصحى احترافية ومشوقة.
340
  1. العنوان: عنوان غامض ومبتكر باللغة العربية (مثل: "تسريب الظلال"، "الرسالة المشفرة للعميل"، "الملف الملتحم").
341
  2. القصة: قصة سياقية واقعية (3-4 جمل) عن حادثة أمنية، تسريب، أو عميل يقوم بنقل رسائل سرية مخفية في صورة.
@@ -353,11 +499,11 @@ STEGANOGRAPHY_PROMPT = """أنت كبير مصممي سيناريوهات الأ
353
  ]
354
  }}"""
355
 
356
- async def ai_generate_scenario_via_mistral(team_role: str, difficulty: str, hiding_method: str) -> ScenarioSpec:
357
  if not MISTRAL_API_KEY:
358
  raise RuntimeError("Mistral API Key missing")
359
 
360
- prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method)
361
  body = {
362
  "model": MISTRAL_MODEL,
363
  "messages": [
@@ -385,12 +531,12 @@ async def ai_generate_scenario_via_mistral(team_role: str, difficulty: str, hidi
385
  hints=data.get("hints") or []
386
  )
387
 
388
- async def ai_generate_scenario_via_cloudflare(team_role: str, difficulty: str, hiding_method: str) -> ScenarioSpec:
389
  if not CLOUDFLARE_API_TOKEN or not CLOUDFLARE_ACCOUNT_ID:
390
  raise RuntimeError("Cloudflare settings missing")
391
 
392
  url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/{CLOUDFLARE_MODEL}"
393
- prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method)
394
 
395
  body = {
396
  "messages": [
@@ -417,11 +563,11 @@ async def ai_generate_scenario_via_cloudflare(team_role: str, difficulty: str, h
417
  hints=data.get("hints") or []
418
  )
419
 
420
- async def ai_generate_scenario_via_groq(team_role: str, difficulty: str, hiding_method: str) -> ScenarioSpec:
421
  if not GROQ_API_KEY:
422
  raise RuntimeError("Groq key missing")
423
 
424
- prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method)
425
  body = {
426
  "model": GROQ_MODEL,
427
  "messages": [
@@ -531,6 +677,13 @@ async def refill_pool(team_role: str, count: int = POOL_BATCH) -> int:
531
  methods = ["EOF", "ZIP", "EXIF"]
532
  difficulties = ["مبتدئ", "متوسط", "قوي"]
533
 
 
 
 
 
 
 
 
534
  for i in range(count):
535
  method = methods[i % len(methods)]
536
  difficulty = difficulties[i % len(difficulties)]
@@ -545,7 +698,7 @@ async def refill_pool(team_role: str, count: int = POOL_BATCH) -> int:
545
  ]
546
  for p in providers:
547
  try:
548
- spec = await p(team_role, difficulty, method)
549
  print(f"[steganography] AI {p.__name__} OK: title={spec.title[:40]}")
550
  break
551
  except Exception as e:
@@ -555,12 +708,13 @@ async def refill_pool(team_role: str, count: int = POOL_BATCH) -> int:
555
  if spec is None:
556
  source = "seed"
557
  seed = random.choice([s for s in CURATED_SEEDS if s["hiding_method"] == method] or CURATED_SEEDS)
 
558
  spec = ScenarioSpec(
559
  team_role=team_role,
560
  module="steganography",
561
  difficulty=seed["difficulty"],
562
- title=seed["title"] + f" #{random.randint(10, 99)}",
563
- story=seed["story"],
564
  task_outline=seed["task_outline"],
565
  hiding_method=seed["hiding_method"],
566
  hints=seed["hints"]
 
27
 
28
  import httpx
29
 
30
+ try:
31
+ from app.services.dedup import fetch_existing_titles
32
+ except ImportError:
33
+ def fetch_existing_titles(table: str, role_filter: Optional[str] = None, limit: int = 50) -> list[str]:
34
+ return []
35
+
36
  # Load .env early
37
  try:
38
  from app._env import load_app_env
 
101
  """Generate a valid dark theme JPEG image using PIL (Pillow) or a tiny binary fallback."""
102
  try:
103
  from PIL import Image, ImageDraw
104
+ # Random dark background color to ensure variation
105
+ bg_color = (random.randint(5, 20), random.randint(5, 20), random.randint(15, 30))
106
+ img = Image.new("RGB", (500, 500), color=bg_color)
107
  draw = ImageDraw.Draw(img)
108
+
109
+ # Draw dynamic cyber grid lines
110
+ grid_color = (random.randint(0, 50), random.randint(150, 255), random.randint(150, 255), 40)
111
+ grid_spacing = random.choice([30, 40, 50, 60])
112
+ for i in range(0, 500, grid_spacing):
113
+ draw.line([(i, 0), (i, 500)], fill=grid_color, width=1)
114
+ draw.line([(0, i), (500, i)], fill=grid_color, width=1)
115
+
116
+ # Draw dynamic abstract geometric patterns
117
+ for _ in range(random.randint(3, 8)):
118
+ shape_color = (random.randint(50, 255), random.randint(100, 255), random.randint(100, 255))
119
+ x0 = random.randint(50, 350)
120
+ y0 = random.randint(50, 350)
121
+ x1 = x0 + random.randint(50, 150)
122
+ y1 = y0 + random.randint(50, 150)
123
+ draw.ellipse([x0, y0, x1, y1], outline=shape_color, width=random.choice([1, 2, 3]))
124
+
125
+ draw.text((30, 30), text, fill=(255, 255, 255))
126
+ draw.text((30, 450), f"Security Artifact ID: {uuid.uuid4().hex[:8]}", fill=(120, 120, 120))
127
 
128
  buf = io.BytesIO()
129
  img.save(buf, format="JPEG")
130
  return buf.getvalue()
131
  except Exception:
132
+ # Minimal 1x1 valid JPEG bytes fallback + random string to guarantee different hash
133
+ base_jpeg = (
134
  b"\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x01\x00`\x00`\x00\x00"
135
  b"\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08"
136
  b"\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e"
 
149
  b"\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xda\x00\x0c\x03"
150
  b"\x01\x00\x02\x11\x03\x11\x00?\x00\xed\xfc\xff\xd9"
151
  )
152
+ return base_jpeg + f"\n# ID: {uuid.uuid4().hex}\n".encode("utf-8")
153
+
154
+ def generate_host_file_bytes(filename: str) -> bytes:
155
+ """Generate dynamic dummy bytes for various stego host carriers (.pdf, .zip, .docx, .png)."""
156
+ ext = filename.split(".")[-1].lower()
157
+ unique_id = uuid.uuid4().hex[:10]
158
+ if ext in ("jpg", "jpeg", "jfif"):
159
+ return generate_fallback_image_bytes(f"CyberArena Stock Asset: {unique_id}")
160
+ elif ext == "png":
161
+ # PNG signature + simple dynamic chunk representation
162
+ return b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15c4\x00\x00\x00\nIDATx\x9cc`\x00\x00\x00\x02\x00\x01H\xaf\xa4q\x00\x00\x00\x00IEND\xaeB`\x82" + f"\n# ID: {unique_id}".encode()
163
+ elif ext == "pdf":
164
+ return (
165
+ f"%PDF-1.4\n"
166
+ f"1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj\n"
167
+ f"2 0 obj << /Type /Pages /Kids [3 0 R] /Count 1 >> endobj\n"
168
+ f"3 0 obj << /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Contents 4 0 R >> endobj\n"
169
+ f"4 0 obj << /Length 55 >> stream\n"
170
+ f"BT /F1 24 Tf 100 700 Td (APEX Confidential Artifact ID: {unique_id}) Tj ET\n"
171
+ f"endstream\n"
172
+ f"endobj\n"
173
+ f"xref\n"
174
+ f"0 5\n"
175
+ f"0000000000 65535 f\n"
176
+ f"0000000009 00000 n\n"
177
+ f"0000000058 00000 n\n"
178
+ f"0000000115 00000 n\n"
179
+ f"0000000218 00000 n\n"
180
+ f"trailer << /Size 5 /Root 1 0 R >>\n"
181
+ f"startxref\n"
182
+ f"312\n"
183
+ f"%%EOF\n"
184
+ ).encode("utf-8")
185
+ elif ext == "zip":
186
+ zip_buf = io.BytesIO()
187
+ with zipfile.ZipFile(zip_buf, 'w', zipfile.ZIP_DEFLATED) as zip_file:
188
+ zip_file.writestr("decoy.txt", f"Decoy file ID: {uuid.uuid4().hex}\n")
189
+ return zip_buf.getvalue()
190
+ elif ext in ("docx", "xlsx"):
191
+ zip_buf = io.BytesIO()
192
+ with zipfile.ZipFile(zip_buf, 'w', zipfile.ZIP_DEFLATED) as zip_file:
193
+ zip_file.writestr("[Content_Types].xml", f'<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="xml" ContentType="application/xml"/><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/></Types> <!-- ID: {unique_id} -->')
194
+ return zip_buf.getvalue()
195
+ else:
196
+ # Fallback binary text representation
197
+ return f"APEX Analysis Report {uuid.uuid4().hex}\n==================================\nThis is a secure system log.\n".encode("utf-8")
198
 
199
  async def fetch_random_unsplash_image() -> bytes:
200
  """Fetch an image from Unsplash. Falls back to generating one if offline."""
 
240
  return hide_flag_zip(image_bytes, flag)
241
  raise ValueError(f"Unknown hiding method: {method}")
242
 
243
+ def generate_random_filename(hiding_method: str) -> str:
244
+ """Generates fully randomized, realistic cybersecurity/corporate filenames."""
245
+ prefixes = [
246
+ "suspect", "evidence", "leak", "backup", "invoice", "report", "confidential",
247
+ "intercept", "transfer", "log", "archive", "secret", "dump", "agent_report",
248
+ "network_capture", "shadow_copy", "credentials", "kernel_dump", "key_log",
249
+ "system_config", "traffic_log", "threat_intel", "payload", "exfiltration",
250
+ "covert_data", "unauthorized_access", "encrypted_db", "device_firmware",
251
+ "chat_history", "user_profile", "auth_logs", "database_dump", "cctv_still",
252
+ "blueprint", "memo", "contract", "financials", "audit_trail"
253
+ ]
254
+ nouns = [
255
+ "data", "file", "record", "payload", "evidence", "intel", "capture", "doc",
256
+ "archive", "leak", "image", "packet", "db", "backup", "log", "firmware",
257
+ "receipt", "scan", "capture", "export", "attachment", "snapshot", "dump"
258
+ ]
259
+ if hiding_method == "EXIF":
260
+ ext = random.choice(["jpg", "jpeg", "jfif"])
261
+ else:
262
+ ext = random.choice([
263
+ "jpg", "jpeg", "png", "pdf", "zip", "docx", "xlsx", "txt", "bin", "dat",
264
+ "conf", "db", "tar.gz", "rar", "sys", "bak"
265
+ ])
266
+
267
+ pattern = random.choice([
268
+ f"{random.choice(prefixes)}_{random.choice(nouns)}",
269
+ f"{random.choice(prefixes)}_{random.randint(1000, 9999)}",
270
+ f"{random.choice(prefixes)}",
271
+ f"{random.choice(nouns)}_{random.choice(prefixes)}_{random.randint(10, 99)}"
272
+ ])
273
+ return f"{pattern}.{ext}"
274
+
275
  # --------------------------------------------------------------------------- #
276
  # 2. Challenge Dataclasses
277
  # --------------------------------------------------------------------------- #
 
372
  # 4. Challenge Builder
373
  # --------------------------------------------------------------------------- #
374
 
375
+ def _align_filename_in_story(text: str, filename: str) -> str:
376
+ import re
377
+ # Replace any word resembling a file name with the new filename
378
+ text = re.sub(
379
+ r"[a-zA-Z0-9_\-]+\.(jpg|jpeg|png|pdf|zip|docx|xlsx|txt|bin|dat|conf|db|tar\.gz|rar|sys|bak|jfif)",
380
+ filename,
381
+ text
382
+ )
383
+ return text
384
+
385
  class ChallengeBuilder:
386
  async def build(self, spec: ScenarioSpec, image_bytes: bytes) -> Challenge:
387
  flag_preview = f"CyberArena{{{uuid.uuid4().hex[:16]}}}"
388
  flag_hash = hashlib.sha256(flag_preview.encode()).hexdigest()
389
 
390
+ # Generate fully randomized filename
391
+ filename = generate_random_filename(spec.hiding_method)
392
+ spec.story = _align_filename_in_story(spec.story, filename)
393
+ spec.task_outline = _align_filename_in_story(spec.task_outline, filename)
394
+
395
+ # Decide dynamic host bytes
396
+ ext = filename.split(".")[-1].lower()
397
+ if ext in ("jpg", "jpeg", "png", "jfif") and image_bytes:
398
+ host_bytes = image_bytes
399
+ else:
400
+ host_bytes = generate_host_file_bytes(filename)
401
+
402
+ file_bytes = hide_flag(host_bytes, spec.hiding_method, flag_preview)
403
  files = {
404
  filename: base64.b64encode(file_bytes).decode("utf-8")
405
  }
406
 
407
+ # Generate realistic 'file' command outputs
408
+ file_desc = "JPEG image data, JFIF standard 1.01"
409
+ if ext == "png":
410
+ file_desc = "PNG image data, 1 x 1, 8-bit/color RGBA"
411
+ elif ext == "jfif":
412
+ file_desc = "JPEG image data, JFIF standard 1.01"
413
+ elif ext == "pdf":
414
+ file_desc = "PDF document, version 1.4"
415
+ elif ext == "zip":
416
+ file_desc = "Zip archive data, at least v2.0 to extract"
417
+ elif ext in ("docx", "xlsx"):
418
+ file_desc = "Microsoft Word 2007+ Document (Zip archive)"
419
+ elif ext == "txt":
420
+ file_desc = "ASCII text"
421
+ elif ext in ("bin", "dat", "bak"):
422
+ file_desc = "data"
423
+ elif ext == "conf":
424
+ file_desc = "ASCII text, with CRLF line terminators"
425
+ elif ext == "db":
426
+ file_desc = "SQLite 3.x database"
427
+ elif ext == "tar.gz":
428
+ file_desc = "gzip compressed data, from Unix"
429
+ elif ext == "rar":
430
+ file_desc = "RAR archive data, v5.0"
431
+ elif ext == "sys":
432
+ file_desc = "PE32 system driver (native) Intel 80386"
433
+
434
+ if spec.hiding_method == "ZIP" and ext in ("jpg", "jpeg", "png", "pdf", "jfif"):
435
+ file_desc += " (with appended Zip archive data)"
436
+
437
  # Command outputs metadata for UI cheat sheets / dashboard previews
438
  command_outputs = {
439
  "ls": {"stdout": filename, "stderr": "", "exit_code": 0},
440
  f"file {filename}": {
441
+ "stdout": f"{filename}: {file_desc}",
 
 
442
  "stderr": "",
443
  "exit_code": 0
444
  }
 
477
  مستوى الصعوبة: {difficulty}
478
  الطريقة المحددة للإخفاء: {hiding_method}
479
 
480
+ تنبيه هام جداً لمنع التكرار (قاعدة صارمة ضد التشابه):
481
+ يجب أن تتجنب تماماً تكرار أي من العناوين أو الأفكار أو السياقات المستعملة مؤخراً.
482
+ العناوين التي تم توليدها مؤخراً ويُمنع استخدامها أو تكرار أي فكرة منها هي:
483
+ {blacklisted_titles}
484
+
485
  السيناريو يجب أن يكون بلغة عربية فصحى احترافية ومشوقة.
486
  1. العنوان: عنوان غامض ومبتكر باللغة العربية (مثل: "تسريب الظلال"، "الرسالة المشفرة للعميل"، "الملف الملتحم").
487
  2. القصة: قصة سياقية واقعية (3-4 جمل) عن حادثة أمنية، تسريب، أو عميل يقوم بنقل رسائل سرية مخفية في صورة.
 
499
  ]
500
  }}"""
501
 
502
+ async def ai_generate_scenario_via_mistral(team_role: str, difficulty: str, hiding_method: str, blacklisted_titles: str = "") -> ScenarioSpec:
503
  if not MISTRAL_API_KEY:
504
  raise RuntimeError("Mistral API Key missing")
505
 
506
+ prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method, blacklisted_titles=blacklisted_titles)
507
  body = {
508
  "model": MISTRAL_MODEL,
509
  "messages": [
 
531
  hints=data.get("hints") or []
532
  )
533
 
534
+ async def ai_generate_scenario_via_cloudflare(team_role: str, difficulty: str, hiding_method: str, blacklisted_titles: str = "") -> ScenarioSpec:
535
  if not CLOUDFLARE_API_TOKEN or not CLOUDFLARE_ACCOUNT_ID:
536
  raise RuntimeError("Cloudflare settings missing")
537
 
538
  url = f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/{CLOUDFLARE_MODEL}"
539
+ prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method, blacklisted_titles=blacklisted_titles)
540
 
541
  body = {
542
  "messages": [
 
563
  hints=data.get("hints") or []
564
  )
565
 
566
+ async def ai_generate_scenario_via_groq(team_role: str, difficulty: str, hiding_method: str, blacklisted_titles: str = "") -> ScenarioSpec:
567
  if not GROQ_API_KEY:
568
  raise RuntimeError("Groq key missing")
569
 
570
+ prompt = STEGANOGRAPHY_PROMPT.format(difficulty=difficulty, hiding_method=hiding_method, blacklisted_titles=blacklisted_titles)
571
  body = {
572
  "model": GROQ_MODEL,
573
  "messages": [
 
677
  methods = ["EOF", "ZIP", "EXIF"]
678
  difficulties = ["مبتدئ", "متوسط", "قوي"]
679
 
680
+ recent_titles = []
681
+ try:
682
+ recent_titles = fetch_existing_titles("steganography_challenges", team_role, limit=30)
683
+ except Exception as e:
684
+ print(f"[steganography] Failed to fetch recent titles: {e}")
685
+ blacklisted_titles_str = "\n".join([f"- {t}" for t in recent_titles]) if recent_titles else "لا يوجد عناوين سابقة حالياً."
686
+
687
  for i in range(count):
688
  method = methods[i % len(methods)]
689
  difficulty = difficulties[i % len(difficulties)]
 
698
  ]
699
  for p in providers:
700
  try:
701
+ spec = await p(team_role, difficulty, method, blacklisted_titles=blacklisted_titles_str)
702
  print(f"[steganography] AI {p.__name__} OK: title={spec.title[:40]}")
703
  break
704
  except Exception as e:
 
708
  if spec is None:
709
  source = "seed"
710
  seed = random.choice([s for s in CURATED_SEEDS if s["hiding_method"] == method] or CURATED_SEEDS)
711
+ rand_tag = uuid.uuid4().hex[:6]
712
  spec = ScenarioSpec(
713
  team_role=team_role,
714
  module="steganography",
715
  difficulty=seed["difficulty"],
716
+ title=f"{seed['title']} - رمز {rand_tag}",
717
+ story=seed["story"] + f" [معرف التدقيق: {rand_tag}]",
718
  task_outline=seed["task_outline"],
719
  hiding_method=seed["hiding_method"],
720
  hints=seed["hints"]
app/generators/vulnerability_hunter.py CHANGED
@@ -47,6 +47,7 @@ import random
47
  import re
48
  import sys
49
  import time
 
50
  from typing import Optional
51
 
52
  # Load .env early so the CLI works without manual export
@@ -827,6 +828,8 @@ UNIQUENESS RULES — ZERO TOLERANCE FOR DUPLICATES:
827
  - The vulnerable_code MUST use a different programming pattern, different data structures, and different business logic each time.
828
  - The title and story MUST describe a completely different scenario — not just a paraphrase.
829
  - Vary the code style: different imports, different module structure, different comments.
 
 
830
 
831
  Return ONLY this exact JSON structure (no other text):
832
  {{"title": "عنوان التحدي بالعربية", "story": "قصة التحدي بالعربية", "task_outline": "ادرس الكود التالي وحدد الثغرة الأمنية الوحيدة الموجودة فيه ثم اكتب اسمها بالإنجليزية (مفتاح قصير) في حقل الإجابة.", "vulnerable_code": "الكود الكامل المصاب بالثغرة", "vulnerability_type": "{vuln_type}", "vulnerability_class": "{vuln_class}", "vulnerability_description": "شرح الثغرة بالعربية", "difficulty": "{difficulty}", "hints": [{{"level": 1, "text": "...", "xp_cost": 10}}, {{"level": 2, "text": "...", "xp_cost": 20}}, {{"level": 3, "text": "...", "xp_cost": 40}}]}}
@@ -1017,7 +1020,7 @@ async def _call_ai(prompt: str, system: str = "", model: str = "") -> Optional[s
1017
  return None
1018
 
1019
 
1020
- async def ai_generate_challenge(language: str, vuln_type: str, difficulty: str) -> Optional[dict]:
1021
  vuln_class = VULNERABILITY_CLASS.get(vuln_type, "Security Misconfiguration")
1022
  diff_profile = DIFFICULTY_PROFILE.get(difficulty, DIFFICULTY_PROFILE["متوسط"])
1023
 
@@ -1030,6 +1033,7 @@ async def ai_generate_challenge(language: str, vuln_type: str, difficulty: str)
1030
  difficulty_obviousness=diff_profile["obviousness"],
1031
  code_min=diff_profile["code_lines"][0],
1032
  code_max=diff_profile["code_lines"][1],
 
1033
  )
1034
 
1035
  base_prompt = f"ولّد كود {language} بطول {diff_profile['code_lines'][0]}-{diff_profile['code_lines'][1]} سطر يحتوي على ثغرة {vuln_type} (OWASP: {vuln_class}) وفق الهيكل المطلوب. مستوى الصعوبة: {diff_profile['label']}."
@@ -1275,10 +1279,18 @@ async def refill_pool(team_role: str, count: int) -> int:
1275
  if target <= 0:
1276
  return 0
1277
 
 
 
 
 
 
 
 
 
1278
  async def _gen_one(i: int) -> bool:
1279
  lang, vuln, diff = _pick_rotation_slot(int(time.time()) + i)
1280
  print(f" [{team_role}] Generating: {lang} / {vuln} / {diff}")
1281
- challenge = await ai_generate_challenge(lang, vuln, diff)
1282
  if challenge:
1283
  if await _insert_to_db(challenge):
1284
  print(f" [+] Inserted AI: {challenge['title']}")
@@ -1289,6 +1301,11 @@ async def refill_pool(team_role: str, count: int) -> int:
1289
  if not matching:
1290
  matching = seeds
1291
  seed = random.choice(matching)
 
 
 
 
 
1292
  if await _insert_to_db(seed):
1293
  print(f" [+] Inserted seed fallback ({diff})")
1294
  return True
 
47
  import re
48
  import sys
49
  import time
50
+ import uuid
51
  from typing import Optional
52
 
53
  # Load .env early so the CLI works without manual export
 
828
  - The vulnerable_code MUST use a different programming pattern, different data structures, and different business logic each time.
829
  - The title and story MUST describe a completely different scenario — not just a paraphrase.
830
  - Vary the code style: different imports, different module structure, different comments.
831
+ - DONT GENERATE ANY OF THE FOLLOWING TITLES (THEY ARE ALREADY IN THE DATABASE):
832
+ {blacklisted_titles}
833
 
834
  Return ONLY this exact JSON structure (no other text):
835
  {{"title": "عنوان التحدي بالعربية", "story": "قصة التحدي بالعربية", "task_outline": "ادرس الكود التالي وحدد الثغرة الأمنية الوحيدة الموجودة فيه ثم اكتب اسمها بالإنجليزية (مفتاح قصير) في حقل الإجابة.", "vulnerable_code": "الكود الكامل المصاب بالثغرة", "vulnerability_type": "{vuln_type}", "vulnerability_class": "{vuln_class}", "vulnerability_description": "شرح الثغرة بالعربية", "difficulty": "{difficulty}", "hints": [{{"level": 1, "text": "...", "xp_cost": 10}}, {{"level": 2, "text": "...", "xp_cost": 20}}, {{"level": 3, "text": "...", "xp_cost": 40}}]}}
 
1020
  return None
1021
 
1022
 
1023
+ async def ai_generate_challenge(language: str, vuln_type: str, difficulty: str, blacklisted_titles: str = "") -> Optional[dict]:
1024
  vuln_class = VULNERABILITY_CLASS.get(vuln_type, "Security Misconfiguration")
1025
  diff_profile = DIFFICULTY_PROFILE.get(difficulty, DIFFICULTY_PROFILE["متوسط"])
1026
 
 
1033
  difficulty_obviousness=diff_profile["obviousness"],
1034
  code_min=diff_profile["code_lines"][0],
1035
  code_max=diff_profile["code_lines"][1],
1036
+ blacklisted_titles=blacklisted_titles or "لا يوجد عناوين سابقة حالياً.",
1037
  )
1038
 
1039
  base_prompt = f"ولّد كود {language} بطول {diff_profile['code_lines'][0]}-{diff_profile['code_lines'][1]} سطر يحتوي على ثغرة {vuln_type} (OWASP: {vuln_class}) وفق الهيكل المطلوب. مستوى الصعوبة: {diff_profile['label']}."
 
1279
  if target <= 0:
1280
  return 0
1281
 
1282
+ recent_titles = []
1283
+ try:
1284
+ from app.services.dedup import fetch_existing_titles
1285
+ recent_titles = fetch_existing_titles("vulnerability_hunter_challenges", team_role, limit=30)
1286
+ except Exception as e:
1287
+ print(f"[vuln-hunter] Failed to fetch recent titles: {e}")
1288
+ blacklisted_titles_str = "\n".join([f"- {t}" for t in recent_titles]) if recent_titles else "لا يوجد عناوين سابقة حالياً."
1289
+
1290
  async def _gen_one(i: int) -> bool:
1291
  lang, vuln, diff = _pick_rotation_slot(int(time.time()) + i)
1292
  print(f" [{team_role}] Generating: {lang} / {vuln} / {diff}")
1293
+ challenge = await ai_generate_challenge(lang, vuln, diff, blacklisted_titles=blacklisted_titles_str)
1294
  if challenge:
1295
  if await _insert_to_db(challenge):
1296
  print(f" [+] Inserted AI: {challenge['title']}")
 
1301
  if not matching:
1302
  matching = seeds
1303
  seed = random.choice(matching)
1304
+ # Ensure unique title and details to prevent duplication
1305
+ rand_tag = uuid.uuid4().hex[:6]
1306
+ seed = dict(seed)
1307
+ seed["title"] = f"{seed.get('title', 'تحدي ثغرة')} - رمز {rand_tag}"
1308
+ seed["story"] = seed.get("story", "") + f" [معرف التدقيق: {rand_tag}]"
1309
  if await _insert_to_db(seed):
1310
  print(f" [+] Inserted seed fallback ({diff})")
1311
  return True
app/services/dedup.py CHANGED
@@ -125,6 +125,11 @@ def _fetch_existing_codes(table: str, code_col: str,
125
  except Exception:
126
  return []
127
 
 
 
 
 
 
128
 
129
  # ---------------------------------------------------------------------------
130
  # Public API — one function per generator
 
125
  except Exception:
126
  return []
127
 
128
+ def fetch_existing_titles(table: str, role_filter: Optional[str] = None,
129
+ limit: int = 200) -> List[str]:
130
+ """Fetch recent titles from a challenge table (public wrapper)."""
131
+ return _fetch_existing_titles(table, role_filter, limit)
132
+
133
 
134
  # ---------------------------------------------------------------------------
135
  # Public API — one function per generator