discover

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package discover implements network discovery for the scan nodes.

This is a plant-floor inventory tool. Everything in it is bounded by an operator-configured CIDR allowlist, because the nodes are reachable by anyone who can edit a flow, and "it is on an isolated network" is an assumption that stops being true the day somebody bridges a VLAN.

Index

Constants

View Source
const (
	PortModbus     = 502
	PortOPCUA      = 4840
	PortEtherNetIP = 44818
	PortBACnet     = 47808
	PortS7         = 102
	PortFINS       = 9600
	PortHTTP       = 80
	PortHTTPS      = 443
	PortSSH        = 22
	PortMQTT       = 1883
)

Well-known OT ports. These are what a discovery sweep is actually looking for on a plant floor; a generic 1-65535 scan is both slower and less useful.

View Source
const MaxSweepHosts = 4096

MaxSweepHosts caps a single sweep.

A /16 is 65,534 addresses. Allowing one node to enqueue that many probes would saturate an OT link that is often 100Mb and shared with the process traffic that actually matters.

Variables

DefaultPorts is the sweep used when a node does not narrow it.

View Source
var ErrDisabled = fmt.Errorf("discovery is disabled; enable it and list the " +
	"networks the scan nodes may probe")

ErrDisabled is returned when discovery is off.

Functions

func Hosts

func Hosts(p netip.Prefix) ([]netip.Addr, error)

Hosts expands a prefix into the addresses to probe, excluding the network and broadcast addresses for IPv4 prefixes shorter than /31.

func Interfaces

func Interfaces() ([]map[string]any, error)

Interfaces reports the host's network interfaces.

In macvlan mode this is how a flow learns the address it was given on the OT VLAN, which it needs in order to know what to sweep.

func PortName

func PortName(p int) string

PortName labels a port in the output, so an operator reading a device list sees "modbus" rather than 502.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

Collector accumulates results from concurrent probes.

func NewCollector

func NewCollector() *Collector

NewCollector returns an empty collector.

func (*Collector) Add

func (c *Collector) Add(d Device)

Add merges a finding, keyed by address, so that several probe types discovering the same device produce one entry rather than four.

func (*Collector) Devices

func (c *Collector) Devices() []Device

Devices returns the findings, sorted by address.

type Device

type Device struct {
	Address  string            `json:"address"`
	MAC      string            `json:"mac,omitempty"`
	Hostname string            `json:"hostname,omitempty"`
	Vendor   string            `json:"vendor,omitempty"`
	Ports    []int             `json:"ports,omitempty"`
	Protocol string            `json:"protocol,omitempty"`
	Details  map[string]any    `json:"details,omitempty"`
	Source   string            `json:"source"`
	Extra    map[string]string `json:"extra,omitempty"`
}

Device is one thing found on the network.

func Sweep

func Sweep(ctx context.Context, scope *Scope, prefix netip.Prefix, opts Options) ([]Device, error)

Sweep probes every address in a range and returns what answered.

TCP connect rather than raw SYN. A connect scan needs no elevated capability, works identically inside a container with capabilities dropped, and — more importantly on a plant floor — completes the handshake rather than leaving half-open connections on a PLC whose TCP stack may have a connection table of eight.

type ErrOutOfScope

type ErrOutOfScope struct{ Target string }

ErrOutOfScope is returned for a target outside the allowlist.

func (*ErrOutOfScope) Error

func (e *ErrOutOfScope) Error() string

type Identification

type Identification struct {
	Protocol string
	Details  map[string]any
}

Identification is what a protocol probe learned about a device.

type Options

type Options struct {
	// Timeout bounds one probe.
	Timeout time.Duration
	// Concurrency bounds simultaneous probes. OT links are frequently 100Mb and
	// shared with the process traffic that matters, so this defaults low: a
	// discovery scan must never be the reason a line stutters.
	Concurrency int
	// Ports to probe on each host.
	Ports []int
	// ResolveNames does a reverse DNS lookup per responding host.
	ResolveNames bool
	// Identify runs the protocol-specific identification probes against ports
	// that answered.
	Identify bool
}

Options tune a sweep.

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope decides what the scan nodes are permitted to probe.

func NewScope

func NewScope(enabled bool, cidrs []string) (*Scope, error)

NewScope builds a scope from the configured CIDRs.

An enabled scope with no CIDRs is an error rather than "everything". The permissive reading of an empty list is how a tool meant to inventory one factory segment ends up scanning a corporate network.

func (*Scope) CheckAddr

func (s *Scope) CheckAddr(addr netip.Addr) error

CheckAddr reports whether a single address may be probed.

func (*Scope) CheckHost

func (s *Scope) CheckHost(host string) error

CheckHost resolves a host and reports whether every address it resolves to is in scope.

Every address, not any: a name that resolves to one in-scope and one out-of-scope address must be refused, or DNS becomes the way around the allowlist.

func (*Scope) CheckPrefix

func (s *Scope) CheckPrefix(p netip.Prefix) error

CheckPrefix reports whether an entire range may be swept.

The range must be fully contained in an allowed prefix. Overlap is not enough: sweeping 10.0.0.0/8 because 10.20.30.0/24 is allowed would be exactly the failure this exists to prevent.

func (*Scope) Enabled

func (s *Scope) Enabled() bool

Enabled reports whether discovery is switched on at all.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL