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