Skip to content

Instantly share code, notes, and snippets.

@ShubhamRasal
Created January 19, 2023 17:41
Show Gist options
  • Save ShubhamRasal/04d7746d5e7c1fcb3ac0a35bcfd4eb44 to your computer and use it in GitHub Desktop.
Save ShubhamRasal/04d7746d5e7c1fcb3ac0a35bcfd4eb44 to your computer and use it in GitHub Desktop.
nuclie-as-sdk
package main
import (
"context"
"fmt"
"log"
"os"
"path"
"time"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/disk"
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader"
"github.com/projectdiscovery/nuclei/v2/pkg/core"
"github.com/projectdiscovery/nuclei/v2/pkg/core/inputs"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/parsers"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/hosterrorscache"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/interactsh"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolinit"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate"
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
"github.com/projectdiscovery/nuclei/v2/pkg/types"
"github.com/projectdiscovery/ratelimit"
)
// working nuclei
func runNuclei(template, target []string, out *[]string) {
cache := hosterrorscache.New(30, hosterrorscache.DefaultMaxHostsCount)
defer cache.Close()
mockProgress := &testutils.MockProgressClient{}
// out := []string{}
outputWriter := testutils.NewMockOutputWriter()
outputWriter.WriteCallback = func(event *output.ResultEvent) {
*out = append(*out, fmt.Sprintf("%v\n", event))
}
defaultOpts := types.DefaultOptions()
defaultOpts.ExcludeTags = config.ReadIgnoreFile().Tags
defaultOpts.Sandbox = true
defaultOpts.JSONRequests = true
defaultOpts.Templates = template
_ = protocolstate.Init(defaultOpts)
_ = protocolinit.Init(defaultOpts)
interactOpts := interactsh.NewDefaultOptions(outputWriter, nil, mockProgress)
interactClient, err := interactsh.New(interactOpts)
if err != nil {
fmt.Println(err, "could not create interact client")
}
defer interactClient.Close()
home, _ := os.UserHomeDir()
catalog := disk.NewCatalog(path.Join(home, "nuclei-templates"))
ratelimiter := ratelimit.New(context.Background(), 150, time.Second)
defer ratelimiter.Stop()
executerOpts := protocols.ExecuterOptions{
Output: outputWriter,
Options: defaultOpts,
Progress: mockProgress,
Catalog: catalog,
RateLimiter: ratelimiter,
Interactsh: interactClient,
HostErrorsCache: cache,
Colorizer: aurora.NewAurora(true),
ResumeCfg: types.NewResumeCfg(),
}
engine := core.New(defaultOpts)
engine.SetExecuterOptions(executerOpts)
workflowLoader, err := parsers.NewLoader(&executerOpts)
if err != nil {
log.Fatalf("Could not create workflow loader: %s\n", err)
}
executerOpts.WorkflowLoader = workflowLoader
configObject, err := config.ReadConfiguration()
if err != nil {
fmt.Println(err, "could not read configuration file")
}
store, err := loader.New(loader.NewConfig(defaultOpts, configObject, catalog, executerOpts))
if err != nil {
fmt.Println(err, "could not create loader")
}
store.Load()
var metaInput []*contextargs.MetaInput
for _, tar := range target {
metaInput = append(metaInput, &contextargs.MetaInput{
Input: tar,
})
}
input := &inputs.SimpleInputProvider{Inputs: metaInput}
_ = engine.Execute(store.Templates(), input)
engine.WorkPool().Wait() // Wait for the scan to finish
outputWriter.Close()
return
}
func runNotWorking(template, target []string) []string {
cache := hosterrorscache.New(30, hosterrorscache.DefaultMaxHostsCount)
defer cache.Close()
mockProgress := &testutils.MockProgressClient{}
out := []string{}
outputWriter := testutils.NewMockOutputWriter()
outputWriter.WriteCallback = func(event *output.ResultEvent) {
out = append(out, fmt.Sprintf("%v\n", event))
}
defaultOpts := types.DefaultOptions()
defaultOpts.ExcludeTags = config.ReadIgnoreFile().Tags
defaultOpts.Sandbox = true
defaultOpts.JSONRequests = true
defaultOpts.Templates = template
_ = protocolstate.Init(defaultOpts)
_ = protocolinit.Init(defaultOpts)
interactOpts := interactsh.NewDefaultOptions(outputWriter, nil, mockProgress)
interactClient, err := interactsh.New(interactOpts)
if err != nil {
fmt.Println(err, "could not create interact client")
}
defer interactClient.Close()
home, _ := os.UserHomeDir()
catalog := disk.NewCatalog(path.Join(home, "nuclei-templates"))
ratelimiter := ratelimit.New(context.Background(), 150, time.Second)
defer ratelimiter.Stop()
executerOpts := protocols.ExecuterOptions{
Output: outputWriter,
Options: defaultOpts,
Progress: mockProgress,
Catalog: catalog,
RateLimiter: ratelimiter,
// Interactsh: interactClient,
HostErrorsCache: cache,
Colorizer: aurora.NewAurora(true),
ResumeCfg: types.NewResumeCfg(),
}
engine := core.New(defaultOpts)
engine.SetExecuterOptions(executerOpts)
workflowLoader, err := parsers.NewLoader(&executerOpts)
if err != nil {
log.Fatalf("Could not create workflow loader: %s\n", err)
}
executerOpts.WorkflowLoader = workflowLoader
configObject, err := config.ReadConfiguration()
if err != nil {
fmt.Println(err, "could not read configuration file")
}
store, err := loader.New(loader.NewConfig(defaultOpts, configObject, catalog, executerOpts))
if err != nil {
fmt.Println(err, "could not create loader")
}
store.Load()
var metaInput []*contextargs.MetaInput
for _, tar := range target {
metaInput = append(metaInput, &contextargs.MetaInput{
Input: tar,
})
}
input := &inputs.SimpleInputProvider{Inputs: metaInput}
_ = engine.Execute(store.Templates(), input)
engine.WorkPool().Wait() // Wait for the scan to finish
outputWriter.Close()
return out
}
func execute1() {
// not working as supposed to
templates := []string{
"/home/shubham/nuclei-templates/dns/mx-fingerprint.yaml",
}
targets := []string{
"https://example.com",
}
restuls := runNotWorking(templates, targets)
fmt.Println("got results", restuls)
fmt.Println("between")
restuls = runNotWorking(templates, targets)
fmt.Println("got results", restuls)
}
func execute2() {
// working as supposed to
templates := []string{
"/home/shubham/nuclei-templates/dns/mx-fingerprint.yaml",
"/home/shubham/nuclei-templates/ssl/tls-version.yaml",
}
targets := []string{
"https://example.com",
}
var out []string
runNuclei(templates, targets, &out)
fmt.Println("got results", out)
out = []string{}
runNuclei(templates, targets, &out)
fmt.Println("got results", out)
}
func main() {
// execute1()
execute2()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment