fscan/Core/Scanner.go

225 lines
5.3 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"
2024-12-18 22:00:18 +08:00
"github.com/shadow1ng/fscan/Common"
"github.com/shadow1ng/fscan/WebScan/lib"
2024-12-20 17:54:36 +08:00
"strconv"
2020-12-29 17:17:10 +08:00
"strings"
"sync"
)
2024-12-20 17:32:25 +08:00
// Scan 执行扫描主流程
2024-12-19 16:15:53 +08:00
func Scan(info Common.HostInfo) {
2024-12-18 15:18:58 +08:00
fmt.Println("[*] 开始信息扫描...")
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 {
2024-12-20 17:32:25 +08:00
fmt.Printf("[-] 解析主机错误: %v\n", 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 {
2024-12-18 15:18:58 +08:00
// ICMP存活性检测
2024-12-20 17:32:25 +08:00
if (Common.DisablePing == false && len(hosts) > 1) || Common.IsICMPScan() {
hosts = CheckLive(hosts, Common.UsePing)
fmt.Printf("[+] ICMP存活主机数量: %d\n", len(hosts))
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)
fmt.Printf("[+] 存活端口数量: %d\n", len(alivePorts))
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
2024-12-20 17:32:25 +08:00
fmt.Printf("[+] 总计存活端口: %d\n", 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 17:32:25 +08:00
// 准备URL扫描目标
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 {
fmt.Println("[*] 开始漏洞扫描...")
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 {
fmt.Printf("[-] 无效的目标地址格式: %s\n", targetIP)
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-20 17:32:25 +08:00
2024-12-21 18:26:44 +08:00
// 获取要执行的插件列表
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}
}
// 统一处理所有目标和插件
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)
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-21 18:26:44 +08:00
// 非本地扫描模式的常规处理
if len(plugin.Ports) > 0 {
if plugin.HasPort(targetPort) {
2024-12-20 17:54:36 +08:00
AddScan(pluginName, target, ch, wg)
}
2024-12-21 18:26:44 +08:00
} else {
AddScan(pluginName, target, ch, wg)
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.LogWG.Wait()
close(Common.Results)
fmt.Printf("[+] 扫描已完成: %v/%v\n", Common.End, Common.Num)
}
2024-12-18 15:18:58 +08:00
// Mutex用于保护共享资源的并发访问
2021-03-30 18:12:54 +08:00
var Mutex = &sync.Mutex{}
2024-12-18 15:18:58 +08:00
// AddScan 添加扫描任务到并发队列
2024-12-20 17:32:25 +08:00
func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
2024-12-18 15:18:58 +08:00
// 获取信号量,控制并发数
2022-08-16 11:18:09 +08:00
*ch <- struct{}{}
2024-12-18 15:18:58 +08:00
// 添加等待组计数
2020-12-29 17:17:10 +08:00
wg.Add(1)
2024-12-18 15:18:58 +08:00
// 启动goroutine执行扫描任务
2020-12-29 17:17:10 +08:00
go func() {
2024-12-18 15:18:58 +08:00
defer func() {
wg.Done() // 完成任务后减少等待组计数
<-*ch // 释放信号量
}()
// 增加总任务数
2021-03-30 18:12:54 +08:00
Mutex.Lock()
2024-12-18 22:00:18 +08:00
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
// 增加已完成任务数
2021-03-30 18:12:54 +08:00
Mutex.Lock()
2024-12-18 22:00:18 +08:00
Common.End += 1
2021-03-30 18:12:54 +08:00
Mutex.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 {
2024-12-21 17:21:41 +08:00
fmt.Printf("[-] 扫描错误 %v:%v - %v\n", info.Host, info.Ports, err)
2024-05-11 16:04:02 +08:00
}
}()
2024-12-18 15:18:58 +08:00
2024-12-18 21:56:08 +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 {
fmt.Printf("[*] 扫描类型 %v 无对应插件,已跳过\n", *name)
return
}
// 直接调用扫描函数
if err := plugin.ScanFunc(info); err != nil {
2024-12-21 17:21:41 +08:00
fmt.Printf("[-] 扫描错误 %v:%v - %v\n", 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
}