2021-01-02 02:39:27 +05:30
|
|
|
package executer
|
2020-12-29 01:30:07 +05:30
|
|
|
|
|
|
|
|
import (
|
2021-01-16 14:10:24 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2021-02-01 16:21:49 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
2020-12-29 01:30:07 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Executer executes a group of requests for a protocol
|
|
|
|
|
type Executer struct {
|
2021-01-02 02:39:27 +05:30
|
|
|
requests []protocols.Request
|
2020-12-29 01:30:07 +05:30
|
|
|
options *protocols.ExecuterOptions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ protocols.Executer = &Executer{}
|
|
|
|
|
|
|
|
|
|
// NewExecuter creates a new request executer for list of requests
|
2021-01-02 02:39:27 +05:30
|
|
|
func NewExecuter(requests []protocols.Request, options *protocols.ExecuterOptions) *Executer {
|
2020-12-29 01:30:07 +05:30
|
|
|
return &Executer{requests: requests, options: options}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compile compiles the execution generators preparing any requests possible.
|
|
|
|
|
func (e *Executer) Compile() error {
|
|
|
|
|
for _, request := range e.requests {
|
|
|
|
|
err := request.Compile(e.options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Requests returns the total number of requests the rule will perform
|
2020-12-29 15:38:14 +05:30
|
|
|
func (e *Executer) Requests() int {
|
|
|
|
|
var count int
|
2020-12-29 01:30:07 +05:30
|
|
|
for _, request := range e.requests {
|
2021-01-12 02:00:11 +05:30
|
|
|
count += request.Requests()
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
|
|
|
|
return count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Execute executes the protocol group and returns true or false if results were found.
|
|
|
|
|
func (e *Executer) Execute(input string) (bool, error) {
|
|
|
|
|
var results bool
|
|
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
dynamicValues := make(map[string]interface{})
|
2021-01-16 14:10:24 +05:30
|
|
|
previous := make(map[string]interface{})
|
2020-12-29 01:30:07 +05:30
|
|
|
for _, req := range e.requests {
|
2021-02-26 13:13:11 +05:30
|
|
|
req := req
|
|
|
|
|
|
2021-01-16 14:10:24 +05:30
|
|
|
err := req.ExecuteWithResults(input, dynamicValues, previous, func(event *output.InternalWrappedEvent) {
|
|
|
|
|
ID := req.GetID()
|
|
|
|
|
if ID != "" {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
for k, v := range event.InternalEvent {
|
|
|
|
|
builder.WriteString(ID)
|
|
|
|
|
builder.WriteString("_")
|
|
|
|
|
builder.WriteString(k)
|
|
|
|
|
previous[builder.String()] = v
|
|
|
|
|
builder.Reset()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
if event.OperatorsResult == nil {
|
2021-01-01 19:36:21 +05:30
|
|
|
return
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
2021-01-02 02:39:27 +05:30
|
|
|
for _, result := range event.Results {
|
2021-02-02 12:10:47 +05:30
|
|
|
if e.options.IssuesClient != nil {
|
|
|
|
|
if err := e.options.IssuesClient.CreateIssue(result); err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not create issue on tracker: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
results = true
|
2021-02-26 13:13:11 +05:30
|
|
|
_ = e.options.Output.Write(result)
|
2021-01-16 12:26:38 +05:30
|
|
|
e.options.Progress.IncrementMatched()
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
})
|
|
|
|
|
if err != nil {
|
2021-08-16 21:24:37 +05:30
|
|
|
if e.options.HostErrorsCache != nil {
|
|
|
|
|
if e.options.HostErrorsCache.CheckError(err) {
|
|
|
|
|
e.options.HostErrorsCache.MarkFailed(input)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-08 19:01:40 +05:30
|
|
|
gologger.Warning().Msgf("[%s] Could not execute request for %s: %s\n", e.options.TemplateID, input, err)
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return results, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-01-01 19:36:21 +05:30
|
|
|
func (e *Executer) ExecuteWithResults(input string, callback protocols.OutputEventCallback) error {
|
|
|
|
|
dynamicValues := make(map[string]interface{})
|
2021-01-16 14:10:24 +05:30
|
|
|
previous := make(map[string]interface{})
|
|
|
|
|
|
2020-12-29 01:30:07 +05:30
|
|
|
for _, req := range e.requests {
|
2021-02-26 13:13:11 +05:30
|
|
|
req := req
|
|
|
|
|
|
2021-02-01 16:21:49 +05:30
|
|
|
err := req.ExecuteWithResults(input, dynamicValues, previous, func(event *output.InternalWrappedEvent) {
|
2021-01-16 14:10:24 +05:30
|
|
|
ID := req.GetID()
|
|
|
|
|
if ID != "" {
|
|
|
|
|
builder := &strings.Builder{}
|
|
|
|
|
for k, v := range event.InternalEvent {
|
|
|
|
|
builder.WriteString(ID)
|
|
|
|
|
builder.WriteString("_")
|
|
|
|
|
builder.WriteString(k)
|
|
|
|
|
previous[builder.String()] = v
|
|
|
|
|
builder.Reset()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
if event.OperatorsResult == nil {
|
2021-01-01 19:36:21 +05:30
|
|
|
return
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
callback(event)
|
|
|
|
|
})
|
2021-02-01 16:21:49 +05:30
|
|
|
if err != nil {
|
2021-08-16 21:24:37 +05:30
|
|
|
if e.options.HostErrorsCache != nil {
|
|
|
|
|
if e.options.HostErrorsCache.CheckError(err) {
|
|
|
|
|
e.options.HostErrorsCache.MarkFailed(input)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-08 19:01:40 +05:30
|
|
|
gologger.Warning().Msgf("[%s] Could not execute request for %s: %s\n", e.options.TemplateID, input, err)
|
2021-02-01 16:21:49 +05:30
|
|
|
}
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
return nil
|
2020-12-29 01:30:07 +05:30
|
|
|
}
|