fix+perf: 修复10个bug & 10项性能优化

Bug修复:
- clustersend CEL结果判断从字符串比较改为类型断言
- Nuclei DSL matcher安全降级为false避免误报
- clusterpoc发现漏洞后返回true修正语义
- reverseCheck加10s超时防止ceye API阻塞
- doSearch/bmatches正则编译结果缓存到sync.Map
- evalset CEL求值失败时存空字符串而非原始表达式
- CEL wait()函数加nil Reverse指针检查防panic
- MongoDB readMongoMsg应用timeout参数设置读超时
- TXTWriter.Close确保Sync失败后仍调用file.Close

性能优化:
- 指纹regex缓存从RWMutex+map改为sync.Map消除锁竞争
- CaseInsensitive指纹词加载时预小写化避免匹配时分配
- 版本提取FindAllStringSubmatch限制返回数量
- i18n.Tr用strconv.Itoa替代Sprintf减少分配
- POC加载用atomic.Bool+DCLP消除热路径锁
- 结果缓冲map预分配容量减少rehash
- HTTP连接池参数随并发数动态调整
- getRuleHash去除反射+Headers排序保证确定性dedup
- POC并发加载用channel替代Mutex收集结果
This commit is contained in:
ZacharyZcR
2026-06-14 22:23:49 +08:00
parent 8402be98e3
commit a115499793
13 changed files with 169 additions and 111 deletions
+19 -3
View File
@@ -143,12 +143,28 @@ func InitHTTPClient(ThreadsNum int, DownProxy string, Timeout time.Duration, max
KeepAlive: keepAlive,
}
// 连接池参数随并发数动态调整
maxConns := ThreadsNum * 2
if maxConns < 20 {
maxConns = 20
}
if maxConns > 200 {
maxConns = 200
}
idlePerHost := ThreadsNum / 2
if idlePerHost < 5 {
idlePerHost = 5
}
if idlePerHost > 20 {
idlePerHost = 20
}
// 配置Transport参数
tr := &http.Transport{
DialContext: dialer.DialContext,
MaxConnsPerHost: 100, // 增加到100,避免连接池耗尽
MaxIdleConns: 100, // 保留100个空闲连接
MaxIdleConnsPerHost: 10, // 每主机保留10个空闲连接
MaxConnsPerHost: maxConns,
MaxIdleConns: maxConns,
MaxIdleConnsPerHost: idlePerHost,
IdleConnTimeout: keepAlive,
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS10, InsecureSkipVerify: true},
TLSHandshakeTimeout: 5 * time.Second,
+6 -2
View File
@@ -3,6 +3,7 @@ package lib
import (
"bytes"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
@@ -368,11 +369,14 @@ func reverseCheck(r *Reverse, timeout int64) bool {
apiURL := fmt.Sprintf("http://api.ceye.io/v1/records?token=%s&type=dns&filter=%s",
ceyeAPI, sub)
// 创建并发送请求
req, err := http.NewRequest("GET", apiURL, nil)
// 创建并发送请求(带超时控制,避免 ceye API 无响应时阻塞)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", apiURL, nil)
if err != nil {
return false
}
// nil session: CEL 回调无法获取 session,回退到全局限速(反连检查请求量极低,可接受)
resp, err := DoRequest(req, false, nil)
if err != nil {
return false
+1 -1
View File
@@ -31,7 +31,7 @@ func registerMiscImplementations() []*functions.Overload {
Operator: "reverse_wait_int",
Binary: func(lhs ref.Val, rhs ref.Val) ref.Val {
reverse, ok := lhs.Value().(*Reverse)
if !ok {
if !ok || reverse == nil {
return types.ValOrErr(lhs, "unexpected type '%v' passed to wait", lhs.Type())
}
timeout, ok := rhs.(types.Int)
+12 -4
View File
@@ -70,11 +70,19 @@ func registerStringImplementations() []*functions.Overload {
if !ok {
return types.ValOrErr(rhs, "unexpected type '%v' passed to bmatch", rhs.Type())
}
ok, err := regexp.Match(string(v1), v2)
if err != nil {
return types.NewErr("%v", err)
pattern := string(v1)
var re *regexp.Regexp
if cached, found := regexCache.Load(pattern); found {
re = cached.(*regexp.Regexp)
} else {
compiled, err := regexp.Compile(pattern)
if err != nil {
return types.NewErr("%v", err)
}
actual, _ := regexCache.LoadOrStore(pattern, compiled)
re = actual.(*regexp.Regexp)
}
return types.Bool(ok)
return types.Bool(re.Match(v2))
},
},
{
+2 -2
View File
@@ -307,8 +307,8 @@ func convertNucleiMatchers(matchers []NucleiMatcher, matchersCondition string) s
matcherConds = append(matcherConds, nucleiRegexCondition(m.Part, pattern))
}
case "dsl":
// DSL类型暂不支持,使用默认匹配
matcherConds = append(matcherConds, "response.status == 200")
// DSL类型暂不支持,安全降级为 false 避免误报
matcherConds = append(matcherConds, "false")
}
// 单个matcher内的条件组合
+40 -17
View File
@@ -3,11 +3,13 @@ package lib
import (
"crypto/md5" //nolint:gosec // G501: MD5用于POC规则去重,非加密用途
"fmt"
"io"
"math/rand" //nolint:gosec // G404: math/rand用于生成测试数据,非加密用途
"net/http"
"net/url"
"os"
"regexp"
"sort"
"strings"
"sync"
"time"
@@ -91,7 +93,7 @@ func CheckMultiPoc(req *http.Request, pocs []*Poc, workers int, pocCtx *POCConte
// 因为clusterpoc已在内部处理了漏洞输出
if isVulnerable && vulName != "" {
// 构造漏洞详细信息
details := make(map[string]interface{})
details := make(map[string]interface{}, 6)
details["vulnerability_type"] = task.Poc.Name
details["vulnerability_name"] = vulName
@@ -341,14 +343,22 @@ func executeRules(oReq *http.Request, p *Poc, variableMap map[string]interface{}
return false, "", nil
}
var regexCache sync.Map
// doSearch 在响应体中执行正则匹配并提取命名捕获组
func doSearch(re string, body string) map[string]string {
// 编译正则表达式
r, err := regexp.Compile(re)
// 正则表达式编译
if err != nil {
common.LogError(i18n.Tr("webscan_regex_compile_error", err))
return nil
// 编译正则表达式(带缓存)
var r *regexp.Regexp
if cached, ok := regexCache.Load(re); ok {
r = cached.(*regexp.Regexp)
} else {
compiled, err := regexp.Compile(re)
if err != nil {
common.LogError(i18n.Tr("webscan_regex_compile_error", err))
return nil
}
actual, _ := regexCache.LoadOrStore(re, compiled)
r = actual.(*regexp.Regexp)
}
// 执行正则匹配
@@ -537,7 +547,7 @@ func clusterpoc(oReq *http.Request, p *Poc, variableMap map[string]interface{},
if ruleIndex == len(p.Rules)-1 {
// 最终规则成功,记录完整的结果并返回
recordVulnerabilityResult(targetURL, p, strMap, false, pocCtx.Session)
return false, nil
return true, nil
}
break paramLoop
}
@@ -620,11 +630,25 @@ func applyParametersToRule(
return hasReplacement, replacedParams
}
// getRuleHash 计算规则的MD5哈希值用于去重
// getRuleHash 计算规则的MD5哈希值用于去重(无反射,Headers 排序保证确定性)
func getRuleHash(rule *Rules) string {
//nolint:gosec // G401: MD5用于规则去重,非加密用途
ruleDigest := md5.Sum([]byte(fmt.Sprintf("%v", rule)))
return fmt.Sprintf("%x", ruleDigest)
h := md5.New()
_, _ = io.WriteString(h, rule.Method)
_, _ = io.WriteString(h, rule.Path)
_, _ = io.WriteString(h, rule.Body)
if len(rule.Headers) > 0 {
keys := make([]string, 0, len(rule.Headers))
for k := range rule.Headers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
_, _ = io.WriteString(h, k)
_, _ = io.WriteString(h, rule.Headers[k])
}
}
return fmt.Sprintf("%x", h.Sum(nil))
}
// recordVulnerabilityResult 记录漏洞检测结果
@@ -830,11 +854,10 @@ func clustersend(oReq *http.Request, variableMap map[string]interface{}, req *Re
}
// 检查表达式执行结果
if fmt.Sprintf("%v", out) == "false" {
return false, nil
if flag, ok := out.Value().(bool); ok {
return flag, nil
}
return true, nil
return false, nil
}
// cloneRules 深度复制Rules结构体
@@ -870,8 +893,8 @@ func cloneMap(tags map[string]string) map[string]string {
func evalset(env *cel.Env, variableMap map[string]interface{}, k string, expression string) (string, error) {
out, err := Evaluate(env, expression, variableMap)
if err != nil {
variableMap[k] = expression
return expression, err
variableMap[k] = ""
return "", err
}
// 根据不同类型处理输出