feat: 分离结果输出和日志

This commit is contained in:
ZacharyZcR
2025-01-14 23:38:58 +08:00
parent c6c613a17b
commit 97e9ac7161
38 changed files with 1526 additions and 879 deletions
+42 -33
View File
@@ -16,8 +16,9 @@ func ElasticScan(info *Common.HostInfo) (tmperr error) {
}
maxRetries := Common.MaxRetries
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
Common.LogDebug(fmt.Sprintf("开始扫描 %v:%v", info.Host, info.Ports))
Common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
Common.LogDebug("尝试无认证访问...")
// 首先测试无认证访问
@@ -27,8 +28,22 @@ func ElasticScan(info *Common.HostInfo) (tmperr error) {
}
flag, err := ElasticConn(info, "", "")
if flag && err == nil {
Common.LogSuccess(fmt.Sprintf("Elasticsearch服务 %v:%v 无需认证",
info.Host, info.Ports))
successMsg := fmt.Sprintf("Elasticsearch服务 %s 无需认证", target)
Common.LogSuccess(successMsg)
// 保存无认证访问结果
result := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: map[string]interface{}{
"port": info.Ports,
"service": "elasticsearch",
"type": "unauthorized-access",
},
}
Common.SaveResult(result)
return err
}
if err != nil && Common.CheckErrs(err) != nil {
@@ -61,7 +76,6 @@ func ElasticScan(info *Common.HostInfo) (tmperr error) {
Common.LogDebug(fmt.Sprintf("第%d次重试: %s:%s", retryCount+1, user, pass))
}
// 执行连接尝试
done := make(chan struct {
success bool
err error
@@ -78,36 +92,49 @@ func ElasticScan(info *Common.HostInfo) (tmperr error) {
}
}(user, pass)
// 等待结果或超时
var err error
select {
case result := <-done:
err = result.err
if result.success && err == nil {
Common.LogSuccess(fmt.Sprintf("Elasticsearch服务 %v:%v 爆破成功 用户名: %v 密码: %v",
info.Host, info.Ports, user, pass))
successMsg := fmt.Sprintf("Elasticsearch服务 %s 爆破成功 用户名: %v 密码: %v",
target, user, pass)
Common.LogSuccess(successMsg)
// 保存弱密码结果
vulnResult := &Common.ScanResult{
Time: time.Now(),
Type: Common.VULN,
Target: info.Host,
Status: "vulnerable",
Details: map[string]interface{}{
"port": info.Ports,
"service": "elasticsearch",
"username": user,
"password": pass,
"type": "weak-password",
},
}
Common.SaveResult(vulnResult)
return nil
}
case <-time.After(time.Duration(Common.Timeout) * time.Second):
err = fmt.Errorf("连接超时")
}
// 处理错误情况
if err != nil {
errlog := fmt.Sprintf("Elasticsearch服务 %v:%v 尝试失败 用户名: %v 密码: %v 错误: %v",
info.Host, info.Ports, user, pass, err)
errlog := fmt.Sprintf("Elasticsearch服务 %s 尝试失败 用户名: %v 密码: %v 错误: %v",
target, user, pass, err)
Common.LogError(errlog)
// 检查是否需要重试
if retryErr := Common.CheckErrs(err); retryErr != nil {
if retryCount == maxRetries-1 {
continue
}
continue // 继续重试
continue
}
}
break // 如果不需要重试,跳出重试循环
break
}
}
}
@@ -121,7 +148,6 @@ func ElasticConn(info *Common.HostInfo, user string, pass string) (bool, error)
host, port := info.Host, info.Ports
timeout := time.Duration(Common.Timeout) * time.Second
// 构造请求客户端
client := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
@@ -129,39 +155,22 @@ func ElasticConn(info *Common.HostInfo, user string, pass string) (bool, error)
},
}
// 构造基础URL
baseURL := fmt.Sprintf("http://%s:%s", host, port)
// 创建请求
req, err := http.NewRequest("GET", baseURL+"/_cat/indices", nil)
if err != nil {
return false, err
}
// 如果提供了认证信息,添加Basic认证头
if user != "" || pass != "" {
auth := base64.StdEncoding.EncodeToString([]byte(user + ":" + pass))
req.Header.Add("Authorization", "Basic "+auth)
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return false, err
}
defer resp.Body.Close()
// 检查响应状态
if resp.StatusCode == 200 {
result := fmt.Sprintf("Elasticsearch服务 %v:%v ", host, port)
if user != "" {
result += fmt.Sprintf("爆破成功 用户名: %v 密码: %v", user, pass)
} else {
result += "无需认证即可访问"
}
Common.LogSuccess(result)
return true, nil
}
return false, fmt.Errorf("认证失败")
return resp.StatusCode == 200, nil
}