2021-02-21 16:31:34 +05:30
package headless
import (
2022-02-24 12:31:08 +05:30
"github.com/corpix/uarand"
2021-02-21 16:31:34 +05:30
"github.com/pkg/errors"
2021-09-07 17:31:46 +03:00
2022-02-04 11:43:42 +01:00
"github.com/projectdiscovery/fileutil"
2022-02-24 12:31:08 +05:30
useragent "github.com/projectdiscovery/nuclei/v2/pkg/model/types/userAgent"
2021-02-21 16:31:34 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
2022-02-04 11:43:42 +01:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
2021-02-21 16:31:34 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/headless/engine"
)
// Request contains a Headless 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
2021-08-23 23:50:45 +05:30
ID string ` yaml:"id,omitempty" jsonschema:"title=id of the request,description=Optional ID of the headless request" `
2021-02-21 16:31:34 +05:30
2022-02-04 11:43:42 +01: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.
AttackType generators . AttackTypeHolder ` yaml:"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" `
// 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.
2022-02-04 11:46:11 +01:00
Payloads map [ string ] interface { } ` yaml:"payloads,omitempty" jsonschema:"title=payloads for the headless request,description=Payloads contains any payloads for the current request" `
2022-02-04 11:43:42 +01:00
2021-07-27 16:03:56 +05:30
// description: |
// Steps is the list of actions to run for headless request
2021-08-23 23:50:45 +05:30
Steps [ ] * engine . Action ` yaml:"steps,omitempty" jsonschema:"title=list of actions for headless request,description=List of actions to run for headless request" `
2021-02-21 16:31:34 +05:30
2022-02-24 12:31:08 +05:30
// descriptions: |
// User-Agent is the type of user-agent to use for the request.
UserAgent useragent . UserAgentHolder ` yaml:"user_agent,omitempty" jsonschema:"title=user agent for the headless request,description=User agent for the headless request" `
// description: |
// If UserAgent is set to custom, customUserAgent is the custom user-agent to use for the request.
CustomUserAgent string ` yaml:"custom_user_agent,omitempty" jsonschema:"title=custom user agent for the headless request,description=Custom user agent for the headless request" `
compiledUserAgent string
2021-02-21 16:31:34 +05:30
// Operators for the current request go here.
operators . Operators ` yaml:",inline,omitempty" `
CompiledOperators * operators . Operators ` yaml:"-" `
// cache any variables that may be needed for operation.
2022-02-04 11:43:42 +01:00
options * protocols . ExecuterOptions
generator * generators . PayloadGenerator
2021-02-21 16:31:34 +05:30
}
2021-11-26 16:23:54 +05:30
// 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 16:41:55 +02:00
"resp,body,data" : "Headless response received from client (default)" ,
2021-11-26 16:23:54 +05:30
}
2021-02-21 16:31:34 +05:30
// 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 14:30:04 +03:00
func ( request * Request ) GetID ( ) string {
return request . ID
2021-02-21 16:31:34 +05:30
}
// Compile compiles the protocol request for further execution.
2021-10-01 14:30:04 +03:00
func ( request * Request ) Compile ( options * protocols . ExecuterOptions ) error {
2022-02-04 11:43:42 +01:00
// TODO: logic similar to network + http => probably can be refactored
// Resolve payload paths from vars if they exists
for name , payload := range options . Options . VarsPayload ( ) {
payloadStr , ok := payload . ( string )
// check if inputs contains the payload
2022-02-05 00:37:03 +01:00
if ok && fileutil . FileExists ( payloadStr ) {
2022-02-04 11:43:42 +01:00
if request . Payloads == nil {
request . Payloads = make ( map [ string ] interface { } )
}
request . Payloads [ name ] = payloadStr
}
}
if len ( request . Payloads ) > 0 {
var err error
2022-10-19 03:51:45 +05:30
request . generator , err = generators . New ( request . Payloads , request . AttackType . Value , options . TemplatePath , options . Catalog , options . Options . AttackType )
2022-02-04 11:43:42 +01:00
if err != nil {
return errors . Wrap ( err , "could not parse payloads" )
}
}
2022-02-24 12:31:08 +05:30
// 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 14:30:04 +03:00
if len ( request . Matchers ) > 0 || len ( request . Extractors ) > 0 {
compiled := & request . Operators
2022-06-24 23:09:27 +05:30
compiled . ExcludeMatchers = options . ExcludeMatchers
compiled . TemplateID = options . TemplateID
2021-02-21 16:31:34 +05:30
if err := compiled . Compile ( ) ; err != nil {
return errors . Wrap ( err , "could not compile operators" )
}
2021-10-01 14:30:04 +03:00
request . CompiledOperators = compiled
2021-02-21 16:31:34 +05:30
}
2021-10-01 14:30:04 +03:00
request . options = options
2021-02-21 16:31:34 +05:30
return nil
}
// Requests returns the total number of requests the YAML rule will perform
2021-10-01 14:30:04 +03:00
func ( request * Request ) Requests ( ) int {
2021-02-21 16:31:34 +05:30
return 1
}