113 lines
3.3 KiB
Go
Raw Normal View History

2020-12-30 14:54:20 +05:30
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/common/expressions"
2020-12-30 14:54:20 +05:30
"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 14:10:24 +05:30
ID string `yaml:"id"`
2021-02-18 01:37:48 +01:00
// Address is the address to send requests to (host:port:tls combos generally)
Address []string `yaml:"host"`
2021-02-19 00:10:06 +01:00
addresses []addressKV
2020-12-30 14:54:20 +05:30
// Payload is the payload to send for the network request
Inputs []*Input `yaml:"inputs"`
2020-12-30 14:54:20 +05:30
// 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,omitempty"`
2020-12-30 14:54:20 +05:30
CompiledOperators *operators.Operators
// cache any variables that may be needed for operation.
dialer *fastdialer.Dialer
options *protocols.ExecuterOptions
}
2021-02-19 00:10:06 +01:00
type addressKV struct {
ip string
port string
tls bool
}
// 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-02-16 14:46:23 +05:30
// Read is the number of bytes to read from socket
Read int `yaml:"read"`
2021-02-24 20:11:21 +05:30
// Name is the optional name of the input to provide matching on
Name string `yaml:"name"`
}
2021-01-16 14:10:24 +05:30
// GetID returns the unique ID of the request if any.
func (r *Request) GetID() string {
return r.ID
}
2020-12-30 14:54:20 +05:30
// Compile compiles the protocol request for further execution.
func (r *Request) Compile(options *protocols.ExecuterOptions) error {
2021-02-19 00:10:06 +01:00
var shouldUseTLS bool
2021-02-23 18:13:00 +05:30
var err error
for _, address := range r.Address {
2021-02-19 00:10:06 +01:00
// check if the connection should be encrypted
2021-02-23 18:13:00 +05:30
if strings.HasPrefix(address, "tls://") {
2021-02-19 00:10:06 +01:00
shouldUseTLS = true
2021-02-23 18:13:00 +05:30
address = strings.TrimPrefix(address, "tls://")
2021-02-19 00:10:06 +01:00
}
if strings.Contains(address, ":") {
2021-02-26 13:13:11 +05:30
addressHost, addressPort, portErr := net.SplitHostPort(address)
if portErr != nil {
return errors.Wrap(portErr, "could not parse address")
}
2021-02-19 00:10:06 +01:00
r.addresses = append(r.addresses, addressKV{ip: addressHost, port: addressPort, tls: shouldUseTLS})
} else {
2021-02-19 00:10:06 +01:00
r.addresses = append(r.addresses, addressKV{ip: address, tls: shouldUseTLS})
2020-12-30 14:54:20 +05:30
}
}
// Pre-compile any input dsl functions before executing the request.
for _, input := range r.Inputs {
if input.Type != "" {
continue
}
2021-02-26 13:13:11 +05:30
if compiled, evalErr := expressions.Evaluate(input.Data, map[string]interface{}{}); evalErr == nil {
input.Data = compiled
}
}
2020-12-30 14:54:20 +05:30
// 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 19:38:16 +05:30
return len(r.Address)
2020-12-30 14:54:20 +05:30
}