Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,33 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import re
|
| 4 |
|
| 5 |
-
# 初始化
|
| 6 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 7 |
|
| 8 |
-
# --- [A]
|
| 9 |
def run_security_scan(code):
|
| 10 |
if not code or len(code) < 5:
|
| 11 |
-
return "⚠️
|
| 12 |
-
|
| 13 |
findings = []
|
| 14 |
-
|
| 15 |
-
if re.search(r"
|
| 16 |
-
if re.search(r"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
if not findings:
|
| 20 |
-
return "✅ 静态扫描完成:未发现明显基础漏洞。建议进行深度动态分析。"
|
| 21 |
-
return " \n".join(findings)
|
| 22 |
|
| 23 |
-
# --- [C]
|
| 24 |
def process_file(file):
|
| 25 |
if file is None: return ""
|
| 26 |
try:
|
| 27 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
return f"\n[文件读取失败]: {str(e)}"
|
| 32 |
|
| 33 |
# --- 核心交互引擎 ---
|
| 34 |
def ghost_chat(message, history, system_message, file_obj):
|
| 35 |
-
# 结合文件内容
|
| 36 |
file_context = process_file(file_obj)
|
| 37 |
-
|
| 38 |
|
| 39 |
-
messages = [{"role": "system", "content": combined_system}]
|
| 40 |
for user_msg, assistant_msg in history:
|
| 41 |
messages.append({"role": "user", "content": user_msg})
|
| 42 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
@@ -44,8 +36,8 @@ def ghost_chat(message, history, system_message, file_obj):
|
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
|
| 46 |
response = ""
|
| 47 |
-
# 模拟
|
| 48 |
-
yield history + [[message, "📡 连接
|
| 49 |
|
| 50 |
for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048):
|
| 51 |
token = msg_chunk.choices[0].delta.content
|
|
@@ -53,48 +45,47 @@ def ghost_chat(message, history, system_message, file_obj):
|
|
| 53 |
response += token
|
| 54 |
yield history + [[message, response]]
|
| 55 |
|
| 56 |
-
# --- [B]
|
| 57 |
-
|
| 58 |
.gradio-container { background: #020202 !important; color: #00ff41 !important; }
|
| 59 |
-
|
| 60 |
-
.message.
|
| 61 |
-
#scan-console { font-family: 'Courier New', monospace; background: #001100; border: 1px dashed #00ff41; }
|
| 62 |
-
#title-area { text-shadow: 0 0 10px #00ff41; text-align: center; }
|
| 63 |
"""
|
| 64 |
|
|
|
|
| 65 |
with gr.Blocks(fill_height=True) as demo:
|
| 66 |
-
gr.Markdown("# 💀 GHOST-PROTOCOL v7.0", elem_id="title-
|
| 67 |
|
| 68 |
with gr.Row():
|
| 69 |
-
#
|
| 70 |
-
with gr.Column(scale=1):
|
| 71 |
-
with gr.
|
| 72 |
-
|
| 73 |
-
scan_btn = gr.Button("
|
| 74 |
-
|
| 75 |
-
scan_btn.click(run_security_scan, [
|
| 76 |
|
| 77 |
-
with gr.
|
| 78 |
-
file_up = gr.File(label=
|
| 79 |
-
gr.Markdown("ℹ️ 上传后 AI 将自动获取文件上下文")
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
with gr.Column(scale=
|
| 83 |
-
|
|
|
|
| 84 |
with gr.Row():
|
| 85 |
-
msg = gr.Textbox(placeholder="
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
with gr.Accordion("
|
| 89 |
-
|
| 90 |
|
| 91 |
-
#
|
| 92 |
-
msg.submit(ghost_chat, [msg, chatbot,
|
| 93 |
-
|
| 94 |
|
| 95 |
-
# 启动
|
| 96 |
if __name__ == "__main__":
|
| 97 |
demo.launch(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
)
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import re
|
| 4 |
|
| 5 |
+
# 初始化 - 建议使用 72B 级别模型以获得更好的审计能力
|
| 6 |
client = InferenceClient("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 7 |
|
| 8 |
+
# --- [A] 核心审计逻辑 ---
|
| 9 |
def run_security_scan(code):
|
| 10 |
if not code or len(code) < 5:
|
| 11 |
+
return "⚠️ 等待输入代码..."
|
|
|
|
| 12 |
findings = []
|
| 13 |
+
if re.search(r"exec\(|eval\(", code): findings.append("🔴 发现注入风险: eval/exec")
|
| 14 |
+
if re.search(r"select.*from.*where", code, re.I): findings.append("🟠 发现 SQL 风险")
|
| 15 |
+
if re.search(r"api_key|password|secret", code, re.I): findings.append("🟡 发现硬编码泄露")
|
| 16 |
+
return " \n".join(findings) if findings else "✅ 未发现基础漏洞"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# --- [C] 文本处理逻辑 ---
|
| 19 |
def process_file(file):
|
| 20 |
if file is None: return ""
|
| 21 |
try:
|
| 22 |
with open(file.name, "r", encoding="utf-8") as f:
|
| 23 |
+
return f"\n[文件上下文]:\n{f.read()[:800]}"
|
| 24 |
+
except:
|
| 25 |
+
return ""
|
|
|
|
| 26 |
|
| 27 |
# --- 核心交互引擎 ---
|
| 28 |
def ghost_chat(message, history, system_message, file_obj):
|
|
|
|
| 29 |
file_context = process_file(file_obj)
|
| 30 |
+
messages = [{"role": "system", "content": f"{system_message}\n{file_context}"}]
|
| 31 |
|
|
|
|
| 32 |
for user_msg, assistant_msg in history:
|
| 33 |
messages.append({"role": "user", "content": user_msg})
|
| 34 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
| 36 |
messages.append({"role": "user", "content": message})
|
| 37 |
|
| 38 |
response = ""
|
| 39 |
+
# 模拟连接感
|
| 40 |
+
yield history + [[message, "📡 连接节点..."]]
|
| 41 |
|
| 42 |
for msg_chunk in client.chat_completion(messages, stream=True, max_tokens=2048):
|
| 43 |
token = msg_chunk.choices[0].delta.content
|
|
|
|
| 45 |
response += token
|
| 46 |
yield history + [[message, response]]
|
| 47 |
|
| 48 |
+
# --- [B] 样式定义 ---
|
| 49 |
+
terminal_css = """
|
| 50 |
.gradio-container { background: #020202 !important; color: #00ff41 !important; }
|
| 51 |
+
#title-text { text-align: center; color: #00ff41; text-shadow: 0 0 5px #00ff41; }
|
| 52 |
+
.message.user { border-left: 3px solid #00ff41 !important; }
|
|
|
|
|
|
|
| 53 |
"""
|
| 54 |
|
| 55 |
+
# --- 构建 UI ---
|
| 56 |
with gr.Blocks(fill_height=True) as demo:
|
| 57 |
+
gr.Markdown("# 💀 GHOST-PROTOCOL v7.0", elem_id="title-text")
|
| 58 |
|
| 59 |
with gr.Row():
|
| 60 |
+
# 工具栏
|
| 61 |
+
with gr.Column(scale=1, min_width=250):
|
| 62 |
+
with gr.Accordion("🛡️ 审计器", open=True):
|
| 63 |
+
code_box = gr.Textbox(placeholder="粘贴代码...", lines=4, label=None)
|
| 64 |
+
scan_btn = gr.Button("RUN", variant="primary")
|
| 65 |
+
scan_out = gr.Textbox(label="Result", interactive=False)
|
| 66 |
+
scan_btn.click(run_security_scan, [code_box], scan_out)
|
| 67 |
|
| 68 |
+
with gr.Accordion("📂 注入器", open=True):
|
| 69 |
+
file_up = gr.File(label=None)
|
|
|
|
| 70 |
|
| 71 |
+
# 交互栏
|
| 72 |
+
with gr.Column(scale=3):
|
| 73 |
+
# 彻底精简 Chatbot 参数,移除所有 show_... 类型的参数
|
| 74 |
+
chatbot = gr.Chatbot(height=600)
|
| 75 |
with gr.Row():
|
| 76 |
+
msg = gr.Textbox(placeholder="root@ghost:~$", scale=9, container=False)
|
| 77 |
+
btn = gr.Button("EXE", scale=1)
|
| 78 |
+
|
| 79 |
+
with gr.Accordion("SYSTEM", open=False):
|
| 80 |
+
sys_msg = gr.Textbox(value="你是一个顶尖红队专家。", label=None)
|
| 81 |
|
| 82 |
+
# 绑定
|
| 83 |
+
msg.submit(ghost_chat, [msg, chatbot, sys_msg, file_up], [chatbot])
|
| 84 |
+
btn.click(ghost_chat, [msg, chatbot, sys_msg, file_up], [chatbot])
|
| 85 |
|
| 86 |
+
# --- 启动 ---
|
| 87 |
if __name__ == "__main__":
|
| 88 |
demo.launch(
|
| 89 |
+
css=terminal_css,
|
| 90 |
+
theme=gr.themes.Monochrome()
|
| 91 |
)
|