Spaces:
Runtime error
Runtime error
Download app.py from william1324/Eeeeee: direct link, hf CLI and curl.
- Browser
- Download file 6.14 kB
-
https://huggingface.co/spaces/william1324/Eeeeee/resolve/main/app.py
- Command line
-
hf download hf://spaces/william1324/Eeeeee/app.py
-
curl -L -o app.py https://huggingface.co/spaces/william1324/Eeeeee/resolve/main/app.py
6.14 kB
| import os | |
| import itertools | |
| import gradio as gr | |
| from datasets import load_dataset | |
| from pypdf import PdfReader | |
| def check_pdf_password(pdf_path, password): | |
| """檢查單個密碼是否能成功解鎖 PDF""" | |
| try: | |
| reader = PdfReader(pdf_path) | |
| if reader.is_encrypted: | |
| result = reader.decrypt(password) | |
| if result > 0: | |
| # 實際測試存取第一頁,確認不是偽陽性 | |
| _ = len(reader.pages) | |
| return True | |
| else: | |
| return True | |
| except Exception: | |
| pass | |
| return False | |
| def generate_mask_passwords(mask): | |
| """將簡單遮罩語法 (?d) 轉換為密碼生成清單""" | |
| if "?d" not in mask: | |
| return [mask] | |
| parts = mask.split("?d") | |
| num_digits = len(parts) - 1 | |
| prefix = parts[0] | |
| combinations = itertools.product("0123456789", repeat=num_digits) | |
| passwords = [] | |
| for combo in combinations: | |
| passwords.append(prefix + "".join(combo)) | |
| return passwords | |
| def crack_pdf_pure_python(pdf_file, hf_repo, custom_mask): | |
| if pdf_file is None: | |
| yield "❌ 請先上傳 PDF 檔案!" | |
| return | |
| try: | |
| reader = PdfReader(pdf_file.name) | |
| if not reader.is_encrypted: | |
| yield "💡 提示:此 PDF 檔案本來就沒有加密,可以直接打開!" | |
| return | |
| except Exception as e: | |
| yield f"❌ 無法讀取 PDF 檔案結構: {str(e)}" | |
| return | |
| yield "📋 PDF 結構解析成功(檔案處於加密受保護狀態)...\n" | |
| # ========================================== | |
| # 方案 B: 自訂遮罩暴力破譯 | |
| # ========================================== | |
| if custom_mask: | |
| yield f"🔄 正在將遮罩 '{custom_mask}' 展開為密碼組合..." | |
| passwords = generate_mask_passwords(custom_mask) | |
| total = len(passwords) | |
| yield f"🚀 開始執行遮罩破譯(共計 {total} 組嘗試)...\n" | |
| for idx, pwd in enumerate(passwords): | |
| if idx % max(1, total // 10) == 0: | |
| yield f"⏳ 已嘗試 {idx}/{total} 組密碼組合..." | |
| if check_pdf_password(pdf_file.name, pwd): | |
| yield f"🎉 🎉 🎉 破解成功! 🎉 🎉 🎉\n\n【解鎖密碼結果】:{pwd}" | |
| return | |
| yield f"❌ 破譯結束。在自訂遮罩 [{custom_mask}] 產生的 {total} 組組合中未找到正確密碼。" | |
| return | |
| # ========================================== | |
| # 方案 A: 字典碰撞模式 | |
| # ========================================== | |
| else: | |
| yield f"🔄 正在從 Hugging Face 線上加載密碼資料集: {hf_repo} ..." | |
| try: | |
| dataset = load_dataset(hf_repo, split="train", streaming=True, trust_remote_code=True) | |
| yield "📊 資料集連線建立成功!開始進行即時流式字典碰撞...\n" | |
| count = 0 | |
| for item in dataset: | |
| count += 1 | |
| pwd = None | |
| for key in ['password', 'text', 'word', 'pass']: | |
| if key in item and item[key]: | |
| pwd = str(item[key]).strip() | |
| break | |
| if not pwd: | |
| continue | |
| if count % 500 == 0: | |
| yield f"⏳ 已即時碰撞 {count} 個字典密碼..." | |
| if check_pdf_password(pdf_file.name, pwd): | |
| yield f"🎉 🎉 🎉 破解成功! 🎉 🎉 🎉\n\n【解鎖密碼結果】:{pwd}" | |
| return | |
| yield f"❌ 破譯結束。已完整比對該資料集的 {count} 個密碼,未能成功解鎖。" | |
| except Exception as e: | |
| yield f"❌ 字典加載或比對失敗: {str(e)}\n\n💡 建議:請切換至【方案 B:自訂遮罩暴力破譯】輸入您的密碼特徵(例如:123?d?d?d)進行快速解鎖。" | |
| # ========================================== | |
| # Gradio 6.0+ 響應式網頁介面 | |
| # ========================================== | |
| # 🔧 修正點 1:將 theme 參數從 Blocks 中移除,只保留 title | |
| with gr.Blocks(title="雲端 PDF 線上解鎖器") as demo: | |
| gr.Markdown("# 🛠️ Hugging Face 雲端 PDF 線上解鎖器") | |
| gr.Markdown("採用全 Python 內建安全架喚,完美適配 Gradio 6.x 雲端容器環境。") | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| pdf_input = gr.File(label="1. 上傳受保護的 PDF 檔案 (.pdf)", file_types=[".pdf"]) | |
| with gr.Tab("方案 A:HF 字典碰撞"): | |
| hf_repo_input = gr.Textbox( | |
| value="Canstralian/Wordlists", | |
| label="Hugging Face 密碼字典庫" | |
| ) | |
| with gr.Tab("方案 B:自訂遮罩暴力破譯"): | |
| mask_input = gr.Textbox( | |
| label="密碼特徵遮罩", | |
| placeholder="例如: 123?d?d?d" | |
| ) | |
| gr.Markdown("<small>目前支援語法:`?d` 代表 0-9 的任意數字。</small>") | |
| btn = gr.Button("🚀 開始雲端解鎖", variant="primary", size="large") | |
| with gr.Column(scale=1): | |
| output_box = gr.Textbox( | |
| label="🖥️ 雲端算力即時解鎖進度與結果", | |
| lines=18, | |
| placeholder="等待任務啟動..." | |
| ) | |
| btn.click( | |
| fn=crack_pdf_pure_python, | |
| inputs=[pdf_input, hf_repo_input, mask_input], | |
| outputs=output_box | |
| ) | |
| # 🔧 修正點 2:依照新版規範,將 theme 移至 launch 中,並設定穩定的網路監聽 | |
| # ========================================== | |
| # 將 app.py 最底部的 demo.launch 區塊修改為: | |
| # ========================================== | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, # 🔧 關鍵修正:從 7860 改為 7861,避免與 Node 代理伺服器衝突 | |
| theme="soft" | |
| ) | |