Skip to content

Commit e25cb43

Browse files
authored
Merge pull request #2 from chainreactors/feat/export-filter
Feat/export filter
2 parents 9da86a0 + b524cff commit e25cb43

41 files changed

Lines changed: 2903 additions & 2187 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 152 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,45 +35,44 @@ SDK 采用简单的四组件架构:
3535
所有引擎支持双重加载模式:
3636

3737
- **本地模式**: 从嵌入数据或文件系统加载
38-
- **远程模式**: 从 Cyberhub API 加载,支持 source 过滤
38+
- **远程模式**: 从 Cyberhub API 加载,支持 sources 过滤
3939

4040
## 快速开始
4141

4242
### Fingers - 指纹识别
4343

4444
```go
4545
import (
46-
"context"
4746
"github.com/chainreactors/sdk/fingers"
4847
)
4948

5049
// 创建并加载引擎
51-
config := fingers.NewConfig().
52-
SetCyberhubURL("http://127.0.0.1:8080").
53-
SetAPIKey("your_key")
50+
config := fingers.NewConfig()
51+
config.WithCyberhub("http://127.0.0.1:8080", "your_key")
5452

5553
engine, _ := fingers.NewEngine(config)
56-
libEngine, _ := engine.Load(context.Background())
5754

58-
// 检测指纹
59-
frameworks, _ := libEngine.DetectContent(httpResponse)
55+
// 方式一:使用 Match API 直接检测
56+
frameworks, _ := engine.Match(httpResponseBytes)
57+
58+
// 方式二:使用底层 Engine
59+
libEngine := engine.Get()
60+
frameworks, _ = libEngine.DetectContent(httpResponseBytes)
6061
```
6162

6263
### Neutron - POC 扫描
6364

6465
```go
6566
import (
66-
"context"
6767
"github.com/chainreactors/sdk/neutron"
6868
)
6969

7070
// 创建并加载引擎
71-
config := neutron.NewConfig().
72-
SetCyberhubURL("http://127.0.0.1:8080").
73-
SetAPIKey("your_key")
71+
config := neutron.NewConfig()
72+
config.WithCyberhub("http://127.0.0.1:8080", "your_key")
7473

7574
engine, _ := neutron.NewEngine(config)
76-
templates, _ := engine.Load(context.Background()) // 自动编译
75+
templates := engine.Get() // 自动编译
7776

7877
// 执行 POC
7978
for _, t := range templates {
@@ -88,26 +87,30 @@ for _, t := range templates {
8887

8988
```go
9089
import (
91-
"context"
9290
"github.com/chainreactors/sdk/gogo"
9391
"github.com/chainreactors/sdk/fingers"
9492
"github.com/chainreactors/sdk/neutron"
9593
)
9694

9795
// 加载指纹库
9896
fingersEngine, _ := fingers.NewEngine(fingersConfig)
99-
libEngine, _ := fingersEngine.Load(ctx)
100-
fEngine := libEngine.GetEngine("fingers")
10197

10298
// 加载 POC
10399
neutronEngine, _ := neutron.NewEngine(neutronConfig)
104-
templates, _ := neutronEngine.Load(ctx)
105100

106101
// 创建集成扫描器
107-
gogoEngine := gogo.NewGogoEngineWithFingersAndNeutron(nil, fEngine, templates)
102+
gogoConfig := gogo.NewConfig().
103+
WithFingersEngine(fingersEngine).
104+
WithNeutronEngine(neutronEngine)
105+
gogoEngine := gogo.NewEngine(gogoConfig)
108106
gogoEngine.Init()
109107

110108
// 执行扫描
109+
gogoCtx := gogo.NewContext().
110+
SetThreads(1000).
111+
SetVersionLevel(2).
112+
SetExploit("all").
113+
SetDelay(5)
111114
task := gogo.NewScanTask("192.168.1.0/24", "80,443,8080")
112115
resultCh, _ := gogoEngine.Execute(gogoCtx, task)
113116

@@ -120,14 +123,16 @@ for result := range resultCh {
120123

121124
```go
122125
import (
123-
"context"
124126
"github.com/chainreactors/sdk/spray"
125127
)
126128

127129
engine := spray.NewEngine(nil)
128130
engine.Init()
129131

130132
urls := []string{"http://example.com", "http://target.com"}
133+
sprayCtx := spray.NewContext().
134+
SetThreads(100).
135+
SetTimeout(10)
131136
task := spray.NewCheckTask(urls)
132137
resultCh, _ := engine.Execute(sprayCtx, task)
133138

@@ -142,39 +147,45 @@ for result := range resultCh {
142147
### Fingers 配置
143148

144149
```go
145-
config := fingers.NewConfig().
146-
SetCyberhubURL("http://127.0.0.1:8080").
147-
SetAPIKey("your_key").
148-
SetSource("github"). // 可选:按来源过滤
149-
SetTimeout(10 * time.Second).
150-
SetMaxRetries(3)
150+
config := fingers.NewConfig()
151+
config.WithCyberhub("http://127.0.0.1:8080", "your_key")
152+
config.SetSources("github") // 可选:按来源过滤
153+
config.WithLocalFile("fingers.yaml") // 可选:从导出的 YAML 加载
154+
config.SetTimeout(10 * time.Second)
151155
```
152156

153157
### Neutron 配置
154158

155159
```go
156-
config := neutron.NewConfig().
157-
SetCyberhubURL("http://127.0.0.1:8080").
158-
SetAPIKey("your_key").
159-
SetSource("github"). // 可选:按来源过滤
160-
SetLocalPath("./pocs"). // 可选:本地 POC 目录
161-
SetTimeout(10 * time.Second)
160+
config := neutron.NewConfig()
161+
config.WithCyberhub("http://127.0.0.1:8080", "your_key")
162+
config.SetSources("github") // 可选:按来源过滤
163+
config.WithLocalFile("./pocs") // 可选:本地 POC 目录
164+
config.SetTimeout(10 * time.Second)
162165
```
163166

164167
### GoGo 配置
165168

166169
```go
167170
config := gogo.NewConfig().
171+
WithFingersEngine(fingersEngine).
172+
WithNeutronEngine(neutronEngine)
173+
```
174+
175+
### GoGo 运行时上下文
176+
177+
```go
178+
ctx := gogo.NewContext().
168179
SetThreads(1000).
169180
SetVersionLevel(2). // 0-3,数值越高检测越深
170181
SetExploit("all"). // none/all/known
171182
SetDelay(5) // 请求超时时间
172183
```
173184

174-
### Spray 配置
185+
### Spray 运行时上下文
175186

176187
```go
177-
config := spray.NewConfig().
188+
ctx := spray.NewContext().
178189
SetThreads(100).
179190
SetTimeout(10)
180191
```
@@ -201,31 +212,41 @@ sdk/
201212
├── fingers/ # 指纹识别引擎
202213
│ ├── engine.go # 核心引擎实现
203214
│ ├── config.go # 配置
204-
│ └── api.go # 公共 API
215+
│ ├── types.go # 类型定义
216+
│ ├── additions.go # 扩展方法 (AddFingers, AddFingersFile)
217+
│ ├── init.go # 注册入口
218+
│ └── README.md # 引擎文档
205219
206220
├── neutron/ # POC 扫描引擎
207-
│ ├── engine.go # 核心引擎(自动编译)
208-
│ ├── config.go # 配置
209-
│ └── api.go # 公共 API
221+
│ ├── engine.go # 核心引擎(自动编译)
222+
│ ├── config.go # 配置
223+
│ ├── types.go # 类型定义
224+
│ ├── templates.go # Templates 辅助类型 (Filter, Merge)
225+
│ ├── additions.go # 扩展方法 (AddPocs, AddPocsFile)
226+
│ └── README.md # 引擎文档
210227
211-
├── gogo/ # 端口扫描(集成)
212-
│ ├── gogo.go # 支持 Fingers/Neutron 的引擎
213-
│ ├── config.go # 配置
214-
│ └── api.go # 公共 API
228+
├── gogo/ # 端口扫描(集成)
229+
│ ├── gogo.go # 支持 Fingers/Neutron 的引擎
230+
│ ├── types.go # 类型定义和配置
231+
│ ├── init.go # 注册入口
232+
│ └── README.md # 引擎文档
215233
216-
├── spray/ # HTTP 检测引擎
217-
│ ├── spray.go # 核心引擎实现
218-
│ ├── config.go # 配置
219-
│ └── api.go # 公共 API
234+
├── spray/ # HTTP 检测引擎
235+
│ ├── spray.go # 核心引擎实现
236+
│ ├── types.go # 类型定义和配置
237+
│ ├── init.go # 注册入口
238+
│ └── README.md # 引擎文档
220239
221240
├── pkg/
222-
│ ├── cyberhub/ # 统一 API 客户端
223-
│ │ ├── client.go # HTTP 客户端(支持 gzip)
224-
│ │ └── types.go # API 类型
225-
│ ├── interface.go # 核心 SDK 接口
226-
│ └── helper.go # 工具函数
241+
│ ├── cyberhub/ # 统一 API 客户端
242+
│ │ ├── client.go # HTTP 客户端(支持 gzip)
243+
│ │ ├── config.go # 客户端配置
244+
│ │ └── types.go # API 类型
245+
│ ├── association/ # 关联索引
246+
│ │ └── index.go # 指纹-POC 关联索引
247+
│ └── interface.go # 核心 SDK 接口
227248
228-
└── examples/ # CLI 工具实现
249+
└── examples/ # CLI 工具实现
229250
├── fingers/
230251
├── neutron/
231252
├── gogo/
@@ -239,8 +260,7 @@ sdk/
239260

240261
所有引擎都支持从 Cyberhub 加载数据:
241262
- Gzip 压缩处理
242-
- 自动重试(指数退避)
243-
- 基于 source 的过滤
263+
- 基于 sources 的过滤
244264
- API Key 认证
245265

246266
### POC 自动编译
@@ -255,7 +275,84 @@ Neutron 引擎在加载时自动编译 POC:
255275
GoGo 可以同时集成 Fingers 和 Neutron:
256276
- 模板按指纹、ID、标签建立索引
257277
- 9,444 个 POC 生成 61,267 条索引(多重索引)
258-
- 根据检测到的指纹自动匹配模板
278+
279+
### 基于指纹筛选 POC 示例
280+
281+
下面示例演示:Fingers 命中指纹后,使用 `neutron.Templates.Filter` 从模板集中筛选相关 POC 并执行。
282+
283+
```go
284+
package main
285+
286+
import (
287+
"strings"
288+
289+
"github.com/chainreactors/sdk/fingers"
290+
"github.com/chainreactors/sdk/neutron"
291+
neutronTemplates "github.com/chainreactors/neutron/templates"
292+
)
293+
294+
func main() {
295+
// 1) 指纹识别
296+
fConfig := fingers.NewConfig().WithCyberhub("http://127.0.0.1:8080", "your_key")
297+
fEngine, _ := fingers.NewEngine(fConfig)
298+
299+
// 使用 Match API 直接匹配
300+
frameworks, _ := fEngine.Match([]byte("raw http response"))
301+
302+
// 收集指纹名称
303+
fingerNames := make(map[string]struct{})
304+
for _, frame := range frameworks {
305+
fingerNames[strings.ToLower(frame.Name)] = struct{}{}
306+
}
307+
308+
// 2) 加载 POC 并使用 Filter 筛选
309+
nConfig := neutron.NewConfig().WithCyberhub("http://127.0.0.1:8080", "your_key")
310+
nEngine, _ := neutron.NewEngine(nConfig)
311+
312+
// 使用 Templates.Filter 按指纹/标签筛选
313+
filtered := (neutron.Templates{}).Merge(nEngine.Get()).Filter(func(t *neutronTemplates.Template) bool {
314+
// 按 Fingers 字段匹配
315+
for _, finger := range t.Fingers {
316+
if _, ok := fingerNames[strings.ToLower(finger)]; ok {
317+
return true
318+
}
319+
}
320+
// 按 Tags 匹配
321+
for _, tag := range t.GetTags() {
322+
if _, ok := fingerNames[strings.ToLower(tag)]; ok {
323+
return true
324+
}
325+
}
326+
return false
327+
})
328+
329+
// 3) 执行筛选后的 POC
330+
task := &neutron.ExecuteTask{
331+
Target: "http://target.example",
332+
Templates: filtered.Templates(),
333+
}
334+
resultCh, _ := nEngine.Execute(neutron.NewContext(), task)
335+
for range resultCh {
336+
// consume results
337+
}
338+
}
339+
```
340+
341+
也可以使用 `pkg/association` 包中的 `FingerPOCIndex` 进行更高效的关联查询。
342+
343+
### 动态扩展
344+
345+
引擎支持在运行时动态添加指纹和 POC:
346+
347+
```go
348+
// Fingers: 动态添加指纹
349+
engine.AddFingers(newFingers) // 添加指纹切片
350+
engine.AddFingersFile("./custom.yaml") // 从文件/目录加载
351+
352+
// Neutron: 动态添加 POC
353+
engine.AddPocs(newTemplates) // 添加模板切片
354+
engine.AddPocsFile("./custom-pocs/") // 从文件/目录加载
355+
```
259356

260357
## 开发
261358

@@ -275,7 +372,7 @@ go test ./spray -v
275372
### 添加新引擎
276373

277374
1. 实现 `pkg/interface.go` 中的核心接口
278-
2. 创建引擎包,包含 `engine.go``config.go``api.go`
375+
2. 创建引擎包,包含 `engine.go``config.go``init.go`
279376
3.`engine.go` 的 init 函数中注册
280377
4.`examples/` 中添加 CLI 工具
281378

0 commit comments

Comments
 (0)