Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/protocols/headless/headless.go
2070 views
1
package headless
2
3
import (
4
"github.com/pkg/errors"
5
6
"github.com/projectdiscovery/nuclei/v3/pkg/fuzz"
7
useragent "github.com/projectdiscovery/nuclei/v3/pkg/model/types/userAgent"
8
"github.com/projectdiscovery/nuclei/v3/pkg/operators"
9
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
10
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
11
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/headless/engine"
12
uagent "github.com/projectdiscovery/useragent"
13
fileutil "github.com/projectdiscovery/utils/file"
14
)
15
16
// Request contains a Headless protocol request to be made from a template
17
type Request struct {
18
// ID is the optional id of the request
19
ID string `yaml:"id,omitempty" json:"id,omitempty" jsonschema:"title=id of the request,description=Optional ID of the headless request"`
20
21
// description: |
22
// Attack is the type of payload combinations to perform.
23
//
24
// Batteringram is inserts the same payload into all defined payload positions at once, pitchfork combines multiple payload sets and clusterbomb generates
25
// permutations and combinations for all payloads.
26
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"`
27
// description: |
28
// Payloads contains any payloads for the current request.
29
//
30
// Payloads support both key-values combinations where a list
31
// of payloads is provided, or optionally a single file can also
32
// be provided as payload which will be read on run-time.
33
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"`
34
35
// description: |
36
// Steps is the list of actions to run for headless request
37
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"`
38
39
// descriptions: |
40
// User-Agent is the type of user-agent to use for the request.
41
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"`
42
43
// description: |
44
// If UserAgent is set to custom, customUserAgent is the custom user-agent to use for the request.
45
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"`
46
compiledUserAgent string
47
// description: |
48
// StopAtFirstMatch stops the execution of the requests and template as soon as a match is found.
49
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"`
50
51
// Operators for the current request go here.
52
operators.Operators `yaml:",inline,omitempty" json:",inline,omitempty"`
53
CompiledOperators *operators.Operators `yaml:"-" json:"-"`
54
55
// cache any variables that may be needed for operation.
56
options *protocols.ExecutorOptions
57
generator *generators.PayloadGenerator
58
59
// Fuzzing describes schema to fuzz headless requests
60
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"`
61
62
// description: |
63
// SelfContained specifies if the request is self-contained.
64
SelfContained bool `yaml:"-" json:"-"`
65
66
// description: |
67
// CookieReuse is an optional setting that enables cookie reuse
68
// Deprecated: This is default now. Use disable-cookie to disable cookie reuse. cookie-reuse will be removed in future releases.
69
CookieReuse bool `yaml:"cookie-reuse,omitempty" json:"cookie-reuse,omitempty" jsonschema:"title=optional cookie reuse enable,description=Optional setting that enables cookie reuse"`
70
71
// description: |
72
// DisableCookie is an optional setting that disables cookie reuse
73
DisableCookie bool `yaml:"disable-cookie,omitempty" json:"disable-cookie,omitempty" jsonschema:"title=optional disable cookie reuse,description=Optional setting that disables cookie reuse"`
74
}
75
76
// RequestPartDefinitions contains a mapping of request part definitions and their
77
// description. Multiple definitions are separated by commas.
78
// Definitions not having a name (generated on runtime) are prefixed & suffixed by <>.
79
var RequestPartDefinitions = map[string]string{
80
"template-id": "ID of the template executed",
81
"template-info": "Info Block of the template executed",
82
"template-path": "Path of the template executed",
83
"host": "Host is the input to the template",
84
"matched": "Matched is the input which was matched upon",
85
"type": "Type is the type of request made",
86
"req": "Headless request made from the client",
87
"resp,body,data": "Headless response received from client (default)",
88
}
89
90
// Step is a headless protocol request step.
91
type Step struct {
92
// Action is the headless action to execute for the script
93
Action string `yaml:"action"`
94
}
95
96
// GetID returns the unique ID of the request if any.
97
func (request *Request) GetID() string {
98
return request.ID
99
}
100
101
// Compile compiles the protocol request for further execution.
102
func (request *Request) Compile(options *protocols.ExecutorOptions) error {
103
request.options = options
104
105
// TODO: logic similar to network + http => probably can be refactored
106
// Resolve payload paths from vars if they exists
107
for name, payload := range options.Options.Vars.AsMap() {
108
payloadStr, ok := payload.(string)
109
// check if inputs contains the payload
110
if ok && fileutil.FileExists(payloadStr) {
111
if request.Payloads == nil {
112
request.Payloads = make(map[string]interface{})
113
}
114
request.Payloads[name] = payloadStr
115
}
116
}
117
118
if len(request.Payloads) > 0 {
119
var err error
120
request.generator, err = generators.New(request.Payloads, request.AttackType.Value, options.TemplatePath, options.Catalog, options.Options.AttackType, request.options.Options)
121
if err != nil {
122
return errors.Wrap(err, "could not parse payloads")
123
}
124
}
125
126
// Compile User-Agent
127
switch request.UserAgent.Value {
128
case useragent.Off:
129
request.compiledUserAgent = " "
130
case useragent.Default:
131
request.compiledUserAgent = ""
132
case useragent.Custom:
133
if request.CustomUserAgent == "" {
134
return errors.New("please set custom_user_agent in the template")
135
}
136
request.compiledUserAgent = request.CustomUserAgent
137
case useragent.Random:
138
userAgent := uagent.PickRandom()
139
request.compiledUserAgent = userAgent.Raw
140
}
141
142
if len(request.Matchers) > 0 || len(request.Extractors) > 0 {
143
compiled := &request.Operators
144
compiled.ExcludeMatchers = options.ExcludeMatchers
145
compiled.TemplateID = options.TemplateID
146
if err := compiled.Compile(); err != nil {
147
return errors.Wrap(err, "could not compile operators")
148
}
149
request.CompiledOperators = compiled
150
}
151
152
if len(request.Fuzzing) > 0 {
153
for _, rule := range request.Fuzzing {
154
if fuzzingMode := options.Options.FuzzingMode; fuzzingMode != "" {
155
rule.Mode = fuzzingMode
156
}
157
if fuzzingType := options.Options.FuzzingType; fuzzingType != "" {
158
rule.Type = fuzzingType
159
}
160
if err := rule.Compile(request.generator, request.options); err != nil {
161
return errors.Wrap(err, "could not compile fuzzing rule")
162
}
163
}
164
}
165
166
return nil
167
}
168
169
// Requests returns the total number of requests the YAML rule will perform
170
func (request *Request) Requests() int {
171
return 1
172
}
173
174
// UpdateOptions replaces this request's options with a new copy
175
func (r *Request) UpdateOptions(opts *protocols.ExecutorOptions) {
176
r.options.ApplyNewEngineOptions(opts)
177
}
178
179