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"
2021-02-24 12:07:16 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
2021-07-06 18:27:30 +05:30
"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-08-03 22:33:50 +05:30
// ID is the ID of the request
2021-08-23 23:50:45 +05:30
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: |
2021-08-23 23:50:45 +05:30
// 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}}"}
2021-08-23 23:50:45 +05:30
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"
2021-08-23 23:50:45 +05:30
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.
2021-08-23 23:50:45 +05:30
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-06 18:27:30 +05:30
2021-07-27 16:03:56 +05:30
// description: |
// Inputs contains inputs for the network socket
2021-08-23 23:50:45 +05:30
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"
2021-08-23 23:50:45 +05:30
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.
2021-02-21 16:31:34 +05:30
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
2021-07-06 18:27:30 +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
}
2021-02-19 00:10:06 +01:00
type addressKV struct {
ip string
port string
tls bool
2020-12-30 21:14:04 +05:30
}
2020-12-30 16:47:22 +05:30
// 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')\""
2021-08-23 23:50:45 +05:30
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"
2021-08-23 23:50:45 +05:30
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.
//
// This can be used for protcols which expected an immediate response. You can
// 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"
2021-08-23 23:50:45 +05:30
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\""
2021-08-23 23:50:45 +05:30
Name string ` yaml:"name,omitempty" jsonschema:"title=optional name for data read,description=Optional name of the data read to provide matching on" `
2020-12-30 16:47:22 +05:30
}
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
2021-07-06 18:27:30 +05:30
r . options = options
2020-12-30 21:14:04 +05:30
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
}
2020-12-30 21:14:04 +05:30
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" )
2020-12-30 21:14:04 +05:30
}
2021-02-19 00:10:06 +01:00
r . addresses = append ( r . addresses , addressKV { ip : addressHost , port : addressPort , tls : shouldUseTLS } )
2020-12-30 21:14:04 +05:30
} 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
}
}
2021-02-24 12:07:16 +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
2021-02-24 12:07:16 +05:30
}
}
2020-12-30 14:54:20 +05:30
2021-07-06 18:27:30 +05:30
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 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
}
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
}