145 lines
3.5 KiB
Go
Raw Normal View History

2020-06-26 10:23:54 +02:00
package workflows
import (
2020-07-10 09:04:38 +02:00
"strings"
"sync"
tengo "github.com/d5/tengo/v2"
2020-06-29 17:43:08 +05:30
"github.com/projectdiscovery/gologger"
2020-07-01 16:17:24 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/executor"
"github.com/projectdiscovery/nuclei/v2/pkg/generators"
2020-06-26 10:23:54 +02:00
)
2020-06-26 15:10:42 +02:00
// NucleiVar within the scripting engine
2020-06-26 10:23:54 +02:00
type NucleiVar struct {
tengo.ObjectImpl
2020-07-10 09:04:38 +02:00
Templates []*Template
URL string
InternalVars map[string]interface{}
sync.RWMutex
2020-06-29 17:43:08 +05:30
}
// Template contains HTTPOptions and DNSOptions for a single template
type Template struct {
2020-06-26 15:10:42 +02:00
HTTPOptions *executor.HTTPOptions
DNSOptions *executor.DNSOptions
2020-06-26 10:23:54 +02:00
}
2020-06-26 15:10:42 +02:00
// TypeName of the variable
2020-06-26 10:23:54 +02:00
func (n *NucleiVar) TypeName() string {
return "nuclei-var"
}
2020-06-26 15:10:42 +02:00
// CanCall can be called from within the scripting engine
2020-06-26 10:23:54 +02:00
func (n *NucleiVar) CanCall() bool {
return true
}
2020-06-26 15:10:42 +02:00
// Call logic - actually it doesn't require arguments
2020-06-26 10:23:54 +02:00
func (n *NucleiVar) Call(args ...tengo.Object) (ret tengo.Object, err error) {
2020-07-10 09:04:38 +02:00
n.InternalVars = make(map[string]interface{})
2020-07-13 03:30:07 +02:00
externalVars := make(map[string]interface{})
// if external variables are specified and matches the template ones, these gets overwritten
if len(args) == 1 {
m := args[0]
if m.CanIterate() {
i := m.Iterate()
for i.Next() {
key, ok := tengo.ToString(i.Key())
if !ok {
continue
}
2020-07-13 03:30:07 +02:00
value := tengo.ToInterface(i.Value())
externalVars[key] = value
}
}
}
2020-06-29 17:43:08 +05:30
var gotResult bool
for _, template := range n.Templates {
if template.HTTPOptions != nil {
for _, request := range template.HTTPOptions.Template.RequestsHTTP {
2020-07-13 03:30:07 +02:00
request.Payloads = generators.MergeMaps(request.Payloads, externalVars)
2020-06-29 17:43:08 +05:30
template.HTTPOptions.HTTPRequest = request
httpExecutor, err := executor.NewHTTPExecutor(template.HTTPOptions)
if err != nil {
gologger.Warningf("Could not compile request for template '%s': %s\n", template.HTTPOptions.Template.ID, err)
continue
}
2020-07-10 09:04:38 +02:00
result := httpExecutor.ExecuteHTTP(n.URL)
if result.Error != nil {
gologger.Warningf("Could not send request for template '%s': %s\n", template.HTTPOptions.Template.ID, result.Error)
2020-06-29 17:43:08 +05:30
continue
}
2020-07-10 09:04:38 +02:00
2020-06-29 17:43:08 +05:30
if httpExecutor.GotResults() {
gotResult = true
2020-07-10 09:04:38 +02:00
n.addResults(&result)
2020-06-29 17:43:08 +05:30
}
}
2020-06-26 15:10:42 +02:00
}
2020-06-29 17:43:08 +05:30
if template.DNSOptions != nil {
for _, request := range template.DNSOptions.Template.RequestsDNS {
template.DNSOptions.DNSRequest = request
dnsExecutor := executor.NewDNSExecutor(template.DNSOptions)
2020-07-10 09:04:38 +02:00
result := dnsExecutor.ExecuteDNS(n.URL)
if result.Error != nil {
gologger.Warningf("Could not compile request for template '%s': %s\n", template.HTTPOptions.Template.ID, result.Error)
2020-06-29 17:43:08 +05:30
continue
}
2020-07-10 09:04:38 +02:00
2020-06-29 17:43:08 +05:30
if dnsExecutor.GotResults() {
gotResult = true
2020-07-10 09:04:38 +02:00
n.addResults(&result)
2020-06-29 17:43:08 +05:30
}
}
2020-06-26 15:10:42 +02:00
}
}
2020-07-10 09:04:38 +02:00
2020-06-29 17:43:08 +05:30
if gotResult {
return tengo.TrueValue, nil
}
return tengo.FalseValue, nil
2020-06-26 10:23:54 +02:00
}
2020-07-10 09:04:38 +02:00
func (n *NucleiVar) IsFalsy() bool {
n.RLock()
defer n.RUnlock()
return len(n.InternalVars) == 0
}
func (n *NucleiVar) addResults(r *executor.Result) {
n.RLock()
defer n.RUnlock()
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
}
// Probably can be improved but as of now just joining all extractors with new line
res = &tengo.String{Value: strings.Join(r.([]string), "\n")}
return
}