Categorygithub.com/packetloop/zgrab2
modulepackage
0.0.0-20240523144707-f5b61b103af6
Repository: https://github.com/packetloop/zgrab2.git
Documentation: pkg.go.dev

# README

ZGrab 2.0

NETSCOUT NOTES: This repository is a direct clone of a 3rd party vendor.
No changes should ever be made on vendor branches to allow us to stay in sync withhttps://github.com/zmap/zgrab2. NETSCOUT development changes should be worked on a branch based from 'netscout-master' and merged into the 'netscout-master' branch.

This repo contains the new ZGrab framework, and will eventually replace https://github.com/zmap/zgrab.

Building

You will need to have a valid $GOPATH set up, for more information about $GOPATH, see https://golang.org/doc/code.html.

Once you have a working $GOPATH, run:

$ go get github.com/packetloop/zgrab2

This will install zgrab under $GOPATH/src/github.com/packetloop/zgrab2

$ cd $GOPATH/src/github.com/packetloop/zgrab2
$ make

Single Module Usage

ZGrab2 supports modules. For example, to run the ssh module use

./zgrab2 ssh

Module specific options must be included after the module. Application specific options can be specified at any time.

Input Format

Targets are specified with input files or from stdin, in CSV format. Each input line has three fields:

IP, DOMAIN, TAG

Each line must specify IP, DOMAIN, or both. If only DOMAIN is provided, scanners perform a DNS hostname lookup to determine the IP address. If both IP and DOMAIN are provided, scanners connect to IP but use DOMAIN in protocol-specific contexts, such as the HTTP HOST header and TLS SNI extension.

If the IP field contains a CIDR block, the framework will expand it to one target for each IP address in the block.

The TAG field is optional and used with the --trigger scanner argument.

Unused fields can be blank, and trailing unused fields can be omitted entirely. For backwards compatibility, the parser allows lines with only one field to contain DOMAIN.

These are examples of valid input lines:

10.0.0.1
domain.com
10.0.0.1, domain.com
10.0.0.1, domain.com, tag
10.0.0.1, , tag
, domain.com, tag
192.168.0.0/24, , tag

Multiple Module Usage

To run a scan with multiple modules, a .ini file must be used with the multiple module. Below is an example .ini file with the corresponding zgrab2 command.

multiple.ini

[Application Options]
output-file="output.txt"
input-file="input.txt"
[http]
name="http80"
port=80
endpoint="/"
[http]
name="http8080"
port=8080
endpoint="/"
[ssh]
port=22
./zgrab2 multiple -c multiple.ini

Application Options must be the initial section name. Other section names should correspond exactly to the relevant zgrab2 module name. The default name for each module is the command name. If the same module is to be used multiple times then name must be specified and unique.

Multiple module support is particularly powerful when combined with input tags and the --trigger scanner argument. For example, this input contains targets with two different tags:

141.212.113.199, , tagA
216.239.38.21, censys.io, tagB

Invoking zgrab2 with the following multiple configuration will perform an SSH grab on the first target above and an HTTP grab on the second target:

[ssh]
trigger="tagA"
name="ssh22"
port=22

[http]
trigger="tagB"
name="http80"
port=80

Adding New Protocols

Add module to modules/ that satisfies the following interfaces: Scanner, ScanModule, ScanFlags.

The flags struct must embed zgrab2.BaseFlags. In the modules init() function the following must be included.

func init() {
    var newModule NewModule
    _, err := zgrab2.AddCommand("module", "short description", "long description of module", portNumber, &newModule)
    if err != nil {
        log.Fatal(err)
    }
}

Output schema

To add a schema for the new module, add a module under schemas, and update schemas/__init__.py to ensure that it is loaded.

See schemas/README.md for details.

Integration tests

To add integration tests for the new module, run integration_tests/new.sh [your_new_protocol_name]. This will add stub shell scripts in integration_tests/your_new_protocol_name; update these as needed. See integration_tests/mysql/* for an example. The only hard requirement is that the test.sh script drops its output in $ZGRAB_OUTPUT/[your-module]/*.json, so that it can be validated against the schema.

How to Run Integration Tests

To run integration tests, you must have Docker installed. Then, you can follow the following steps to run integration tests:

$ go get github.com/jmespath/jp && go build github.com/jmespath/jp
$ pip install --user zschema
$ make integration-test

Running the integration tests will generate quite a bit of debug output. To ensure that tests completed successfully, you can check for a successful exit code after the tests complete:

$ echo $?
0

License

ZGrab2.0 is licensed under Apache 2.0 and ISC. For more information, see the LICENSE file.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

AddCommand adds a module to the parser and returns a pointer to a flags.command object or an error.
AddGroup exposes the parser's AddGroup function, allowing extension of the global arguments.
DetectScanError returns a ScanError that attempts to detect the status from the given error.
DialTimeoutConnection dials the target and returns a net.Conn that uses the configured single timeout for all operations.
DialTimeoutConnectionEx dials the target and returns a net.Conn that uses the configured timeouts for Read/Write operations.
No description provided by the author
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/packetloop/zgrab2/wiki/Scanner-details).
GetFlagMapFromList returns a FlagMap function mapping the ith bit to the ith entry of bits.
GetFlagMapFromMap returns a FlagMap function that uses mapping to do the mapping.
No description provided by the author
GetMetaFile returns the file to which metadata should be output.
GetModule returns the registered module that corresponds to the given name or nil otherwise.
No description provided by the author
GetTargetsBitmap reads targets from a bitmap source.
GetTargetsCSV reads targets from a CSV source, generates ScanTargets, and delivers them to the provided channel.
GetTimeoutConnectionDialer gets a Dialer that dials connections with the given timeout.
GetTimeoutDialFunc returns a DialFunc that dials with the given timeout.
No description provided by the author
InputTargetsCSV is an InputTargetsFunc that calls GetTargetsCSV with the CSV file provided on the command line.
No description provided by the author
IsTimeoutError checks if the given error corresponds to a timeout (of any type).
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/packetloop/zgrab2/wiki/Scanner-details).
LogPanic is intended to be called from within defer -- if there was no panic, it returns without doing anything.
MakeMonitor returns a Monitor object that can be used to collect and send the status of a running scan.
MapFlagsToSet gets the "set" (map of strings to true) of values corresponding to the bits in flags.
NewDialer creates a new Dialer with default settings.
NewIniParser creates and returns a ini parser initialized with the default parser.
NewScanError returns a ScanError with the given status and error.
NewTimeoutConnection returns a new TimeoutConnection with the appropriate defaults.
OutputResultsFile is an OutputResultsFunc that write results to a filename provided on the command line.
No description provided by the author
No description provided by the author
ParseCommandLine parses the commands given on the command line and validates the framework configuration (global options) immediately after parsing.
ParseCSVTarget takes a record from a CSV-format input file and returns the specified ipnet, domain, and tag, or an error.
PrintScanners prints all registered scanners.
Process sets up an output encoder, input reader, and starts grab workers.
ReadAvaiable reads what it can without blocking for more than defaultReadTimeout per read, or defaultTotalTimeout for the whole session.
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.
ReadUntilRegex calls connection.Read() until it returns an error, or the cumulatively-read data matches the given regexp.
RegisterScan registers each individual scanner to be ran by the framework.
RunScanner runs a single scan on a target and returns the resulting data.
SetInputFunc sets the target input function to the provided function.
SetOutputFunc sets the result output function to the provided function.
TLDMatches checks for a strict TLD match.
TryGetScanStatus attempts to get the ScanStatus enum value corresponding to the given error.
WidenMapKeys copies a map with int keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
WidenMapKeys16 copies a map with uint8 keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
WidenMapKeys32 copies a map with uint8 keys into an equivalent map with uint64 keys for use in the FlagsToSet function.
WidenMapKeys8 copies a map with uint8 keys into an equivalent map with uint64 keys for use in the FlagsToSet function.

# Constants

No description provided by the author
ReadLimitExceededActionError causes the Read call to return n, ErrReadLimitExceeded (in addition to truncating).
ReadLimitExceededActionNotSet is a placeholder for the zero value, so that explicitly set values can be distinguished from the empty default.
ReadLimitExceededActionPanic causes the Read call to panic(ErrReadLimitExceeded).
ReadLimitExceededActionTruncate causes the connection to truncate at BytesReadLimit bytes and return a bogus io.EOF error.
The application reported an error.
The TCP connection was unexpectedly closed.
TCP connection was actively rejected.
No response to TCP connection request.
Timed out waiting on data.
Received data incompatible with the target protocol.
The protocol in question was positively identified and the scan encountered no errors.
Catch-all for unrecognized errors.

# Variables

DefaultBytesReadLimit is the maximum number of bytes to read per connection when no explicit value is provided.
DefaultReadLimitExceededAction is the action used when no explicit action is set.
DefaultSessionTimeout is the default maximum time a connection may be used when no explicit value is provided.
ErrInvalidArguments is thrown if the command-line arguments invalid.
ErrInvalidResponse is returned when the server returns a syntactically-invalid response.
ErrMismatchedFlags is thrown if the flags for one module type are passed to an incompatible module type.
ErrReadLimitExceeded is returned / panic'd from Read if the read limit is exceeded when the ReadLimitExceededAction is error / panic.
ErrUnexpectedResponse is returned when the server returns a syntactically-valid but unexpected response.
No description provided by the author

# Structs

BaseFlags contains the options that every flags type must embed.
Config is the high level framework options that will be parsed from the command line.
Dialer provides Dial and DialContext methods to get connections with the given timeout.
Grab contains all scan responses for a single host.
Monitor is a collection of states per scans and a channel to communicate those scans to the monitor.
MultipleCommand contains the command line options for running.
ScanError an error that also includes a ScanStatus.
ScanResponse is the result of a scan on a single host.
ScanTarget is the host that will be scanned.
State contains the respective number of successes and failures for a given scan.
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.
No description provided by the author
Common flags for TLS configuration -- include this in your module's ScanFlags implementation to use the common TLS code Adapted from modules/ssh.go.
No description provided by the author
UDPFlags contains the common options used for all UDP scans.

# Interfaces

ScanFlags is an interface which must be implemented by all types sent to the flag parser.
ScanModule is an interface which represents a module that the framework can manipulate.
Scanner is an interface that represents all functions necessary to run a scan.

# Type aliases

FlagMap is a function that maps a single-bit bitmask (i.e.
InputTargetsFunc is a function type for target input functions.
OutputResultsFunc is a function type for result output functions.
ReadLimitExceededAction describes how the connection reacts to an attempt to read more data than permitted.
ScanStatus is the enum value that states how the scan ended.