nuclei/v2/pkg/protocols/network/network.go

92 lines
2.5 KiB
Go
Raw Normal View History

2020-12-30 09:24:20 +00:00
package network
import (
"net"
"strings"
"github.com/pkg/errors"
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/network/networkclientpool"
)
// Request contains a Network protocol request to be made from a template
type Request struct {
2021-01-16 08:40:24 +00:00
ID string `yaml:"id"`
2020-12-30 09:24:20 +00:00
// Address is the address to send requests to (host:port combos generally)
Address []string `yaml:"host"`
addresses []keyValue
2020-12-30 09:24:20 +00:00
// Payload is the payload to send for the network request
Inputs []*Input `yaml:"inputs"`
2020-12-30 09:24:20 +00:00
// ReadSize is the size of response to read (1024 if not provided by default)
ReadSize int `yaml:"read-size"`
// Operators for the current request go here.
operators.Operators `yaml:",inline"`
CompiledOperators *operators.Operators
// cache any variables that may be needed for operation.
dialer *fastdialer.Dialer
options *protocols.ExecuterOptions
}
// keyValue is a key value pair
type keyValue struct {
key string
value string
}
// Input is the input to send on the network
type Input struct {
// Data is the data to send as the input
Data string `yaml:"data"`
// Type is the type of input - hex, text.
Type string `yaml:"type"`
}
2021-01-16 08:40:24 +00:00
// GetID returns the unique ID of the request if any.
func (r *Request) GetID() string {
return r.ID
}
2020-12-30 09:24:20 +00:00
// Compile compiles the protocol request for further execution.
func (r *Request) Compile(options *protocols.ExecuterOptions) error {
var err error
for _, address := range r.Address {
if strings.Contains(address, ":") {
addressHost, addressPort, err := net.SplitHostPort(address)
if err != nil {
return errors.Wrap(err, "could not parse address")
}
r.addresses = append(r.addresses, keyValue{key: addressHost, value: addressPort})
} else {
r.addresses = append(r.addresses, keyValue{key: address})
2020-12-30 09:24:20 +00:00
}
}
// Create a client for the class
client, err := networkclientpool.Get(options.Options, &networkclientpool.Configuration{})
if err != nil {
return errors.Wrap(err, "could not get network client")
}
r.dialer = client
if len(r.Matchers) > 0 || len(r.Extractors) > 0 {
compiled := &r.Operators
if err := compiled.Compile(); err != nil {
return errors.Wrap(err, "could not compile operators")
}
r.CompiledOperators = compiled
}
r.options = options
return nil
}
// Requests returns the total number of requests the YAML rule will perform
func (r *Request) Requests() int {
2021-01-11 14:08:16 +00:00
return len(r.Address)
2020-12-30 09:24:20 +00:00
}