mirror of
https://github.com/projectdiscovery/nuclei
synced 2026-06-08 16:50:47 +00:00
609cf3daba
WithConfigFile/Bytes now decode YAML directly into a typed RuntimeConfig struct and apply it via MergeOptions(opts). The goflags-backed reflection overlay had too much surface area (266-line flag inventory duplicate plus a flag-default vs DefaultOptions diff) for the actual SDK use case, which is accepting a known set of scan knobs from cloud-shipped YAML. RuntimeConfig schema covers tags/severity filtering, headers/vars, interactsh, socks5-proxy, plus rate-limit/bulk-size/concurrency/timeout/ retries/rate-limit-host. Scalar knobs use *int so omitted keys preserve the engine's existing value. Drops internal/runner/flags.go (266 lines) and lib/config_load.go (86 lines). Reverts internal/runner/runner.go to dev (no SetupPDCPUpload export; SDK inlines its own PDCP wrap). Keeps the LoadReportingOptionsFromBytes extract in internal/runner/options.go for WithReportingConfigFile/Bytes.
123 lines
3.2 KiB
Go
123 lines
3.2 KiB
Go
//go:build !race
|
|
|
|
package nuclei_test
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/kitabisa/go-ci"
|
|
nuclei "github.com/projectdiscovery/nuclei/v3/lib"
|
|
"github.com/remeh/sizedwaitgroup"
|
|
)
|
|
|
|
// A very simple example on how to use nuclei engine
|
|
func ExampleNucleiEngine() {
|
|
// create nuclei engine with options
|
|
ne, err := nuclei.NewNucleiEngine(
|
|
nuclei.WithTemplateFilters(nuclei.TemplateFilters{IDs: []string{"self-signed-ssl"}}), // only run self-signed-ssl template
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// load targets and optionally probe non http/https targets
|
|
ne.LoadTargets([]string{"scanme.sh"}, false)
|
|
// when callback is nil it nuclei will print JSON output to stdout
|
|
err = ne.ExecuteWithCallback(nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer ne.Close()
|
|
|
|
// Output:
|
|
// [self-signed-ssl] scanme.sh:443
|
|
}
|
|
|
|
func ExampleThreadSafeNucleiEngine() {
|
|
// create nuclei engine with options
|
|
ne, err := nuclei.NewThreadSafeNucleiEngine()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
// setup sizedWaitgroup to handle concurrency
|
|
// here we are using sizedWaitgroup to limit concurrency to 1
|
|
// but can be anything in general
|
|
sg := sizedwaitgroup.New(1)
|
|
|
|
// scan 1 = run dns templates on scanme.sh
|
|
sg.Add()
|
|
go func() {
|
|
defer sg.Done()
|
|
err = ne.ExecuteNucleiWithOpts([]string{"scanme.sh"},
|
|
nuclei.WithTemplateFilters(nuclei.TemplateFilters{IDs: []string{"nameserver-fingerprint"}}), // only run self-signed-ssl template
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
// scan 2 = run dns templates on honey.scanme.sh
|
|
sg.Add()
|
|
go func() {
|
|
defer sg.Done()
|
|
err = ne.ExecuteNucleiWithOpts([]string{"honey.scanme.sh"}, nuclei.WithTemplateFilters(nuclei.TemplateFilters{ProtocolTypes: "dns"}))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}()
|
|
|
|
// wait for all scans to finish
|
|
sg.Wait()
|
|
defer ne.Close()
|
|
|
|
// Output:
|
|
// [nameserver-fingerprint] scanme.sh
|
|
// [caa-fingerprint] honey.scanme.sh
|
|
}
|
|
|
|
// ExampleWithPDCPUpload uploads findings to the PDCP dashboard from SDK code,
|
|
// matching `-dashboard -scan-id -team-id` on the CLI. Pass an existing scanID
|
|
// to append; pass empty to let the server create a new scan.
|
|
func ExampleWithPDCPUpload() {
|
|
ne, err := nuclei.NewNucleiEngine(
|
|
nuclei.WithTemplateFilters(nuclei.TemplateFilters{IDs: []string{"self-signed-ssl"}}),
|
|
nuclei.WithPDCPUpload("" /* scanID */, "" /* teamID, "" = personal */),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer ne.Close()
|
|
ne.LoadTargets([]string{"scanme.sh"}, false)
|
|
if err := ne.ExecuteWithCallback(nil); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// ExampleWithConfigFile ingests a RuntimeConfig YAML (tags, severity,
|
|
// exclude-tags, headers, vars, etc.) and merges it into the engine options.
|
|
func ExampleWithConfigFile() {
|
|
ne, err := nuclei.NewNucleiEngine(
|
|
nuclei.WithConfigFile("nuclei.yaml"),
|
|
nuclei.WithTemplateFilters(nuclei.TemplateFilters{IDs: []string{"self-signed-ssl"}}),
|
|
)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer ne.Close()
|
|
ne.LoadTargets([]string{"scanme.sh"}, false)
|
|
if err := ne.ExecuteWithCallback(nil); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
// this file only contains testtables examples https://go.dev/blog/examples
|
|
// and actual functionality test are in sdk_test.go
|
|
if ci.IsCI() {
|
|
// no need to run this test on github actions
|
|
return
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
}
|