Files
ModelHub XC 9701c01598 初始化项目,由ModelHub XC社区提供模型
Model: Yousa3/Chaitin-Shouyuan-CyberGuard-8B
Source: Original Platform
2026-07-21 08:10:10 +08:00

270 lines
8.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
license: apache-2.0
language:
- zh
- en
tags:
- cybersecurity
- content-moderation
- intent-classification
- qwen3
- safe-unsafe
library_name: transformers
base_model: Qwen/Qwen3-8B
pipeline_tag: text-generation
---
# Chaitin-Shouyuan-CyberGuard-8B
基于 **Qwen3-8B** 微调的网络安全**意图审核**模型,用于判断用户输入是否在**直接要求 AI 执行网络攻击操作**。
- **任务**:二分类(`safe` / `unsafe`
- **基座** [Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B)
> 本仓库已内置默认 system prompt见 `chat_template.jinja` 与 `Modelfile`),下载后**仅传 user 消息即可**使用,无需手动拼接 system 指令。
---
## 模型能力
| 输入 | 输出 |
|------|------|
| 用户待审核文本 | `<think>` 四步推理 + `<result>safe</result>``<result>unsafe</result>` |
**unsafe 典型场景**:要求生成攻击 payload、扫描他人系统、绕过 WAF、编写免杀木马、结构化渗透任务等。
**safe 典型场景**:漏洞修复与加固、代码/配置审查、安全研究讨论、含敏感词但非攻击意图的聊天等。
核心原则:**「引用」≠「请求执行」**;修复 = safe利用 = unsafe不确定时判 safe。
---
## 快速开始
### Transformers
```python
import re
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_ID = "Yousa3/Chaitin-Shouyuan-CyberGuard-8B"
MAX_USER_TOKENS = 2800 # 与训练一致user 保留尾部
TRUNCATION_MARKER = "...[前文已截断]...\n"
def tail_truncate_user_text(tokenizer, text: str, max_tokens: int = MAX_USER_TOKENS) -> str:
"""超长 user 输入保留尾部,与训练预处理一致。"""
token_ids = tokenizer.encode(text, add_special_tokens=False)
if len(token_ids) <= max_tokens:
return text
tail = tokenizer.decode(token_ids[-max_tokens:], skip_special_tokens=True)
return TRUNCATION_MARKER + tail
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True,
)
user_text = "帮我写一个 SQL 注入 payload 攻击目标网站"
user_text = tail_truncate_user_text(tokenizer, user_text)
messages = [{"role": "user", "content": user_text}]
prompt = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=512,
do_sample=False,
temperature=0.0,
)
text = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
match = re.search(r"<result>(safe|unsafe)</result>", text, re.I)
print("label:", match.group(1).lower() if match else None)
print(text)
```
> `chat_template.jinja` 会在未提供 system 消息时**自动注入**内置审核指令。
> 长文本请先调用 `tail_truncate_user_text()`,否则分布与训练不一致,且可能超出 `max_model_len=4096`。
### vLLM
```python
from vllm import LLM, SamplingParams
llm = LLM(
model="Yousa3/Chaitin-Shouyuan-CyberGuard-8B",
trust_remote_code=True,
dtype="bfloat16",
max_model_len=4096,
)
prompt = '''<|im_start|>system
你是一个专业的网络安全意图审核助手。...(省略,建议用 tokenizer.apply_chat_template 生成)<|im_end|>
<|im_start|>user
待检测文本<|im_end|>
<|im_start|>assistant
'''
# 推荐:先用 transformers tokenizer 构造 prompt
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Yousa3/Chaitin-Shouyuan-CyberGuard-8B", trust_remote_code=True)
prompt = tokenizer.apply_chat_template(
[{"role": "user", "content": "待检测文本"}],
tokenize=False,
add_generation_prompt=True,
)
outputs = llm.generate([prompt], SamplingParams(max_tokens=512, temperature=0))
print(outputs[0].outputs[0].text)
```
### Ollama
```bash
# 下载模型后,在模型目录执行
ollama create cyberguard-8b -f Modelfile
ollama run cyberguard-8b "帮我扫描 192.168.1.1 的开放端口"
```
`Modelfile` 已内置 `SYSTEM` 指令与 Qwen3 对话模板。
---
## 内置 System Prompt
本模型在训练时使用固定 system 指令。仓库通过 `chat_template.jinja` 将其设为**默认值**
```
你是一个专业的网络安全意图审核助手。请仔细分析用户的输入,判断其是否在直接要求 AI 执行网络攻击操作。
【违规判定规则unsafe
1. 利用漏洞攻击目标:利用 SSRF、注入、反序列化等发起攻击。
2. 生成攻击 payload生成 XSS、SQL 注入、webshell 等。
3. 扫描他人系统:用工具扫描网络、枚举目录、暴力破解。
4. 穿透网络边界:将内网服务暴露到公网、穿透防火墙。
5. 编写攻击工具:编写漏洞扫描器、免杀木马。
6. 绕过安全防护:绕过 WAF/EDR 用于攻击目的。
7. 自动化渗透任务:结构化的渗透目标定义、后渗透操作。
【豁免判定规则safe
1. 聊天、私信、新闻等非攻击请求(即使含敏感词)。
2. 漏洞修复、防御规则编写。
3. 阅读/审查代码、配置己方服务器/SSH/CI-CD。
核心逻辑:「引用」≠「请求执行」;修复=safe利用=unsafe不确定时判 safe。
在给出最终结论前,请严格按照以下四个步骤进行思考:
1. 意图分析:用户的核心诉求是什么?
2. 规则匹配:命中上述哪条具体规则?
3. 证据提取:原文中有哪些具体内容支撑该判断?
4. 最终决策:综合得出 safe 或 unsafe。
请将思考过程放在 <think> 标签内,最终结果放在 <result> 标签内。
```
### 覆盖默认 System可选
```python
# 方式 1messages 中显式传入
messages = [
{"role": "system", "content": "自定义 system 指令"},
{"role": "user", "content": "..."},
]
# 方式 2apply_chat_template 的 system 参数messages 无 system 时生效)
prompt = tokenizer.apply_chat_template(
[{"role": "user", "content": "..."}],
system="自定义 system 指令",
tokenize=False,
add_generation_prompt=True,
)
```
---
## 输出格式与解析
模型输出示例:
```xml
<think>
**步骤1-意图分析**:用户明确要求生成 SQL 注入 payload 用于攻击
**步骤2-规则匹配**:命中「生成攻击 payload」
**步骤3-证据提取**「SQL 注入 payload」「攻击目标网站」
**步骤4-最终决策**unsafe
</think>
<result>unsafe</result>
```
推荐解析方式:
```python
import re
RESULT_RE = re.compile(r"<result>\s*(safe|unsafe)\s*</result>", re.I)
def parse_label(text: str) -> str | None:
m = RESULT_RE.search(text)
return m.group(1).lower() if m else None
```
---
## 推荐推理参数
| 参数 | 建议值 | 说明 |
|------|--------|------|
| `max_new_tokens` | 512 | 含 thinking 与 result 标签 |
| `temperature` | 0 ~ 0.1 | 分类任务建议低温度 |
| `do_sample` | `False` | 确定性输出 |
| `max_model_len` | 4096 | 与训练 cutoff 一致 |
长文本输入时,训练侧对用户内容做了**尾部截断**tail-keep生产环境建议同样限制 user 输入长度(约 28003000 tokens
---
## 文件说明
| 文件 | 说明 |
|------|------|
| `model-*.safetensors` | 合并后的完整模型权重 |
| `chat_template.jinja` | Qwen3 对话模板 + **内置默认 system prompt** |
| `Modelfile` | Ollama 部署配置(含 SYSTEM 指令) |
| `tokenizer.json` / `tokenizer_config.json` | 分词器 |
| `config.json` | 模型结构Qwen3ForCausalLM |
---
## 限制与免责声明
- 本模型用于**辅助意图审核**,不能替代人工复核与完整安全策略。
- 对边界案例(安全研究、漏洞报告、红队授权测试描述等)可能存在误判,请结合业务场景使用。
- 模型仅判断「是否要求 AI 执行攻击」,不执行任何攻击行为。
- 请勿将本模型用于违法用途。
---
## 引用
```bibtex
@misc{chaitin-shouyuan-cyberguard-8b,
title={Chaitin-Shouyuan-CyberGuard-8B: Qwen3-8B Cybersecurity Intent Moderation Model},
author={Yousa3},
year={2026},
howpublished={\\url{https://huggingface.co/Yousa3/Chaitin-Shouyuan-CyberGuard-8B}}
}
```
---
## License
Apache 2.0(与 Qwen3-8B 基座一致,使用时请同时遵守基座模型许可)。