Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a crash with uninitialized interactsh client #1251

Merged
merged 5 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions v2/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,21 @@ func New(options *types.Options) (*Runner, error) {
}
}

if !options.NoInteractsh {
opts := interactsh.NewDefaultOptions(runner.output, runner.issuesClient, runner.progress)
opts.Debug = runner.options.Debug
opts.ServerURL = options.InteractshURL
opts.Authorization = options.InteractshToken
opts.CacheSize = int64(options.InteractionsCacheSize)
opts.Eviction = time.Duration(options.InteractionsEviction) * time.Second
opts.ColldownPeriod = time.Duration(options.InteractionsCooldownPeriod) * time.Second
opts.PollDuration = time.Duration(options.InteractionsPollDuration) * time.Second

interactshClient, err := interactsh.New(opts)
if err != nil {
gologger.Error().Msgf("Could not create interactsh client: %s", err)
} else {
runner.interactsh = interactshClient
}
opts := interactsh.NewDefaultOptions(runner.output, runner.issuesClient, runner.progress)
opts.Debug = runner.options.Debug
opts.ServerURL = options.InteractshURL
opts.Authorization = options.InteractshToken
opts.CacheSize = int64(options.InteractionsCacheSize)
opts.Eviction = time.Duration(options.InteractionsEviction) * time.Second
opts.ColldownPeriod = time.Duration(options.InteractionsCooldownPeriod) * time.Second
opts.PollDuration = time.Duration(options.InteractionsPollDuration) * time.Second
opts.NoInteractsh = runner.options.NoInteractsh

interactshClient, err := interactsh.New(opts)
if err != nil {
gologger.Error().Msgf("Could not create interactsh client: %s", err)
} else {
runner.interactsh = interactshClient
}

if options.RateLimitMinute > 0 {
Expand Down
4 changes: 0 additions & 4 deletions v2/pkg/operators/matchers/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ func (m *Matcher) CompileMatchers() error {
if !ok {
return fmt.Errorf("unknown matcher type specified: %s", m.Type)
}
// By default, match on body if user hasn't provided any specific items
if m.Part == "" {
m.Part = "response"
}

// Compile the regexes
for _, regex := range m.Regex {
Expand Down
6 changes: 5 additions & 1 deletion v2/pkg/protocols/common/interactsh/interactsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ type Options struct {
Progress progress.Progress
// Debug specifies whether debugging output should be shown for interactsh-client
Debug bool

NoInteractsh bool
}

const defaultMaxInteractionsCount = 5000
Expand Down Expand Up @@ -118,6 +120,9 @@ func NewDefaultOptions(output output.Writer, reporting *reporting.Client, progre
}

func (c *Client) firstTimeInitializeClient() error {
if c.options.NoInteractsh {
return nil // do not init if disabled
}
interactsh, err := client.New(&client.Options{
ServerURL: c.options.ServerURL,
Token: c.options.Authorization,
Expand Down Expand Up @@ -221,7 +226,6 @@ func (c *Client) Close() bool {
// It accepts data to replace as well as the URL to replace placeholders
// with generated uniquely for each request.
func (c *Client) ReplaceMarkers(data string, interactshURLs []string) (string, []string) {

for strings.Contains(data, interactshURLMarker) {
url := c.URL()
interactshURLs = append(interactshURLs, url)
Expand Down
31 changes: 22 additions & 9 deletions v2/pkg/protocols/http/build_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ func (r *requestGenerator) Make(baseURL string, dynamicValues map[string]interfa
}
ctx := context.Background()

data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)

for payloadName, payloadValue := range payloads {
payloads[payloadName], r.interactshURLs = r.options.Interactsh.ReplaceMarkers(types.ToString(payloadValue), r.interactshURLs)
if r.options.Interactsh != nil {
data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)
for payloadName, payloadValue := range payloads {
payloads[payloadName], r.interactshURLs = r.options.Interactsh.ReplaceMarkers(types.ToString(payloadValue), r.interactshURLs)
}
} else {
for payloadName, payloadValue := range payloads {
payloads[payloadName] = types.ToString(payloadValue)
}
}

parsed, err := url.Parse(baseURL)
Expand Down Expand Up @@ -171,7 +176,9 @@ func baseURLWithTemplatePrefs(data string, parsed *url.URL) (string, *url.URL) {

// MakeHTTPRequestFromModel creates a *http.Request from a request template
func (r *requestGenerator) makeHTTPRequestFromModel(ctx context.Context, data string, values, generatorValues map[string]interface{}) (*generatedRequest, error) {
data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)
if r.options.Interactsh != nil {
data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)
}

// Combine the template payloads along with base
// request values.
Expand Down Expand Up @@ -204,7 +211,9 @@ func (r *requestGenerator) makeHTTPRequestFromModel(ctx context.Context, data st

// makeHTTPRequestFromRaw creates a *http.Request from a raw request
func (r *requestGenerator) makeHTTPRequestFromRaw(ctx context.Context, baseURL, data string, values, payloads map[string]interface{}) (*generatedRequest, error) {
data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)
if r.options.Interactsh != nil {
data, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(data, r.interactshURLs)
}
return r.handleRawWithPayloads(ctx, data, baseURL, values, payloads)
}

Expand Down Expand Up @@ -268,7 +277,9 @@ func (r *requestGenerator) handleRawWithPayloads(ctx context.Context, rawRequest
func (r *requestGenerator) fillRequest(req *http.Request, values map[string]interface{}) (*retryablehttp.Request, error) {
// Set the header values requested
for header, value := range r.request.Headers {
value, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(value, r.interactshURLs)
if r.options.Interactsh != nil {
value, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(value, r.interactshURLs)
}
value, err := expressions.Evaluate(value, values)
if err != nil {
return nil, errors.Wrap(err, "could not evaluate helper expressions")
Expand All @@ -286,8 +297,10 @@ func (r *requestGenerator) fillRequest(req *http.Request, values map[string]inte

// Check if the user requested a request body
if r.request.Body != "" {
var body string
body, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(r.request.Body, r.interactshURLs)
body := r.request.Body
if r.options.Interactsh != nil {
body, r.interactshURLs = r.options.Interactsh.ReplaceMarkers(r.request.Body, r.interactshURLs)
}
body, err := expressions.Evaluate(body, values)
if err != nil {
return nil, errors.Wrap(err, "could not evaluate helper expressions")
Expand Down
7 changes: 6 additions & 1 deletion v2/pkg/protocols/network/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ func (request *Request) executeRequestWithPayloads(actualAddress, address, input
case "hex":
data, err = hex.DecodeString(input.Data)
default:
input.Data, interactshURLs = request.options.Interactsh.ReplaceMarkers(input.Data, []string{})
data = []byte(input.Data)
}
if err != nil {
Expand All @@ -150,6 +149,12 @@ func (request *Request) executeRequestWithPayloads(actualAddress, address, input
}
reqBuilder.Grow(len(input.Data))

if request.options.Interactsh != nil {
var transformedData string
transformedData, interactshURLs = request.options.Interactsh.ReplaceMarkers(string(data), []string{})
data = []byte(transformedData)
}

finalData, dataErr := expressions.EvaluateByte(data, payloads)
if dataErr != nil {
request.options.Output.Request(request.options.TemplatePath, address, request.Type().String(), dataErr)
Expand Down
1 change: 1 addition & 0 deletions v2/pkg/protocols/offlinehttp/operators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestHTTPOperatorExtract(t *testing.T) {
extractor := &extractors.Extractor{
Type: "kval",
KVal: []string{"test-header"},
Part: "header",
}
err = extractor.CompileExtractors()
require.Nil(t, err, "could not compile kval extractor")
Expand Down