dddd/common/protocol.go

115 lines
2.6 KiB
Go
Raw Normal View History

2023-08-18 08:55:46 +02:00
package common
import (
2024-04-03 06:32:26 +02:00
"dddd/ddout"
2023-08-18 08:55:46 +02:00
"dddd/structs"
"dddd/utils"
"fmt"
"github.com/lcvvvv/gonmap"
"github.com/projectdiscovery/gologger"
"strconv"
"strings"
"sync"
"time"
)
2024-04-03 06:32:26 +02:00
func GetProtocol(hostPorts []string, threads int, timeout int) {
2023-08-18 08:55:46 +02:00
if len(hostPorts) == 0 {
return
}
hostPorts = utils.RemoveDuplicateElement(hostPorts)
if len(hostPorts) < threads {
threads = len(hostPorts)
}
2024-01-02 14:50:55 +01:00
gologger.AuditTimeLogger("TCP指纹识别识别目标: %s", strings.Join(hostPorts, ","))
2023-08-18 08:55:46 +02:00
workers := threads
Addrs := make(chan string, len(hostPorts))
defer close(Addrs)
results := make(chan structs.ProtocolResult, len(hostPorts))
defer close(results)
var wg sync.WaitGroup
//接收结果
go func() {
for found := range results {
if found.Status == int(gonmap.Closed) {
wg.Done()
continue
}
if found.Status == gonmap.Open || found.Response == nil {
2024-04-03 06:32:26 +02:00
ddout.FormatOutput(ddout.OutputMessage{
Type: "Nmap",
IP: found.IP,
Port: strconv.Itoa(found.Port),
Protocol: "tcp",
})
2023-08-18 08:55:46 +02:00
wg.Done()
continue
}
if found.Port == 23 && found.Response.FingerPrint.Service == "" {
found.Response.FingerPrint.Service = "telnet"
}
hostPort := fmt.Sprintf("%s:%v", found.IP, found.Port)
structs.GlobalIPPortMapLock.Lock()
_, ok := structs.GlobalIPPortMap[hostPort]
structs.GlobalIPPortMapLock.Unlock()
if !ok {
structs.GlobalBannerHMap.Set(hostPort, []byte(found.Response.Raw))
structs.GlobalIPPortMapLock.Lock()
structs.GlobalIPPortMap[hostPort] = found.Response.FingerPrint.Service
structs.GlobalIPPortMapLock.Unlock()
}
2024-04-03 06:32:26 +02:00
proto := found.Response.FingerPrint.Service
if proto == "" {
proto = "tcp"
2023-08-18 08:55:46 +02:00
}
2024-04-03 06:32:26 +02:00
ddout.FormatOutput(ddout.OutputMessage{
Type: "Nmap",
IP: found.IP,
Port: strconv.Itoa(found.Port),
Protocol: proto,
})
2023-08-18 08:55:46 +02:00
wg.Done()
}
}()
//多线程扫描
for i := 0; i < workers; i++ {
go func() {
scanner := gonmap.New()
2024-04-03 06:32:26 +02:00
scanner.SetTimeout(time.Duration(timeout) * time.Second)
2023-08-18 08:55:46 +02:00
for addr := range Addrs {
t := strings.Split(addr, ":")
if len(t) < 2 {
continue
}
ip := t[0]
port, err := strconv.Atoi(t[1])
if err != nil || port > 65535 {
continue
}
2024-04-03 06:32:26 +02:00
status, response := scanner.Scan(ip, port)
2023-08-18 08:55:46 +02:00
var data structs.ProtocolResult
data.IP = ip
data.Port = port
data.Status = int(status)
data.Response = response
results <- data
}
}()
}
//添加扫描目标
for _, hostPort := range hostPorts {
wg.Add(1)
Addrs <- hostPort
}
wg.Wait()
2024-01-02 14:50:55 +01:00
gologger.AuditTimeLogger("TCP指纹识别结束")
2023-08-18 08:55:46 +02:00
}