ing
This commit is contained in:
parent
2e81139374
commit
18fe18ccef
|
@ -15,10 +15,6 @@ type DiscoveryHost struct {
|
|||
DiscoveryPort *DiscoveryPort `json:"discoveryPort"`
|
||||
}
|
||||
|
||||
func (dh *DiscoveryHost) Contains(ip string) bool {
|
||||
|
||||
}
|
||||
|
||||
type DiscoveryPort struct {
|
||||
FirstScanRange int `json:"firstScanRange"`
|
||||
LastScanRange int `json:"lastScanRange"`
|
||||
|
@ -30,6 +26,22 @@ type DiscoveryPort struct {
|
|||
DiscoveryService *DiscoveryService `json:"discoveryService"`
|
||||
}
|
||||
|
||||
func (dp *DiscoveryPort) Contains(port int) bool {
|
||||
if dp.FirstScanRange < port {
|
||||
return false
|
||||
}
|
||||
if dp.LastScanRange > port {
|
||||
return false
|
||||
}
|
||||
for _, p := range dp.ExcludePorts {
|
||||
if p == port {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
type DiscoveryService struct {
|
||||
IncludeServices []string `json:"includeServices"`
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ func ScanHost(zone *model.Zone, dh *model.DiscoveryHost, resultChan chan interfa
|
|||
logging.Logger().Debug(fmt.Sprintf("Discovery: arp channel is closed"))
|
||||
return
|
||||
}
|
||||
if h := handlePacketARP(zone, cr, hosts, packet); nil != h {
|
||||
if h := handlePacketARP(zone, hostRanges, hosts, packet); nil != h {
|
||||
resultChan <- h
|
||||
}
|
||||
case <-stopChan:
|
||||
|
@ -111,15 +111,27 @@ func sendARP(ps pcap.PCapScanner, zone *model.Zone, hostRanges []net.IP, stopCha
|
|||
return nil
|
||||
}
|
||||
|
||||
func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*model.Host, packet *layers.ARP) *model.Host {
|
||||
func handlePacketARP(zone *model.Zone, hostRanges []net.IP, hosts map[string]*model.Host, packet *layers.ARP) *model.Host {
|
||||
if packet.Operation != layers.ARPReply {
|
||||
return nil
|
||||
}
|
||||
|
||||
ip := net.IP(packet.SourceProtAddress)
|
||||
if _, ok := hosts[ip.String()]; ok || !cr.Contains(ip) {
|
||||
if _, ok := hosts[ip.String()]; ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
inRange := false
|
||||
for _, h := range hostRanges {
|
||||
if h.Equal(ip) {
|
||||
inRange = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !inRange {
|
||||
return nil
|
||||
}
|
||||
|
||||
h := &model.Host{}
|
||||
h.IP = ip.String()
|
||||
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
|
||||
|
@ -131,6 +143,30 @@ func handlePacketARP(zone *model.Zone, cr cidr.CIDRRanger, hosts map[string]*mod
|
|||
}
|
||||
|
||||
func getTargetHostRange(dh *model.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP, error) {
|
||||
var firstIP net.IP
|
||||
if "" != dh.FirstScanRange {
|
||||
firstIP = net.ParseIP(dh.FirstScanRange)
|
||||
if nil == firstIP {
|
||||
return nil, fmt.Errorf("Discovery: IP(%v) of FirstScanRange host is not valid", firstIP)
|
||||
}
|
||||
}
|
||||
var lastIP net.IP
|
||||
if "" != dh.LastScanRange {
|
||||
lastIP = net.ParseIP(dh.LastScanRange)
|
||||
if nil == lastIP {
|
||||
return nil, fmt.Errorf("Discovery: IP(%v) of LastScanRange host is not valid", lastIP)
|
||||
}
|
||||
}
|
||||
|
||||
includeIPs := make([]net.IP, 0)
|
||||
for _, iHost := range dh.IncludeHosts {
|
||||
iIP := net.ParseIP(iHost)
|
||||
if nil == iIP {
|
||||
return nil, fmt.Errorf("Discovery: IP(%v) of include host is not valid", iHost)
|
||||
}
|
||||
includeIPs = append(includeIPs, iIP)
|
||||
}
|
||||
|
||||
excludeIPs := make([]net.IP, 0)
|
||||
for _, eHost := range dh.ExcludeHosts {
|
||||
eIP := net.ParseIP(eHost)
|
||||
|
@ -140,7 +176,10 @@ func getTargetHostRange(dh *model.DiscoveryHost, cr cidr.CIDRRanger) ([]net.IP,
|
|||
excludeIPs = append(excludeIPs, eIP)
|
||||
}
|
||||
|
||||
ranges := cr.Range(excludeIPs)
|
||||
ranges, err := cr.Ranges(firstIP, lastIP, includeIPs, excludeIPs)
|
||||
if nil != err {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ranges, nil
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ func scanPortTCP(host *model.Host, dp *model.DiscoveryPort, resultChan chan inte
|
|||
logging.Logger().Debug(fmt.Sprintf("Discovery: tcp channel is closed"))
|
||||
return
|
||||
}
|
||||
if p := handlePacketTCP(host, ports, packet); nil != p {
|
||||
if p := handlePacketTCP(host, dp, ports, packet); nil != p {
|
||||
resultChan <- p
|
||||
}
|
||||
case <-stopChan:
|
||||
|
@ -108,7 +108,7 @@ Loop:
|
|||
return nil
|
||||
}
|
||||
|
||||
func handlePacketTCP(host *model.Host, ports map[int]*model.Port, packet *layers.TCP) *model.Port {
|
||||
func handlePacketTCP(host *model.Host, dp *model.DiscoveryPort, ports map[int]*model.Port, packet *layers.TCP) *model.Port {
|
||||
if nil == packet || packet.DstPort != 60000 {
|
||||
return nil
|
||||
}
|
||||
|
@ -117,24 +117,20 @@ func handlePacketTCP(host *model.Host, ports map[int]*model.Port, packet *layers
|
|||
return nil
|
||||
}
|
||||
|
||||
portNumber := int(packet.SrcPort)
|
||||
port := int(packet.SrcPort)
|
||||
|
||||
if _, ok := ports[portNumber]; ok || !cr.Contains(ip) {
|
||||
if _, ok := ports[port]; ok || !dp.Contains(port) {
|
||||
return nil
|
||||
}
|
||||
h := &model.Host{}
|
||||
h.IP = ip.String()
|
||||
h.Mac = net.HardwareAddr(packet.SourceHwAddress).String()
|
||||
h.Zone = zone
|
||||
|
||||
hosts[ip.String()] = h
|
||||
|
||||
p := &model.Port{
|
||||
PortType: model.PortTypeTCP,
|
||||
PortNumber: portNumber,
|
||||
PortNumber: port,
|
||||
}
|
||||
p.Host = host
|
||||
|
||||
ports[port] = p
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ func scanPortUDP(host *model.Host, dp *model.DiscoveryPort, resultChan chan inte
|
|||
}()
|
||||
|
||||
go func() {
|
||||
ports := make(map[int]*model.Port)
|
||||
|
||||
for {
|
||||
select {
|
||||
case packet, ok := <-udpChan:
|
||||
|
@ -41,7 +43,7 @@ func scanPortUDP(host *model.Host, dp *model.DiscoveryPort, resultChan chan inte
|
|||
logging.Logger().Debug(fmt.Sprintf("Discovery: udp channel is closed"))
|
||||
return
|
||||
}
|
||||
if p := handlePacketUDP(host, packet); nil != p {
|
||||
if p := handlePacketUDP(host, dp, ports, packet); nil != p {
|
||||
resultChan <- p
|
||||
}
|
||||
case <-stopChan:
|
||||
|
@ -119,7 +121,7 @@ func sendUDP(host *model.Host, dp *model.DiscoveryPort, stopChan chan struct{})
|
|||
return nil
|
||||
}
|
||||
|
||||
func handlePacketUDP(host *model.Host, packet gopacket.Packet) *model.Port {
|
||||
func handlePacketUDP(host *model.Host, dp *model.DiscoveryPort, ports map[int]*model.Port, packet gopacket.Packet) *model.Port {
|
||||
ipLayer := packet.Layer(layers.LayerTypeIPv4)
|
||||
|
||||
if ipLayer.(*layers.IPv4).SrcIP.String() == host.Zone.IP {
|
||||
|
@ -133,12 +135,17 @@ func handlePacketUDP(host *model.Host, packet gopacket.Packet) *model.Port {
|
|||
srcIP := ipLayer.(*layers.IPv4).SrcIP
|
||||
port := int(udp.SrcPort)
|
||||
logging.Logger().Debug(fmt.Sprintf("Discovery: IP of UPD(%d) src %v", port, srcIP))
|
||||
if _, ok := ports[port]; ok || !dp.Contains(port) {
|
||||
return nil
|
||||
}
|
||||
|
||||
p := &model.Port{
|
||||
PortType: model.PortTypeUDP,
|
||||
PortNumber: port,
|
||||
}
|
||||
p.Host = host
|
||||
ports[port] = p
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user