2020-08-29 13:26:11 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
tengo "github.com/d5/tengo/v2"
|
|
|
|
"github.com/d5/tengo/v2/stdlib"
|
|
|
|
"github.com/karrick/godirwalk"
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/progress"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/atomicboolean"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/executer"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/requests"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
|
2020-10-12 18:15:21 +00:00
|
|
|
"github.com/remeh/sizedwaitgroup"
|
2020-08-29 13:26:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// workflowTemplates contains the initialized workflow templates per template group
|
|
|
|
type workflowTemplates struct {
|
|
|
|
Name string
|
|
|
|
Templates []*workflows.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
// processTemplateWithList processes a template and runs the enumeration on all the targets
|
2020-11-01 14:12:25 +00:00
|
|
|
func (r *Runner) processTemplateWithList(p *progress.Progress, template *templates.Template, request interface{}) bool {
|
2020-08-29 13:26:11 +00:00
|
|
|
var httpExecuter *executer.HTTPExecuter
|
|
|
|
var dnsExecuter *executer.DNSExecuter
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Create an executer based on the request type.
|
|
|
|
switch value := request.(type) {
|
|
|
|
case *requests.DNSRequest:
|
|
|
|
dnsExecuter = executer.NewDNSExecuter(&executer.DNSOptions{
|
2020-10-22 10:42:16 +00:00
|
|
|
TraceLog: r.traceLog,
|
2020-08-29 13:26:11 +00:00
|
|
|
Debug: r.options.Debug,
|
|
|
|
Template: template,
|
|
|
|
DNSRequest: value,
|
2020-09-10 11:02:01 +00:00
|
|
|
Writer: r.output,
|
2020-08-29 13:26:11 +00:00
|
|
|
JSON: r.options.JSON,
|
|
|
|
JSONRequests: r.options.JSONRequests,
|
2020-10-19 20:27:38 +00:00
|
|
|
NoMeta: r.options.NoMeta,
|
2020-08-29 13:26:11 +00:00
|
|
|
ColoredOutput: !r.options.NoColor,
|
|
|
|
Colorizer: r.colorizer,
|
|
|
|
Decolorizer: r.decolorizer,
|
2020-11-15 23:40:32 +00:00
|
|
|
RateLimiter: r.ratelimiter,
|
2020-08-29 13:26:11 +00:00
|
|
|
})
|
|
|
|
case *requests.BulkHTTPRequest:
|
|
|
|
httpExecuter, err = executer.NewHTTPExecuter(&executer.HTTPOptions{
|
2020-10-22 10:42:16 +00:00
|
|
|
TraceLog: r.traceLog,
|
2020-10-06 19:38:44 +00:00
|
|
|
Debug: r.options.Debug,
|
|
|
|
Template: template,
|
|
|
|
BulkHTTPRequest: value,
|
|
|
|
Writer: r.output,
|
|
|
|
Timeout: r.options.Timeout,
|
|
|
|
Retries: r.options.Retries,
|
|
|
|
ProxyURL: r.options.ProxyURL,
|
|
|
|
ProxySocksURL: r.options.ProxySocksURL,
|
|
|
|
CustomHeaders: r.options.CustomHeaders,
|
|
|
|
JSON: r.options.JSON,
|
|
|
|
JSONRequests: r.options.JSONRequests,
|
2020-10-19 20:27:38 +00:00
|
|
|
NoMeta: r.options.NoMeta,
|
2020-10-06 19:38:44 +00:00
|
|
|
CookieReuse: value.CookieReuse,
|
|
|
|
ColoredOutput: !r.options.NoColor,
|
|
|
|
Colorizer: &r.colorizer,
|
|
|
|
Decolorizer: r.decolorizer,
|
|
|
|
StopAtFirstMatch: r.options.StopAtFirstMatch,
|
2020-10-18 01:09:24 +00:00
|
|
|
PF: r.pf,
|
2020-11-01 16:07:36 +00:00
|
|
|
Dialer: r.dialer,
|
2020-11-15 23:40:32 +00:00
|
|
|
RateLimiter: r.ratelimiter,
|
2020-08-29 13:26:11 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
p.Drop(request.(*requests.BulkHTTPRequest).GetRequestCount())
|
|
|
|
gologger.Warningf("Could not create http client: %s\n", err)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalresult atomicboolean.AtomBool
|
|
|
|
|
2020-10-12 18:15:21 +00:00
|
|
|
wg := sizedwaitgroup.New(r.options.BulkSize)
|
2020-08-29 13:26:11 +00:00
|
|
|
|
2020-11-15 23:40:32 +00:00
|
|
|
r.hm.Scan(func(k, _ []byte) error {
|
|
|
|
URL := string(k)
|
2020-10-12 18:15:21 +00:00
|
|
|
wg.Add()
|
2020-08-29 13:26:11 +00:00
|
|
|
go func(URL string) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
2020-10-11 09:46:43 +00:00
|
|
|
var result *executer.Result
|
2020-08-29 13:26:11 +00:00
|
|
|
|
|
|
|
if httpExecuter != nil {
|
2020-10-09 21:11:07 +00:00
|
|
|
result = httpExecuter.ExecuteHTTP(p, URL)
|
2020-08-29 13:26:11 +00:00
|
|
|
globalresult.Or(result.GotResults)
|
|
|
|
}
|
|
|
|
|
|
|
|
if dnsExecuter != nil {
|
|
|
|
result = dnsExecuter.ExecuteDNS(p, URL)
|
|
|
|
globalresult.Or(result.GotResults)
|
|
|
|
}
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2020-09-19 20:41:32 +00:00
|
|
|
gologger.Warningf("[%s] Could not execute step: %s\n", r.colorizer.Colorizer.BrightBlue(template.ID), result.Error)
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
2020-10-09 21:11:07 +00:00
|
|
|
}(URL)
|
2020-11-15 23:40:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-08-29 13:26:11 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// See if we got any results from the executers
|
|
|
|
return globalresult.Get()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessWorkflowWithList coming from stdin or list of targets
|
2020-11-01 14:12:25 +00:00
|
|
|
func (r *Runner) processWorkflowWithList(p *progress.Progress, workflow *workflows.Workflow) bool {
|
2020-09-19 15:55:05 +00:00
|
|
|
result := false
|
|
|
|
|
2020-08-29 13:26:11 +00:00
|
|
|
workflowTemplatesList, err := r.preloadWorkflowTemplates(p, workflow)
|
|
|
|
if err != nil {
|
|
|
|
gologger.Warningf("Could not preload templates for workflow %s: %s\n", workflow.ID, err)
|
2020-09-19 15:55:05 +00:00
|
|
|
return result
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
logicBytes := []byte(workflow.Logic)
|
|
|
|
|
2020-10-12 18:15:21 +00:00
|
|
|
wg := sizedwaitgroup.New(r.options.BulkSize)
|
2020-08-29 13:26:11 +00:00
|
|
|
|
2020-11-15 23:40:32 +00:00
|
|
|
r.hm.Scan(func(k, _ []byte) error {
|
|
|
|
targetURL := string(k)
|
2020-10-12 18:15:21 +00:00
|
|
|
wg.Add()
|
2020-08-29 13:26:11 +00:00
|
|
|
|
|
|
|
go func(targetURL string) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
script := tengo.NewScript(logicBytes)
|
|
|
|
script.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
|
|
|
|
|
2020-09-19 15:55:05 +00:00
|
|
|
variables := make(map[string]*workflows.NucleiVar)
|
|
|
|
|
2020-08-29 13:26:11 +00:00
|
|
|
for _, workflowTemplate := range *workflowTemplatesList {
|
2020-09-19 15:55:05 +00:00
|
|
|
name := workflowTemplate.Name
|
|
|
|
variable := &workflows.NucleiVar{Templates: workflowTemplate.Templates, URL: targetURL}
|
|
|
|
err := script.Add(name, variable)
|
2020-08-29 13:26:11 +00:00
|
|
|
if err != nil {
|
|
|
|
gologger.Errorf("Could not initialize script for workflow '%s': %s\n", workflow.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
2020-09-19 15:55:05 +00:00
|
|
|
variables[name] = variable
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := script.RunContext(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
gologger.Errorf("Could not execute workflow '%s': %s\n", workflow.ID, err)
|
|
|
|
}
|
|
|
|
|
2020-09-19 15:55:05 +00:00
|
|
|
for _, variable := range variables {
|
2020-09-19 20:13:11 +00:00
|
|
|
result = !variable.IsFalsy()
|
2020-09-19 15:55:05 +00:00
|
|
|
if result {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 13:26:11 +00:00
|
|
|
}(targetURL)
|
2020-11-15 23:40:32 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-08-29 13:26:11 +00:00
|
|
|
|
|
|
|
wg.Wait()
|
2020-09-19 15:55:05 +00:00
|
|
|
|
|
|
|
return result
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-01 14:12:25 +00:00
|
|
|
func (r *Runner) preloadWorkflowTemplates(p *progress.Progress, workflow *workflows.Workflow) (*[]workflowTemplates, error) {
|
2020-08-29 13:26:11 +00:00
|
|
|
var jar *cookiejar.Jar
|
|
|
|
|
|
|
|
if workflow.CookieReuse {
|
|
|
|
var err error
|
|
|
|
jar, err = cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single yaml provided
|
|
|
|
var wflTemplatesList []workflowTemplates
|
|
|
|
|
|
|
|
for name, value := range workflow.Variables {
|
|
|
|
// Check if the template is an absolute path or relative path.
|
|
|
|
// If the path is absolute, use it. Otherwise,
|
|
|
|
if isRelative(value) {
|
|
|
|
newPath, err := r.resolvePath(value)
|
|
|
|
if err != nil {
|
|
|
|
newPath, err = resolvePathWithBaseFolder(filepath.Dir(workflow.GetPath()), value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
value = newPath
|
|
|
|
}
|
|
|
|
|
|
|
|
var wtlst []*workflows.Template
|
|
|
|
|
|
|
|
if strings.HasSuffix(value, ".yaml") {
|
|
|
|
t, err := templates.Parse(value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
template := &workflows.Template{Progress: p}
|
|
|
|
if len(t.BulkRequestsHTTP) > 0 {
|
|
|
|
template.HTTPOptions = &executer.HTTPOptions{
|
2020-11-20 08:55:11 +00:00
|
|
|
TraceLog: r.traceLog,
|
|
|
|
Debug: r.options.Debug,
|
|
|
|
Writer: r.output,
|
|
|
|
Template: t,
|
|
|
|
Timeout: r.options.Timeout,
|
|
|
|
Retries: r.options.Retries,
|
|
|
|
ProxyURL: r.options.ProxyURL,
|
|
|
|
ProxySocksURL: r.options.ProxySocksURL,
|
|
|
|
CustomHeaders: r.options.CustomHeaders,
|
|
|
|
JSON: r.options.JSON,
|
|
|
|
JSONRequests: r.options.JSONRequests,
|
|
|
|
CookieJar: jar,
|
|
|
|
ColoredOutput: !r.options.NoColor,
|
|
|
|
Colorizer: &r.colorizer,
|
|
|
|
Decolorizer: r.decolorizer,
|
|
|
|
PF: r.pf,
|
|
|
|
RateLimiter: r.ratelimiter,
|
|
|
|
NoMeta: r.options.NoMeta,
|
|
|
|
StopAtFirstMatch: r.options.StopAtFirstMatch,
|
|
|
|
Dialer: r.dialer,
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
} else if len(t.RequestsDNS) > 0 {
|
|
|
|
template.DNSOptions = &executer.DNSOptions{
|
2020-10-22 10:42:16 +00:00
|
|
|
TraceLog: r.traceLog,
|
2020-08-29 13:26:11 +00:00
|
|
|
Debug: r.options.Debug,
|
|
|
|
Template: t,
|
2020-09-10 11:02:01 +00:00
|
|
|
Writer: r.output,
|
2020-09-11 16:12:27 +00:00
|
|
|
JSON: r.options.JSON,
|
|
|
|
JSONRequests: r.options.JSONRequests,
|
2020-08-29 13:26:11 +00:00
|
|
|
ColoredOutput: !r.options.NoColor,
|
|
|
|
Colorizer: r.colorizer,
|
|
|
|
Decolorizer: r.decolorizer,
|
2020-11-20 08:55:11 +00:00
|
|
|
NoMeta: r.options.NoMeta,
|
|
|
|
RateLimiter: r.ratelimiter,
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if template.DNSOptions != nil || template.HTTPOptions != nil {
|
|
|
|
wtlst = append(wtlst, template)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
matches := []string{}
|
|
|
|
|
|
|
|
err := godirwalk.Walk(value, &godirwalk.Options{
|
|
|
|
Callback: func(path string, d *godirwalk.Dirent) error {
|
|
|
|
if !d.IsDir() && strings.HasSuffix(path, ".yaml") {
|
|
|
|
matches = append(matches, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
ErrorCallback: func(path string, err error) godirwalk.ErrorAction {
|
|
|
|
return godirwalk.SkipNode
|
|
|
|
},
|
|
|
|
Unsorted: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0 matches means no templates were found in directory
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return nil, fmt.Errorf("no match found in the directory %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, match := range matches {
|
|
|
|
t, err := templates.Parse(match)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
template := &workflows.Template{Progress: p}
|
|
|
|
if len(t.BulkRequestsHTTP) > 0 {
|
|
|
|
template.HTTPOptions = &executer.HTTPOptions{
|
|
|
|
Debug: r.options.Debug,
|
2020-09-10 11:02:01 +00:00
|
|
|
Writer: r.output,
|
2020-08-29 13:26:11 +00:00
|
|
|
Template: t,
|
|
|
|
Timeout: r.options.Timeout,
|
|
|
|
Retries: r.options.Retries,
|
|
|
|
ProxyURL: r.options.ProxyURL,
|
|
|
|
ProxySocksURL: r.options.ProxySocksURL,
|
|
|
|
CustomHeaders: r.options.CustomHeaders,
|
|
|
|
CookieJar: jar,
|
2020-11-12 20:21:41 +00:00
|
|
|
TraceLog: r.traceLog,
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
} else if len(t.RequestsDNS) > 0 {
|
|
|
|
template.DNSOptions = &executer.DNSOptions{
|
|
|
|
Debug: r.options.Debug,
|
|
|
|
Template: t,
|
2020-09-10 11:02:01 +00:00
|
|
|
Writer: r.output,
|
2020-11-12 20:21:41 +00:00
|
|
|
TraceLog: r.traceLog,
|
2020-08-29 13:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if template.DNSOptions != nil || template.HTTPOptions != nil {
|
|
|
|
wtlst = append(wtlst, template)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wflTemplatesList = append(wflTemplatesList, workflowTemplates{Name: name, Templates: wtlst})
|
|
|
|
}
|
|
|
|
|
|
|
|
return &wflTemplatesList, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolvePathWithBaseFolder(baseFolder, templateName string) (string, error) {
|
|
|
|
templatePath := path.Join(baseFolder, templateName)
|
|
|
|
if _, err := os.Stat(templatePath); !os.IsNotExist(err) {
|
|
|
|
gologger.Debugf("Found template in current directory: %s\n", templatePath)
|
|
|
|
return templatePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("no such path found: %s", templateName)
|
|
|
|
}
|