mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 19:35:27 +00:00
* Add s3 bucket template provider - Refactor the custom github template code - add interface for template provider * Validate if aws creds are passed if bucket flag - refactor s3 provider struct to take client - add function which returns the aws s3 client - update error messages * Add aws s3 bucket flags documentation in README.md - Rename the github_test.go to customTemplate_test.go * go mod update * Move template provider code to pkg/external/customtemplates dir * Added initial data_source sync to cloud * Misc * Add pagination to scan output and scan list (#2858) * Add pagination to scan output and scan list * Use time based parameters instead of page numbers * Fix linting errors * Do not check limits at client, check at server * Remove unused constant * Misc update * Removed unnecessary flags * Misc * Misc * Misc endpoint additions * Added more routes * Typo fix * Misc fixes * Misc * Misc fixes to cloud target logic + use int for IDs * Misc * Misc fixes * Misc * Misc fixes * readme update * Add JSON output support for list-scan option (#2876) * Add JSON output support for list-scan option * Fix typo in cloud JSON output description * Following changes - Update status(finished, running) to be lower-case by default - Convert status to upper-case in DisplayScanList() * Update status to be lower-case by default * Remove additional json flag, instead use existing * Merge conflict * Accomodate comment changes and restructure code Co-authored-by: Jaideep K <jaideep@one2n.in> * Use integer IDs for scan tasks * Added get-templates-targets endpoint + JSON + validation * Added target count list * misc option / description updates * Added changes as per code review * duplicate options + typo updates * Added tablewriter for tabular data writing by default * Fixed list scan endpoint * Review changes * workflow fix * Added cloud tags etc based filtering (#3070) * Added omitempty for filtering request * go mod tidy * misc format update Co-authored-by: shubhamrasal <shubhamdharmarasal@gmail.com> Co-authored-by: Ice3man <nizamulrana@gmail.com> Co-authored-by: Jaideep Khandelwal <jdk2588@gmail.com> Co-authored-by: Siddharth Shashikar <60960197+shashikarsiddharth@users.noreply.github.com> Co-authored-by: Jaideep K <jaideep@one2n.in>
320 lines
9.1 KiB
Go
320 lines
9.1 KiB
Go
package runner
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/olekukonko/tablewriter"
|
|
"github.com/pkg/errors"
|
|
"github.com/projectdiscovery/gologger"
|
|
"github.com/projectdiscovery/nuclei/v2/internal/runner/nucleicloud"
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
)
|
|
|
|
// Get all the scan lists for a user/apikey.
|
|
func (r *Runner) getScanList(limit int) error {
|
|
lastTime := "2099-01-02 15:04:05 +0000 UTC"
|
|
header := []string{"ID", "Timestamp", "Targets", "Templates", "Matched", "Duration", "Status"}
|
|
|
|
var (
|
|
values [][]string
|
|
count int
|
|
)
|
|
for {
|
|
items, err := r.cloudClient.GetScans(limit, lastTime)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(items) == 0 {
|
|
break
|
|
}
|
|
for _, v := range items {
|
|
count++
|
|
lastTime = v.CreatedAt.String()
|
|
res := nucleicloud.PrepareScanListOutput(v)
|
|
if r.options.JSON {
|
|
_ = jsoniter.NewEncoder(os.Stdout).Encode(res)
|
|
} else if !r.options.NoTables {
|
|
values = append(values, []string{strconv.FormatInt(res.ScanID, 10), res.Timestamp, strconv.Itoa(res.Target), strconv.Itoa(res.Template), strconv.Itoa(res.ScanResult), res.ScanTime, res.ScanStatus})
|
|
} else {
|
|
gologger.Silent().Msgf("%d. [%s] [TARGETS: %d] [TEMPLATES: %d] [MATCHED: %d] [DURATION: %s] [STATUS: %s]\n", res.ScanID, res.Timestamp, res.Target, res.Template, res.ScanResult, res.ScanTime, strings.ToUpper(res.ScanStatus))
|
|
|
|
}
|
|
}
|
|
}
|
|
if count == 0 {
|
|
return errors.New("no scan found")
|
|
}
|
|
if !r.options.NoTables {
|
|
r.prettyPrintTable(header, values)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) listDatasources() error {
|
|
datasources, err := r.cloudClient.ListDatasources()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(datasources) == 0 {
|
|
return errors.New("no cloud datasource found")
|
|
}
|
|
|
|
header := []string{"ID", "UpdatedAt", "Type", "Repo", "Path"}
|
|
var values [][]string
|
|
for _, source := range datasources {
|
|
if r.options.JSON {
|
|
_ = jsoniter.NewEncoder(os.Stdout).Encode(source)
|
|
} else if !r.options.NoTables {
|
|
values = append(values, []string{strconv.FormatInt(source.ID, 10), source.Updatedat.Format(nucleicloud.DDMMYYYYhhmmss), source.Type, source.Repo, source.Path})
|
|
} else {
|
|
gologger.Silent().Msgf("%d. [%s] [%s] [%s] %s", source.ID, source.Updatedat.Format(nucleicloud.DDMMYYYYhhmmss), source.Type, source.Repo, source.Path)
|
|
}
|
|
}
|
|
if !r.options.NoTables {
|
|
r.prettyPrintTable(header, values)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) listTargets() error {
|
|
items, err := r.cloudClient.ListTargets("")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(items) == 0 {
|
|
return errors.New("no target found")
|
|
}
|
|
|
|
header := []string{"ID", "Reference", "Count"}
|
|
var values [][]string
|
|
for _, source := range items {
|
|
if r.options.JSON {
|
|
_ = jsoniter.NewEncoder(os.Stdout).Encode(source)
|
|
} else if !r.options.NoTables {
|
|
values = append(values, []string{strconv.FormatInt(source.ID, 10), source.Reference, strconv.FormatInt(source.Count, 10)})
|
|
} else {
|
|
gologger.Silent().Msgf("%d. %s (%d)", source.ID, source.Reference, source.Count)
|
|
}
|
|
}
|
|
if !r.options.NoTables {
|
|
r.prettyPrintTable(header, values)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) listTemplates() error {
|
|
items, err := r.cloudClient.ListTemplates("")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if len(items) == 0 {
|
|
return errors.New("no template found")
|
|
}
|
|
|
|
header := []string{"ID", "Reference"}
|
|
var values [][]string
|
|
for _, source := range items {
|
|
if r.options.JSON {
|
|
_ = jsoniter.NewEncoder(os.Stdout).Encode(source)
|
|
} else if !r.options.NoTables {
|
|
values = append(values, []string{strconv.FormatInt(source.ID, 10), source.Reference})
|
|
} else {
|
|
gologger.Silent().Msgf("%d. %s", source.ID, source.Reference)
|
|
}
|
|
}
|
|
if !r.options.NoTables {
|
|
r.prettyPrintTable(header, values)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) prettyPrintTable(header []string, values [][]string) {
|
|
writer := tablewriter.NewWriter(os.Stdout)
|
|
writer.SetHeader(header)
|
|
writer.AppendBulk(values)
|
|
writer.Render()
|
|
}
|
|
|
|
func (r *Runner) deleteScan(id string) error {
|
|
ID, _ := strconv.ParseInt(id, 10, 64)
|
|
deleted, err := r.cloudClient.DeleteScan(ID)
|
|
if !deleted.OK {
|
|
gologger.Error().Msgf("Error in deleting the scan %s.", id)
|
|
} else {
|
|
gologger.Info().Msgf("Scan deleted %s.", id)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *Runner) getResults(id string, limit int) error {
|
|
ID, _ := strconv.ParseInt(id, 10, 64)
|
|
err := r.cloudClient.GetResults(ID, false, limit, func(re *output.ResultEvent) {
|
|
if outputErr := r.output.Write(re); outputErr != nil {
|
|
gologger.Warning().Msgf("Could not write output: %s", outputErr)
|
|
}
|
|
})
|
|
return err
|
|
}
|
|
|
|
func (r *Runner) getTarget(id string) error {
|
|
ID, _ := strconv.ParseInt(id, 10, 64)
|
|
reader, err := r.cloudClient.GetTarget(ID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get target")
|
|
}
|
|
defer reader.Close()
|
|
|
|
_, _ = io.Copy(os.Stdout, reader)
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) getTemplate(id string) error {
|
|
ID, _ := strconv.ParseInt(id, 10, 64)
|
|
reader, err := r.cloudClient.GetTemplate(ID)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not get template")
|
|
}
|
|
defer reader.Close()
|
|
|
|
_, _ = io.Copy(os.Stdout, reader)
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) removeDatasource(datasource string) error {
|
|
var source string
|
|
ID, parseErr := strconv.ParseInt(datasource, 10, 64)
|
|
if parseErr != nil {
|
|
source = datasource
|
|
}
|
|
|
|
err := r.cloudClient.RemoveDatasource(ID, source)
|
|
if err == nil {
|
|
gologger.Info().Msgf("Datasource deleted %s", datasource)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *Runner) addTemplate(location string) error {
|
|
walkErr := filepath.WalkDir(location, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".yaml") {
|
|
return nil
|
|
}
|
|
base := filepath.Base(path)
|
|
reference, templateErr := r.cloudClient.AddTemplate(base, path)
|
|
if templateErr != nil {
|
|
gologger.Error().Msgf("Could not upload %s: %s", path, templateErr)
|
|
} else if reference != "" {
|
|
gologger.Info().Msgf("Uploaded template %s: %s", base, reference)
|
|
}
|
|
return nil
|
|
})
|
|
return walkErr
|
|
}
|
|
|
|
func (r *Runner) addTarget(location string) error {
|
|
walkErr := filepath.WalkDir(location, func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() || !strings.EqualFold(filepath.Ext(path), ".txt") {
|
|
return nil
|
|
}
|
|
base := filepath.Base(location)
|
|
reference, targetErr := r.cloudClient.AddTarget(base, location)
|
|
if targetErr != nil {
|
|
gologger.Error().Msgf("Could not upload %s: %s", location, targetErr)
|
|
} else if reference != "" {
|
|
gologger.Info().Msgf("Uploaded target %s: %s", base, reference)
|
|
}
|
|
return nil
|
|
})
|
|
return walkErr
|
|
}
|
|
|
|
func (r *Runner) removeTarget(item string) error {
|
|
response, err := r.cloudClient.ListTargets(item)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not list targets")
|
|
}
|
|
for _, item := range response {
|
|
if err := r.cloudClient.RemoveTarget(item.ID); err != nil {
|
|
gologger.Error().Msgf("Error in deleting target %s: %s", item.Reference, err)
|
|
} else {
|
|
gologger.Info().Msgf("Target deleted %s", item.Reference)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) removeTemplate(item string) error {
|
|
response, err := r.cloudClient.ListTemplates(item)
|
|
if err != nil {
|
|
return errors.Wrap(err, "could not list templates")
|
|
}
|
|
for _, item := range response {
|
|
if err := r.cloudClient.RemoveTemplate(item.ID); err != nil {
|
|
gologger.Error().Msgf("Error in deleting template %s: %s", item.Reference, err)
|
|
} else {
|
|
gologger.Info().Msgf("Template deleted %s", item.Reference)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// initializeCloudDataSources initializes cloud data sources
|
|
func (r *Runner) addCloudDataSource(source string) error {
|
|
switch source {
|
|
case "s3":
|
|
token := strings.Join([]string{r.options.AwsAccessKey, r.options.AwsSecretKey, r.options.AwsRegion}, ":")
|
|
if _, err := r.processDataSourceItem(r.options.AwsBucketName, token, "s3"); err != nil {
|
|
return err
|
|
}
|
|
case "github":
|
|
for _, repo := range r.options.GithubTemplateRepo {
|
|
if _, err := r.processDataSourceItem(repo, r.options.GithubToken, "github"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *Runner) processDataSourceItem(repo, token, Type string) (int64, error) {
|
|
ID, err := r.cloudClient.StatusDataSource(nucleicloud.StatusDataSourceRequest{Repo: repo, Token: token})
|
|
if err != nil {
|
|
if !strings.Contains(err.Error(), "no rows in result set") {
|
|
return 0, errors.Wrap(err, "could not get data source status")
|
|
}
|
|
|
|
gologger.Info().Msgf("Adding new data source + syncing: %s\n", repo)
|
|
resp, err := r.cloudClient.AddDataSource(nucleicloud.AddDataSourceRequest{Type: Type, Repo: repo, Token: token})
|
|
if err != nil {
|
|
return 0, errors.Wrap(err, "could not add data source")
|
|
}
|
|
ID = resp.ID
|
|
if err = r.cloudClient.SyncDataSource(resp.ID); err != nil {
|
|
return 0, errors.Wrap(err, "could not sync data source")
|
|
}
|
|
if resp.Secret != "" {
|
|
gologger.Info().Msgf("Webhook URL for added source: %s/datasources/%s/webhook", r.options.CloudURL, resp.Hash)
|
|
gologger.Info().Msgf("Secret for webhook: %s", resp.Secret)
|
|
}
|
|
}
|
|
if r.options.UpdateTemplates {
|
|
gologger.Info().Msgf("Syncing data source: %s (%d)\n", repo, ID)
|
|
if err = r.cloudClient.SyncDataSource(ID); err != nil {
|
|
return 0, errors.Wrap(err, "could not sync data source")
|
|
}
|
|
}
|
|
return ID, nil
|
|
}
|