null822 commited on
Commit
c5ddfde
·
verified ·
1 Parent(s): 9adab1f

Add comprehensive model card

Browse files
Files changed (1) hide show
  1. README.md +204 -3
README.md CHANGED
@@ -1,3 +1,204 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ - zh
5
+ library_name: transformers
6
+ tags:
7
+ - security
8
+ - webshell-detection
9
+ - malware-detection
10
+ - cybersecurity
11
+ - code-classification
12
+ - php
13
+ - asp
14
+ - jsp
15
+ - python
16
+ - perl
17
+ license: mit
18
+ datasets:
19
+ - null822/webshell-sample
20
+ base_model:
21
+ - microsoft/codebert-base
22
+ - huawei-noah/TinyBERT_General_4L_312D
23
+ pipeline_tag: text-classification
24
+ widget:
25
+ - text: "<?php eval($_POST['cmd']); ?>"
26
+ example_title: "Malicious WebShell Example"
27
+ - text: "<?php echo 'Hello World'; ?>"
28
+ example_title: "Normal PHP Code"
29
+ ---
30
+
31
+ # WebShell Detection Models Collection
32
+
33
+ ## 模型概述 / Model Overview
34
+
35
+ 这是一个用于检测恶意 WebShell 代码的机器学习模型集合,基于 BERT 架构进行微调。本仓库包含四个模型变体,针对不同的使用场景进行了优化。
36
+
37
+ This is a collection of machine learning models for detecting malicious WebShell code, fine-tuned on BERT architectures. The repository contains four model variants optimized for different use cases.
38
+
39
+ ## 模型变体 / Model Variants
40
+
41
+ ### 1. full_codebert_model
42
+ - **基础模型**: microsoft/codebert-base
43
+ - **训练数据**: 多语言数据集(PHP, ASP, JSP, Python, Perl, HTML, JavaScript, Shell等)
44
+ - **参数量**: ~125M
45
+ - **特点**: 高精度,适合准确性要求高的场景
46
+
47
+ ### 2. full_tinybert_model
48
+ - **基础模型**: huawei-noah/TinyBERT_General_4L_312D
49
+ - **训练数据**: 多语言数据集
50
+ - **参数量**: ~14.5M
51
+ - **特点**: 轻量级,快速推理,适合资源受限环境
52
+
53
+ ### 3. php_codebert_model
54
+ - **基础模型**: microsoft/codebert-base
55
+ - **训练数据**: 仅 PHP 代码数据集
56
+ - **参数量**: ~125M
57
+ - **特点**: 专门针对 PHP WebShell 检测优化
58
+
59
+ ### 4. php_tinybert_model
60
+ - **基础模型**: huawei-noah/TinyBERT_General_4L_312D
61
+ - **训练数据**: 仅 PHP 代码数据集
62
+ - **参数量**: ~14.5M
63
+ - **特点**: PHP 专用轻量级模型
64
+
65
+ ## 支持的文件类型 / Supported File Types
66
+
67
+ - PHP (.php)
68
+ - ASP (.asp, .aspx)
69
+ - JSP (.jsp, .jspx)
70
+ - Python (.py)
71
+ - Perl (.pl)
72
+ - HTML (.html, .htm)
73
+ - JavaScript (.js)
74
+ - Shell scripts (.sh)
75
+ - CGI (.cgi)
76
+ - Java (.java)
77
+
78
+ ## 使用方法 / Usage
79
+
80
+ ### 基本使用 / Basic Usage
81
+
82
+ ```python
83
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
84
+ import torch
85
+
86
+ # 选择模型变体 / Choose model variant
87
+ model_name = "null822/webshell-detect-bert"
88
+ subfolder = "full_tinybert_model" # 或其他变体
89
+
90
+ # 加载模型 / Load model
91
+ tokenizer = AutoTokenizer.from_pretrained(model_name, subfolder=subfolder)
92
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, subfolder=subfolder)
93
+
94
+ def detect_webshell(code_text):
95
+ inputs = tokenizer(code_text, return_tensors="pt", truncation=True, max_length=512)
96
+ with torch.no_grad():
97
+ outputs = model(**inputs)
98
+ prediction = torch.argmax(outputs.logits, dim=1).item()
99
+ return "Malicious WebShell" if prediction == 1 else "Normal Code"
100
+
101
+ # 示例 / Example
102
+ code = "<?php eval($_POST['cmd']); ?>"
103
+ result = detect_webshell(code)
104
+ print(result) # 输出: Malicious WebShell
105
+ ```
106
+
107
+ ### 批量检测 / Batch Detection
108
+
109
+ ```python
110
+ def batch_detect(code_list):
111
+ results = []
112
+ for code in code_list:
113
+ result = detect_webshell(code)
114
+ results.append(result)
115
+ return results
116
+
117
+ # 示例 / Example
118
+ codes = [
119
+ "<?php echo 'Hello World'; ?>",
120
+ "<?php eval($_POST['cmd']); ?>",
121
+ "<?php system($_GET['c']); ?>"
122
+ ]
123
+ results = batch_detect(codes)
124
+ ```
125
+
126
+ ### 文件检测 / File Detection
127
+
128
+ ```python
129
+ def detect_file(file_path):
130
+ try:
131
+ with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
132
+ content = f.read()
133
+ return detect_webshell(content)
134
+ except Exception as e:
135
+ return f"Error reading file: {e}"
136
+
137
+ # 示例 / Example
138
+ result = detect_file("suspicious_file.php")
139
+ ```
140
+
141
+ ## 模型选择指南 / Model Selection Guide
142
+
143
+ | 使用场景 | 推荐模型 | 理由 |
144
+ |---------|---------|------|
145
+ | 生产环境,高精度要求 | `full_codebert_model` | 最高准确率 |
146
+ | 资源受限,需要快速响应 | `full_tinybert_model` | 平衡性能和资源消耗 |
147
+ | 专门检测PHP WebShell | `php_codebert_model` | PHP优化,高精度 |
148
+ | PHP检测,资源受限 | `php_tinybert_model` | PHP专用轻量级 |
149
+
150
+ ## 性能指标 / Performance Metrics
151
+
152
+ 模型在测试集上的表现:
153
+
154
+ - **Accuracy**: >95%
155
+ - **Precision**: >94%
156
+ - **Recall**: >96%
157
+ - **F1-Score**: >95%
158
+
159
+ *具体指标可能因测试数据集而异*
160
+
161
+ ## 训练数据 / Training Data
162
+
163
+ - **数据集**: [null822/webshell-sample](https://huggingface.co/datasets/null822/webshell-sample)
164
+ - **样本数量**: 5000+ 代码样本
165
+ - **数据来源**:
166
+ - 正常代码:开源项目和合法代码仓库
167
+ - 恶意代码:已知的 WebShell 样本和恶意脚本
168
+ - **数据处理**: Base64编码确保安全传输和存储
169
+
170
+ ## 限制和注意事项 / Limitations
171
+
172
+ 1. **上下文长度**: 最大支持512个token
173
+ 2. **语言��持**: 主要针对英文代码和常见编程语言
174
+ 3. **误报**: 复杂的正常代码可能被误判为恶意
175
+ 4. **更新需求**: 需要定期使用新的威胁样本重新训练
176
+
177
+ ## 部署建议 / Deployment Recommendations
178
+
179
+ 1. **生产环境**: 建议使用 `full_codebert_model` 以获得最佳准确性
180
+ 2. **边缘设备**: 使用 TinyBERT 变体以减少资源消耗
181
+ 3. **实时检测**: 考虑批处理以提高效率
182
+ 4. **安全集成**: 结合其他安全工具使用,不应作为唯一防护手段
183
+
184
+ ## 引用 / Citation
185
+
186
+ 如果您使用了这些模型,请引用:
187
+
188
+ ```bibtex
189
+ @misc{webshell-detect-bert,
190
+ title={WebShell Detection Models based on BERT},
191
+ author={null822},
192
+ year={2025},
193
+ publisher={Hugging Face},
194
+ howpublished={\url{https://huggingface.co/null822/webshell-detect-bert}}
195
+ }
196
+ ```
197
+
198
+ ## 许可证 / License
199
+
200
+ MIT License
201
+
202
+ ## 联系方式 / Contact
203
+
204
+ 如有问题或建议,请通过 GitHub Issues 联系。