2021-02-03 13:08:31 +05:30
|
|
|
package testutils
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-19 13:39:28 +02:00
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
2022-10-12 22:04:37 -05:00
|
|
|
"github.com/projectdiscovery/ratelimit"
|
2021-09-03 16:48:39 +03:00
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
"github.com/logrusorgru/aurora"
|
2021-11-25 17:03:56 +02:00
|
|
|
|
2021-02-27 20:54:22 +05:30
|
|
|
"github.com/projectdiscovery/gologger/levels"
|
2023-04-19 21:58:48 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
|
2022-08-10 23:35:58 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/disk"
|
2021-07-12 17:20:01 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model"
|
2021-09-03 16:48:39 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
|
2021-02-03 13:08:31 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
2021-03-09 17:19:03 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/progress"
|
2021-02-03 13:08:31 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolinit"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Init initializes the protocols and their configurations
|
|
|
|
|
func Init(options *types.Options) {
|
2021-02-26 13:13:11 +05:30
|
|
|
_ = protocolinit.Init(options)
|
2021-02-03 13:08:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultOptions is the default options structure for nuclei during mocking.
|
|
|
|
|
var DefaultOptions = &types.Options{
|
2021-11-05 15:27:49 +05:30
|
|
|
Metrics: false,
|
|
|
|
|
Debug: false,
|
|
|
|
|
DebugRequests: false,
|
|
|
|
|
DebugResponse: false,
|
|
|
|
|
Silent: false,
|
|
|
|
|
Verbose: false,
|
|
|
|
|
NoColor: true,
|
|
|
|
|
UpdateTemplates: false,
|
2023-03-31 05:59:29 -04:00
|
|
|
JSONL: false,
|
2021-11-05 15:27:49 +05:30
|
|
|
JSONRequests: false,
|
|
|
|
|
EnableProgressBar: false,
|
|
|
|
|
TemplateList: false,
|
|
|
|
|
Stdin: false,
|
|
|
|
|
StopAtFirstMatch: false,
|
|
|
|
|
NoMeta: false,
|
|
|
|
|
Project: false,
|
|
|
|
|
MetricsPort: 0,
|
|
|
|
|
BulkSize: 25,
|
|
|
|
|
TemplateThreads: 10,
|
|
|
|
|
Timeout: 5,
|
|
|
|
|
Retries: 1,
|
|
|
|
|
RateLimit: 150,
|
|
|
|
|
ProjectPath: "",
|
|
|
|
|
Severities: severity.Severities{},
|
|
|
|
|
Targets: []string{},
|
|
|
|
|
TargetsFilePath: "",
|
|
|
|
|
Output: "",
|
2021-11-10 10:00:03 -06:00
|
|
|
Proxy: []string{},
|
2021-11-05 15:27:49 +05:30
|
|
|
TraceLogFile: "",
|
|
|
|
|
Templates: []string{},
|
|
|
|
|
ExcludedTemplates: []string{},
|
|
|
|
|
CustomHeaders: []string{},
|
2022-11-03 20:27:18 +05:30
|
|
|
InteractshURL: "https://oast.fun",
|
2021-11-05 15:27:49 +05:30
|
|
|
InteractionsCacheSize: 5000,
|
|
|
|
|
InteractionsEviction: 60,
|
2021-11-25 18:54:16 +02:00
|
|
|
InteractionsCoolDownPeriod: 5,
|
2021-11-05 15:27:49 +05:30
|
|
|
InteractionsPollDuration: 5,
|
2022-11-03 20:27:18 +05:30
|
|
|
GithubTemplateRepo: []string{},
|
|
|
|
|
GithubToken: "",
|
2021-02-03 13:08:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TemplateInfo contains info for a mock executed template.
|
|
|
|
|
type TemplateInfo struct {
|
|
|
|
|
ID string
|
2021-07-12 17:20:01 +03:00
|
|
|
Info model.Info
|
2021-02-03 13:08:31 +05:30
|
|
|
Path string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMockExecuterOptions creates a new mock executeroptions struct
|
2023-05-31 16:58:10 -04:00
|
|
|
func NewMockExecuterOptions(options *types.Options, info *TemplateInfo) *protocols.ExecutorOptions {
|
2023-01-13 13:41:05 +05:30
|
|
|
progressImpl, _ := progress.NewStatsTicker(0, false, false, false, false, 0)
|
2023-05-31 16:58:10 -04:00
|
|
|
executerOpts := &protocols.ExecutorOptions{
|
2021-02-03 13:08:31 +05:30
|
|
|
TemplateID: info.ID,
|
|
|
|
|
TemplateInfo: info.Info,
|
|
|
|
|
TemplatePath: info.Path,
|
2021-11-05 03:01:41 +05:30
|
|
|
Output: NewMockOutputWriter(),
|
2021-02-03 13:08:31 +05:30
|
|
|
Options: options,
|
2021-02-26 13:13:11 +05:30
|
|
|
Progress: progressImpl,
|
2021-02-03 13:08:31 +05:30
|
|
|
ProjectFile: nil,
|
2021-02-08 02:07:19 +05:30
|
|
|
IssuesClient: nil,
|
2021-02-21 16:31:34 +05:30
|
|
|
Browser: nil,
|
2023-04-19 21:58:48 +05:30
|
|
|
Catalog: disk.NewCatalog(config.DefaultConfig.TemplatesDirectory),
|
2022-11-28 12:49:30 +05:30
|
|
|
RateLimiter: ratelimit.New(context.Background(), uint(options.RateLimit), time.Second),
|
2021-02-03 13:08:31 +05:30
|
|
|
}
|
|
|
|
|
return executerOpts
|
|
|
|
|
}
|
2021-02-27 20:54:22 +05:30
|
|
|
|
|
|
|
|
// NoopWriter is a NooP gologger writer.
|
|
|
|
|
type NoopWriter struct{}
|
|
|
|
|
|
|
|
|
|
// Write writes the data to an output writer.
|
|
|
|
|
func (n *NoopWriter) Write(data []byte, level levels.Level) {}
|
2021-11-05 03:01:41 +05:30
|
|
|
|
|
|
|
|
// MockOutputWriter is a mocked output writer.
|
|
|
|
|
type MockOutputWriter struct {
|
|
|
|
|
aurora aurora.Aurora
|
|
|
|
|
RequestCallback func(templateID, url, requestType string, err error)
|
|
|
|
|
WriteCallback func(o *output.ResultEvent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewMockOutputWriter creates a new mock output writer
|
|
|
|
|
func NewMockOutputWriter() *MockOutputWriter {
|
|
|
|
|
return &MockOutputWriter{aurora: aurora.NewAurora(false)}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close closes the output writer interface
|
|
|
|
|
func (m *MockOutputWriter) Close() {}
|
|
|
|
|
|
|
|
|
|
// Colorizer returns the colorizer instance for writer
|
|
|
|
|
func (m *MockOutputWriter) Colorizer() aurora.Aurora {
|
|
|
|
|
return m.aurora
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write writes the event to file and/or screen.
|
|
|
|
|
func (m *MockOutputWriter) Write(result *output.ResultEvent) error {
|
|
|
|
|
if m.WriteCallback != nil {
|
|
|
|
|
m.WriteCallback(result)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Request writes a log the requests trace log
|
|
|
|
|
func (m *MockOutputWriter) Request(templateID, url, requestType string, err error) {
|
|
|
|
|
if m.RequestCallback != nil {
|
|
|
|
|
m.RequestCallback(templateID, url, requestType, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 17:03:56 +02:00
|
|
|
// WriteFailure writes the event to file and/or screen.
|
2021-11-22 17:53:25 +05:30
|
|
|
func (m *MockOutputWriter) WriteFailure(result output.InternalEvent) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-04-01 14:29:02 -05:00
|
|
|
func (m *MockOutputWriter) WriteStoreDebugData(host, templateID, eventType string, data string) {
|
|
|
|
|
|
|
|
|
|
}
|
2021-11-22 17:53:25 +05:30
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
type MockProgressClient struct{}
|
|
|
|
|
|
|
|
|
|
// Stop stops the progress recorder.
|
|
|
|
|
func (m *MockProgressClient) Stop() {}
|
|
|
|
|
|
|
|
|
|
// Init inits the progress bar with initial details for scan
|
|
|
|
|
func (m *MockProgressClient) Init(hostCount int64, rulesCount int, requestCount int64) {}
|
|
|
|
|
|
|
|
|
|
// AddToTotal adds a value to the total request count
|
|
|
|
|
func (m *MockProgressClient) AddToTotal(delta int64) {}
|
|
|
|
|
|
|
|
|
|
// IncrementRequests increments the requests counter by 1.
|
|
|
|
|
func (m *MockProgressClient) IncrementRequests() {}
|
|
|
|
|
|
2023-01-13 13:41:05 +05:30
|
|
|
// SetRequests sets the counter by incrementing it with a delta
|
|
|
|
|
func (m *MockProgressClient) SetRequests(count uint64) {}
|
|
|
|
|
|
2021-11-05 03:01:41 +05:30
|
|
|
// IncrementMatched increments the matched counter by 1.
|
|
|
|
|
func (m *MockProgressClient) IncrementMatched() {}
|
|
|
|
|
|
|
|
|
|
// IncrementErrorsBy increments the error counter by count.
|
|
|
|
|
func (m *MockProgressClient) IncrementErrorsBy(count int64) {}
|
|
|
|
|
|
|
|
|
|
// IncrementFailedRequestsBy increments the number of requests counter by count
|
|
|
|
|
// along with errors.
|
|
|
|
|
func (m *MockProgressClient) IncrementFailedRequestsBy(count int64) {}
|