2021-01-02 02:39:27 +05:30
|
|
|
package executer
|
2020-12-29 01:30:07 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"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}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 03:17:07 +05:30
|
|
|
// GetRequests returns the requests the rule will perform
|
|
|
|
|
func (e *Executer) GetRequests() []protocols.Request {
|
|
|
|
|
return e.requests
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-29 01:30:07 +05:30
|
|
|
// 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{})
|
2020-12-29 01:30:07 +05:30
|
|
|
for _, req := range e.requests {
|
2021-01-01 19:36:21 +05:30
|
|
|
err := req.ExecuteWithResults(input, dynamicValues, func(event *output.InternalWrappedEvent) {
|
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 {
|
2020-12-29 01:30:07 +05:30
|
|
|
results = true
|
|
|
|
|
e.options.Output.Write(result)
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
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{})
|
2020-12-29 01:30:07 +05:30
|
|
|
for _, req := range e.requests {
|
2021-01-01 19:36:21 +05:30
|
|
|
_ = req.ExecuteWithResults(input, dynamicValues, func(event *output.InternalWrappedEvent) {
|
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)
|
|
|
|
|
})
|
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
|
|
|
}
|