-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
var.go
194 lines (166 loc) · 4.58 KB
/
var.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package workflows
import (
"sync"
tengo "github.com/d5/tengo/v2"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/executer"
"github.com/projectdiscovery/nuclei/v2/pkg/generators"
)
// NucleiVar within the scripting engine
type NucleiVar struct {
tengo.ObjectImpl
Templates []*Template
URL string
InternalVars map[string]interface{}
sync.RWMutex
}
// Template contains HTTPOptions and DNSOptions for a single template
type Template struct {
HTTPOptions *executer.HTTPOptions
DNSOptions *executer.DNSOptions
}
// TypeName of the variable
func (n *NucleiVar) TypeName() string {
return "nuclei-var"
}
// CanCall can be called from within the scripting engine
func (n *NucleiVar) CanCall() bool {
return true
}
// Call logic - args[0]=headers, args[1]=payloads
func (n *NucleiVar) Call(args ...tengo.Object) (ret tengo.Object, err error) {
n.InternalVars = make(map[string]interface{})
headers := make(map[string]string)
externalVars := make(map[string]interface{})
// if external variables are specified and matches the template ones, these gets overwritten
if len(args) >= 1 {
headers = iterableToMapString(args[0])
}
// if external variables are specified and matches the template ones, these gets overwritten
if len(args) >= 2 {
externalVars = iterableToMap(args[1])
}
var gotResult bool
for _, template := range n.Templates {
if template.HTTPOptions != nil {
for _, request := range template.HTTPOptions.Template.RequestsHTTP {
// apply externally supplied payloads if any
request.Headers = generators.MergeMapsWithStrings(request.Headers, headers)
// apply externally supplied payloads if any
request.Payloads = generators.MergeMaps(request.Payloads, externalVars)
template.HTTPOptions.HTTPRequest = request
httpExecuter, err := executer.NewHTTPExecuter(template.HTTPOptions)
if err != nil {
gologger.Warningf("Could not compile request for template '%s': %s\n", template.HTTPOptions.Template.ID, err)
continue
}
result := httpExecuter.ExecuteHTTP(n.URL)
if result.Error != nil {
gologger.Warningf("Could not send request for template '%s': %s\n", template.HTTPOptions.Template.ID, result.Error)
continue
}
if httpExecuter.GotResults() {
gotResult = true
n.addResults(&result)
}
}
}
if template.DNSOptions != nil {
for _, request := range template.DNSOptions.Template.RequestsDNS {
template.DNSOptions.DNSRequest = request
dnsExecuter := executer.NewDNSExecuter(template.DNSOptions)
result := dnsExecuter.ExecuteDNS(n.URL)
if result.Error != nil {
gologger.Warningf("Could not compile request for template '%s': %s\n", template.HTTPOptions.Template.ID, result.Error)
continue
}
if dnsExecuter.GotResults() {
gotResult = true
n.addResults(&result)
}
}
}
}
if gotResult {
return tengo.TrueValue, nil
}
return tengo.FalseValue, nil
}
func (n *NucleiVar) IsFalsy() bool {
n.RLock()
defer n.RUnlock()
return len(n.InternalVars) == 0
}
func (n *NucleiVar) addResults(r *executer.Result) {
n.RLock()
defer n.RUnlock()
// add payload values as first, they will be accessible if not overwritter through
// payload_name (from template) => value
for k, v := range r.Meta {
n.InternalVars[k] = v
}
for k, v := range r.Matches {
n.InternalVars[k] = v
}
for k, v := range r.Extractions {
n.InternalVars[k] = v
}
}
// IndexGet returns the value for the given key.
func (n *NucleiVar) IndexGet(index tengo.Object) (res tengo.Object, err error) {
strIdx, ok := tengo.ToString(index)
if !ok {
err = tengo.ErrInvalidIndexType
return
}
r, ok := n.InternalVars[strIdx]
if !ok {
return tengo.UndefinedValue, nil
}
switch r.(type) {
case string:
res = &tengo.String{Value: r.(string)}
case []string:
rr, ok := r.([]string)
if !ok {
break
}
var resA []tengo.Object
for _, rrr := range rr {
resA = append(resA, &tengo.String{Value: rrr})
}
res = &tengo.Array{Value: resA}
}
return
}
func iterableToMap(t tengo.Object) map[string]interface{} {
m := make(map[string]interface{})
if t.CanIterate() {
i := t.Iterate()
for i.Next() {
key, ok := tengo.ToString(i.Key())
if !ok {
continue
}
value := tengo.ToInterface(i.Value())
m[key] = value
}
}
return m
}
func iterableToMapString(t tengo.Object) map[string]string {
m := make(map[string]string)
if t.CanIterate() {
i := t.Iterate()
for i.Next() {
key, ok := tengo.ToString(i.Key())
if !ok {
continue
}
if value, ok := tengo.ToString(i.Value()); ok {
m[key] = value
}
}
}
return m
}