package nuclei
import (
"context"
"errors"
"time"
"github.com/projectdiscovery/goflags"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/ratelimit"
"github.com/projectdiscovery/utils/errkit"
"github.com/projectdiscovery/nuclei/v3/pkg/authprovider"
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
"github.com/projectdiscovery/nuclei/v3/pkg/output"
"github.com/projectdiscovery/nuclei/v3/pkg/progress"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/hosterrorscache"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/interactsh"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/vardump"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/headless/engine"
"github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
pkgtypes "github.com/projectdiscovery/nuclei/v3/pkg/types"
)
type TemplateSources struct {
Templates []string
Workflows []string
RemoteTemplates []string
RemoteWorkflows []string
TrustedDomains []string
}
func WithTemplatesOrWorkflows(sources TemplateSources) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.Templates = sources.Templates
e.opts.Workflows = sources.Workflows
e.opts.TemplateURLs = sources.RemoteTemplates
e.opts.WorkflowURLs = sources.RemoteWorkflows
e.opts.RemoteTemplateDomainList = append(e.opts.RemoteTemplateDomainList, sources.TrustedDomains...)
return nil
}
}
type TemplateFilters struct {
Severity string
ExcludeSeverities string
ProtocolTypes string
ExcludeProtocolTypes string
Authors []string
Tags []string
ExcludeTags []string
IncludeTags []string
IDs []string
ExcludeIDs []string
TemplateCondition []string
}
func WithTemplateFilters(filters TemplateFilters) NucleiSDKOptions {
return func(e *NucleiEngine) error {
s := severity.Severities{}
if err := s.Set(filters.Severity); err != nil {
return err
}
es := severity.Severities{}
if err := es.Set(filters.ExcludeSeverities); err != nil {
return err
}
pt := types.ProtocolTypes{}
if err := pt.Set(filters.ProtocolTypes); err != nil {
return err
}
ept := types.ProtocolTypes{}
if err := ept.Set(filters.ExcludeProtocolTypes); err != nil {
return err
}
e.opts.Authors = filters.Authors
e.opts.Tags = filters.Tags
e.opts.ExcludeTags = filters.ExcludeTags
e.opts.IncludeTags = filters.IncludeTags
e.opts.IncludeIds = filters.IDs
e.opts.ExcludeIds = filters.ExcludeIDs
e.opts.Severities = s
e.opts.ExcludeSeverities = es
e.opts.Protocols = pt
e.opts.ExcludeProtocols = ept
e.opts.IncludeConditions = filters.TemplateCondition
return nil
}
}
type InteractshOpts interactsh.Options
func WithInteractshOptions(opts InteractshOpts) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithInteractshOptions")
}
optsPtr := &opts
e.interactshOpts = (*interactsh.Options)(optsPtr)
return nil
}
}
type Concurrency struct {
TemplateConcurrency int
HostConcurrency int
HeadlessHostConcurrency int
HeadlessTemplateConcurrency int
JavascriptTemplateConcurrency int
TemplatePayloadConcurrency int
ProbeConcurrency int
}
func WithConcurrency(opts Concurrency) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if opts.TemplateConcurrency <= 0 {
return errors.New("template threads must be at least 1")
}
if opts.HostConcurrency <= 0 {
return errors.New("host concurrency must be at least 1")
}
if opts.HeadlessHostConcurrency <= 0 {
return errors.New("headless host concurrency must be at least 1")
}
if opts.HeadlessTemplateConcurrency <= 0 {
return errors.New("headless template threads must be at least 1")
}
if opts.JavascriptTemplateConcurrency <= 0 {
return errors.New("js must be at least 1")
}
if opts.TemplatePayloadConcurrency <= 0 {
return errors.New("payload concurrency must be at least 1")
}
if opts.ProbeConcurrency <= 0 {
return errors.New("probe concurrency must be at least 1")
}
e.opts.TemplateThreads = opts.TemplateConcurrency
e.opts.BulkSize = opts.HostConcurrency
e.opts.HeadlessBulkSize = opts.HeadlessHostConcurrency
e.opts.HeadlessTemplateThreads = opts.HeadlessTemplateConcurrency
e.opts.JsConcurrency = opts.JavascriptTemplateConcurrency
e.opts.PayloadConcurrency = opts.TemplatePayloadConcurrency
e.opts.ProbeConcurrency = opts.ProbeConcurrency
return nil
}
}
func WithResponseReadSize(responseReadSize int) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if responseReadSize < 0 {
return errors.New("response read size must be non-negative")
}
e.opts.ResponseReadSize = responseReadSize
return nil
}
}
func WithGlobalRateLimit(maxTokens int, duration time.Duration) NucleiSDKOptions {
return WithGlobalRateLimitCtx(context.Background(), maxTokens, duration)
}
func WithGlobalRateLimitCtx(ctx context.Context, maxTokens int, duration time.Duration) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.RateLimit = maxTokens
e.opts.RateLimitDuration = duration
e.rateLimiter = ratelimit.New(ctx, uint(e.opts.RateLimit), e.opts.RateLimitDuration)
return nil
}
}
type HeadlessOpts struct {
PageTimeout int
ShowBrowser bool
HeadlessOptions []string
UseChrome bool
}
func EnableHeadlessWithOpts(hopts *HeadlessOpts) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.Headless = true
if hopts != nil {
e.opts.HeadlessOptionalArguments = hopts.HeadlessOptions
e.opts.PageTimeout = hopts.PageTimeout
e.opts.ShowBrowser = hopts.ShowBrowser
e.opts.UseInstalledChrome = hopts.UseChrome
}
if engine.MustDisableSandbox() {
e.Logger.Warning().Msgf("The current platform and privileged user will run the browser without sandbox")
}
browser, err := engine.New(e.opts)
if err != nil {
return err
}
e.browserInstance = browser
return nil
}
}
type StatsOptions struct {
Interval int
JSON bool
MetricServerPort int
}
func EnableStatsWithOpts(opts StatsOptions) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "EnableStatsWithOpts")
}
if opts.Interval == 0 {
opts.Interval = 5
}
e.opts.StatsInterval = opts.Interval
e.enableStats = true
e.opts.StatsJSON = opts.JSON
e.opts.MetricsPort = opts.MetricServerPort
return nil
}
}
type VerbosityOptions struct {
Verbose bool
Silent bool
Debug bool
DebugRequest bool
DebugResponse bool
ShowVarDump bool
}
func WithVerbosity(opts VerbosityOptions) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithVerbosity")
}
e.opts.Verbose = opts.Verbose
e.opts.Silent = opts.Silent
e.opts.Debug = opts.Debug
e.opts.DebugRequests = opts.DebugRequest
e.opts.DebugResponse = opts.DebugResponse
if opts.ShowVarDump {
vardump.EnableVarDump = true
}
return nil
}
}
type NetworkConfig struct {
DisableMaxHostErr bool
Interface string
InternalResolversList []string
LeaveDefaultPorts bool
MaxHostError int
Retries int
SourceIP string
SystemResolvers bool
Timeout int
TrackError []string
}
func WithNetworkConfig(opts NetworkConfig) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithNetworkConfig")
}
e.opts.NoHostErrors = opts.DisableMaxHostErr
e.opts.MaxHostError = opts.MaxHostError
if e.opts.ShouldUseHostError() {
maxHostError := opts.MaxHostError
if e.opts.TemplateThreads > maxHostError {
e.Logger.Warning().Msg("The concurrency value is higher than max-host-error")
e.Logger.Info().Msgf("Adjusting max-host-error to the concurrency value: %d", e.opts.TemplateThreads)
maxHostError = e.opts.TemplateThreads
e.opts.MaxHostError = maxHostError
}
cache := hosterrorscache.New(maxHostError, hosterrorscache.DefaultMaxHostsCount, e.opts.TrackError)
cache.SetVerbose(e.opts.Verbose)
e.hostErrCache = cache
}
e.opts.Timeout = opts.Timeout
e.opts.Retries = opts.Retries
e.opts.LeaveDefaultPorts = opts.LeaveDefaultPorts
e.opts.Interface = opts.Interface
e.opts.SourceIP = opts.SourceIP
e.opts.SystemResolvers = opts.SystemResolvers
e.opts.InternalResolversList = opts.InternalResolversList
return nil
}
}
func WithProxy(proxy []string, proxyInternalRequests bool) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithProxy")
}
e.opts.Proxy = proxy
e.opts.ProxyInternal = proxyInternalRequests
return nil
}
}
func WithScanStrategy(strategy string) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.ScanStrategy = strategy
return nil
}
}
type OutputWriter output.Writer
func UseOutputWriter(writer OutputWriter) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "UseOutputWriter")
}
e.customWriter = writer
return nil
}
}
type StatsWriter progress.Progress
func UseStatsWriter(writer StatsWriter) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "UseStatsWriter")
}
e.customProgress = writer
return nil
}
}
func WithTemplateUpdateCallback(disableTemplatesAutoUpgrade bool, callback func(newVersion string)) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithTemplateUpdateCallback")
}
e.disableTemplatesAutoUpgrade = disableTemplatesAutoUpgrade
e.onUpdateAvailableCallback = callback
return nil
}
}
func WithSandboxOptions(allowLocalFileAccess bool, restrictLocalNetworkAccess bool) NucleiSDKOptions {
return func(e *NucleiEngine) error {
if e.mode == threadSafe {
return errkit.Wrap(ErrOptionsNotSupported, "WithSandboxOptions")
}
e.opts.AllowLocalFileAccess = allowLocalFileAccess
e.opts.RestrictLocalNetworkAccess = restrictLocalNetworkAccess
return nil
}
}
func EnableCodeTemplates() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.EnableCodeTemplates = true
e.opts.EnableSelfContainedTemplates = true
return nil
}
}
func EnableSelfContainedTemplates() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.EnableSelfContainedTemplates = true
return nil
}
}
func EnableGlobalMatchersTemplates() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.EnableGlobalMatchersTemplates = true
return nil
}
}
func DisableTemplateCache() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.DoNotCacheTemplates = true
return nil
}
}
func EnableFileTemplates() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.EnableFileTemplates = true
return nil
}
}
func WithHeaders(headers []string) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.CustomHeaders = headers
return nil
}
}
func WithVars(vars []string) NucleiSDKOptions {
runtimeVars := goflags.RuntimeMap{}
for _, v := range vars {
err := runtimeVars.Set(v)
if err != nil {
return func(e *NucleiEngine) error {
return err
}
}
}
return func(e *NucleiEngine) error {
e.opts.Vars = runtimeVars
return nil
}
}
func EnablePassiveMode() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.OfflineHTTP = true
e.opts.DisableHTTPProbe = true
return nil
}
}
func EnableMatcherStatus() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.MatcherStatus = true
return nil
}
}
func WithAuthProvider(provider authprovider.AuthProvider) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.authprovider = provider
return nil
}
}
func LoadSecretsFromFile(files []string, prefetch bool) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.SecretsFile = goflags.StringSlice(files)
e.opts.PreFetchSecrets = prefetch
return nil
}
}
func DASTMode() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.DAST = true
return nil
}
}
func SignedTemplatesOnly() NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.DisableUnsignedTemplates = true
return nil
}
}
func WithCatalog(cat catalog.Catalog) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.catalog = cat
return nil
}
}
func DisableUpdateCheck() NucleiSDKOptions {
return func(e *NucleiEngine) error {
DefaultConfig.DisableUpdateCheck()
return nil
}
}
func WithResumeFile(file string) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts.Resume = file
return nil
}
}
func WithLogger(logger *gologger.Logger) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.Logger = logger
if e.opts != nil {
e.opts.Logger = logger
}
if e.executerOpts != nil {
e.executerOpts.Logger = logger
}
return nil
}
}
func WithOptions(opts *pkgtypes.Options) NucleiSDKOptions {
return func(e *NucleiEngine) error {
e.opts = opts
return nil
}
}