2021-02-21 16:31:34 +05:30
|
|
|
package headless
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
2021-03-01 14:20:56 +05:30
|
|
|
"time"
|
2021-02-21 16:31:34 +05:30
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-29 19:43:46 +03:00
|
|
|
|
2021-02-21 16:31:34 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-10-01 14:24:45 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
2021-02-21 16:31:34 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
|
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-08-23 16:21:18 +03:00
|
|
|
func (r *Request) ExecuteWithResults(input string, metadata, previous output.InternalEvent /*TODO review unused parameter*/, callback protocols.OutputEventCallback) error {
|
2021-02-21 16:31:34 +05:30
|
|
|
instance, err := r.options.Browser.NewInstance()
|
|
|
|
|
if err != nil {
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, input, "headless", err)
|
2021-03-02 02:22:15 +01:00
|
|
|
r.options.Progress.IncrementFailedRequestsBy(1)
|
2021-02-21 16:31:34 +05:30
|
|
|
return errors.Wrap(err, "could get html element")
|
|
|
|
|
}
|
|
|
|
|
defer instance.Close()
|
|
|
|
|
|
|
|
|
|
parsed, err := url.Parse(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, input, "headless", err)
|
2021-03-02 02:22:15 +01:00
|
|
|
r.options.Progress.IncrementFailedRequestsBy(1)
|
2021-02-21 16:31:34 +05:30
|
|
|
return errors.Wrap(err, "could get html element")
|
|
|
|
|
}
|
2021-03-01 14:20:56 +05:30
|
|
|
out, page, err := instance.Run(parsed, r.Steps, time.Duration(r.options.Options.PageTimeout)*time.Second)
|
2021-02-21 16:31:34 +05:30
|
|
|
if err != nil {
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, input, "headless", err)
|
2021-03-02 02:22:15 +01:00
|
|
|
r.options.Progress.IncrementFailedRequestsBy(1)
|
2021-02-21 16:31:34 +05:30
|
|
|
return errors.Wrap(err, "could get html element")
|
|
|
|
|
}
|
|
|
|
|
defer page.Close()
|
|
|
|
|
|
|
|
|
|
r.options.Output.Request(r.options.TemplateID, input, "headless", nil)
|
|
|
|
|
r.options.Progress.IncrementRequests()
|
|
|
|
|
gologger.Verbose().Msgf("Sent Headless request to %s", input)
|
|
|
|
|
|
|
|
|
|
reqBuilder := &strings.Builder{}
|
|
|
|
|
if r.options.Options.Debug || r.options.Options.DebugRequests {
|
|
|
|
|
gologger.Info().Msgf("[%s] Dumped Headless request for %s", r.options.TemplateID, input)
|
|
|
|
|
|
|
|
|
|
for _, act := range r.Steps {
|
|
|
|
|
reqBuilder.WriteString(act.String())
|
|
|
|
|
reqBuilder.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
gologger.Print().Msgf("%s", reqBuilder.String())
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:24:45 +03:00
|
|
|
var responseBody string
|
2021-02-21 16:31:34 +05:30
|
|
|
html, err := page.Page().Element("html")
|
|
|
|
|
if err == nil {
|
2021-10-01 14:24:45 +03:00
|
|
|
responseBody, _ = html.HTML()
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
2021-10-01 14:24:45 +03:00
|
|
|
outputEvent := r.responseToDSLMap(responseBody, reqBuilder.String(), input, input)
|
2021-02-21 16:31:34 +05:30
|
|
|
for k, v := range out {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:24:45 +03:00
|
|
|
event := createEvent(r, outputEvent)
|
|
|
|
|
|
|
|
|
|
if r.options.Options.Debug || r.options.Options.DebugResponse {
|
|
|
|
|
gologger.Debug().Msgf("[%s] Dumped Headless response for %s", r.options.TemplateID, input)
|
|
|
|
|
gologger.Print().Msgf("%s", responsehighlighter.Highlight(event.OperatorsResult, responseBody, r.options.Options.NoColor))
|
|
|
|
|
}
|
2021-09-29 19:43:46 +03:00
|
|
|
|
|
|
|
|
callback(event)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:24:45 +03:00
|
|
|
func createEvent(request *Request, outputEvent output.InternalEvent) *output.InternalWrappedEvent {
|
2021-02-21 16:31:34 +05:30
|
|
|
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
|
2021-09-29 19:43:46 +03:00
|
|
|
|
2021-09-30 21:05:12 +03:00
|
|
|
if request.CompiledOperators != nil {
|
2021-10-01 14:24:45 +03:00
|
|
|
result, ok := request.CompiledOperators.Execute(outputEvent, request.Match, request.Extract)
|
2021-02-21 16:31:34 +05:30
|
|
|
if ok && result != nil {
|
|
|
|
|
event.OperatorsResult = result
|
2021-09-29 19:43:46 +03:00
|
|
|
event.Results = request.MakeResultEvent(event)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
}
|
2021-09-30 21:05:12 +03:00
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
return event
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|