mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-22 03:10:42 +08:00
refactor: 输出格式重构,重构SMB、SMB2、FTP的一些验证逻辑
This commit is contained in:
+3
-3
@@ -75,7 +75,7 @@ func probeWithICMP(hostslist []string, chanHosts chan string) {
|
||||
return
|
||||
}
|
||||
|
||||
Common.LogError(err)
|
||||
Common.LogError(fmt.Sprintf("ICMP监听失败: %v", err))
|
||||
fmt.Println("[-] 正在尝试无监听ICMP探测...")
|
||||
|
||||
// 尝试无监听ICMP探测
|
||||
@@ -86,7 +86,7 @@ func probeWithICMP(hostslist []string, chanHosts chan string) {
|
||||
return
|
||||
}
|
||||
|
||||
Common.LogError(err)
|
||||
Common.LogError(fmt.Sprintf("ICMP连接失败: %v", err))
|
||||
fmt.Println("[-] 当前用户权限不足,无法发送ICMP包")
|
||||
fmt.Println("[*] 切换为PING方式探测...")
|
||||
|
||||
@@ -266,7 +266,7 @@ func ExecCommandPing(ip string) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var command *exec.Cmd
|
||||
// 根据操作系统选择不同的ping命令
|
||||
switch runtime.GOOS {
|
||||
|
||||
+16
-60
@@ -3,13 +3,11 @@ package Core
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"github.com/Ullaakut/nmap"
|
||||
"github.com/google/gopacket"
|
||||
"github.com/google/gopacket/layers"
|
||||
"github.com/google/gopacket/pcap"
|
||||
"github.com/shadow1ng/fscan/Common"
|
||||
"golang.org/x/net/ipv4"
|
||||
"log"
|
||||
"net"
|
||||
"runtime"
|
||||
"sort"
|
||||
@@ -25,12 +23,12 @@ type Addr struct {
|
||||
|
||||
func PortScan(hostslist []string, ports string, timeout int64) []string {
|
||||
var AliveAddress []string
|
||||
var mu sync.Mutex // 添加互斥锁保护 AliveAddress
|
||||
var mu sync.Mutex
|
||||
|
||||
// 解析端口列表
|
||||
probePorts := Common.ParsePort(ports)
|
||||
if len(probePorts) == 0 {
|
||||
fmt.Printf("[-] 端口格式错误: %s, 请检查端口格式\n", ports)
|
||||
Common.LogError(fmt.Sprintf("端口格式错误: %s", ports))
|
||||
return AliveAddress
|
||||
}
|
||||
|
||||
@@ -75,12 +73,11 @@ func PortScan(hostslist []string, ports string, timeout int64) []string {
|
||||
}
|
||||
}
|
||||
|
||||
// 按顺序关闭并等待
|
||||
close(addrs)
|
||||
workerWg.Wait() // 等待所有扫描worker完成
|
||||
wg.Wait() // 等待所有扫描任务完成
|
||||
close(results) // 关闭结果通道
|
||||
resultWg.Wait() // 等待结果处理完成
|
||||
workerWg.Wait()
|
||||
wg.Wait()
|
||||
close(results)
|
||||
resultWg.Wait()
|
||||
|
||||
return AliveAddress
|
||||
}
|
||||
@@ -111,10 +108,7 @@ func PortConnect(addr Addr, respondingHosts chan<- string, timeout int64, wg *sy
|
||||
|
||||
// 记录开放端口
|
||||
address := fmt.Sprintf("%s:%d", addr.ip, addr.port)
|
||||
protocol := "TCP"
|
||||
result := fmt.Sprintf("[+] %s端口开放 %s", protocol, address)
|
||||
Common.LogSuccess(result)
|
||||
|
||||
Common.LogSuccess(fmt.Sprintf("端口开放 %s", address))
|
||||
respondingHosts <- address
|
||||
}
|
||||
|
||||
@@ -168,30 +162,27 @@ func SynScan(ip string, port int, timeout int64) (bool, error) {
|
||||
|
||||
sendConn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("创建发送套接字失败: %v", err)
|
||||
return false, fmt.Errorf("发送套接字错误: %v", err)
|
||||
}
|
||||
defer sendConn.Close()
|
||||
|
||||
rawConn, err := ipv4.NewRawConn(sendConn)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("获取原始连接失败: %v", err)
|
||||
return false, fmt.Errorf("原始连接错误: %v", err)
|
||||
}
|
||||
|
||||
dstIP := net.ParseIP(ip)
|
||||
if dstIP == nil {
|
||||
return false, fmt.Errorf("无效的IP地址: %s", ip)
|
||||
return false, fmt.Errorf("IP地址无效: %s", ip)
|
||||
}
|
||||
|
||||
// 打开正确的网络接口
|
||||
handle, err := pcap.OpenLive(ifName, 65536, true, pcap.BlockForever)
|
||||
if err != nil {
|
||||
// 如果失败,尝试查找可用接口
|
||||
ifaces, err := pcap.FindAllDevs()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("无法找到网络接口: %v", err)
|
||||
return false, fmt.Errorf("网络接口错误: %v", err)
|
||||
}
|
||||
|
||||
// 遍历查找可用接口
|
||||
var found bool
|
||||
for _, iface := range ifaces {
|
||||
handle, err = pcap.OpenLive(iface.Name, 65536, true, pcap.BlockForever)
|
||||
@@ -202,7 +193,7 @@ func SynScan(ip string, port int, timeout int64) (bool, error) {
|
||||
}
|
||||
|
||||
if !found {
|
||||
return false, fmt.Errorf("无法打开任何网络接口")
|
||||
return false, fmt.Errorf("未找到可用网络接口")
|
||||
}
|
||||
}
|
||||
defer handle.Close()
|
||||
@@ -210,9 +201,10 @@ func SynScan(ip string, port int, timeout int64) (bool, error) {
|
||||
srcPort := 12345 + port
|
||||
filter := fmt.Sprintf("tcp and src port %d and dst port %d", port, srcPort)
|
||||
if err := handle.SetBPFFilter(filter); err != nil {
|
||||
return false, fmt.Errorf("设置过滤器失败: %v", err)
|
||||
return false, fmt.Errorf("过滤器错误: %v", err)
|
||||
}
|
||||
|
||||
// TCP头部设置保持不变
|
||||
tcpHeader := &ipv4.Header{
|
||||
Version: 4,
|
||||
Len: 20,
|
||||
@@ -222,6 +214,7 @@ func SynScan(ip string, port int, timeout int64) (bool, error) {
|
||||
Dst: dstIP,
|
||||
}
|
||||
|
||||
// SYN包构造保持不变
|
||||
synPacket := make([]byte, 20)
|
||||
binary.BigEndian.PutUint16(synPacket[0:2], uint16(srcPort))
|
||||
binary.BigEndian.PutUint16(synPacket[2:4], uint16(port))
|
||||
@@ -237,7 +230,7 @@ func SynScan(ip string, port int, timeout int64) (bool, error) {
|
||||
binary.BigEndian.PutUint16(synPacket[16:18], checksum)
|
||||
|
||||
if err := rawConn.WriteTo(tcpHeader, synPacket, nil); err != nil {
|
||||
return false, fmt.Errorf("发送SYN包失败: %v", err)
|
||||
return false, fmt.Errorf("SYN包发送错误: %v", err)
|
||||
}
|
||||
|
||||
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
|
||||
@@ -311,43 +304,6 @@ func calculateTCPChecksum(tcpHeader []byte, srcIP, dstIP net.IP) uint16 {
|
||||
return ^uint16(sum)
|
||||
}
|
||||
|
||||
func UDPScan(ip string, port int, timeout int64) (bool, error) {
|
||||
// 构造端口字符串
|
||||
portStr := fmt.Sprintf("%d", port)
|
||||
|
||||
// 配置nmap扫描
|
||||
scanner, err := nmap.NewScanner(
|
||||
nmap.WithTargets(ip),
|
||||
nmap.WithPorts(portStr),
|
||||
nmap.WithUDPScan(),
|
||||
nmap.WithTimingTemplate(nmap.TimingAggressive),
|
||||
)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("创建扫描器失败: %v", err)
|
||||
}
|
||||
|
||||
// 执行扫描
|
||||
result, warnings, err := scanner.Run()
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("扫描执行失败: %v", err)
|
||||
}
|
||||
if warnings != nil {
|
||||
log.Printf("扫描警告: %v", warnings)
|
||||
}
|
||||
|
||||
// 检查结果
|
||||
for _, host := range result.Hosts {
|
||||
for _, p := range host.Ports {
|
||||
if int(p.ID) == port &&
|
||||
(p.State.State == "open" || p.State.State == "open|filtered") {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// 获取系统对应的接口名
|
||||
func getInterfaceName() string {
|
||||
switch runtime.GOOS {
|
||||
|
||||
+19
-37
@@ -7,12 +7,12 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Scan 执行扫描主流程
|
||||
func Scan(info Common.HostInfo) {
|
||||
fmt.Println("[*] 开始信息扫描...")
|
||||
|
||||
Common.LogInfo("开始信息扫描")
|
||||
Common.ParseScanMode(Common.ScanMode)
|
||||
|
||||
ch := make(chan struct{}, Common.ThreadNum)
|
||||
@@ -28,7 +28,7 @@ func Scan(info Common.HostInfo) {
|
||||
// 初始化并解析目标
|
||||
hosts, err := Common.ParseIP(info.Host, Common.HostsFile, Common.ExcludeHosts)
|
||||
if err != nil {
|
||||
fmt.Printf("[-] 解析主机错误: %v\n", err)
|
||||
Common.LogError(fmt.Sprintf("解析主机错误: %v", err))
|
||||
return
|
||||
}
|
||||
lib.Inithttp()
|
||||
@@ -42,50 +42,44 @@ func Scan(info Common.HostInfo) {
|
||||
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 {
|
||||
// ICMP存活性检测
|
||||
if (Common.DisablePing == false && len(hosts) > 1) || Common.IsICMPScan() {
|
||||
hosts = CheckLive(hosts, Common.UsePing)
|
||||
fmt.Printf("[+] ICMP存活主机数量: %d\n", len(hosts))
|
||||
Common.LogInfo(fmt.Sprintf("存活主机数量: %d", len(hosts)))
|
||||
if Common.IsICMPScan() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 获取存活端口
|
||||
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))
|
||||
Common.LogInfo(fmt.Sprintf("存活端口数量: %d", len(alivePorts)))
|
||||
if Common.IsPortScan() {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 处理自定义端口
|
||||
if len(Common.HostPort) > 0 {
|
||||
alivePorts = append(alivePorts, Common.HostPort...)
|
||||
alivePorts = Common.RemoveDuplicate(alivePorts)
|
||||
Common.HostPort = nil
|
||||
fmt.Printf("[+] 存活端口数量: %d\n", len(alivePorts))
|
||||
Common.LogInfo(fmt.Sprintf("存活端口数量: %d", len(alivePorts)))
|
||||
}
|
||||
|
||||
targetInfos = prepareTargetInfos(alivePorts, info)
|
||||
}
|
||||
|
||||
// 准备URL扫描目标
|
||||
for _, url := range Common.URLs {
|
||||
urlInfo := info
|
||||
urlInfo.Url = url
|
||||
targetInfos = append(targetInfos, urlInfo)
|
||||
}
|
||||
|
||||
// 执行扫描任务
|
||||
if len(targetInfos) > 0 {
|
||||
fmt.Println("[*] 开始漏洞扫描...")
|
||||
Common.LogInfo("开始漏洞扫描")
|
||||
executeScans(targetInfos, ch, wg)
|
||||
}
|
||||
}
|
||||
@@ -96,7 +90,7 @@ func prepareTargetInfos(alivePorts []string, baseInfo Common.HostInfo) []Common.
|
||||
for _, targetIP := range alivePorts {
|
||||
hostParts := strings.Split(targetIP, ":")
|
||||
if len(hostParts) != 2 {
|
||||
fmt.Printf("[-] 无效的目标地址格式: %s\n", targetIP)
|
||||
Common.LogError(fmt.Sprintf("无效的目标地址格式: %s", targetIP))
|
||||
continue
|
||||
}
|
||||
info := baseInfo
|
||||
@@ -112,54 +106,42 @@ func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGro
|
||||
var pluginsToRun []string
|
||||
isSinglePlugin := false
|
||||
|
||||
// 获取要执行的插件列表
|
||||
if plugins := Common.GetPluginsForMode(mode); plugins != nil {
|
||||
// 预设模式下使用配置的插件组
|
||||
pluginsToRun = plugins
|
||||
fmt.Printf("[*] 正在加载插件组: %s\n", mode)
|
||||
Common.LogInfo(fmt.Sprintf("加载插件组: %s", mode))
|
||||
} else {
|
||||
// 单插件模式
|
||||
pluginsToRun = []string{mode}
|
||||
isSinglePlugin = true
|
||||
fmt.Printf("[*] 正在加载单插件: %s\n", mode)
|
||||
Common.LogInfo(fmt.Sprintf("使用单个插件: %s", mode))
|
||||
}
|
||||
|
||||
// 统一处理所有目标和插件
|
||||
for _, target := range targets {
|
||||
targetPort, _ := strconv.Atoi(target.Ports)
|
||||
|
||||
for _, pluginName := range pluginsToRun {
|
||||
// 获取插件信息
|
||||
plugin, exists := Common.PluginManager[pluginName]
|
||||
if !exists {
|
||||
fmt.Printf("[-] 插件 %s 不存在\n", pluginName)
|
||||
Common.LogError(fmt.Sprintf("插件 %s 不存在", pluginName))
|
||||
continue
|
||||
}
|
||||
|
||||
// 本地扫描模式的特殊处理
|
||||
if Common.LocalScan {
|
||||
if len(plugin.Ports) == 0 {
|
||||
fmt.Printf("[+] 载入插件: %s\n", pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// 单插件模式直接执行,不检查端口
|
||||
if isSinglePlugin {
|
||||
fmt.Printf("[+] 载入插件: %s\n", pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
continue
|
||||
}
|
||||
|
||||
// 预设模式下的常规处理
|
||||
if len(plugin.Ports) > 0 {
|
||||
if plugin.HasPort(targetPort) {
|
||||
fmt.Printf("[+] 载入插件: %s (端口: %d)\n", pluginName, targetPort)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[+] 载入插件: %s\n", pluginName)
|
||||
AddScan(pluginName, target, ch, wg)
|
||||
}
|
||||
}
|
||||
@@ -169,9 +151,11 @@ func executeScans(targets []Common.HostInfo, ch *chan struct{}, wg *sync.WaitGro
|
||||
// finishScan 完成扫描任务
|
||||
func finishScan(wg *sync.WaitGroup) {
|
||||
wg.Wait()
|
||||
// 先发送最后的成功消息
|
||||
Common.LogSuccess(fmt.Sprintf("扫描已完成: %v/%v", Common.End, Common.Num))
|
||||
// 等待日志处理完成后再关闭通道
|
||||
Common.LogWG.Wait()
|
||||
close(Common.Results)
|
||||
fmt.Printf("[+] 扫描已完成: %v/%v\n", Common.End, Common.Num)
|
||||
}
|
||||
|
||||
// Mutex用于保护共享资源的并发访问
|
||||
@@ -193,7 +177,7 @@ func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.Wa
|
||||
|
||||
// 增加总任务数
|
||||
Mutex.Lock()
|
||||
Common.Num += 1
|
||||
atomic.AddInt64(&Common.Num, 1)
|
||||
Mutex.Unlock()
|
||||
|
||||
// 执行扫描
|
||||
@@ -201,7 +185,7 @@ func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.Wa
|
||||
|
||||
// 增加已完成任务数
|
||||
Mutex.Lock()
|
||||
Common.End += 1
|
||||
atomic.AddInt64(&Common.End, 1)
|
||||
Mutex.Unlock()
|
||||
}()
|
||||
}
|
||||
@@ -210,20 +194,18 @@ func AddScan(plugin string, info Common.HostInfo, ch *chan struct{}, wg *sync.Wa
|
||||
func ScanFunc(name *string, info *Common.HostInfo) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
fmt.Printf("[-] 扫描错误 %v:%v - %v\n", info.Host, info.Ports, err)
|
||||
Common.LogError(fmt.Sprintf("扫描错误 %v:%v - %v", info.Host, info.Ports, err))
|
||||
}
|
||||
}()
|
||||
|
||||
// 检查插件是否存在
|
||||
plugin, exists := Common.PluginManager[*name]
|
||||
if !exists {
|
||||
fmt.Printf("[*] 扫描类型 %v 无对应插件,已跳过\n", *name)
|
||||
Common.LogInfo(fmt.Sprintf("扫描类型 %v 无对应插件,已跳过", *name))
|
||||
return
|
||||
}
|
||||
|
||||
// 直接调用扫描函数
|
||||
if err := plugin.ScanFunc(info); err != nil {
|
||||
fmt.Printf("[-] 扫描错误 %v:%v - %v\n", info.Host, info.Ports, err)
|
||||
Common.LogError(fmt.Sprintf("扫描错误 %v:%v - %v", info.Host, info.Ports, err))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user