Skip to content

Commit

Permalink
Added nucleiignore feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Ice3man543 committed Aug 23, 2020
1 parent 3eb37df commit 320f312
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
42 changes: 42 additions & 0 deletions v2/internal/runner/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"archive/zip"
"bufio"
"bytes"
"context"
"errors"
Expand All @@ -27,6 +28,9 @@ type nucleiConfig struct {
TemplatesDirectory string `json:"templates-directory,omitempty"`
CurrentVersion string `json:"current-version,omitempty"`
LastChecked time.Time `json:"last-checked,omitempty"`

// ignorePaths ignores all the paths listed unless specified manually
ignorePaths []string `json:"ignore-paths,omitempty"`
}

// nucleiConfigFilename is the filename of nuclei configuration file.
Expand Down Expand Up @@ -76,6 +80,44 @@ func (r *Runner) writeConfiguration(config *nucleiConfig) error {
return nil
}

const nucleiIgnoreFile = ".nuclei-ignore"

// readNucleiIgnoreFile reads the nuclei ignore file marking it in map
func (r *Runner) readNucleiIgnoreFile() {
file, err := os.Open(path.Join(r.templatesConfig.TemplatesDirectory, nucleiIgnoreFile))
if err != nil {
return
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if text == "" {
continue
}
r.templatesConfig.ignorePaths = append(r.templatesConfig.ignorePaths, text)
}
}

// checkIfInNucleiIgnore checks if a path falls under nuclei-ignore rules.
func (r *Runner) checkIfInNucleiIgnore(item string) bool {
for _, paths := range r.templatesConfig.ignorePaths {
// If we have a path to ignore, check if it's in the item.
if paths[len(paths)] == '/' {
if strings.Contains(item, paths) {
return true
}
continue
}
// Check for file based extension in ignores
if strings.HasSuffix(item, paths) {
return true
}
}
return false
}

// updateTemplates checks if the default list of nuclei-templates
// exist in the users home directory, if not the latest revision
// is downloaded from github.
Expand Down
20 changes: 13 additions & 7 deletions v2/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"github.com/logrusorgru/aurora"
"io"
"io/ioutil"
"net/http/cookiejar"
Expand All @@ -15,6 +14,8 @@ import (
"strings"
"sync"

"github.com/logrusorgru/aurora"

tengo "github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/stdlib"
"github.com/karrick/godirwalk"
Expand Down Expand Up @@ -63,6 +64,10 @@ func New(options *Options) (*Runner, error) {
if (len(options.Templates) == 0 || (options.Targets == "" && !options.Stdin && options.Target == "")) && options.UpdateTemplates {
os.Exit(0)
}
// Read nucleiignore file if given a templateconfig
if runner.templatesConfig != nil {
runner.readNucleiIgnoreFile()
}

// output coloring
useColor := !options.NoColor
Expand Down Expand Up @@ -278,10 +283,6 @@ func (r *Runner) getTemplatesFor(definitions []string) []string {
continue
}

for _, i := range matches {
processed[i] = true
}

// couldn't find templates in directory
if len(matches) == 0 {
gologger.Labelf("Error, no templates were found with '%s'.\n", absPath)
Expand All @@ -290,7 +291,12 @@ func (r *Runner) getTemplatesFor(definitions []string) []string {
gologger.Labelf("Identified %d templates\n", len(matches))
}

allTemplates = append(allTemplates, matches...)
for _, match := range matches {
if !r.checkIfInNucleiIgnore(match) {
processed[match] = true
allTemplates = append(allTemplates, match)
}
}
} else {
// determine file/directory
isFile, err := isFilePath(absPath)
Expand All @@ -316,7 +322,7 @@ func (r *Runner) getTemplatesFor(definitions []string) []string {
err = godirwalk.Walk(absPath, &godirwalk.Options{
Callback: func(path string, d *godirwalk.Dirent) error {
if !d.IsDir() && strings.HasSuffix(path, ".yaml") {
if isNewPath(path, processed) {
if !r.checkIfInNucleiIgnore(path) && isNewPath(path, processed) {
matches = append(matches, path)
processed[path] = true
}
Expand Down

0 comments on commit 320f312

Please sign in to comment.