mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-07-13 12:52:44 +08:00
refactor: UDP扫描换用Nmap
This commit is contained in:
parent
1a5f789ba8
commit
fa1d787c84
@ -3,11 +3,13 @@ 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"
|
||||
@ -316,49 +318,39 @@ func calculateTCPChecksum(tcpHeader []byte, srcIP, dstIP net.IP) uint16 {
|
||||
}
|
||||
|
||||
func UDPScan(ip string, port int, timeout int64) (bool, error) {
|
||||
sendConn, err := net.ListenPacket("udp4", "0.0.0.0:0")
|
||||
// 构造端口字符串
|
||||
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("创建UDP套接字失败: %v", err)
|
||||
}
|
||||
defer sendConn.Close()
|
||||
|
||||
dstAddr := &net.UDPAddr{
|
||||
IP: net.ParseIP(ip),
|
||||
Port: port,
|
||||
return false, fmt.Errorf("创建扫描器失败: %v", err)
|
||||
}
|
||||
|
||||
// 根据端口发送对应的探测包
|
||||
var probe []byte
|
||||
switch port {
|
||||
case 161: // SNMP
|
||||
// SNMP GetRequest
|
||||
probe = []byte{
|
||||
0x30, 0x26, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70,
|
||||
0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x19, 0x02,
|
||||
0x04, 0x6b, 0x8b, 0x44, 0x5b, 0x02, 0x01, 0x00,
|
||||
0x02, 0x01, 0x00, 0x30, 0x0b, 0x30, 0x09, 0x06,
|
||||
0x05, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x05, 0x00,
|
||||
// 执行扫描
|
||||
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
|
||||
}
|
||||
}
|
||||
default:
|
||||
probe = []byte{0x00}
|
||||
}
|
||||
|
||||
_, err = sendConn.WriteTo(probe, dstAddr)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("发送UDP包失败: %v", err)
|
||||
}
|
||||
|
||||
sendConn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Second))
|
||||
|
||||
buffer := make([]byte, 65507)
|
||||
n, _, err := sendConn.ReadFrom(buffer)
|
||||
|
||||
// 收到响应则认为端口开放
|
||||
if err == nil && n > 0 {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// ICMP Unreachable 或其他错误都认为端口关闭
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
1
go.mod
1
go.mod
@ -35,6 +35,7 @@ require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||
github.com/BurntSushi/toml v0.3.1 // indirect
|
||||
github.com/Ullaakut/nmap v2.0.2+incompatible // indirect
|
||||
github.com/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/eapache/go-resiliency v1.7.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -24,6 +24,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
|
||||
github.com/IBM/sarama v1.43.3 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
|
||||
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/Ullaakut/nmap v2.0.2+incompatible h1:edw45QpSQBQ2B/Hqfg86Bt5rrK79tp/fAcqIHyNSdQs=
|
||||
github.com/Ullaakut/nmap v2.0.2+incompatible/go.mod h1:fkC066hwfcoKwlI7DS2ARTggSVtBTZYCjVH1TzuTMaQ=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI=
|
||||
|
Loading…
Reference in New Issue
Block a user