2017-11-18 13:03:37 +00:00
|
|
|
package ipv4
|
|
|
|
|
2017-11-21 05:31:17 +00:00
|
|
|
import (
|
2017-11-21 12:47:55 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2017-11-21 05:31:17 +00:00
|
|
|
"sync"
|
2017-11-21 12:47:55 +00:00
|
|
|
"time"
|
2017-11-18 13:03:37 +00:00
|
|
|
|
2017-11-21 12:47:55 +00:00
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-11-21 05:31:17 +00:00
|
|
|
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
|
2017-11-21 12:47:55 +00:00
|
|
|
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
|
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/layers"
|
2017-11-21 05:31:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func scanPortUDP(host *model.Host, dp *model.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
|
|
|
|
defer func() {
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2017-11-18 13:03:37 +00:00
|
|
|
|
2017-11-21 12:47:55 +00:00
|
|
|
ps, err := pcap.RetainScanner(host.Zone)
|
|
|
|
if nil != err {
|
|
|
|
errChan <- fmt.Errorf("Discovery: Cannot retain pcap instance %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
pcap.ReleaseScanner(host.Zone)
|
|
|
|
}()
|
|
|
|
|
|
|
|
udpChan := ps.OpenUDP(host.IP)
|
|
|
|
defer func() {
|
|
|
|
ps.CloseUDP(host.IP, udpChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case packet, ok := <-udpChan:
|
|
|
|
if !ok {
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Discovery: tcp channel is closed"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if p := handlePacketUDP(host, packet); nil != p {
|
|
|
|
resultChan <- p
|
|
|
|
}
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := sendUDP(host, dp, stopChan); nil != err {
|
|
|
|
errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
timer := time.NewTimer(3 * time.Second)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendUDP(host *model.Host, dp *model.DiscoveryPort, stopChan chan struct{}) error {
|
|
|
|
tcpPacket, err := makePacketPortTCP(host)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
tcpPacket.PacketConn.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
buf := gopacket.NewSerializeBuffer()
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
for portNumber := dp.FirstScanRange; portNumber < dp.LastScanRange; portNumber++ {
|
|
|
|
if nil != dp.ExcludePorts {
|
|
|
|
for _, exPortNumber := range dp.ExcludePorts {
|
|
|
|
if portNumber == exPortNumber {
|
|
|
|
continue Loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tcpPacket.TCP.DstPort = layers.TCPPort(portNumber)
|
|
|
|
tcpPacket.TCP.SetNetworkLayerForChecksum(tcpPacket.IP)
|
|
|
|
|
|
|
|
if err := gopacket.SerializeLayers(buf, tcpPacket.Opts, tcpPacket.TCP); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tcpPacket.PacketConn.WriteTo(buf.Bytes(), &net.IPAddr{IP: tcpPacket.IP.DstIP}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Microsecond * 100)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func handlePacketUDP(host *model.Host, packet *layers.TCP) *model.Port {
|
|
|
|
if nil == packet || packet.DstPort != 60000 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if packet.RST {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
portNumber := int(packet.SrcPort)
|
|
|
|
p := &model.Port{
|
|
|
|
PortType: model.PortTypeTCP,
|
|
|
|
PortNumber: portNumber,
|
|
|
|
}
|
|
|
|
p.Host = host
|
|
|
|
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
type PortPacketTCP struct {
|
|
|
|
IP *layers.IPv4
|
|
|
|
TCP *layers.TCP
|
|
|
|
Opts gopacket.SerializeOptions
|
|
|
|
PacketConn net.PacketConn
|
|
|
|
}
|
|
|
|
|
|
|
|
func makePacketPortTCP(host *model.Host) (*PortPacketTCP, error) {
|
|
|
|
packetTCP := &PortPacketTCP{}
|
|
|
|
|
|
|
|
srcIP := net.ParseIP(host.Zone.IP)
|
|
|
|
if nil == srcIP {
|
|
|
|
return nil, fmt.Errorf("Discovery: IP(%s) of zone is not valid", host.Zone.IP)
|
|
|
|
}
|
|
|
|
dstIP := net.ParseIP(host.IP)
|
|
|
|
if nil == dstIP {
|
|
|
|
return nil, fmt.Errorf("Discovery: IP(%s) of host is not valid", host.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
packetTCP.IP = &layers.IPv4{
|
|
|
|
SrcIP: srcIP.To4(),
|
|
|
|
DstIP: dstIP.To4(),
|
|
|
|
Version: 4,
|
|
|
|
TTL: 64,
|
|
|
|
Protocol: layers.IPProtocolTCP,
|
|
|
|
}
|
|
|
|
packetTCP.TCP = &layers.TCP{
|
|
|
|
SrcPort: 60000,
|
|
|
|
DstPort: 0, // will be incremented during the scan
|
|
|
|
SYN: true,
|
|
|
|
Seq: 0,
|
|
|
|
}
|
|
|
|
packetTCP.Opts = gopacket.SerializeOptions{
|
|
|
|
ComputeChecksums: true,
|
|
|
|
FixLengths: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Discovery: SYN create socket error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
packetTCP.PacketConn = conn
|
|
|
|
|
|
|
|
return packetTCP, nil
|
2017-11-18 13:03:37 +00:00
|
|
|
}
|