2022-09-19 01:13:59 +05:30
|
|
|
package runner
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/internal/runner/nucleicloud"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/loader"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/core"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
|
|
|
|
"go.uber.org/atomic"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-22 04:06:52 +05:30
|
|
|
const DDMMYYYYhhmmss = "2006-01-02 15:04:05"
|
|
|
|
|
|
2022-09-19 01:13:59 +05:30
|
|
|
// runStandardEnumeration runs standard enumeration
|
|
|
|
|
func (r *Runner) runStandardEnumeration(executerOpts protocols.ExecuterOptions, store *loader.Store, engine *core.Engine) (*atomic.Bool, error) {
|
|
|
|
|
if r.options.AutomaticScan {
|
|
|
|
|
return r.executeSmartWorkflowInput(executerOpts, store, engine)
|
|
|
|
|
}
|
|
|
|
|
return r.executeTemplatesInput(store, engine)
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-22 04:06:52 +05:30
|
|
|
// Get all the scan lists for a user/apikey.
|
|
|
|
|
func (r *Runner) getScanList() error {
|
|
|
|
|
items, err := r.cloudClient.GetScans()
|
|
|
|
|
loc, _ := time.LoadLocation("Local")
|
|
|
|
|
|
|
|
|
|
for _, v := range items {
|
|
|
|
|
status := "FINISHED"
|
|
|
|
|
t := v.FinishedAt
|
|
|
|
|
duration := t.Sub(v.CreatedAt)
|
|
|
|
|
if !v.Finished {
|
|
|
|
|
status = "RUNNING"
|
|
|
|
|
t = time.Now().UTC()
|
|
|
|
|
duration = t.Sub(v.CreatedAt)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val := v.CreatedAt.In(loc).Format(DDMMYYYYhhmmss)
|
|
|
|
|
|
|
|
|
|
gologger.Silent().Msgf("%s [%s] [STATUS: %s] [MATCHED: %d] [TARGETS: %d] [TEMPLATES: %d] [DURATION: %s]\n", v.Id, val, status, v.Matches, v.Targets, v.Templates, duration)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) deleteScan(id string) error {
|
|
|
|
|
deleted, err := r.cloudClient.DeleteScan(id)
|
|
|
|
|
if !deleted.OK {
|
|
|
|
|
gologger.Info().Msgf("Error in deleting the scan %s.", id)
|
|
|
|
|
} else {
|
|
|
|
|
gologger.Info().Msgf("Scan deleted %s.", id)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *Runner) getResults(id string) error {
|
|
|
|
|
err := r.cloudClient.GetResults(id, func(re *output.ResultEvent) {
|
|
|
|
|
if outputErr := r.output.Write(re); outputErr != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not write output: %s", outputErr)
|
|
|
|
|
}
|
|
|
|
|
}, false)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-19 01:13:59 +05:30
|
|
|
// runCloudEnumeration runs cloud based enumeration
|
2022-10-22 04:06:52 +05:30
|
|
|
func (r *Runner) runCloudEnumeration(store *loader.Store, nostore bool) (*atomic.Bool, error) {
|
2022-09-19 01:13:59 +05:30
|
|
|
now := time.Now()
|
|
|
|
|
defer func() {
|
|
|
|
|
gologger.Info().Msgf("Scan execution took %s", time.Since(now))
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
results := &atomic.Bool{}
|
|
|
|
|
|
|
|
|
|
targets := make([]string, 0, r.hmapInputProvider.Count())
|
2022-10-20 17:23:00 +05:30
|
|
|
r.hmapInputProvider.Scan(func(value string) bool {
|
2022-09-19 01:13:59 +05:30
|
|
|
targets = append(targets, value)
|
2022-10-20 17:23:00 +05:30
|
|
|
return true
|
2022-09-19 01:13:59 +05:30
|
|
|
})
|
|
|
|
|
templates := make([]string, 0, len(store.Templates()))
|
|
|
|
|
for _, template := range store.Templates() {
|
|
|
|
|
templates = append(templates, getTemplateRelativePath(template.Path))
|
|
|
|
|
}
|
2022-10-22 04:06:52 +05:30
|
|
|
taskID, err := r.cloudClient.AddScan(&nucleicloud.AddScanRequest{
|
2022-09-19 01:13:59 +05:30
|
|
|
RawTargets: targets,
|
|
|
|
|
PublicTemplates: templates,
|
2022-10-22 04:06:52 +05:30
|
|
|
IsTemporary: nostore,
|
2022-09-19 01:13:59 +05:30
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return results, err
|
|
|
|
|
}
|
|
|
|
|
gologger.Info().Msgf("Created task with ID: %s", taskID)
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
|
2022-10-22 04:06:52 +05:30
|
|
|
err = r.cloudClient.GetResults(taskID, func(re *output.ResultEvent) {
|
2022-09-19 08:38:52 +02:00
|
|
|
results.CompareAndSwap(false, true)
|
2022-09-19 01:13:59 +05:30
|
|
|
|
|
|
|
|
if outputErr := r.output.Write(re); outputErr != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not write output: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if r.issuesClient != nil {
|
|
|
|
|
if err := r.issuesClient.CreateIssue(re); err != nil {
|
|
|
|
|
gologger.Warning().Msgf("Could not create issue on tracker: %s", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-22 04:06:52 +05:30
|
|
|
}, true)
|
2022-09-19 01:13:59 +05:30
|
|
|
return results, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getTemplateRelativePath(templatePath string) string {
|
|
|
|
|
splitted := strings.SplitN(templatePath, "nuclei-templates", 2)
|
|
|
|
|
if len(splitted) < 2 {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
return strings.TrimPrefix(splitted[1], "/")
|
|
|
|
|
}
|