From 8dfa9cee39a5f2c4f158564b20cee96052c41d50 Mon Sep 17 00:00:00 2001 From: Ice3man Date: Thu, 27 Jan 2022 12:14:32 +0530 Subject: [PATCH] Added debug-req/resp support for interactsh interactions (#1491) * Added debug-req/resp support for interactsh interactions * Added format function for interact debug logs + misc fixes * Added function for interact debug header * Typo fix * Enable debug logging for req/resp debug flag --- v2/internal/runner/options.go | 2 +- v2/internal/runner/runner.go | 3 ++ .../protocols/common/interactsh/interactsh.go | 48 ++++++++++++++----- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/v2/internal/runner/options.go b/v2/internal/runner/options.go index 5650efa3c..5c666282a 100644 --- a/v2/internal/runner/options.go +++ b/v2/internal/runner/options.go @@ -152,7 +152,7 @@ func configureOutput(options *types.Options) { if options.Verbose || options.Validate { gologger.DefaultLogger.SetMaxLevel(levels.LevelVerbose) } - if options.Debug { + if options.Debug || options.DebugRequests || options.DebugResponse { gologger.DefaultLogger.SetMaxLevel(levels.LevelDebug) } if options.NoColor { diff --git a/v2/internal/runner/runner.go b/v2/internal/runner/runner.go index 4d8c01482..8302ccad9 100644 --- a/v2/internal/runner/runner.go +++ b/v2/internal/runner/runner.go @@ -183,6 +183,9 @@ func New(options *types.Options) (*Runner, error) { opts.PollDuration = time.Duration(options.InteractionsPollDuration) * time.Second opts.NoInteractsh = runner.options.NoInteractsh opts.StopAtFirstMatch = runner.options.StopAtFirstMatch + opts.Debug = runner.options.Debug + opts.DebugRequest = runner.options.DebugRequests + opts.DebugResponse = runner.options.DebugResponse interactshClient, err := interactsh.New(opts) if err != nil { gologger.Error().Msgf("Could not create interactsh client: %s", err) diff --git a/v2/pkg/protocols/common/interactsh/interactsh.go b/v2/pkg/protocols/common/interactsh/interactsh.go index a61ec4cc1..650d485c5 100644 --- a/v2/pkg/protocols/common/interactsh/interactsh.go +++ b/v2/pkg/protocols/common/interactsh/interactsh.go @@ -75,7 +75,9 @@ type Options struct { // Progress is the nuclei progress bar implementation. Progress progress.Progress // Debug specifies whether debugging output should be shown for interactsh-client - Debug bool + Debug bool + DebugRequest bool + DebugResponse bool // DisableHttpFallback controls http retry in case of https failure for server url DisableHttpFallback bool // NoInteractsh disables the engine @@ -146,8 +148,8 @@ func (c *Client) firstTimeInitializeClient() error { c.hostname = interactDomain interactsh.StartPolling(c.pollDuration, func(interaction *server.Interaction) { - if c.options.Debug { - debugPrintInteraction(interaction) + if c.options.Debug || c.options.DebugRequest || c.options.DebugResponse { + c.debugPrintInteraction(interaction) } item := c.requests.Get(interaction.UniqueID) @@ -343,26 +345,48 @@ func HasMatchers(op *operators.Operators) bool { return false } -func debugPrintInteraction(interaction *server.Interaction) { +func (c *Client) debugPrintInteraction(interaction *server.Interaction) { builder := &bytes.Buffer{} switch interaction.Protocol { case "dns": - builder.WriteString(fmt.Sprintf("[%s] Received DNS interaction (%s) from %s at %s", interaction.FullId, interaction.QType, interaction.RemoteAddress, interaction.Timestamp.Format("2006-01-02 15:04:05"))) - builder.WriteString(fmt.Sprintf("\n-----------\nDNS Request\n-----------\n\n%s\n\n------------\nDNS Response\n------------\n\n%s\n\n", interaction.RawRequest, interaction.RawResponse)) + builder.WriteString(formatInteractionHeader("DNS", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp)) + if c.options.DebugRequest || c.options.Debug { + builder.WriteString(formatInteractionMessage("DNS Request", interaction.RawRequest)) + } + if c.options.DebugResponse || c.options.Debug { + builder.WriteString(formatInteractionMessage("DNS Response", interaction.RawResponse)) + } case "http": - builder.WriteString(fmt.Sprintf("[%s] Received HTTP interaction from %s at %s", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp.Format("2006-01-02 15:04:05"))) - builder.WriteString(fmt.Sprintf("\n------------\nHTTP Request\n------------\n\n%s\n\n-------------\nHTTP Response\n-------------\n\n%s\n\n", interaction.RawRequest, interaction.RawResponse)) + builder.WriteString(formatInteractionHeader("HTTP", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp)) + if c.options.DebugRequest || c.options.Debug { + builder.WriteString(formatInteractionMessage("HTTP Request", interaction.RawRequest)) + } + if c.options.DebugResponse || c.options.Debug { + builder.WriteString(formatInteractionMessage("HTTP Response", interaction.RawResponse)) + } case "smtp": - builder.WriteString(fmt.Sprintf("[%s] Received SMTP interaction from %s at %s", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp.Format("2006-01-02 15:04:05"))) - builder.WriteString(fmt.Sprintf("\n------------\nSMTP Interaction\n------------\n\n%s\n\n", interaction.RawRequest)) + builder.WriteString(formatInteractionHeader("SMTP", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp)) + if c.options.DebugRequest || c.options.Debug || c.options.DebugResponse { + builder.WriteString(formatInteractionMessage("SMTP Interaction", interaction.RawRequest)) + } case "ldap": - builder.WriteString(fmt.Sprintf("[%s] Received LDAP interaction from %s at %s", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp.Format("2006-01-02 15:04:05"))) - builder.WriteString(fmt.Sprintf("\n------------\nLDAP Interaction\n------------\n\n%s\n\n", interaction.RawRequest)) + builder.WriteString(formatInteractionHeader("LDAP", interaction.FullId, interaction.RemoteAddress, interaction.Timestamp)) + if c.options.DebugRequest || c.options.Debug || c.options.DebugResponse { + builder.WriteString(formatInteractionMessage("LDAP Interaction", interaction.RawRequest)) + } } fmt.Fprint(os.Stderr, builder.String()) } +func formatInteractionHeader(protocol, ID, address string, at time.Time) string { + return fmt.Sprintf("[%s] Received %s interaction from %s at %s", ID, protocol, address, at.Format("2006-01-02 15:04:05")) +} + +func formatInteractionMessage(key, value string) string { + return fmt.Sprintf("\n------------\n%s\n------------\n\n%s\n\n", key, value) +} + func hash(templateID, host string) string { h := sha1.New() h.Write([]byte(templateID))