mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-09-14 14:06:44 +08:00

主要更改: - 统一包目录命名为小写(Core→core, Plugins→plugins, WebScan→webscan) - 更新所有import路径以符合Go语言命名规范 - 重构parsers模块,简化复杂的工厂模式(从2000+行优化至400行) - 移除i18n兼容层,统一使用模块化i18n包 - 简化Core/Manager.go架构(从591行优化至133行) - 清理冗余文件:备份文件、构建产物、测试配置、重复图片 - 移除TestDocker测试环境配置目录 - 解决变量命名冲突问题 性能优化: - 减少代码复杂度60-70% - 提升构建和运行性能 - 保持完整功能兼容性 代码质量: - 符合Go语言最佳实践 - 统一命名规范 - 优化项目结构
145 lines
3.5 KiB
Go
145 lines
3.5 KiB
Go
package Plugins
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gosnmp/gosnmp"
|
|
"github.com/shadow1ng/fscan/common"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// SNMPScan 执行SNMP服务扫描
|
|
func SNMPScan(info *common.HostInfo) (tmperr error) {
|
|
if common.DisableBrute {
|
|
return
|
|
}
|
|
|
|
maxRetries := common.MaxRetries
|
|
portNum, _ := strconv.Atoi(info.Ports)
|
|
defaultCommunities := []string{"public", "private", "cisco", "community"}
|
|
timeout := time.Duration(common.Timeout) * time.Second
|
|
target := fmt.Sprintf("%v:%v", info.Host, info.Ports)
|
|
|
|
common.LogDebug(fmt.Sprintf("开始扫描 %s", target))
|
|
common.LogDebug(fmt.Sprintf("尝试默认 community 列表 (总数: %d)", len(defaultCommunities)))
|
|
|
|
tried := 0
|
|
total := len(defaultCommunities)
|
|
|
|
for _, community := range defaultCommunities {
|
|
tried++
|
|
common.LogDebug(fmt.Sprintf("[%d/%d] 尝试 community: %s", tried, total, community))
|
|
|
|
for retryCount := 0; retryCount < maxRetries; retryCount++ {
|
|
if retryCount > 0 {
|
|
common.LogDebug(fmt.Sprintf("第%d次重试: community: %s", retryCount+1, community))
|
|
}
|
|
|
|
done := make(chan struct {
|
|
success bool
|
|
sysDesc string
|
|
err error
|
|
}, 1)
|
|
|
|
go func(community string) {
|
|
success, sysDesc, err := SNMPConnect(info, community, portNum)
|
|
select {
|
|
case done <- struct {
|
|
success bool
|
|
sysDesc string
|
|
err error
|
|
}{success, sysDesc, err}:
|
|
default:
|
|
}
|
|
}(community)
|
|
|
|
var err error
|
|
select {
|
|
case result := <-done:
|
|
err = result.err
|
|
if result.success && err == nil {
|
|
successMsg := fmt.Sprintf("SNMP服务 %s community: %v 连接成功", target, community)
|
|
if result.sysDesc != "" {
|
|
successMsg += fmt.Sprintf(" System: %v", result.sysDesc)
|
|
}
|
|
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": "snmp",
|
|
"community": community,
|
|
"type": "weak-community",
|
|
"system": result.sysDesc,
|
|
},
|
|
}
|
|
common.SaveResult(vulnResult)
|
|
return nil
|
|
}
|
|
case <-time.After(timeout):
|
|
err = fmt.Errorf("连接超时")
|
|
}
|
|
|
|
if err != nil {
|
|
errlog := fmt.Sprintf("SNMP服务 %s 尝试失败 community: %v 错误: %v",
|
|
target, community, err)
|
|
common.LogError(errlog)
|
|
|
|
if retryErr := common.CheckErrs(err); retryErr != nil {
|
|
if retryCount == maxRetries-1 {
|
|
continue
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
common.LogDebug(fmt.Sprintf("扫描完成,共尝试 %d 个 community", tried))
|
|
return tmperr
|
|
}
|
|
|
|
// SNMPConnect 尝试SNMP连接
|
|
func SNMPConnect(info *common.HostInfo, community string, portNum int) (bool, string, error) {
|
|
host := info.Host
|
|
timeout := time.Duration(common.Timeout) * time.Second
|
|
|
|
snmp := &gosnmp.GoSNMP{
|
|
Target: host,
|
|
Port: uint16(portNum),
|
|
Community: community,
|
|
Version: gosnmp.Version2c,
|
|
Timeout: timeout,
|
|
Retries: 1,
|
|
}
|
|
|
|
err := snmp.Connect()
|
|
if err != nil {
|
|
return false, "", err
|
|
}
|
|
defer snmp.Conn.Close()
|
|
|
|
oids := []string{"1.3.6.1.2.1.1.1.0"}
|
|
result, err := snmp.Get(oids)
|
|
if err != nil {
|
|
return false, "", err
|
|
}
|
|
|
|
if len(result.Variables) > 0 {
|
|
var sysDesc string
|
|
if result.Variables[0].Type != gosnmp.NoSuchObject {
|
|
sysDesc = strings.TrimSpace(string(result.Variables[0].Value.([]byte)))
|
|
}
|
|
return true, sysDesc, nil
|
|
}
|
|
|
|
return false, "", fmt.Errorf("认证失败")
|
|
}
|