From a303cd5723f4d3562c533b920acac578ff7898ad Mon Sep 17 00:00:00 2001 From: Kevin Antoine <5072452+kevin-ntn@users.noreply.github.com> Date: Wed, 22 Apr 2020 14:32:13 +0200 Subject: [PATCH] Support for multiple fingerprints in single template #25 --- internal/runner/runner.go | 50 +++++++++++++++++++++------------------ pkg/matchers/matchers.go | 2 ++ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 7e32ffb4..9de03ca1 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -205,40 +205,44 @@ func (r *Runner) sendRequest(template *templates.Template, URL string, writer *b } // Check if the matcher matched - if !matcher.Match(resp, body, headers) { - continue reqLoop + if matcher.Match(resp, body, headers) { + // If there is an extractor, run it. + var extractorResults []string + for _, extractor := range request.Extractors { + part := extractor.GetPart() + if part == extractors.AllPart || part == extractors.HeaderPart && headers == "" { + headers = headersToString(resp.Header) + } + extractorResults = append(extractorResults, extractor.Extract(body, headers)...) + } + + // All the matchers matched, print the output on the screen + output := buildOutput(template, req, extractorResults, matcher) + gologger.Silentf("%s", output) + + if writer != nil { + r.outputMutex.Lock() + writer.WriteString(output) + r.outputMutex.Unlock() + } } } - - // If there is an extractor, run it. - var extractorResults []string - for _, extractor := range request.Extractors { - part := extractor.GetPart() - if part == extractors.AllPart || part == extractors.HeaderPart && headers == "" { - headers = headersToString(resp.Header) - } - extractorResults = append(extractorResults, extractor.Extract(body, headers)...) - } - - // All the matchers matched, print the output on the screen - output := buildOutput(template, req, extractorResults) - gologger.Silentf("%s", output) - - if writer != nil { - r.outputMutex.Lock() - writer.WriteString(output) - r.outputMutex.Unlock() - } + continue reqLoop } } } // buildOutput builds an output text for writing results -func buildOutput(template *templates.Template, req *retryablehttp.Request, extractorResults []string) string { +func buildOutput(template *templates.Template, req *retryablehttp.Request, extractorResults []string, matcher *matchers.Matcher) string { builder := &strings.Builder{} builder.WriteRune('[') builder.WriteString(template.ID) + if len(matcher.Name) > 0 { + builder.WriteString(":") + builder.WriteString(matcher.Name) + } builder.WriteString("] ") + // Escape the URL by replacing all % with %% URL := req.URL.String() escapedURL := strings.Replace(URL, "%", "%%", -1) diff --git a/pkg/matchers/matchers.go b/pkg/matchers/matchers.go index ffc0bfec..c9cabe84 100644 --- a/pkg/matchers/matchers.go +++ b/pkg/matchers/matchers.go @@ -20,6 +20,8 @@ type Matcher struct { // Regex are the regex pattern required to be present in the response Regex []string `yaml:"regex,omitempty"` // regexCompiled is the compiled variant + // Matcher Name to be displayed in result output. + Name string `yaml:"name,omitempty"` regexCompiled []*regexp.Regexp // Condition is the optional condition between two matcher variables