refactor: 将auto.json嵌入编译文件中

- 使用Go embed包将AV/EDR规则数据库嵌入到可执行文件
- 移除对外部auto.json文件的依赖
- 提高工具的便携性和独立性
- 将auto.json移动到avdetect插件目录
- 更新loadAVDatabase方法使用嵌入数据
This commit is contained in:
ZacharyZcR 2025-08-10 10:57:31 +08:00
parent 4a33b89738
commit 1cfb21ed64
2 changed files with 9 additions and 30 deletions

View File

@ -3,6 +3,7 @@ package avdetect
import (
"bufio"
"context"
_ "embed"
"encoding/json"
"fmt"
"os"
@ -18,6 +19,9 @@ import (
"github.com/shadow1ng/fscan/plugins/local"
)
//go:embed auto.json
var embeddedAVDatabase []byte
// AVDetectPlugin AV/EDR检测插件
type AVDetectPlugin struct {
*local.BaseLocalPlugin
@ -325,41 +329,16 @@ func (p *AVDetectPlugin) ScanLocal(ctx context.Context, info *common.HostInfo) (
// loadAVDatabase 加载AV/EDR数据库
func (p *AVDetectPlugin) loadAVDatabase() error {
// 尝试多个可能的配置文件路径
possiblePaths := []string{
p.configPath,
"auto.json",
filepath.Join(".", "auto.json"),
filepath.Join("config", "auto.json"),
filepath.Join("..", "auto.json"),
}
var configData []byte
var err error
var usedPath string
for _, path := range possiblePaths {
configData, err = os.ReadFile(path)
if err == nil {
usedPath = path
break
}
}
// 首先尝试使用嵌入的数据库
common.LogDebug("使用嵌入的AV/EDR规则数据库")
err := json.Unmarshal(embeddedAVDatabase, &p.avDatabase)
if err != nil {
return fmt.Errorf("无法找到配置文件 auto.json: %v", err)
}
common.LogDebug(fmt.Sprintf("使用配置文件: %s", usedPath))
// 解析JSON
err = json.Unmarshal(configData, &p.avDatabase)
if err != nil {
return fmt.Errorf("解析配置文件失败: %v", err)
return fmt.Errorf("解析嵌入的AV数据库失败: %v", err)
}
if len(p.avDatabase) == 0 {
return fmt.Errorf("配置文件为空或格式错误")
return fmt.Errorf("嵌入的AV数据库为空或格式错误")
}
return nil