refactor: 重构扫描模式逻辑

This commit is contained in:
ZacharyZcR
2024-12-21 18:26:44 +08:00
parent d192b7fc2a
commit c5dcf2c633
5 changed files with 62 additions and 26 deletions
+4 -1
View File
@@ -20,7 +20,7 @@ func init() {
})
Common.RegisterPlugin("telnet", Common.ScanPlugin{
Name: "TELNET",
Name: "Telnet",
Ports: []int{23},
ScanFunc: Plugins.TelnetScan,
})
@@ -116,13 +116,16 @@ func init() {
ScanFunc: Plugins.SmbGhost,
})
// web 相关插件添加 WebPorts 配置
Common.RegisterPlugin("web", Common.ScanPlugin{
Name: "WebTitle",
Ports: Common.ParsePortsFromString(Common.WebPorts), // 将 WebPorts 字符串解析为端口数组
ScanFunc: Plugins.WebTitle,
})
Common.RegisterPlugin("webpoc", Common.ScanPlugin{
Name: "WebPoc",
Ports: Common.ParsePortsFromString(Common.WebPorts), // 将 WebPorts 字符串解析为端口数组
ScanFunc: Plugins.WebPoc,
})
+34 -25
View File
@@ -19,7 +19,7 @@ func Scan(info Common.HostInfo) {
wg := sync.WaitGroup{}
// 本地信息收集模式
if Common.IsLocalScan() {
if Common.LocalScan {
executeScans([]Common.HostInfo{info}, &ch, &wg)
finishScan(&wg)
return
@@ -107,38 +107,47 @@ func prepareTargetInfos(alivePorts []string, baseInfo Common.HostInfo) []Common.
return infos
}
// executeScans 统一执行扫描任务
func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
mode := Common.GetScanMode()
var pluginsToRun []string
// 判断是否是预设模式(大写开头)
// 获取要执行的插件列表
if plugins := Common.GetPluginsForMode(mode); plugins != nil {
// 使用预设模式的插件组
for _, target := range targets {
targetPort, _ := strconv.Atoi(target.Ports) // 转换目标端口为整数
for _, pluginName := range plugins {
// 获取插件信息
plugin, exists := Common.PluginManager[pluginName]
if !exists {
continue
}
// 预设模式下使用配置的插件组
pluginsToRun = plugins
} else {
// 单插件模式下只包含指定的插件
pluginsToRun = []string{mode}
}
// 检查插件是否有默认端口配置
if len(plugin.Ports) > 0 {
// 只有当目标端口在插件支持的端口列表中才执行
if plugin.HasPort(targetPort) {
AddScan(pluginName, target, ch, wg)
}
} else {
// 对于没有指定端口的插件,始终执行
// 统一处理所有目标和插件
for _, target := range targets {
targetPort, _ := strconv.Atoi(target.Ports)
for _, pluginName := range pluginsToRun {
// 获取插件信息
plugin, exists := Common.PluginManager[pluginName]
if !exists {
continue
}
// 本地扫描模式的特殊处理
if Common.LocalScan {
// 只执行没有端口配置的插件
if len(plugin.Ports) == 0 {
AddScan(pluginName, target, ch, wg)
}
continue
}
// 非本地扫描模式的常规处理
if len(plugin.Ports) > 0 {
if plugin.HasPort(targetPort) {
AddScan(pluginName, target, ch, wg)
}
} else {
AddScan(pluginName, target, ch, wg)
}
}
} else {
// 使用单个插件模式,直接执行不做端口检查
for _, target := range targets {
AddScan(mode, target, ch, wg)
}
}
}