portscan

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package portscan probes TCP ports. Phase 1 ships only the TCP connect scanner against a single (addr, port) pair. The SYN scanner and the worker-pool wiring land in Phase 2 / Phase 6.

Index

Constants

View Source
const SYNAvailable = false

SYNAvailable reports whether this build of scry includes the raw-socket SYN scanner. Default builds return false; builds with `-tags rawsock` return true.

Variables

View Source
var ErrSYNUnavailable = errors.New(
	"SYN scanning is not compiled into this binary; rebuild with `-tags rawsock` and install libpcap (Linux) or Npcap (Windows)")

ErrSYNUnavailable is returned by the CLI when --syn is requested on a default build.

Functions

func ParsePorts

func ParsePorts(spec string) ([]uint16, error)

ParsePorts parses the -p flag into a deduplicated, order-preserving slice of ports.

Supported forms (comma-separated, mixable):

22                single port
22,80,443         list
1-1024            range (inclusive)
-                 all ports 1..65535 (-p-)
top100 / top1000  bundled shortlists (see top.go)

func Scan

func Scan(ctx context.Context, it *target.Iterator, cfg Config) <-chan HostResult

Scan iterates addresses from it and probes each Config.Ports (or runs TCP ping when Config.PingOnly is set). Results stream on the returned channel; the channel closes when iteration and in-flight work finish or when ctx is cancelled.

func SynScan

func SynScan(_ context.Context, _ *target.Iterator, _ Config) (<-chan HostResult, error)

SynScan is a stub on builds without the `rawsock` tag. It always returns ErrSYNUnavailable; the CLI checks SYNAvailable first and never reaches this call on a default build.

func Top100

func Top100() []uint16

Top100 returns a copy of the 100 most common TCP ports.

func Top1000

func Top1000() []uint16

Top1000 returns a copy of the top 1000 TCP ports.

Types

type Config

type Config struct {
	Ports       []uint16      // required unless PingOnly
	Timeout     time.Duration // per-probe dial timeout (default 500ms)
	Retries     int           // retries on filtered (default 0)
	Concurrency int           // max sockets in flight (default 2000)
	HostParall  int           // max hosts in flight (default 100)

	// PingOnly skips the port scan and runs TCP ping discovery only (-sn).
	PingOnly bool

	// Banner enables a passive banner grab on every open port.
	Banner bool

	// Progress receives one Tick per host completed. SetTotal is called
	// once up front using target.Iterator.Total(). nil → progress.NewNoop.
	Progress progress.Reporter

	// Resolver enables reverse DNS enrichment. nil disables it (equivalent
	// to --no-dns at the CLI level).
	Resolver *resolver.Cache

	// ScriptEngine runs Lua scripts against each open port. nil disables.
	ScriptEngine *script.Engine

	// Rate bounds SYN packet emission (packets-per-second). 0 disables
	// rate limiting. TCP-connect mode ignores this; the socket semaphore
	// is the pacer there.
	Rate int

	// Adaptive enables the adaptive rate limiter: the SYN sender starts
	// at max(Rate/4, 100) pps and scales up/down based on the observed
	// probe error rate. Ignored when Rate is 0 or when the scan is
	// TCP-connect (not --syn).
	Adaptive bool
}

Config tunes a Scan run. Defaults are speed-first: short timeout, no retries, generous concurrency. Raise Timeout + Retries for accuracy on WAN or lossy links.

type HostResult

type HostResult struct {
	Addr      netip.Addr
	Hostname  string // populated when Config.Resolver was set and PTR succeeded
	Started   time.Time
	Elapsed   time.Duration
	Results   []Result
	Discovery *discovery.Result // populated in PingOnly mode
}

HostResult aggregates all port results for a single host, plus any reverse-DNS and discovery metadata collected in parallel with the scan.

func (HostResult) OpenPorts

func (h HostResult) OpenPorts() []uint16

OpenPorts returns the list of open ports (as found, not sorted).

func (HostResult) Up

func (h HostResult) Up() bool

Up reports whether any probe returned StateOpen, or (in PingOnly mode) whether discovery reached the host.

type Result

type Result struct {
	Addr     netip.Addr
	Port     uint16
	State    State
	RTT      time.Duration
	Err      error           // populated for StateError
	Banner   string          // populated when banner grab is enabled and data was received
	Findings []ScriptFinding // populated when Config.ScriptEngine is set
}

Result is the outcome of a single probe.

func TCPConnect

func TCPConnect(ctx context.Context, addr netip.Addr, port uint16, timeout time.Duration) Result

TCPConnect performs one TCP-connect probe against addr:port using the supplied timeout. State classification:

open      — TCP handshake completed
closed    — received RST / ECONNREFUSED
filtered  — timeout reached with no response
error     — some other error (unreachable host, too many open files, …)

type ScriptFinding

type ScriptFinding struct {
	Script string
	Output string
}

ScriptFinding is one output line produced by a user Lua script.

type State

type State int

State classifies the outcome of a single TCP probe.

const (
	StateUnknown State = iota
	StateOpen
	StateClosed
	StateFiltered
	StateError
)

func (State) String

func (s State) String() string

Jump to

Keyboard shortcuts

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