2021-02-21 11:01:34 +00:00
package headless
import (
2022-02-24 07:01:08 +00:00
"github.com/corpix/uarand"
2021-02-21 11:01:34 +00:00
"github.com/pkg/errors"
2021-09-07 14:31:46 +00:00
2023-10-17 12:14:13 +00:00
useragent "github.com/projectdiscovery/nuclei/v3/pkg/model/types/userAgent"
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/fuzz"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/headless/engine"
2022-11-06 20:24:23 +00:00
fileutil "github.com/projectdiscovery/utils/file"
2021-02-21 11:01:34 +00:00
)
// Request contains a Headless protocol request to be made from a template
type Request struct {
2021-09-07 14:31:46 +00:00
// ID is the optional id of the request
2023-02-07 08:10:40 +00:00
ID string ` yaml:"id,omitempty" json:"id,omitempty" jsonschema:"title=id of the request,description=Optional ID of the headless request" `
2021-02-21 11:01:34 +00:00
2022-02-04 10:43:42 +00:00
// description: |
// Attack is the type of payload combinations to perform.
//
// Batteringram is inserts the same payload into all defined payload positions at once, pitchfork combines multiple payload sets and clusterbomb generates
// permutations and combinations for all payloads.
2023-02-07 08:10:40 +00:00
AttackType generators . AttackTypeHolder ` yaml:"attack,omitempty" json:"attack,omitempty" jsonschema:"title=attack is the payload combination,description=Attack is the type of payload combinations to perform,enum=batteringram,enum=pitchfork,enum=clusterbomb" `
2022-02-04 10:43:42 +00:00
// 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.
2023-02-07 08:10:40 +00:00
Payloads map [ string ] interface { } ` yaml:"payloads,omitempty" json:"payloads,omitempty" jsonschema:"title=payloads for the headless request,description=Payloads contains any payloads for the current request" `
2022-02-04 10:43:42 +00:00
2021-07-27 10:33:56 +00:00
// description: |
// Steps is the list of actions to run for headless request
2023-02-07 08:10:40 +00:00
Steps [ ] * engine . Action ` yaml:"steps,omitempty" json:"steps,omitempty" jsonschema:"title=list of actions for headless request,description=List of actions to run for headless request" `
2021-02-21 11:01:34 +00:00
2022-02-24 07:01:08 +00:00
// descriptions: |
// User-Agent is the type of user-agent to use for the request.
2023-02-07 08:10:40 +00:00
UserAgent useragent . UserAgentHolder ` yaml:"user_agent,omitempty" json:"user_agent,omitempty" jsonschema:"title=user agent for the headless request,description=User agent for the headless request" `
2022-02-24 07:01:08 +00:00
// description: |
// If UserAgent is set to custom, customUserAgent is the custom user-agent to use for the request.
2023-02-07 08:10:40 +00:00
CustomUserAgent string ` yaml:"custom_user_agent,omitempty" json:"custom_user_agent,omitempty" jsonschema:"title=custom user agent for the headless request,description=Custom user agent for the headless request" `
2022-02-24 07:01:08 +00:00
compiledUserAgent string
2022-12-19 14:22:17 +00:00
// description: |
// StopAtFirstMatch stops the execution of the requests and template as soon as a match is found.
2023-02-07 08:10:40 +00:00
StopAtFirstMatch bool ` yaml:"stop-at-first-match,omitempty" json:"stop-at-first-match,omitempty" jsonschema:"title=stop at first match,description=Stop the execution after a match is found" `
2022-02-24 07:01:08 +00:00
2021-02-21 11:01:34 +00:00
// Operators for the current request go here.
2023-02-07 08:10:40 +00:00
operators . Operators ` yaml:",inline,omitempty" json:",inline,omitempty" `
CompiledOperators * operators . Operators ` yaml:"-" json:"-" `
2021-02-21 11:01:34 +00:00
// cache any variables that may be needed for operation.
2023-05-31 20:58:10 +00:00
options * protocols . ExecutorOptions
2022-02-04 10:43:42 +00:00
generator * generators . PayloadGenerator
2023-06-09 00:20:44 +00:00
// Fuzzing describes schema to fuzz headless requests
Fuzzing [ ] * fuzz . Rule ` yaml:"fuzzing,omitempty" json:"fuzzing,omitempty" jsonschema:"title=fuzzin rules for http fuzzing,description=Fuzzing describes rule schema to fuzz headless requests" `
2023-06-26 17:25:51 +00:00
2023-11-02 13:38:20 +00:00
// description: |
// SelfContained specifies if the request is self-contained.
SelfContained bool ` yaml:"-" json:"-" `
2023-06-26 17:25:51 +00:00
// description: |
// CookieReuse is an optional setting that enables cookie reuse
2023-11-18 07:32:10 +00:00
// Deprecated: This is default now. Use disable-cookie to disable cookie reuse. cookie-reuse will be removed in future releases.
2023-06-26 17:25:51 +00:00
CookieReuse bool ` yaml:"cookie-reuse,omitempty" json:"cookie-reuse,omitempty" jsonschema:"title=optional cookie reuse enable,description=Optional setting that enables cookie reuse" `
2023-11-18 07:32:10 +00:00
// description: |
// DisableCookie is an optional setting that disables cookie reuse
DisableCookie bool ` yaml:"disable-cookie,omitempty" json:"disable-cookie,omitempty" jsonschema:"title=optional disable cookie reuse,description=Optional setting that disables cookie reuse" `
2021-02-21 11:01:34 +00:00
}
2021-11-26 10:53:54 +00:00
// RequestPartDefinitions contains a mapping of request part definitions and their
// description. Multiple definitions are separated by commas.
// Definitions not having a name (generated on runtime) are prefixed & suffixed by <>.
var RequestPartDefinitions = map [ string ] string {
"template-id" : "ID of the template executed" ,
"template-info" : "Info Block of the template executed" ,
"template-path" : "Path of the template executed" ,
"host" : "Host is the input to the template" ,
"matched" : "Matched is the input which was matched upon" ,
"type" : "Type is the type of request made" ,
"req" : "Headless request made from the client" ,
2022-02-07 14:41:55 +00:00
"resp,body,data" : "Headless response received from client (default)" ,
2021-11-26 10:53:54 +00:00
}
2021-02-21 11:01:34 +00:00
// Step is a headless protocol request step.
type Step struct {
// Action is the headless action to execute for the script
Action string ` yaml:"action" `
}
// GetID returns the unique ID of the request if any.
2021-10-01 11:30:04 +00:00
func ( request * Request ) GetID ( ) string {
return request . ID
2021-02-21 11:01:34 +00:00
}
// Compile compiles the protocol request for further execution.
2023-05-31 20:58:10 +00:00
func ( request * Request ) Compile ( options * protocols . ExecutorOptions ) error {
2023-08-31 12:33:01 +00:00
request . options = options
2022-02-04 10:43:42 +00:00
// TODO: logic similar to network + http => probably can be refactored
// Resolve payload paths from vars if they exists
2023-03-03 23:27:27 +00:00
for name , payload := range options . Options . Vars . AsMap ( ) {
2022-02-04 10:43:42 +00:00
payloadStr , ok := payload . ( string )
// check if inputs contains the payload
2022-02-04 23:37:03 +00:00
if ok && fileutil . FileExists ( payloadStr ) {
2022-02-04 10:43:42 +00:00
if request . Payloads == nil {
request . Payloads = make ( map [ string ] interface { } )
}
request . Payloads [ name ] = payloadStr
}
}
if len ( request . Payloads ) > 0 {
var err error
2023-08-31 12:33:01 +00:00
request . generator , err = generators . New ( request . Payloads , request . AttackType . Value , options . TemplatePath , options . Catalog , options . Options . AttackType , request . options . Options )
2022-02-04 10:43:42 +00:00
if err != nil {
return errors . Wrap ( err , "could not parse payloads" )
}
}
2022-02-24 07:01:08 +00:00
// Compile User-Agent
switch request . UserAgent . Value {
case useragent . Off :
request . compiledUserAgent = " "
case useragent . Default :
request . compiledUserAgent = ""
case useragent . Custom :
if request . CustomUserAgent == "" {
return errors . New ( "please set custom_user_agent in the template" )
}
request . compiledUserAgent = request . CustomUserAgent
case useragent . Random :
request . compiledUserAgent = uarand . GetRandom ( )
}
2021-10-01 11:30:04 +00:00
if len ( request . Matchers ) > 0 || len ( request . Extractors ) > 0 {
compiled := & request . Operators
2022-06-24 17:39:27 +00:00
compiled . ExcludeMatchers = options . ExcludeMatchers
compiled . TemplateID = options . TemplateID
2021-02-21 11:01:34 +00:00
if err := compiled . Compile ( ) ; err != nil {
return errors . Wrap ( err , "could not compile operators" )
}
2021-10-01 11:30:04 +00:00
request . CompiledOperators = compiled
2021-02-21 11:01:34 +00:00
}
2023-06-09 00:20:44 +00:00
if len ( request . Fuzzing ) > 0 {
for _ , rule := range request . Fuzzing {
if fuzzingMode := options . Options . FuzzingMode ; fuzzingMode != "" {
rule . Mode = fuzzingMode
}
if fuzzingType := options . Options . FuzzingType ; fuzzingType != "" {
rule . Type = fuzzingType
}
if err := rule . Compile ( request . generator , request . options ) ; err != nil {
return errors . Wrap ( err , "could not compile fuzzing rule" )
}
}
}
2021-02-21 11:01:34 +00:00
return nil
}
// Requests returns the total number of requests the YAML rule will perform
2021-10-01 11:30:04 +00:00
func ( request * Request ) Requests ( ) int {
2021-02-21 11:01:34 +00:00
return 1
}