Documentation
¶
Index ¶
- Constants
- Variables
- func AddCommand(command string, shortDescription string, longDescription string, port int, ...) (*flags.Command, error)
- func CloseConnAndHandleError(conn net.Conn)
- func EncodeGrab(raw *Grab, includeDebug bool) ([]byte, error)
- func FlagsToSet(flags uint64, mapping map[uint64]string) (map[string]bool, []uint64)
- func GetDefaultTCPDialer(flags *BaseFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
- func GetDefaultTLSDialer(flags *BaseFlags, tlsFlags *TLSFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
- func GetDefaultTLSWrapper(tlsFlags *TLSFlags) ...
- func GetDefaultUDPDialer(flags *BaseFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
- func GetMetaFile() *os.File
- func GetTargetsCSV(source io.Reader, ch chan<- ScanTarget) error
- func HasCtxExpired(ctx context.Context) bool
- func InputTargetsCSV(ch chan<- ScanTarget) error
- func IsTimeoutError(err error) bool
- func ListFlagsToSet(flags uint64, labels []string) (map[string]bool, []uint64)
- func LogPanic(format string, args ...any)
- func MapFlagsToSet(flags uint64, mapping FlagMap) (map[string]bool, []uint64)
- func NewFakeResolver(ipstr string) (*net.Resolver, error)
- func NewIniParser() *flags.IniParser
- func OutputResults(w *bufio.Writer, results <-chan []byte) error
- func ParseCSVTarget(fields []string) (ipnet *net.IPNet, domain string, tag string, port string, err error)
- func PrintScanners()
- func Process(mon *Monitor)
- func ReadAvailable(conn net.Conn) ([]byte, error)
- func ReadAvailableWithOptions(conn net.Conn, bufferSize int, readTimeout time.Duration, ...) ([]byte, error)
- func ReadUntilRegex(connection net.Conn, res []byte, expr *regexp.Regexp) (int, error)
- func RegisterScan(name string, scanner Scanner)
- func SetInputFunc(f InputTargetsFunc)
- func SetOutputFunc(f OutputResultsFunc)
- func TLDMatches(host1 string, host2 string) bool
- func ValidateAndHandleFrameworkConfiguration()
- func WidenMapKeys(input map[int]string) map[uint64]string
- func WidenMapKeys16(input map[uint16]string) map[uint64]string
- func WidenMapKeys32(input map[uint32]string) map[uint64]string
- func WidenMapKeys8(input map[uint8]string) map[uint64]string
- type BaseFlags
- type Config
- type Dialer
- type DialerGroup
- type DialerGroupConfig
- type FakeDNSServer
- type FlagMap
- type GeneralOptions
- type Grab
- type InputOutputOptions
- type InputTargetsFunc
- type ModuleMetadata
- type ModuleSet
- type Monitor
- type MultipleCommand
- type NetworkingOptions
- type OutputResultsFunc
- type ReadLimitExceededAction
- type ScanError
- type ScanFlags
- type ScanModule
- type ScanResponse
- type ScanStatus
- type ScanTarget
- type Scanner
- type TLSConnection
- type TLSFlags
- type TLSLog
- type TimeoutConnection
- func (c *TimeoutConnection) Close() error
- func (c *TimeoutConnection) Read(b []byte) (n int, err error)
- func (c *TimeoutConnection) SaturateTimeoutsToReadAndWriteTimeouts()
- func (c *TimeoutConnection) SetDeadline(deadline time.Time) error
- func (c *TimeoutConnection) SetReadDeadline(deadline time.Time) error
- func (c *TimeoutConnection) SetWriteDeadline(deadline time.Time) error
- func (c *TimeoutConnection) Write(b []byte) (n int, err error)
- type TransportProtocol
Examples ¶
Constants ¶
const ( IPVersionCapabilityTimeout = 10 * time.Second IPVersionCapabilityIPv4Address = "1.1.1.1:80" // Cloudflare has this IP/Port redirect to https://one.one.one.one. We can use it to test if this host has IPv4 connectivity IPVersionCapabilityIPv6Address = "2606:4700:4700::1111:80" // Same as above for IPv6 )
const ( // ReadLimitExceededActionNotSet is a placeholder for the zero value, so that explicitly set values can be // distinguished from the empty default. ReadLimitExceededActionNotSet = ReadLimitExceededAction("") // ReadLimitExceededActionTruncate causes the connection to truncate at BytesReadLimit bytes and return a bogus // io.EOF error. The fact that a truncation took place is logged at debug level. ReadLimitExceededActionTruncate = ReadLimitExceededAction("truncate") // ReadLimitExceededActionError causes the Read call to return n, ErrReadLimitExceeded (in addition to truncating). ReadLimitExceededActionError = ReadLimitExceededAction("error") // ReadLimitExceededActionPanic causes the Read call to panic(ErrReadLimitExceeded). ReadLimitExceededActionPanic = ReadLimitExceededAction("panic") )
const ( SCAN_SUCCESS = ScanStatus("success") // The protocol in question was positively identified and the scan encountered no errors SCAN_CONNECTION_REFUSED = ScanStatus("connection-refused") // TCP connection was actively rejected SCAN_CONNECTION_TIMEOUT = ScanStatus("connection-timeout") // No response to TCP connection request SCAN_CONNECTION_CLOSED = ScanStatus("connection-closed") // The TCP connection was unexpectedly closed SCAN_HANDSHAKE_ERROR = ScanStatus("handshake-error") // The security (TLS, etc) handshake failed SCAN_IO_TIMEOUT = ScanStatus("io-timeout") // Timed out waiting on data SCAN_PROTOCOL_ERROR = ScanStatus("protocol-error") // Received data incompatible with the target protocol SCAN_APPLICATION_ERROR = ScanStatus("application-error") // The application reported an error SCAN_INVALID_INPUTS = ScanStatus("invalid-inputs") // The inputs to the scan were invalid SCAN_BLOCKLISTED_TARGET = ScanStatus("blocklisted-target") // The target was blocklisted SCAN_UNKNOWN_ERROR = ScanStatus("unknown-error") // Catch-all for unrecognized errors )
TODO: Conform to standard string const format (names, capitalization, hyphens/underscores, etc) TODO: Enumerate further status types TODO: lump connection closed / io timeout? TODO: Add SCAN_TLS_PROTOCOL_ERROR? For purely TLS-wrapped protocols, SCAN_PROTOCOL_ERROR is fine -- but for protocols that have a non-TLS bootstrap (e.g. a STARTTLS procedure), SCAN_PROTOCOL_ERROR is misleading, since it did get far-enough into the application protocol to start TLS handshaking -- but a garbled TLS handshake is certainly not a SCAN_APPLICATION_ERROR
const (
ErrTotalTimeout = errTotalTimeout("timeout")
)
Variables ¶
var ( // DefaultBytesReadLimit is the maximum number of bytes to read per connection when no explicit value is provided. DefaultBytesReadLimit = 256 * 1024 * 1024 // DefaultReadLimitExceededAction is the action used when no explicit action is set. DefaultReadLimitExceededAction = ReadLimitExceededActionTruncate // DefaultSessionTimeout is the default maximum time a connection may be used when no explicit value is provided. DefaultSessionTimeout = 1 * time.Minute )
var ErrInsufficientBuffer = errors.New("not enough buffer space")
var ErrInvalidArguments = errors.New("invalid arguments")
ErrInvalidArguments is thrown if the command-line arguments invalid.
var ErrInvalidResponse = errors.New("invalid response")
ErrInvalidResponse is returned when the server returns a syntactically-invalid response.
var ErrMismatchedFlags = errors.New("mismatched flag/module")
ErrMismatchedFlags is thrown if the flags for one module type are passed to an incompatible module type.
var ErrReadLimitExceeded = errors.New("read limit exceeded")
ErrReadLimitExceeded is returned / panic'd from Read if the read limit is exceeded when the ReadLimitExceededAction is error / panic.
var ErrUnexpectedResponse = errors.New("unexpected response")
ErrUnexpectedResponse is returned when the server returns a syntactically-valid but unexpected response.
Functions ¶
func AddCommand ¶
func AddCommand(command string, shortDescription string, longDescription string, port int, m ScanModule) (*flags.Command, error)
AddCommand adds a module to the parser and returns a pointer to a flags.command object or an error
func CloseConnAndHandleError ¶ added in v0.2.0
CloseConnAndHandleError closes the connection and logs an error if it fails. Convenience function for code-reuse.
func EncodeGrab ¶ added in v0.1.5
EncodeGrab serializes a Grab to JSON, handling the debug fields if necessary.
func FlagsToSet ¶
FlagsToSet converts an integer flags variable to a set of string labels corresponding to each bit, in the format described by the wiki (see https://github.com/zmap/zgrab2/wiki/Scanner-details). The mapping maps the bit mask value (i.e. a number of the form (1 << x)) to the label for that bit. Flags not present in mapping are appended to the unknown list.
Example ¶
output, unknowns := FlagsToSet(0x5, WidenMapKeys(map[int]string{
0x1: "bit0",
0x2: "bit1",
0x8: "bit3",
}))
for k, v := range output {
fmt.Printf("%s: %v\n", k, v)
}
for _, v := range unknowns {
fmt.Printf("Unknown: 0x%01x", v)
}
Output: bit0: true Unknown: 0x4
func GetDefaultTCPDialer ¶ added in v0.2.0
func GetDefaultTCPDialer(flags *BaseFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
GetDefaultTCPDialer returns a TCP dialer suitable for modules with default TCP behavior
func GetDefaultTLSDialer ¶ added in v0.2.0
func GetDefaultTLSDialer(flags *BaseFlags, tlsFlags *TLSFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
GetDefaultTLSDialer returns a TLS-over-TCP dialer suitable for modules with default TLS behavior
func GetDefaultTLSWrapper ¶ added in v0.2.0
func GetDefaultTLSWrapper(tlsFlags *TLSFlags) func(ctx context.Context, t *ScanTarget, conn net.Conn) (*TLSConnection, error)
GetDefaultTLSWrapper uses the TLS flags to create a wrapper that upgrades a TCP connection to a TLS connection.
func GetDefaultUDPDialer ¶ added in v0.2.0
func GetDefaultUDPDialer(flags *BaseFlags) func(ctx context.Context, t *ScanTarget, addr string) (net.Conn, error)
GetDefaultUDPDialer returns a UDP dialer suitable for modules with default UDP behavior
func GetMetaFile ¶
GetMetaFile returns the file to which metadata should be output
func GetTargetsCSV ¶
func GetTargetsCSV(source io.Reader, ch chan<- ScanTarget) error
GetTargetsCSV reads targets from a CSV source, generates ScanTargets, and delivers them to the provided channel.
func HasCtxExpired ¶ added in v0.2.0
HasCtxExpired checks if the context has expired. Common function used in various places.
func InputTargetsCSV ¶
func InputTargetsCSV(ch chan<- ScanTarget) error
InputTargetsCSV is an InputTargetsFunc that calls GetTargetsCSV with the CSV file provided on the command line.
func IsTimeoutError ¶
IsTimeoutError checks if the given error corresponds to a timeout (of any type).
func ListFlagsToSet ¶
ListFlagsToSet converts an integer flags variable to a set of string labels corresponding to each bit, in the format described by the wiki (see https://github.com/zmap/zgrab2/wiki/Scanner-details). The ith entry of labels gives the label for the ith bit (i.e. flags & (1<<i)). Empty strings in labels are treated as unknown, as are bits beyond the end of the list. Unknown flags are appended to the unknown list.
Example ¶
output, unknowns := ListFlagsToSet(0x5, []string{
"bit0",
"bit1",
"",
"bit3",
})
for k, v := range output {
fmt.Printf("%s: %v\n", k, v)
}
for _, v := range unknowns {
fmt.Printf("Unknown: 0x%01x", v)
}
Output: bit0: true Unknown: 0x4
func LogPanic ¶
LogPanic is intended to be called from within defer -- if there was no panic, it returns without doing anything. Otherwise, it logs the stacktrace, the panic error, and the provided message before re-raising the original panic. Example:
defer zgrab2.LogPanic("Error decoding body '%x'", body)
func MapFlagsToSet ¶
MapFlagsToSet gets the "set" (map of strings to true) of values corresponding to the bits in flags. For each bit i set in flags, the result will have result[mapping(i << i)] = true. Any bits for which the mapping returns a non-nil error are instead appended to the unknowns list.
Example (Error) ¶
output, unknowns := MapFlagsToSet(0x1b, func(bit uint64) (string, error) {
if bit < 0x10 {
return fmt.Sprintf("bit0x%01x", bit), nil
} else {
return "", fmt.Errorf("Unrecognized flag 0x%02x", bit)
}
})
for k, v := range output {
fmt.Printf("%s: %v\n", k, v)
}
for _, v := range unknowns {
fmt.Printf("Unknown: 0x%02x", v)
}
Output: bit0x1: true bit0x2: true bit0x8: true Unknown: 0x10
Example (Success) ¶
output, unknowns := MapFlagsToSet(0xb, func(bit uint64) (string, error) {
return fmt.Sprintf("bit0x%01x", bit), nil
})
for k, v := range output {
fmt.Printf("%s: %v\n", k, v)
}
for _, v := range unknowns {
fmt.Printf("Unknown: 0x%01x", v)
}
Output: bit0x1: true bit0x2: true bit0x8: true
func NewFakeResolver ¶ added in v0.1.2
Fake DNS Resolver, to force a DNS lookup to return a pinned address Inspired by the golang net/dnsclient_unix_test.go code
For a given IP, create a new Resolver that wraps a fake DNS server. This resolver will always return an IP that is represented by "ipstr", for DNS queries of the same IP type. Otherwise, it will return a DNS lookup error.
func NewIniParser ¶
NewIniParser creates and returns a ini parser initialized with the default parser
func OutputResults ¶ added in v0.1.4
OutputResults writes results to a buffered Writer from a channel.
func ParseCSVTarget ¶
func ParseCSVTarget(fields []string) (ipnet *net.IPNet, domain string, tag string, port string, err error)
ParseCSVTarget takes a record from a CSV-format input file and returns the specified ipnet, domain, tag and port or an error.
ZGrab2 input files have four fields:
IP, DOMAIN, TAG, PORT
Each line specifies a target to scan by its IP address, domain name or both, as well as an optional tag used to determine which scanners will be invoked.
Port number has been added to the end of the line for compatibility reasons. A CIDR block may be provided in the IP field, in which case the framework expands the record into targets for every address in the block.
Trailing empty fields may be omitted. Comment lines begin with #, and empty lines are ignored.
func Process ¶
func Process(mon *Monitor)
Process sets up an output encoder, input reader, and starts grab workers.
func ReadAvailable ¶
ReadAvaiable reads what it can without blocking for more than defaultReadTimeout per read, or defaultTotalTimeout for the whole session. Reads at most defaultMaxReadSize bytes.
func ReadAvailableWithOptions ¶
func ReadAvailableWithOptions(conn net.Conn, bufferSize int, readTimeout time.Duration, totalTimeout time.Duration, maxReadSize int) ([]byte, error)
ReadAvailableWithOptions reads whatever can be read (up to maxReadSize) from conn without blocking for longer than readTimeout per read, or totalTimeout for the entire session. A totalTimeout of 0 means attempt to use the connection's timeout (or, failing that, 1 second). On failure, returns anything it was able to read along with the error.
func ReadUntilRegex ¶
ReadUntilRegex calls connection.Read() until it returns an error, or the cumulatively-read data matches the given regexp
func RegisterScan ¶
RegisterScan registers each individual scanner to be ran by the framework
func SetInputFunc ¶
func SetInputFunc(f InputTargetsFunc)
SetInputFunc sets the target input function to the provided function.
func SetOutputFunc ¶
func SetOutputFunc(f OutputResultsFunc)
SetOutputFunc sets the result output function to the provided function.
func TLDMatches ¶
TLDMatches checks for a strict TLD match
func ValidateAndHandleFrameworkConfiguration ¶ added in v1.0.0
func ValidateAndHandleFrameworkConfiguration()
ValidateAndHandleFrameworkConfiguration configures and validates the ZGrab2 config struct, e.g. taking the --input-file string and opening the file, or starting the Prometheus server, if configured. It is safe to call this function multiple times (and this occurs when using the --multiple command: 1) for parsing the command line flags, and 2) for parsing the INI file). Actions which should only be done once, such as starting the Prometheus server, are guarded by a sync.Once variable.
func WidenMapKeys ¶
WidenMapKeys copies a map with int keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
func WidenMapKeys16 ¶
WidenMapKeys16 copies a map with uint8 keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
func WidenMapKeys32 ¶
WidenMapKeys32 copies a map with uint8 keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
Types ¶
type BaseFlags ¶
type BaseFlags struct {
Port uint `short:"p" long:"port" description:"Specify port to grab on"`
Name string `short:"n" long:"name" description:"Specify name for output json, only necessary if scanning multiple modules"`
ConnectTimeout time.Duration `` /* 133-byte string literal not displayed */
TargetTimeout time.Duration `` /* 150-byte string literal not displayed */
Trigger string `short:"g" long:"trigger" description:"Invoke only on targets with specified tag"`
Verbose bool `short:"v" long:"verbose" description:"More verbose logging, include debug fields in the scan results if implemented"`
}
BaseFlags contains the options that every flags type must embed
type Config ¶
type Config struct {
GeneralOptions // CLI Options related to general framework configuration. Don't fit into any other category
InputOutputOptions // CLI Options related to I/O. Just affects organization of --help
NetworkingOptions // CLI Options related to networking. Just affects organization of --help
Multiple MultipleCommand `command:"multiple" description:"Multiple module actions"`
// contains filtered or unexported fields
}
Config is the high level framework options that will be parsed from the command line
type Dialer ¶
type Dialer struct {
// SessionTimeout is the maximum time to wait for the entire session, after which any operations on the
// connection will fail. Dial-specific timeouts are set on the net.Dialer.
SessionTimeout time.Duration
// ReadTimeout is the maximum time to wait for a Read
ReadTimeout time.Duration
// WriteTimeout is the maximum time to wait for a Write
WriteTimeout time.Duration
// Dialer is an auxiliary dialer used for DialContext (the result gets wrapped in a
// TimeoutConnection).
*net.Dialer
// BytesReadLimit is the maximum number of bytes that connections dialed with this dialer will
// read before erroring.
BytesReadLimit int
// ReadLimitExceededAction describes how connections dialed with this dialer deal with exceeding
// the BytesReadLimit.
ReadLimitExceededAction ReadLimitExceededAction
// Blocklist of IPs we should not dial.
Blocklist cidranger.Ranger
}
Dialer provides Dial and DialContext methods to get connections with the given timeout.
func GetTimeoutConnectionDialer ¶
GetTimeoutConnectionDialer gets a Dialer that dials connections with the given timeout.
func NewDialer ¶
NewDialer creates a new Dialer with default settings. Blocklist, if provided, is used to prevent dialing certain IPs.
func (*Dialer) DialContext ¶
DialContext wraps the connection returned by net.Dialer.DialContext() with a TimeoutConnection.
func (*Dialer) SetRandomLocalAddr ¶ added in v0.2.0
SetRandomLocalAddr sets a random local address and port for the dialer. If either localIPs or localPorts are empty, the IP or port, respectively, will be un-set and the system will choose.
type DialerGroup ¶ added in v0.2.0
type DialerGroup struct {
// TransportAgnosticDialer should be used by most modules that do not need control over the transport layer.
// It abstracts the underlying transport protocol so a module can deal with just the L7 logic. Any protocol that
// doesn't need to know about the underlying transport should use this.
// If the transport is a TLS connection, the dialer should return a zgrab2.TLSConnection so the underlying log can be
// accessed.
TransportAgnosticDialer func(ctx context.Context, target *ScanTarget) (net.Conn, error)
// L4Dialer will be used by any module that needs to have a TCP/UDP connection. Think of following a redirect to an
// http:// server, or a module that needs to start with a TCP connection and then upgrade to TLS as part of the protocol.
// The layered function is needed since we set DialerGroups at Scanner.Init, but modules like HTTP will modify the
// Dialer based on the target, for example to use a fake DNS resolver based on the domain name.
L4Dialer func(target *ScanTarget) func(ctx context.Context, network, addr string) (net.Conn, error)
// TLSWrapper is a function that takes an existing net.Conn and upgrades it to a TLS connection. This is useful for
// modules that need to start with a TCP connection and then upgrade to TLS later as part of the protocol.
// Cannot be used for QUIC connections, as QUIC connections are not "upgraded" from a L4 connection
TLSWrapper func(ctx context.Context, target *ScanTarget, l4Conn net.Conn) (*TLSConnection, error)
Blocklist *cidranger.Ranger // Blocklist is a list of CIDR ranges that should be blocked
}
DialerGroup wraps various dialer functions for a module to use. A module will usually only use a subset of these, and will indicate which ones it needs in the DialerGroupConfig.
func (*DialerGroup) Dial ¶ added in v0.2.0
func (d *DialerGroup) Dial(ctx context.Context, target *ScanTarget) (net.Conn, error)
Dial is used to access the transport agnostic dialer
func (*DialerGroup) GetTLSDialer ¶ added in v0.2.0
func (d *DialerGroup) GetTLSDialer(ctx context.Context, t *ScanTarget) func(network, addr string) (*TLSConnection, error)
GetTLSDialer returns a function that can be used as a standalone TLS dialer. This is useful for modules like HTTP that require this for handling redirects to https://
type DialerGroupConfig ¶ added in v0.2.0
type DialerGroupConfig struct {
// TransportAgnosticDialerProtocol is the L4 transport the module uses by convention (ex: SSH over TCP).
// This is only used to configure the DialerGroup.TransportAgnosticDialer. The L4Dialer can handle both UDP and TCP.
TransportAgnosticDialerProtocol TransportProtocol
// NeedSeparateL4Dialer indicates whether the module needs a dedicated L4 dialer in its DialerGroup.
// Some modules' protocols need to send some command (STARTTLS) after L4 connection and before TLS handshake.
// Others like http need to follow redirects to https:// and http:// servers, which requires both a TLS and L4 (TCP) conn.
// Still others may require a dialer that can handle both TCP and UDP.
// If this is true, the framework will ensure dialerGroup.L4Dialer is set.
// If false, the framework will set the TransportAgnosticDialer
NeedSeparateL4Dialer bool
BaseFlags *BaseFlags
// TLSEnabled indicates whether the module needs a TLS connection. The behavior depends on if NeedsL4Dialer is true.
// If NeedsL4Dialer is true, the framework will set up a TLSWrapper in the DialerGroup so a module can access both.
// If NeedsL4Dialer is false, the framework will set up a TLS dialer as the TransportAgnosticDialer since the module
// has indicated it only needs a TLS connection.
TLSEnabled bool
TLSFlags *TLSFlags // must be non-nil if TLSEnabled is true
}
DialerGroupConfig lets modules communicate what they'd need in a dialer group. The framework uses this to configure a default dialer group for the module.
func (*DialerGroupConfig) GetDefaultDialerGroupFromConfig ¶ added in v0.2.0
func (config *DialerGroupConfig) GetDefaultDialerGroupFromConfig() (*DialerGroup, error)
func (*DialerGroupConfig) Validate ¶ added in v0.2.0
func (config *DialerGroupConfig) Validate() error
Validate checks for various incompatibilities in the DialerGroupConfig
type FakeDNSServer ¶ added in v0.1.2
type FakeDNSServer struct {
// Any domain name will resolve to this IP. It can be either ipv4 or ipv6
IP net.IP
}
func (*FakeDNSServer) DialContext ¶ added in v0.1.2
This merely wraps a custom net.Conn, that is only good for DNS messages
type FlagMap ¶
FlagMap is a function that maps a single-bit bitmask (i.e. a number of the form (1 << x)) to a string representing that bit. If the input is not valid / recognized, it should return a non-nil error, which will cause the flag to be added to the "unknowns" list.
func GetFlagMapFromList ¶
GetFlagMapFromList returns a FlagMap function mapping the ith bit to the ith entry of bits. bits is a list of labels for the corresponding bits; any empty strings (and bits beyond the end of the list) are treated as unknown.
func GetFlagMapFromMap ¶
GetFlagMapFromMap returns a FlagMap function that uses mapping to do the mapping. Values not present in the map are treated as unknown, and a non-nil error is returned in those cases.
type GeneralOptions ¶ added in v1.0.0
type GeneralOptions struct {
Senders int `short:"s" long:"senders" description:"Number of send goroutines to use"`
GOMAXPROCS int `long:"gomaxprocs" description:"Set GOMAXPROCS to set the number of CPU cores to use. 0 uses all available. (default: 0)"`
Prometheus string `long:"prometheus" description:"Address to use for Prometheus server (e.g. localhost:8080). If empty, Prometheus is disabled."`
ReadLimitPerHost int `long:"read-limit-per-host" description:"Maximum total kilobytes to read for a single host"`
}
type Grab ¶
type Grab struct {
IP string `json:"ip,omitempty"`
Port uint `json:"port,omitempty"`
Domain string `json:"domain,omitempty"`
Data map[string]ScanResponse `json:"data,omitempty"`
Error string `json:"error,omitempty"` // an error that affects the entire grab, preventing any data from being returned
}
Grab contains all scan responses for a single host
func BuildGrabFromInputResponse ¶ added in v0.1.5
func BuildGrabFromInputResponse(t *ScanTarget, responses map[string]ScanResponse) *Grab
BuildGrabFromInputResponse constructs a Grab object for a target, given the scan responses.
type InputOutputOptions ¶ added in v1.0.0
type InputOutputOptions struct {
BlocklistFileName string `short:"b" long:"blocklist-file" description:"Blocklist filename, use - for $(HOME)/.config/zgrab2/blocklist.conf."`
InputFileName string `short:"f" long:"input-file" description:"Input filename, use - for stdin."`
LogFileName string `short:"l" long:"log-file" description:"Log filename, use - for stderr."`
MetaFileName string `short:"m" long:"metadata-file" description:"Metadata filename, use - for stderr."`
OutputFileName string `short:"o" long:"output-file" description:"Output filename, use - for stdout."`
StatusUpdatesFileName string `short:"u" long:"status-updates-file" description:"Status updates filename, use - for stderr."`
Debug bool `long:"debug" description:"Include debug fields in the output."`
Flush bool `long:"flush" description:"Flush after each line of output."`
}
type InputTargetsFunc ¶
type InputTargetsFunc func(ch chan<- ScanTarget) error
InputTargetsFunc is a function type for target input functions.
A function of this type generates ScanTargets on the provided channel. It returns nil if there are no further inputs or error.
type ModuleMetadata ¶ added in v1.0.0
type ModuleMetadata struct {
Successes uint `json:"successes"` // number of successful scans for this module
Failures uint `json:"failures"`
CustomMetadata any `json:"custom_metadata,omitempty"` // module-specific metadata, each module can implement this as they see fit
}
ModuleMetadata contains information of the status of a particular module's scan
type ModuleSet ¶ added in v0.1.3
type ModuleSet map[string]ScanModule
ModuleSet is a container holding named scan modules. It is a wrapper around a map.
func NewModuleSet ¶ added in v0.1.3
func NewModuleSet() ModuleSet
NewModuleSet returns an empty ModuleSet.
func (ModuleSet) AddModule ¶ added in v0.1.3
func (s ModuleSet) AddModule(name string, m ScanModule)
AddModule adds m to the ModuleSet, accessible via the given name. If the name is already in the ModuleSet, it is overwritten.
func (ModuleSet) CopyInto ¶ added in v0.1.3
CopyInto copies the modules in s to destination. The sets will be unique, but the underlying ScanModule instances will be the same.
func (ModuleSet) RemoveModule ¶ added in v0.1.3
RemoveModule removes the module at the specified name. If the name is not in the module set, nothing happens.
type Monitor ¶
type Monitor struct {
// Callback is invoked after each scan.
Callback func(string)
// contains filtered or unexported fields
}
Monitor is a collection of states per scans and a channel to communicate those scans to the monitor
func MakeMonitor ¶
MakeMonitor returns a Monitor object that can be used to collect and send the status of a running scan
func (*Monitor) GetStatuses ¶
func (m *Monitor) GetStatuses() map[string]*ModuleMetadata
GetStatuses returns a mapping from scanner names to the current number of successes and failures for that scanner
type MultipleCommand ¶
type MultipleCommand struct {
ConfigFileName string `short:"c" long:"config-file" default:"-" description:"Config filename, use - for stdin"`
ContinueOnError bool `long:"continue-on-error" description:"If proceeding protocols error, do not run following protocols (default: true)"`
BreakOnSuccess bool `long:"break-on-success" description:"If proceeding protocols succeed, do not run following protocols (default: false)"`
}
MultipleCommand contains the command line options for running
func (*MultipleCommand) Help ¶
func (x *MultipleCommand) Help() string
Help returns a usage string that will be output at the command line
func (*MultipleCommand) Validate ¶
func (x *MultipleCommand) Validate(_ []string) error
Validate the options sent to MultipleCommand
type NetworkingOptions ¶ added in v1.0.0
type NetworkingOptions struct {
ConnectionsPerHost int `long:"connections-per-host" description:"Number of times to connect to each host (results in more output)"`
DNSServerRateLimit int `long:"dns-rate-limit" description:"Rate limit for DNS lookups per second."`
DNSResolutionTimeout time.Duration `long:"dns-resolution-timeout" description:"Timeout for DNS resolution of target hostnames."`
CustomDNS string `` /* 184-byte string literal not displayed */
LocalAddrString string `` /* 201-byte string literal not displayed */
LocalPortString string `` /* 158-byte string literal not displayed */
UserIPv4Choice *bool `` /* 209-byte string literal not displayed */
UserIPv6Choice *bool `` /* 246-byte string literal not displayed */
ServerRateLimit int `long:"server-rate-limit" description:"Per-IP rate limit for connections to targets per second."`
}
type OutputResultsFunc ¶
OutputResultsFunc is a function type for result output functions.
A function of this type receives results on the provided channel and outputs them somehow. It returns nil if there are no further results or error.
func OutputResultsWriterFunc ¶ added in v0.1.4
func OutputResultsWriterFunc(w io.Writer) OutputResultsFunc
OutputResultsWriterFunc returns an OutputResultsFunc that wraps an io.Writer in a buffered writer, and uses OutputResults.
type ReadLimitExceededAction ¶
type ReadLimitExceededAction string
ReadLimitExceededAction describes how the connection reacts to an attempt to read more data than permitted.
type ScanError ¶
type ScanError struct {
Status ScanStatus
Err error
}
ScanError an error that also includes a ScanStatus.
func DetectScanError ¶
DetectScanError returns a ScanError that attempts to detect the status from the given error.
func NewScanError ¶
func NewScanError(status ScanStatus, err error) *ScanError
NewScanError returns a ScanError with the given status and error.
type ScanFlags ¶
type ScanFlags interface {
// Help optionally returns any additional help text, e.g. specifying what empty defaults are interpreted as.
Help() string
// Validate enforces all command-line flags and positional arguments have valid values.
Validate([]string) error
}
ScanFlags is an interface which must be implemented by all types sent to the flag parser
type ScanModule ¶
type ScanModule interface {
// NewFlags is called by the framework to pass to the argument parser. The parsed flags will be passed
// to the scanner created by NewScanner().
NewFlags() any
// NewScanner is called by the framework for each time an individual scan is specified in the config or on
// the command-line. The framework will then call scanner.Init(name, flags).
NewScanner() Scanner
// Description returns a string suitable for use as an overview of this module within usage text.
Description() string
}
ScanModule is an interface which represents a module that the framework can manipulate
func GetModule ¶
func GetModule(name string) ScanModule
GetModule returns the registered module that corresponds to the given name or nil otherwise
type ScanResponse ¶
type ScanResponse struct {
// Status is required for all responses.
Status ScanStatus `json:"status"`
// Protocol is the identifier if the protocol that did the scan. In the case of a complex scan, this may differ from
// the scan name.
Protocol string `json:"protocol"`
Port uint `json:"port"`
Result any `json:"result,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Error *string `json:"error,omitempty"`
}
ScanResponse is the result of a scan on a single host
func RunScanner ¶
func RunScanner(ctx context.Context, scanner Scanner, mon *Monitor, target ScanTarget) (string, ScanResponse)
RunScanner runs a single scan on a target and returns the resulting data
type ScanStatus ¶
type ScanStatus string
ScanStatus is the enum value that states how the scan ended.
func TryGetScanStatus ¶
func TryGetScanStatus(err error) ScanStatus
TryGetScanStatus attempts to get the ScanStatus enum value corresponding to the given error. Mostly supports network errors. A nil error is interpreted as SCAN_SUCCESS. An unrecognized error is interpreted as SCAN_UNKNOWN_ERROR.
type ScanTarget ¶
ScanTarget is the host that will be scanned
func (*ScanTarget) Host ¶
func (target *ScanTarget) Host() string
Host gets the host identifier as a string: the IP address if it is available, or the domain if not.
func (ScanTarget) String ¶
func (target ScanTarget) String() string
type Scanner ¶
type Scanner interface {
// Init runs once for this module at library init time
Init(flags ScanFlags) error
// InitPerSender runs once per Goroutine. A single Goroutine will scan some non-deterministic
// subset of the input scan targets
InitPerSender(senderID int) error
// Returns the name passed at init
GetName() string
// Returns the trigger passed at init
GetTrigger() string
// Protocol returns the protocol identifier for the scan.
Protocol() string
// Scan connects to a host. The result should be JSON-serializable. If a scan requires a dialer that isn't set in
// the dialer group, an error will return.
// ctx - The context for a scan, can be used to set timeouts or cancel scans
// dialerGroup - A collection of connection dialers that the module will call to establish L4 and L6 (TLS, typically)
// connections before doing any protocol-specific logic.
// t - The target to scan, including IP or domain, port, and any tags.
// Returns a ScanStatus, the result (or nil) of the protocol scan (entirely protocol dependent), and any error that occurred
Scan(ctx context.Context, dialerGroup *DialerGroup, t *ScanTarget) (ScanStatus, any, error)
// GetDialerGroupConfig returns a DialerGroupConfig that the framework will use to set up the dialer group using the module's
// desired dialer configuration.
GetDialerGroupConfig() *DialerGroupConfig
// GetScanMetadata returns any module-specific metadata that should be included in the final output
// Including json struct tags is up to the module
// If no metadata is needed, return nil
GetScanMetadata() any
}
Scanner exposes the functions for an application module to implement in order to be used by the ZGrab2 scanning framework
type TLSConnection ¶
func (*TLSConnection) Close ¶
func (conn *TLSConnection) Close() error
Close the underlying connection.
func (*TLSConnection) GetLog ¶
func (z *TLSConnection) GetLog() *TLSLog
func (*TLSConnection) Handshake ¶
func (z *TLSConnection) Handshake() error
type TLSFlags ¶
type TLSFlags struct {
Config *tls.Config // Config is ready to use TLS configuration
SessionTicket bool `long:"session-ticket" description:"Send support for TLS Session Tickets and output ticket if presented" json:"session"`
ExtendedMasterSecret bool `long:"extended-master-secret" description:"Offer RFC 7627 Extended Master Secret extension" json:"extended"`
ExtendedRandom bool `long:"extended-random" description:"Send TLS Extended Random Extension" json:"extran"`
NoSNI bool `long:"no-sni" description:"Do not send domain name in TLS Handshake regardless of whether known" json:"sni"`
SCTExt bool `long:"sct" description:"Request Signed Certificate Timestamps during TLS Handshake" json:"sct"`
// TODO: Do we just lump this with Verbose (and put Verbose in TLSFlags)?
KeepClientLogs bool `long:"keep-client-logs" description:"Include the client-side logs in the TLS handshake"`
Time string `long:"time" description:"Explicit request time to use, instead of clock. YYYYMMDDhhmmss format."`
// TODO: directory? glob? How to map server name -> certificate?
Certificates string `long:"certificates" description:"Set of certificates to present to the server"`
// TODO: re-evaluate this, or at least specify the file format
CertificateMap string `long:"certificate-map" description:"A file mapping server names to certificates"`
// TODO: directory? glob?
RootCAs string `long:"root-cas" description:"Set of certificates to use when verifying server certificates"`
// TODO: format?
NextProtos string `long:"next-protos" description:"A list of supported application-level protocols"`
ServerName string `long:"server-name" description:"Server name used for certificate verification and (optionally) SNI"`
VerifyServerCertificate bool `` /* 168-byte string literal not displayed */
// TODO: format? mapping? zgrab1 had flags like ChromeOnly, FirefoxOnly, etc...
CipherSuite string `long:"cipher-suite" description:"A comma-delimited list of hex cipher suites to advertise."`
MinVersion int `long:"min-version" description:"The minimum SSL/TLS version that is acceptable. 0 means that TLS1.0 is the minimum."`
MaxVersion int `long:"max-version" description:"The maximum SSL/TLS version that is acceptable. 0 means use the highest supported value."`
CurvePreferences string `long:"curve-preferences" description:"A list of elliptic curves used in an ECDHE handshake, in order of preference."`
NoECDHE bool `long:"no-ecdhe" description:"Do not allow ECDHE handshakes"`
// TODO: format?
SignatureAlgorithms string `long:"signature-algorithms" description:"Signature and hash algorithms that are acceptable"`
DSAEnabled bool `long:"dsa-enabled" description:"Accept server DSA keys"`
// TODO: format?
ClientRandom string `long:"client-random" description:"Set an explicit Client Random (base64 encoded)"`
// TODO: format?
ClientHello string `long:"client-hello" description:"Set an explicit ClientHello (base64 encoded)"`
OverrideSH bool `long:"override-sig-hash" description:"Override the default SignatureAndHashes TLS option with more expansive default"`
}
Common flags for TLS configuration -- include this in your module's ScanFlags implementation to use the common TLS code Adapted from modules/ssh.go
func (*TLSFlags) GetTLSConfigForTarget ¶
func (t *TLSFlags) GetTLSConfigForTarget(target *ScanTarget) (*tls.Config, error)
type TLSLog ¶
type TLSLog struct {
// TODO include TLSFlags?
HandshakeLog *tls.ServerHandshake `json:"handshake_log"`
}
type TimeoutConnection ¶
type TimeoutConnection struct {
net.Conn
SessionTimeout time.Duration // used to set the connection deadline, set once
ReadTimeout time.Duration // used to set the read deadline, set fresh for each read
WriteTimeout time.Duration // used to set the write deadline, set fresh for each write
BytesRead int
BytesWritten int
BytesReadLimit int
ReadLimitExceededAction ReadLimitExceededAction
Cancel context.CancelFunc
// contains filtered or unexported fields
}
TimeoutConnection wraps an existing net.Conn connection, overriding the Read/Write methods to use the configured timeouts TODO: Refactor this into TimeoutConnection, BoundedReader, LoggedReader, etc
func NewTimeoutConnection ¶
func NewTimeoutConnection(ctx context.Context, conn net.Conn, sessionTimeout, readTimeout, writeTimeout time.Duration, bytesReadLimit int) *TimeoutConnection
NewTimeoutConnection returns a new TimeoutConnection with the appropriate defaults.
func (*TimeoutConnection) Close ¶
func (c *TimeoutConnection) Close() error
Close the underlying connection.
func (*TimeoutConnection) Read ¶
func (c *TimeoutConnection) Read(b []byte) (n int, err error)
TimeoutConnection.Read calls Read() on the underlying connection, using any configured deadlines
func (*TimeoutConnection) SaturateTimeoutsToReadAndWriteTimeouts ¶ added in v0.2.0
func (c *TimeoutConnection) SaturateTimeoutsToReadAndWriteTimeouts()
SaturateTimeoutsToReadAndWriteTimeouts gets the minimum of the context deadline, the timeout, and the read/write timeouts and sets the read/write timeouts accordingly. This is necessary because the underlying connection only supports a deadline on reads and a deadline on writes, so we need to compute the minimum of all these to find what to set the underlying conn's read/write deadlines to.
func (*TimeoutConnection) SetDeadline ¶
func (c *TimeoutConnection) SetDeadline(deadline time.Time) error
SetDeadline sets a read / write deadline that will override the deadline for a single read/write.
func (*TimeoutConnection) SetReadDeadline ¶
func (c *TimeoutConnection) SetReadDeadline(deadline time.Time) error
SetReadDeadline sets an explicit ReadDeadline that will override the timeout for one read.
func (*TimeoutConnection) SetWriteDeadline ¶
func (c *TimeoutConnection) SetWriteDeadline(deadline time.Time) error
SetWriteDeadline sets an explicit WriteDeadline that will override the WriteDeadline for one write.
type TransportProtocol ¶ added in v0.2.0
type TransportProtocol uint
TransportProtocol is an enum for the transport layer protocol of a module
const ( TransportTCP TransportProtocol TransportUDP )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package bin contains functions useful for creating a binary version of ZGrab2.
|
Package bin contains functions useful for creating a binary version of ZGrab2. |
|
cmd
|
|
|
zgrab2
command
|
|
|
lib
|
|
|
http
Package http provides HTTP client and server implementations.
|
Package http provides HTTP client and server implementations. |
|
http/cookiejar
Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar.
|
Package cookiejar implements an in-memory RFC 6265-compliant http.CookieJar. |
|
http/httptest
Package httptest provides utilities for HTTP testing.
|
Package httptest provides utilities for HTTP testing. |
|
http/httptrace
Phillip - This was origianally from internal/nettrace, but I moved it here so tests would function
|
Phillip - This was origianally from internal/nettrace, but I moved it here so tests would function |
|
http/httputil
Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package.
|
Package httputil provides HTTP utility functions, complementing the more common ones in the net/http package. |
|
http/internal
Package internal contains HTTP internals shared by net/http and net/http/httputil.
|
Package internal contains HTTP internals shared by net/http and net/http/httputil. |
|
http/internal/testcert
Package testcert contains a test-only localhost certificate.
|
Package testcert contains a test-only localhost certificate. |
|
http2
// Copyright 2024 The Go Authors.
|
// Copyright 2024 The Go Authors. |
|
http2/h2c
Package h2c implements the unencrypted "h2c" form of HTTP/2.
|
Package h2c implements the unencrypted "h2c" form of HTTP/2. |
|
http2/h2i
command
The h2i command is an interactive HTTP/2 console.
|
The h2i command is an interactive HTTP/2 console. |
|
http2/hpack
Package hpack implements HPACK, a compression format for efficiently representing HTTP header fields in the context of HTTP/2.
|
Package hpack implements HPACK, a compression format for efficiently representing HTTP header fields in the context of HTTP/2. |
|
mysql
Package mysql is a very basic MySQL connection library.
|
Package mysql is a very basic MySQL connection library. |
|
output
Package output contains utilities for processing results from zgrab2 scanners for eventual output and consumption by ztag.
|
Package output contains utilities for processing results from zgrab2 scanners for eventual output and consumption by ztag. |
|
ssh
Package ssh implements an SSH client and server.
|
Package ssh implements an SSH client and server. |
|
ssh/internal/bcrypt_pbkdf
Package bcrypt_pbkdf implements bcrypt_pbkdf(3) from OpenBSD.
|
Package bcrypt_pbkdf implements bcrypt_pbkdf(3) from OpenBSD. |
|
ssh/internal/poly1305
Package poly1305 implements Poly1305 one-time message authentication code as specified in https://cr.yp.to/mac/poly1305-20050329.pdf.
|
Package poly1305 implements Poly1305 one-time message authentication code as specified in https://cr.yp.to/mac/poly1305-20050329.pdf. |
|
ssh/knownhosts
Package knownhosts implements a parser for the OpenSSH known_hosts host key database, and provides utility functions for writing OpenSSH compliant known_hosts files.
|
Package knownhosts implements a parser for the OpenSSH known_hosts host key database, and provides utility functions for writing OpenSSH compliant known_hosts files. |
|
ssh/test
Package test contains integration tests for the github.com/zmap/zgrab2/lib/ssh package.
|
Package test contains integration tests for the github.com/zmap/zgrab2/lib/ssh package. |
|
bacnet
Package bacnet provides a zgrab2 module that scans for bacnet.
|
Package bacnet provides a zgrab2 module that scans for bacnet. |
|
dnp3
Package dnp3 provides a zgrab2 module that scans for dnp3.
|
Package dnp3 provides a zgrab2 module that scans for dnp3. |
|
fox
Package fox provides a zgrab2 module that scans for fox.
|
Package fox provides a zgrab2 module that scans for fox. |
|
ftp
Package ftp contains the zgrab2 Module implementation for FTP(S).
|
Package ftp contains the zgrab2 Module implementation for FTP(S). |
|
http
Package http contains the zgrab2 Module implementation for HTTP(S).
|
Package http contains the zgrab2 Module implementation for HTTP(S). |
|
imap
Package imap provides a zgrab2 module that scans for IMAP mail servers.
|
Package imap provides a zgrab2 module that scans for IMAP mail servers. |
|
ipp
Package ipp provides a zgrab2 module that scans for ipp.
|
Package ipp provides a zgrab2 module that scans for ipp. |
|
jarm
Ref: https://github.com/salesforce/jarm https://engineering.salesforce.com/easily-identify-malicious-servers-on-the-internet-with-jarm-e095edac525a?gi=4dd05e2277e4
|
Ref: https://github.com/salesforce/jarm https://engineering.salesforce.com/easily-identify-malicious-servers-on-the-internet-with-jarm-e095edac525a?gi=4dd05e2277e4 |
|
managesieve
Package main provides a zgrab2 module that scans for ManageSieve servers.
|
Package main provides a zgrab2 module that scans for ManageSieve servers. |
|
modbus
Package modbus provides a zgrab2 module that scans for modbus.
|
Package modbus provides a zgrab2 module that scans for modbus. |
|
mssql
Package mssql provides the zgrab2 scanner module for the MSSQL protocol.
|
Package mssql provides the zgrab2 scanner module for the MSSQL protocol. |
|
mysql
Package mysql provides the mysql implementation of the zgrab2.Module.
|
Package mysql provides the mysql implementation of the zgrab2.Module. |
|
ntp
Package ntp provides a zgrab2 module that probes for the NTP service.
|
Package ntp provides a zgrab2 module that probes for the NTP service. |
|
oracle
Package oracle provides the zgrab2 scanner module for Oracle's TNS protocol.
|
Package oracle provides the zgrab2 scanner module for Oracle's TNS protocol. |
|
pop3
Package pop3 provides a zgrab2 module that scans for POP3 mail servers.
|
Package pop3 provides a zgrab2 module that scans for POP3 mail servers. |
|
postgres
Package postgres contains the postgres zgrab2 Module implementation.
|
Package postgres contains the postgres zgrab2 Module implementation. |
|
pptp
Package pptp contains the zgrab2 Module implementation for PPTP.
|
Package pptp contains the zgrab2 Module implementation for PPTP. |
|
redis
Package redis provides a zgrab2 Module that probes for redis services.
|
Package redis provides a zgrab2 Module that probes for redis services. |
|
siemens
Package siemens provides a zgrab2 module that scans for Siemens S7.
|
Package siemens provides a zgrab2 module that scans for Siemens S7. |
|
smb
Package smb provides a zgrab2 module that scans for smb.
|
Package smb provides a zgrab2 module that scans for smb. |
|
smtp
Package smtp provides a zgrab2 module that scans for SMTP mail servers.
|
Package smtp provides a zgrab2 module that scans for SMTP mail servers. |
|
socks5
Package socks5 contains the zgrab2 Module implementation for SOCKS5.
|
Package socks5 contains the zgrab2 Module implementation for SOCKS5. |
|
telnet
Package telnet provides a zgrab2 module that scans for telnet daemons.
|
Package telnet provides a zgrab2 module that scans for telnet daemons. |
|
tools
|
|