mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-07-14 13:22:35 +08:00
refactor: UDP扫描换用Nmap
This commit is contained in:
parent
1a5f789ba8
commit
fa1d787c84
@ -3,11 +3,13 @@ package Core
|
|||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Ullaakut/nmap"
|
||||||
"github.com/google/gopacket"
|
"github.com/google/gopacket"
|
||||||
"github.com/google/gopacket/layers"
|
"github.com/google/gopacket/layers"
|
||||||
"github.com/google/gopacket/pcap"
|
"github.com/google/gopacket/pcap"
|
||||||
"github.com/shadow1ng/fscan/Common"
|
"github.com/shadow1ng/fscan/Common"
|
||||||
"golang.org/x/net/ipv4"
|
"golang.org/x/net/ipv4"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
@ -316,49 +318,39 @@ func calculateTCPChecksum(tcpHeader []byte, srcIP, dstIP net.IP) uint16 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UDPScan(ip string, port int, timeout int64) (bool, error) {
|
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 {
|
if err != nil {
|
||||||
return false, fmt.Errorf("创建UDP套接字失败: %v", err)
|
return false, fmt.Errorf("创建扫描器失败: %v", err)
|
||||||
}
|
|
||||||
defer sendConn.Close()
|
|
||||||
|
|
||||||
dstAddr := &net.UDPAddr{
|
|
||||||
IP: net.ParseIP(ip),
|
|
||||||
Port: port,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 根据端口发送对应的探测包
|
// 执行扫描
|
||||||
var probe []byte
|
result, warnings, err := scanner.Run()
|
||||||
switch port {
|
if err != nil {
|
||||||
case 161: // SNMP
|
return false, fmt.Errorf("扫描执行失败: %v", err)
|
||||||
// SNMP GetRequest
|
}
|
||||||
probe = []byte{
|
if warnings != nil {
|
||||||
0x30, 0x26, 0x02, 0x01, 0x01, 0x04, 0x06, 0x70,
|
log.Printf("扫描警告: %v", warnings)
|
||||||
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,
|
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
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
1
go.mod
1
go.mod
@ -35,6 +35,7 @@ require (
|
|||||||
filippo.io/edwards25519 v1.1.0 // indirect
|
filippo.io/edwards25519 v1.1.0 // indirect
|
||||||
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
|
||||||
github.com/BurntSushi/toml v0.3.1 // 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/antlr/antlr4/runtime/Go/antlr v1.4.10 // indirect
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/eapache/go-resiliency v1.7.0 // 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 h1:Yj6L2IaNvb2mRBop39N7mmJAHBVY3dTPncr3qGVkxPA=
|
||||||
github.com/IBM/sarama v1.43.3/go.mod h1:FVIRaLrhK3Cla/9FfRF5X9Zua2KpS3SYIXxhac1H+FQ=
|
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/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/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/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||||
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI=
|
github.com/alexbrainman/sspi v0.0.0-20231016080023-1a75b4708caa h1:LHTHcTQiSGT7VVbI0o4wBRNQIgn917usHWOd6VAffYI=
|
||||||
|
Loading…
Reference in New Issue
Block a user