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"
|
2021-02-24 06:37:16 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2021-07-06 12:57:30 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
|
2020-12-30 09:24:20 +00:00
|
|
|
"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"`
|
|
|
|
|
2021-02-18 00:37:48 +00:00
|
|
|
// Address is the address to send requests to (host:port:tls combos generally)
|
2020-12-30 15:44:04 +00:00
|
|
|
Address []string `yaml:"host"`
|
2021-02-18 23:10:06 +00:00
|
|
|
addresses []addressKV
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-07-06 12:57:30 +00:00
|
|
|
// AttackType is the attack type
|
|
|
|
// Sniper, PitchFork and ClusterBomb. Default is Sniper
|
|
|
|
AttackType string `yaml:"attack"`
|
|
|
|
// Path contains the path/s for the request variables
|
|
|
|
Payloads map[string]interface{} `yaml:"payloads"`
|
|
|
|
|
2020-12-30 09:24:20 +00:00
|
|
|
// Payload is the payload to send for the network request
|
2020-12-30 11:17:22 +00:00
|
|
|
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.
|
2021-02-21 11:01:34 +00:00
|
|
|
operators.Operators `yaml:",inline,omitempty"`
|
2020-12-30 09:24:20 +00:00
|
|
|
CompiledOperators *operators.Operators
|
|
|
|
|
2021-07-06 12:57:30 +00:00
|
|
|
generator *generators.Generator
|
|
|
|
attackType generators.Type
|
2020-12-30 09:24:20 +00:00
|
|
|
// cache any variables that may be needed for operation.
|
|
|
|
dialer *fastdialer.Dialer
|
|
|
|
options *protocols.ExecuterOptions
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:10:06 +00:00
|
|
|
type addressKV struct {
|
|
|
|
ip string
|
|
|
|
port string
|
|
|
|
tls bool
|
2020-12-30 15:44:04 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 11:17:22 +00:00
|
|
|
// 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 09:16:23 +00:00
|
|
|
// Read is the number of bytes to read from socket
|
|
|
|
Read int `yaml:"read"`
|
2021-02-24 14:41:21 +00:00
|
|
|
// Name is the optional name of the input to provide matching on
|
|
|
|
Name string `yaml:"name"`
|
2020-12-30 11:17:22 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-02-18 23:10:06 +00:00
|
|
|
var shouldUseTLS bool
|
2021-02-23 12:43:00 +00:00
|
|
|
var err error
|
|
|
|
|
2021-07-06 12:57:30 +00:00
|
|
|
r.options = options
|
2020-12-30 15:44:04 +00:00
|
|
|
for _, address := range r.Address {
|
2021-02-18 23:10:06 +00:00
|
|
|
// check if the connection should be encrypted
|
2021-02-23 12:43:00 +00:00
|
|
|
if strings.HasPrefix(address, "tls://") {
|
2021-02-18 23:10:06 +00:00
|
|
|
shouldUseTLS = true
|
2021-02-23 12:43:00 +00:00
|
|
|
address = strings.TrimPrefix(address, "tls://")
|
2021-02-18 23:10:06 +00:00
|
|
|
}
|
2020-12-30 15:44:04 +00:00
|
|
|
if strings.Contains(address, ":") {
|
2021-02-26 07:43:11 +00:00
|
|
|
addressHost, addressPort, portErr := net.SplitHostPort(address)
|
|
|
|
if portErr != nil {
|
|
|
|
return errors.Wrap(portErr, "could not parse address")
|
2020-12-30 15:44:04 +00:00
|
|
|
}
|
2021-02-18 23:10:06 +00:00
|
|
|
r.addresses = append(r.addresses, addressKV{ip: addressHost, port: addressPort, tls: shouldUseTLS})
|
2020-12-30 15:44:04 +00:00
|
|
|
} else {
|
2021-02-18 23:10:06 +00:00
|
|
|
r.addresses = append(r.addresses, addressKV{ip: address, tls: shouldUseTLS})
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-24 06:37:16 +00:00
|
|
|
// Pre-compile any input dsl functions before executing the request.
|
|
|
|
for _, input := range r.Inputs {
|
|
|
|
if input.Type != "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-26 07:43:11 +00:00
|
|
|
if compiled, evalErr := expressions.Evaluate(input.Data, map[string]interface{}{}); evalErr == nil {
|
|
|
|
input.Data = compiled
|
2021-02-24 06:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-07-06 12:57:30 +00:00
|
|
|
if len(r.Payloads) > 0 {
|
|
|
|
attackType := r.AttackType
|
|
|
|
if attackType == "" {
|
|
|
|
attackType = "sniper"
|
|
|
|
}
|
|
|
|
r.attackType = generators.StringToType[attackType]
|
|
|
|
|
|
|
|
// Resolve payload paths if they are files.
|
|
|
|
for name, payload := range r.Payloads {
|
|
|
|
payloadStr, ok := payload.(string)
|
|
|
|
if ok {
|
|
|
|
final, resolveErr := options.Catalog.ResolvePath(payloadStr, options.TemplatePath)
|
|
|
|
if resolveErr != nil {
|
|
|
|
return errors.Wrap(resolveErr, "could not read payload file")
|
|
|
|
}
|
|
|
|
r.Payloads[name] = final
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r.generator, err = generators.New(r.Payloads, r.attackType, r.options.TemplatePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not parse payloads")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|