overflow_discovery/discovery/ipv4/port_tcp.go

187 lines
4.0 KiB
Go
Raw Permalink Normal View History

2017-11-18 13:03:37 +00:00
package ipv4
import (
2017-12-04 09:37:39 +00:00
"encoding/json"
2017-11-18 13:03:37 +00:00
"fmt"
"net"
2017-12-04 11:59:31 +00:00
"strconv"
2017-11-21 05:31:17 +00:00
"sync"
2017-11-18 13:03:37 +00:00
"time"
"git.loafle.net/commons_go/logging"
2017-12-04 09:37:39 +00:00
oocmd "git.loafle.net/overflow/overflow_commons_go/modules/discovery"
discoveryM "git.loafle.net/overflow/overflow_commons_go/modules/discovery/model"
2017-11-18 13:03:37 +00:00
"git.loafle.net/overflow/overflow_discovery/commons/pcap"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
2017-12-04 09:37:39 +00:00
func scanPortTCP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, resultChan chan interface{}, errChan chan error, stopChan chan struct{}, wg *sync.WaitGroup) {
2017-11-21 05:31:17 +00:00
defer func() {
wg.Done()
}()
2017-11-21 12:47:55 +00:00
2017-11-18 13:03:37 +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)
}()
tcpChan := ps.OpenTCP(host.IP)
defer func() {
ps.CloseTCP(host.IP, tcpChan)
}()
go func() {
2017-12-04 09:37:39 +00:00
ports := make(map[int]*discoveryM.Port)
2017-11-22 05:55:40 +00:00
2017-11-18 13:03:37 +00:00
for {
select {
case packet, ok := <-tcpChan:
if !ok {
2018-03-21 10:34:40 +00:00
logging.Logger().Debugf("Discovery: tcp channel is closed")
2017-11-18 13:03:37 +00:00
return
}
2017-11-22 06:33:41 +00:00
if p := handlePacketTCP(host, dp, ports, packet); nil != p {
2017-11-21 05:31:17 +00:00
resultChan <- p
2017-11-18 13:03:37 +00:00
}
case <-stopChan:
return
}
}
}()
2017-11-21 05:31:17 +00:00
if err := sendTCP(host, dp, stopChan); nil != err {
2017-11-18 13:03:37 +00:00
errChan <- err
return
}
2017-11-21 05:31:17 +00:00
timer := time.NewTimer(20 * time.Second)
select {
case <-stopChan:
return
case <-timer.C:
return
}
2017-11-18 13:03:37 +00:00
}
2017-12-04 09:37:39 +00:00
func sendTCP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, stopChan chan struct{}) error {
2017-11-21 05:31:17 +00:00
tcpPacket, err := makePacketPortTCP(host)
2017-11-18 13:03:37 +00:00
if nil != err {
return err
}
2017-11-21 05:31:17 +00:00
defer func() {
tcpPacket.PacketConn.Close()
}()
2017-11-18 13:03:37 +00:00
buf := gopacket.NewSerializeBuffer()
2017-11-21 05:31:17 +00:00
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 {
2017-11-18 13:03:37 +00:00
return err
}
2017-11-22 05:55:40 +00:00
timer := time.NewTimer(time.Microsecond * 100)
2017-11-18 13:03:37 +00:00
select {
case <-stopChan:
return nil
2017-11-22 05:55:40 +00:00
case <-timer.C:
2017-11-18 13:03:37 +00:00
}
}
2017-11-21 05:31:17 +00:00
2017-11-18 13:03:37 +00:00
return nil
}
2017-12-04 09:37:39 +00:00
func handlePacketTCP(host *discoveryM.Host, dp *discoveryM.DiscoveryPort, ports map[int]*discoveryM.Port, packet *layers.TCP) *discoveryM.Port {
2017-11-18 13:03:37 +00:00
if nil == packet || packet.DstPort != 60000 {
return nil
}
if packet.RST {
return nil
}
2017-11-22 06:33:41 +00:00
port := int(packet.SrcPort)
2018-03-21 10:34:40 +00:00
logging.Logger().Debugf("Discovery: IP of TCP(%d) src %s", port, host.IP)
2017-11-22 05:55:40 +00:00
2017-11-22 06:33:41 +00:00
if _, ok := ports[port]; ok || !dp.Contains(port) {
2017-11-22 05:55:40 +00:00
return nil
}
2017-12-04 09:37:39 +00:00
p := &discoveryM.Port{
PortType: oocmd.PortTypeTCP,
2017-12-04 11:59:31 +00:00
PortNumber: json.Number(strconv.Itoa(port)),
2017-11-18 13:03:37 +00:00
}
p.Host = host
2017-11-22 06:33:41 +00:00
ports[port] = p
2017-11-18 13:03:37 +00:00
return p
}
2017-11-21 05:31:17 +00:00
type PortPacketTCP struct {
IP *layers.IPv4
TCP *layers.TCP
Opts gopacket.SerializeOptions
PacketConn net.PacketConn
}
2017-12-04 09:37:39 +00:00
func makePacketPortTCP(host *discoveryM.Host) (*PortPacketTCP, error) {
2017-11-21 05:31:17 +00:00
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
}