198 lines
7.2 KiB
Go
Raw Normal View History

2020-12-30 14:54:20 +05:30
package network
import (
"net"
"strings"
"github.com/pkg/errors"
2021-09-07 17:31:46 +03:00
2020-12-30 14:54:20 +05:30
"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"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
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-09-07 17:31:46 +03:00
// ID is the optional id of the request
ID string `yaml:"id,omitempty" jsonschema:"title=id of the request,description=ID of the network request"`
2021-01-16 14:10:24 +05:30
2021-07-27 16:03:56 +05:30
// description: |
// Host to send network requests to.
2021-07-27 16:03:56 +05:30
//
// Usually it's set to `{{Hostname}}`. If you want to enable TLS for
// TCP Connection, you can use `tls://{{Hostname}}`.
// examples:
// - value: |
// []string{"{{Hostname}}"}
Address []string `yaml:"host,omitempty" jsonschema:"title=host to send requests to,description=Host to send network requests to"`
2021-02-19 00:10:06 +01:00
addresses []addressKV
2020-12-30 14:54:20 +05:30
2021-07-27 16:03:56 +05:30
// description: |
// Attack is the type of payload combinations to perform.
//
// Sniper is each payload once, pitchfork combines multiple payload sets and clusterbomb generates
// permutations and combinations for all payloads.
// values:
// - "sniper"
// - "pitchfork"
// - "clusterbomb"
AttackType string `yaml:"attack,omitempty" jsonschema:"title=attack is the payload combination,description=Attack is the type of payload combinations to perform,enum=sniper,enum=pitchfork,enum=clusterbomb"`
2021-07-27 16:03:56 +05:30
// description: |
// Payloads contains any payloads for the current request.
//
// Payloads support both key-values combinations where a list
// of payloads is provided, or optionally a single file can also
// be provided as payload which will be read on run-time.
Payloads map[string]interface{} `yaml:"payloads,omitempty" jsonschema:"title=payloads for the network request,description=Payloads contains any payloads for the current request"`
2021-07-27 16:03:56 +05:30
// description: |
// Inputs contains inputs for the network socket
Inputs []*Input `yaml:"inputs,omitempty" jsonschema:"title=inputs for the network request,description=Inputs contains any input/output for the current request"`
2021-07-27 16:03:56 +05:30
// description: |
// ReadSize is the size of response to read at the end
//
// Default value for read-size is 1024.
// examples:
// - value: "2048"
ReadSize int `yaml:"read-size,omitempty" jsonschema:"title=size of network response to read,description=Size of response to read at the end. Default is 1024 bytes"`
2020-12-30 14:54:20 +05:30
// Operators for the current request go here.
operators.Operators `yaml:",inline,omitempty"`
2021-08-04 14:20:48 +05:30
CompiledOperators *operators.Operators `yaml:"-"`
2020-12-30 14:54:20 +05:30
generator *generators.Generator
attackType generators.Type
2020-12-30 14:54:20 +05:30
// cache any variables that may be needed for operation.
dialer *fastdialer.Dialer
options *protocols.ExecuterOptions
dynamicValues map[string]interface{}
2020-12-30 14:54:20 +05:30
}
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 {
2021-07-27 16:03:56 +05:30
// description: |
// Data is the data to send as the input.
//
// It supports DSL Helper Functions as well as normal expressions.
// examples:
// - value: "\"TEST\""
// - value: "\"hex_decode('50494e47')\""
Data string `yaml:"data,omitempty" jsonschema:"title=data to send as input,description=Data is the data to send as the input"`
2021-07-27 16:03:56 +05:30
// description: |
// Type is the type of input specified in `data` field.
//
// Default value is text, but hex can be used for hex formatted data.
// values:
// - "hex"
// - "text"
Type string `yaml:"type,omitempty" jsonschema:"title=type is the type of input data,description=Type of input specified in data field,enum=hex,enum=text"`
2021-07-27 16:03:56 +05:30
// description: |
// Read is the number of bytes to read from socket.
//
2021-09-16 11:33:20 -05:00
// This can be used for protocols which expect an immediate response. You can
2021-07-27 16:03:56 +05:30
// read and write responses one after another and evetually perform matching
// on every data captured with `name` attribute.
//
// The [network docs](https://nuclei.projectdiscovery.io/templating-guide/protocols/network/) highlight more on how to do this.
// examples:
// - value: "1024"
Read int `yaml:"read,omitempty" jsonschema:"title=bytes to read from socket,description=Number of bytes to read from socket"`
2021-07-27 16:03:56 +05:30
// description: |
// Name is the optional name of the data read to provide matching on.
// examples:
// - value: "\"prefix\""
Name string `yaml:"name,omitempty" jsonschema:"title=optional name for data read,description=Optional name of the data read to provide matching on"`
}
2021-01-16 14:10:24 +05:30
// GetID returns the unique ID of the request if any.
func (request *Request) GetID() string {
return request.ID
2021-01-16 14:10:24 +05:30
}
2020-12-30 14:54:20 +05:30
// Compile compiles the protocol request for further execution.
func (request *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
request.options = options
for _, address := range request.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")
}
request.addresses = append(request.addresses, addressKV{ip: addressHost, port: addressPort, tls: shouldUseTLS})
} else {
request.addresses = append(request.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 request.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
if len(request.Payloads) > 0 {
attackType := request.AttackType
if attackType == "" {
attackType = "sniper"
}
request.attackType = generators.StringToType[attackType]
// Resolve payload paths if they are files.
for name, payload := range request.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")
}
request.Payloads[name] = final
}
}
request.generator, err = generators.New(request.Payloads, request.attackType, request.options.TemplatePath)
if err != nil {
return errors.Wrap(err, "could not parse payloads")
}
}
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")
}
request.dialer = client
2020-12-30 14:54:20 +05:30
if len(request.Matchers) > 0 || len(request.Extractors) > 0 {
compiled := &request.Operators
2020-12-30 14:54:20 +05:30
if err := compiled.Compile(); err != nil {
return errors.Wrap(err, "could not compile operators")
}
request.CompiledOperators = compiled
2020-12-30 14:54:20 +05:30
}
return nil
}
// Requests returns the total number of requests the YAML rule will perform
func (request *Request) Requests() int {
return len(request.Address)
2020-12-30 14:54:20 +05:30
}