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"
|
2022-10-03 12:12:20 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
|
2022-02-04 11:43:42 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
|
2021-10-01 14:24:45 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
|
2022-01-31 08:52:36 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/interactsh"
|
2022-08-25 15:37:03 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/utils/vardump"
|
2022-11-01 20:28:50 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/utils"
|
2021-11-03 19:53:45 +05:30
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2021-02-21 16:31:34 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
2022-05-12 05:10:14 -05:00
|
|
|
const couldGetHtmlElementErrorMessage = "could get html element"
|
|
|
|
|
|
2021-11-03 19:53:45 +05:30
|
|
|
// Type returns the type of the protocol request
|
|
|
|
|
func (request *Request) Type() templateTypes.ProtocolType {
|
|
|
|
|
return templateTypes.HeadlessProtocol
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 16:31:34 +05:30
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2022-10-03 12:12:20 +02:00
|
|
|
func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata, previous output.InternalEvent /*TODO review unused parameter*/, callback protocols.OutputEventCallback) error {
|
2022-11-09 14:18:56 +01:00
|
|
|
inputURL := input.MetaInput.Input
|
2022-02-24 12:31:08 +05:30
|
|
|
if request.options.Browser.UserAgent() == "" {
|
|
|
|
|
request.options.Browser.SetUserAgent(request.compiledUserAgent)
|
|
|
|
|
}
|
2022-04-01 23:12:02 +05:30
|
|
|
|
2022-05-27 18:01:56 +02:00
|
|
|
vars := GenerateVariables(inputURL)
|
|
|
|
|
payloads := generators.BuildPayloadFromOptions(request.options.Options)
|
|
|
|
|
values := generators.MergeMaps(vars, metadata, payloads)
|
|
|
|
|
variablesMap := request.options.Variables.Evaluate(values)
|
2022-04-01 23:12:02 +05:30
|
|
|
payloads = generators.MergeMaps(variablesMap, payloads)
|
|
|
|
|
|
2022-02-04 11:43:42 +01:00
|
|
|
if request.generator != nil {
|
|
|
|
|
iterator := request.generator.NewIterator()
|
|
|
|
|
for {
|
|
|
|
|
value, ok := iterator.Value()
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
value = generators.MergeMaps(value, payloads)
|
|
|
|
|
if err := request.executeRequestWithPayloads(inputURL, value, previous, callback); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
value := generators.CopyMap(payloads)
|
|
|
|
|
if err := request.executeRequestWithPayloads(inputURL, value, previous, callback); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (request *Request) executeRequestWithPayloads(inputURL string, payloads map[string]interface{}, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2021-10-01 14:30:04 +03:00
|
|
|
instance, err := request.options.Browser.NewInstance()
|
2021-02-21 16:31:34 +05:30
|
|
|
if err != nil {
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, inputURL, request.Type().String(), err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2022-05-12 05:10:14 -05:00
|
|
|
return errors.Wrap(err, couldGetHtmlElementErrorMessage)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
defer instance.Close()
|
|
|
|
|
|
2022-11-01 20:28:50 +05:30
|
|
|
if vardump.EnableVarDump {
|
2022-08-25 15:37:03 +05:30
|
|
|
gologger.Debug().Msgf("Protocol request variables: \n%s\n", vardump.DumpVariables(payloads))
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 08:52:36 +01:00
|
|
|
instance.SetInteractsh(request.options.Interactsh)
|
|
|
|
|
|
2021-10-30 13:17:47 +03:00
|
|
|
parsedURL, err := url.Parse(inputURL)
|
2021-02-21 16:31:34 +05:30
|
|
|
if err != nil {
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, inputURL, request.Type().String(), err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2022-05-12 05:10:14 -05:00
|
|
|
return errors.Wrap(err, couldGetHtmlElementErrorMessage)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
2022-02-04 11:43:42 +01:00
|
|
|
timeout := time.Duration(request.options.Options.PageTimeout) * time.Second
|
|
|
|
|
out, page, err := instance.Run(parsedURL, request.Steps, payloads, timeout)
|
2021-02-21 16:31:34 +05:30
|
|
|
if err != nil {
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, inputURL, request.Type().String(), err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2022-05-12 05:10:14 -05:00
|
|
|
return errors.Wrap(err, couldGetHtmlElementErrorMessage)
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
defer page.Close()
|
|
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
request.options.Output.Request(request.options.TemplatePath, inputURL, request.Type().String(), nil)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementRequests()
|
2021-10-30 13:17:47 +03:00
|
|
|
gologger.Verbose().Msgf("Sent Headless request to %s", inputURL)
|
2021-02-21 16:31:34 +05:30
|
|
|
|
|
|
|
|
reqBuilder := &strings.Builder{}
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests {
|
2021-10-30 13:17:47 +03:00
|
|
|
gologger.Info().Msgf("[%s] Dumped Headless request for %s", request.options.TemplateID, inputURL)
|
2021-02-21 16:31:34 +05:30
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
for _, act := range request.Steps {
|
2021-02-21 16:31:34 +05:30
|
|
|
reqBuilder.WriteString(act.String())
|
|
|
|
|
reqBuilder.WriteString("\n")
|
|
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
gologger.Print().Msgf(reqBuilder.String())
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2022-01-31 08:52:36 +01:00
|
|
|
|
2021-12-29 09:48:46 +01:00
|
|
|
outputEvent := request.responseToDSLMap(responseBody, reqBuilder.String(), inputURL, inputURL, page.DumpHistory())
|
2021-02-21 16:31:34 +05:30
|
|
|
for k, v := range out {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2022-04-02 02:14:00 +05:30
|
|
|
for k, v := range payloads {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2021-02-21 16:31:34 +05:30
|
|
|
|
2022-01-31 08:52:36 +01:00
|
|
|
var event *output.InternalWrappedEvent
|
|
|
|
|
if len(page.InteractshURLs) == 0 {
|
2022-02-04 11:43:42 +01:00
|
|
|
event = eventcreator.CreateEvent(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse)
|
2022-01-31 08:52:36 +01:00
|
|
|
callback(event)
|
|
|
|
|
} else if request.options.Interactsh != nil {
|
|
|
|
|
event = &output.InternalWrappedEvent{InternalEvent: outputEvent}
|
|
|
|
|
request.options.Interactsh.RequestEvent(page.InteractshURLs, &interactsh.RequestData{
|
|
|
|
|
MakeResultFunc: request.MakeResultEvent,
|
|
|
|
|
Event: event,
|
|
|
|
|
Operators: request.CompiledOperators,
|
|
|
|
|
MatchFunc: request.Match,
|
|
|
|
|
ExtractFunc: request.Extract,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if len(page.InteractshURLs) > 0 {
|
|
|
|
|
event.UsesInteractsh = true
|
|
|
|
|
}
|
2021-10-01 14:24:45 +03:00
|
|
|
|
2021-11-01 20:45:54 +02:00
|
|
|
dumpResponse(event, request.options, responseBody, inputURL)
|
2021-09-29 19:43:46 +03:00
|
|
|
return nil
|
|
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
|
2021-11-01 20:45:54 +02:00
|
|
|
func dumpResponse(event *output.InternalWrappedEvent, requestOptions *protocols.ExecuterOptions, responseBody string, input string) {
|
|
|
|
|
cliOptions := requestOptions.Options
|
|
|
|
|
if cliOptions.Debug || cliOptions.DebugResponse {
|
|
|
|
|
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, responseBody, cliOptions.NoColor, false)
|
|
|
|
|
gologger.Debug().Msgf("[%s] Dumped Headless response for %s\n\n%s", requestOptions.TemplateID, input, highlightedResponse)
|
2021-10-30 13:17:47 +03:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-27 18:01:56 +02:00
|
|
|
|
|
|
|
|
// GenerateVariables will create default variables
|
|
|
|
|
func GenerateVariables(URL string) map[string]interface{} {
|
|
|
|
|
parsed, err := url.Parse(URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-01 20:28:50 +05:30
|
|
|
return utils.GenerateVariables(parsed, false)
|
2022-05-27 18:01:56 +02:00
|
|
|
}
|