fscan/Core/Scanner.go

295 lines
7.1 KiB
Go
Raw Normal View History

2024-12-19 15:24:10 +08:00
package Core
2020-12-29 17:17:10 +08:00
import (
"fmt"
"github.com/schollz/progressbar/v3"
2024-12-18 22:00:18 +08:00
"github.com/shadow1ng/fscan/Common"
"github.com/shadow1ng/fscan/WebScan/lib"
"sort"
2024-12-20 17:54:36 +08:00
"strconv"
2020-12-29 17:17:10 +08:00
"strings"
"sync"
"sync/atomic"
"time"
2020-12-29 17:17:10 +08:00
)
2024-12-20 17:32:25 +08:00
// Scan 执行扫描主流程
2024-12-19 16:15:53 +08:00
func Scan(info Common.HostInfo) {
Common.LogInfo("开始信息扫描")
2024-12-20 17:32:25 +08:00
Common.ParseScanMode(Common.ScanMode)
ch := make(chan struct{}, Common.ThreadNum)
wg := sync.WaitGroup{}
// 本地信息收集模式
2024-12-21 18:26:44 +08:00
if Common.LocalScan {
2024-12-20 17:32:25 +08:00
executeScans([]Common.HostInfo{info}, &ch, &wg)
finishScan(&wg)
2024-12-18 15:18:58 +08:00
return
}
2024-12-20 17:32:25 +08:00
// 初始化并解析目标
hosts, err := Common.ParseIP(info.Host, Common.HostsFile, Common.ExcludeHosts)
2021-12-01 15:22:48 +08:00
if err != nil {
Common.LogError(fmt.Sprintf("解析主机错误: %v", err))
2021-12-01 15:22:48 +08:00
return
}
2023-11-15 10:40:17 +08:00
lib.Inithttp()
2024-12-18 15:18:58 +08:00
2024-12-20 17:32:25 +08:00
// 执行目标扫描
executeScan(hosts, info, &ch, &wg)
finishScan(&wg)
}
// executeScan 执行主扫描流程
func executeScan(hosts []string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
var targetInfos []Common.HostInfo
if len(hosts) > 0 || len(Common.HostPort) > 0 {
if (Common.DisablePing == false && len(hosts) > 1) || Common.IsICMPScan() {
hosts = CheckLive(hosts, Common.UsePing)
Common.LogInfo(fmt.Sprintf("存活主机数量: %d", len(hosts)))
2024-12-20 17:32:25 +08:00
if Common.IsICMPScan() {
2024-12-18 15:18:58 +08:00
return
}
2021-03-05 11:44:21 +08:00
}
2024-12-18 15:18:58 +08:00
2024-12-20 17:32:25 +08:00
var alivePorts []string
if Common.IsWebScan() {
alivePorts = NoPortScan(hosts, Common.Ports)
} else if len(hosts) > 0 {
alivePorts = PortScan(hosts, Common.Ports, Common.Timeout)
Common.LogInfo(fmt.Sprintf("存活端口数量: %d", len(alivePorts)))
2024-12-20 17:32:25 +08:00
if Common.IsPortScan() {
return
}
}
2024-12-18 15:18:58 +08:00
2024-12-18 22:00:18 +08:00
if len(Common.HostPort) > 0 {
2024-12-20 17:32:25 +08:00
alivePorts = append(alivePorts, Common.HostPort...)
alivePorts = Common.RemoveDuplicate(alivePorts)
2024-12-18 22:00:18 +08:00
Common.HostPort = nil
Common.LogInfo(fmt.Sprintf("存活端口数量: %d", len(alivePorts)))
}
2024-12-18 15:18:58 +08:00
2024-12-20 17:32:25 +08:00
targetInfos = prepareTargetInfos(alivePorts, info)
2020-12-29 17:17:10 +08:00
}
2024-12-18 15:18:58 +08:00
2024-12-20 03:46:09 +08:00
for _, url := range Common.URLs {
2024-12-20 17:32:25 +08:00
urlInfo := info
urlInfo.Url = url
targetInfos = append(targetInfos, urlInfo)
}
2024-12-18 15:18:58 +08:00
2024-12-20 17:32:25 +08:00
if len(targetInfos) > 0 {
Common.LogInfo("开始漏洞扫描")
2024-12-20 17:32:25 +08:00
executeScans(targetInfos, ch, wg)
}
2020-12-29 17:17:10 +08:00
}
2024-12-20 17:32:25 +08:00
// prepareTargetInfos 准备扫描目标信息
func prepareTargetInfos(alivePorts []string, baseInfo Common.HostInfo) []Common.HostInfo {
var infos []Common.HostInfo
for _, targetIP := range alivePorts {
hostParts := strings.Split(targetIP, ":")
if len(hostParts) != 2 {
Common.LogError(fmt.Sprintf("无效的目标地址格式: %s", targetIP))
2024-12-20 17:32:25 +08:00
continue
2024-12-18 21:56:08 +08:00
}
2024-12-20 17:32:25 +08:00
info := baseInfo
info.Host = hostParts[0]
info.Ports = hostParts[1]
infos = append(infos, info)
2024-12-18 21:56:08 +08:00
}
2024-12-20 17:32:25 +08:00
return infos
2024-12-18 21:56:08 +08:00
}
2024-12-20 17:32:25 +08:00
func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
mode := Common.GetScanMode()
2024-12-21 18:26:44 +08:00
var pluginsToRun []string
2024-12-22 02:58:55 +08:00
isSinglePlugin := false
2024-12-20 17:32:25 +08:00
if plugins := Common.GetPluginsForMode(mode); plugins != nil {
2024-12-21 18:26:44 +08:00
pluginsToRun = plugins
} else {
pluginsToRun = []string{mode}
2024-12-22 02:58:55 +08:00
isSinglePlugin = true
2024-12-21 18:26:44 +08:00
}
loadedPlugins := make([]string, 0)
// 先遍历一遍计算实际要执行的任务数
actualTasks := 0
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 {
actualTasks++
}
continue
}
if isSinglePlugin {
actualTasks++
continue
}
if len(plugin.Ports) > 0 {
if plugin.HasPort(targetPort) {
actualTasks++
}
} else {
actualTasks++
}
}
}
// 初始化进度条
Common.ProgressBar = progressbar.NewOptions(actualTasks,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionShowCount(),
progressbar.OptionSetWidth(15),
progressbar.OptionSetDescription("[cyan]扫描进度:[reset]"),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "[green]=[reset]",
SaucerHead: "[green]>[reset]",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionUseANSICodes(true),
progressbar.OptionSetRenderBlankState(true),
// 设置更新频率
progressbar.OptionThrottle(65*time.Millisecond),
)
// 确保进度条首次渲染
Common.ProgressBar.RenderBlank()
time.Sleep(100 * time.Millisecond) // 给进度条一个短暂的初始化时间
2024-12-21 18:26:44 +08:00
for _, target := range targets {
targetPort, _ := strconv.Atoi(target.Ports)
for _, pluginName := range pluginsToRun {
plugin, exists := Common.PluginManager[pluginName]
if !exists {
Common.LogError(fmt.Sprintf("插件 %s 不存在", pluginName))
2024-12-21 18:26:44 +08:00
continue
}
if Common.LocalScan {
if len(plugin.Ports) == 0 {
loadedPlugins = append(loadedPlugins, pluginName)
2024-12-21 18:26:44 +08:00
AddScan(pluginName, target, ch, wg)
2024-12-20 17:54:36 +08:00
}
2024-12-21 18:26:44 +08:00
continue
}
2024-12-20 17:54:36 +08:00
2024-12-22 02:58:55 +08:00
if isSinglePlugin {
loadedPlugins = append(loadedPlugins, pluginName)
2024-12-22 02:58:55 +08:00
AddScan(pluginName, target, ch, wg)
continue
}
2024-12-21 18:26:44 +08:00
if len(plugin.Ports) > 0 {
if plugin.HasPort(targetPort) {
loadedPlugins = append(loadedPlugins, pluginName)
2024-12-20 17:54:36 +08:00
AddScan(pluginName, target, ch, wg)
}
2024-12-21 18:26:44 +08:00
} else {
loadedPlugins = append(loadedPlugins, pluginName)
2024-12-21 18:26:44 +08:00
AddScan(pluginName, target, ch, wg)
2024-12-18 21:56:08 +08:00
}
}
}
// 去重并输出实际加载的插件
uniquePlugins := make(map[string]struct{})
for _, p := range loadedPlugins {
uniquePlugins[p] = struct{}{}
}
finalPlugins := make([]string, 0, len(uniquePlugins))
for p := range uniquePlugins {
finalPlugins = append(finalPlugins, p)
}
sort.Strings(finalPlugins)
Common.LogInfo(fmt.Sprintf("加载的插件: %s", strings.Join(finalPlugins, ", ")))
2024-12-18 21:56:08 +08:00
}
2024-12-20 17:32:25 +08:00
// finishScan 完成扫描任务
func finishScan(wg *sync.WaitGroup) {
wg.Wait()
// 确保进度条完成
Common.ProgressBar.Finish()
fmt.Println() // 添加一个换行
Common.LogSuccess(fmt.Sprintf("扫描已完成: %v/%v", Common.End, Common.Num))
2024-12-20 17:32:25 +08:00
}
2024-12-18 15:18:58 +08:00
// Mutex用于保护共享资源的并发访问
2021-03-30 18:12:54 +08:00
var Mutex = &sync.Mutex{}
// AddScan 也需要修改
2024-12-20 17:32:25 +08:00
func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
2022-08-16 11:18:09 +08:00
*ch <- struct{}{}
2020-12-29 17:17:10 +08:00
wg.Add(1)
2024-12-18 15:18:58 +08:00
2020-12-29 17:17:10 +08:00
go func() {
2024-12-18 15:18:58 +08:00
defer func() {
wg.Done()
<-*ch
2024-12-18 15:18:58 +08:00
}()
2021-03-30 18:12:54 +08:00
Mutex.Lock()
atomic.AddInt64(&Common.Num, 1)
2021-03-30 18:12:54 +08:00
Mutex.Unlock()
2024-12-18 15:18:58 +08:00
2024-12-20 17:32:25 +08:00
ScanFunc(&plugin, &info)
2024-12-18 15:18:58 +08:00
Common.OutputMutex.Lock()
atomic.AddInt64(&Common.End, 1)
if Common.ProgressBar != nil {
// 清除当前行
fmt.Print("\033[2K\r")
Common.ProgressBar.Add(1)
}
Common.OutputMutex.Unlock()
2020-12-29 17:17:10 +08:00
}()
}
2024-12-18 21:56:08 +08:00
// ScanFunc 执行扫描插件
2024-12-19 16:15:53 +08:00
func ScanFunc(name *string, info *Common.HostInfo) {
2024-05-11 16:04:02 +08:00
defer func() {
if err := recover(); err != nil {
Common.LogError(fmt.Sprintf("扫描错误 %v:%v - %v", info.Host, info.Ports, err))
2024-05-11 16:04:02 +08:00
}
}()
2024-12-18 15:18:58 +08:00
2024-12-19 16:15:53 +08:00
plugin, exists := Common.PluginManager[*name]
2024-12-18 21:56:08 +08:00
if !exists {
Common.LogInfo(fmt.Sprintf("扫描类型 %v 无对应插件,已跳过", *name))
2024-12-18 21:56:08 +08:00
return
}
if err := plugin.ScanFunc(info); err != nil {
Common.LogError(fmt.Sprintf("扫描错误 %v:%v - %v", info.Host, info.Ports, err))
2024-12-18 21:56:08 +08:00
}
2020-12-29 17:17:10 +08:00
}
2024-12-18 15:18:58 +08:00
// IsContain 检查切片中是否包含指定元素
2020-12-29 17:17:10 +08:00
func IsContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}