mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-23 11:41:53 +08:00
refactor: 输出格式重构,去掉所有插件的多线程,因为多线程会导致结果不准确,加入进度条
This commit is contained in:
+6
-6
@@ -57,7 +57,7 @@ func handleAliveHosts(chanHosts chan string, hostslist []string, isPing bool) {
|
||||
if isPing {
|
||||
protocol = "PING"
|
||||
}
|
||||
fmt.Printf("[+] 目标 %-15s 存活 (%s)\n", ip, protocol)
|
||||
fmt.Printf("目标 %-15s 存活 (%s)\n", ip, protocol)
|
||||
}
|
||||
|
||||
AliveHosts = append(AliveHosts, ip)
|
||||
@@ -76,7 +76,7 @@ func probeWithICMP(hostslist []string, chanHosts chan string) {
|
||||
}
|
||||
|
||||
Common.LogError(fmt.Sprintf("ICMP监听失败: %v", err))
|
||||
fmt.Println("[-] 正在尝试无监听ICMP探测...")
|
||||
fmt.Println("正在尝试无监听ICMP探测...")
|
||||
|
||||
// 尝试无监听ICMP探测
|
||||
conn2, err := net.DialTimeout("ip4:icmp", "127.0.0.1", 3*time.Second)
|
||||
@@ -87,8 +87,8 @@ func probeWithICMP(hostslist []string, chanHosts chan string) {
|
||||
}
|
||||
|
||||
Common.LogError(fmt.Sprintf("ICMP连接失败: %v", err))
|
||||
fmt.Println("[-] 当前用户权限不足,无法发送ICMP包")
|
||||
fmt.Println("[*] 切换为PING方式探测...")
|
||||
fmt.Println("当前用户权限不足,无法发送ICMP包")
|
||||
fmt.Println("切换为PING方式探测...")
|
||||
|
||||
// 降级使用ping探测
|
||||
RunPing(hostslist, chanHosts)
|
||||
@@ -100,7 +100,7 @@ func printAliveStats(hostslist []string) {
|
||||
if len(hostslist) > 1000 {
|
||||
arrTop, arrLen := ArrayCountValueTop(AliveHosts, Common.LiveTop, true)
|
||||
for i := 0; i < len(arrTop); i++ {
|
||||
output := fmt.Sprintf("[*] B段 %-16s 存活主机数: %d", arrTop[i]+".0.0/16", arrLen[i])
|
||||
output := fmt.Sprintf("B段 %-16s 存活主机数: %d", arrTop[i]+".0.0/16", arrLen[i])
|
||||
Common.LogSuccess(output)
|
||||
}
|
||||
}
|
||||
@@ -109,7 +109,7 @@ func printAliveStats(hostslist []string) {
|
||||
if len(hostslist) > 256 {
|
||||
arrTop, arrLen := ArrayCountValueTop(AliveHosts, Common.LiveTop, false)
|
||||
for i := 0; i < len(arrTop); i++ {
|
||||
output := fmt.Sprintf("[*] C段 %-16s 存活主机数: %d", arrTop[i]+".0/24", arrLen[i])
|
||||
output := fmt.Sprintf("C段 %-16s 存活主机数: %d", arrTop[i]+".0/24", arrLen[i])
|
||||
Common.LogSuccess(output)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-7
@@ -81,7 +81,7 @@ func init() {
|
||||
|
||||
Common.RegisterPlugin("activemq", Common.ScanPlugin{
|
||||
Name: "ActiveMQ",
|
||||
Ports: []int{61616, 61613},
|
||||
Ports: []int{61613},
|
||||
ScanFunc: Plugins.ActiveMQScan,
|
||||
})
|
||||
|
||||
@@ -115,12 +115,6 @@ func init() {
|
||||
ScanFunc: Plugins.SNMPScan,
|
||||
})
|
||||
|
||||
Common.RegisterPlugin("zabbix", Common.ScanPlugin{
|
||||
Name: "Zabbix",
|
||||
Ports: []int{80, 443, 8080, 8443, 10051}, // Zabbix常用端口
|
||||
ScanFunc: Plugins.ZabbixScan,
|
||||
})
|
||||
|
||||
Common.RegisterPlugin("modbus", Common.ScanPlugin{
|
||||
Name: "Modbus",
|
||||
Ports: []int{502, 5020}, // Modbus 默认端口
|
||||
|
||||
+91
-17
@@ -2,12 +2,15 @@ package Core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
"github.com/shadow1ng/fscan/Common"
|
||||
"github.com/shadow1ng/fscan/WebScan/lib"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Scan 执行扫描主流程
|
||||
@@ -108,13 +111,68 @@ func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGro
|
||||
|
||||
if plugins := Common.GetPluginsForMode(mode); plugins != nil {
|
||||
pluginsToRun = plugins
|
||||
Common.LogInfo(fmt.Sprintf("加载插件组: %s", mode))
|
||||
} else {
|
||||
pluginsToRun = []string{mode}
|
||||
isSinglePlugin = true
|
||||
Common.LogInfo(fmt.Sprintf("使用单个插件: %s", mode))
|
||||
}
|
||||
|
||||
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) // 给进度条一个短暂的初始化时间
|
||||
|
||||
for _, target := range targets {
|
||||
targetPort, _ := strconv.Atoi(target.Ports)
|
||||
|
||||
@@ -127,66 +185,82 @@ func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGro
|
||||
|
||||
if Common.LocalScan {
|
||||
if len(plugin.Ports) == 0 {
|
||||
loadedPlugins = append(loadedPlugins, pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if isSinglePlugin {
|
||||
loadedPlugins = append(loadedPlugins, pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(plugin.Ports) > 0 {
|
||||
if plugin.HasPort(targetPort) {
|
||||
loadedPlugins = append(loadedPlugins, pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
} else {
|
||||
loadedPlugins = append(loadedPlugins, pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 去重并输出实际加载的插件
|
||||
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, ", ")))
|
||||
}
|
||||
|
||||
// finishScan 完成扫描任务
|
||||
func finishScan(wg *sync.WaitGroup) {
|
||||
wg.Wait()
|
||||
// 先发送最后的成功消息
|
||||
// 确保进度条完成
|
||||
Common.ProgressBar.Finish()
|
||||
fmt.Println() // 添加一个换行
|
||||
Common.LogSuccess(fmt.Sprintf("扫描已完成: %v/%v", Common.End, Common.Num))
|
||||
// 等待日志处理完成后再关闭通道
|
||||
Common.LogWG.Wait()
|
||||
close(Common.Results)
|
||||
}
|
||||
|
||||
// Mutex用于保护共享资源的并发访问
|
||||
var Mutex = &sync.Mutex{}
|
||||
|
||||
// AddScan 添加扫描任务到并发队列
|
||||
// AddScan 也需要修改
|
||||
func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.WaitGroup) {
|
||||
// 获取信号量,控制并发数
|
||||
*ch <- struct{}{}
|
||||
// 添加等待组计数
|
||||
wg.Add(1)
|
||||
|
||||
// 启动goroutine执行扫描任务
|
||||
go func() {
|
||||
defer func() {
|
||||
wg.Done() // 完成任务后减少等待组计数
|
||||
<-*ch // 释放信号量
|
||||
wg.Done()
|
||||
<-*ch
|
||||
}()
|
||||
|
||||
// 增加总任务数
|
||||
Mutex.Lock()
|
||||
atomic.AddInt64(&Common.Num, 1)
|
||||
Mutex.Unlock()
|
||||
|
||||
// 执行扫描
|
||||
ScanFunc(&plugin, &info)
|
||||
|
||||
// 增加已完成任务数
|
||||
Mutex.Lock()
|
||||
Common.OutputMutex.Lock()
|
||||
atomic.AddInt64(&Common.End, 1)
|
||||
Mutex.Unlock()
|
||||
if Common.ProgressBar != nil {
|
||||
// 清除当前行
|
||||
fmt.Print("\033[2K\r")
|
||||
Common.ProgressBar.Add(1)
|
||||
}
|
||||
Common.OutputMutex.Unlock()
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user