nuclei/v2/internal/runner/update.go

530 lines
17 KiB
Go
Raw Normal View History

2020-08-29 15:26:11 +02:00
package runner
import (
"archive/zip"
"bufio"
2020-08-29 15:26:11 +02:00
"bytes"
"context"
"crypto/md5"
"encoding/hex"
2020-08-29 15:26:11 +02:00
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
2021-07-01 14:36:40 +05:30
"regexp"
"runtime"
"strconv"
2020-08-29 15:26:11 +02:00
"strings"
2021-07-25 03:13:46 +05:30
"github.com/apex/log"
2020-08-29 15:26:11 +02:00
"github.com/blang/semver"
"github.com/google/go-github/github"
"github.com/olekukonko/tablewriter"
"github.com/pkg/errors"
2021-09-07 17:31:46 +03:00
2020-08-29 15:26:11 +02:00
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei-updatecheck-api/client"
2021-07-01 14:36:40 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/catalog/config"
"github.com/tj/go-update"
"github.com/tj/go-update/progress"
githubUpdateStore "github.com/tj/go-update/stores/github"
2020-08-29 15:26:11 +02:00
)
const (
userName = "projectdiscovery"
repoName = "nuclei-templates"
nucleiIgnoreFile = ".nuclei-ignore"
nucleiConfigFilename = ".templates-config.json"
2020-08-29 15:26:11 +02:00
)
2021-07-01 14:36:40 +05:30
var reVersion = regexp.MustCompile(`\d+\.\d+\.\d+`)
2020-08-29 15:26:11 +02:00
// updateTemplates checks if the default list of nuclei-templates
// exist in the user's home directory, if not the latest revision
// is downloaded from GitHub.
2020-08-29 15:26:11 +02:00
//
// If the path exists but does not contain the latest version of public templates,
// the new version is downloaded from GitHub to the templates' directory, overwriting the old content.
2020-08-29 15:26:11 +02:00
func (r *Runner) updateTemplates() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
configDir := filepath.Join(home, ".config", "nuclei")
_ = os.MkdirAll(configDir, os.ModePerm)
2020-08-29 15:26:11 +02:00
if err := r.readInternalConfigurationFile(home, configDir); err != nil {
return errors.Wrap(err, "could not read configuration file")
2020-08-29 15:26:11 +02:00
}
// If the config doesn't exist, create it now.
2021-03-22 17:10:58 +05:30
if r.templatesConfig == nil {
2021-07-01 14:36:40 +05:30
currentConfig := &config.Config{
TemplatesDirectory: filepath.Join(home, "nuclei-templates"),
2021-07-01 14:36:40 +05:30
NucleiVersion: config.Version,
2021-03-22 17:10:58 +05:30
}
if writeErr := config.WriteConfiguration(currentConfig); writeErr != nil {
2021-03-22 17:17:58 +05:30
return errors.Wrap(writeErr, "could not write template configuration")
2021-03-22 17:10:58 +05:30
}
r.templatesConfig = currentConfig
}
2021-09-13 15:45:24 +05:30
if r.options.NoUpdateTemplates && !r.options.UpdateTemplates {
return nil
}
2021-09-13 15:03:04 +05:30
client.InitNucleiVersion(config.Version)
r.fetchLatestVersionsFromGithub(configDir) // also fetch the latest versions
2020-08-29 15:26:11 +02:00
ctx := context.Background()
var noTemplatesFound bool
if _, err := os.Stat(r.templatesConfig.TemplatesDirectory); os.IsNotExist(err) {
noTemplatesFound = true
}
if r.templatesConfig.TemplateVersion == "" || (r.options.TemplatesDirectory != "" && r.templatesConfig.TemplatesDirectory != r.options.TemplatesDirectory) || noTemplatesFound {
2021-07-03 16:13:32 +05:30
gologger.Info().Msgf("nuclei-templates are not installed, installing...\n")
2020-08-29 15:26:11 +02:00
// Use the custom location if the user has given a template directory
2021-07-01 14:36:40 +05:30
r.templatesConfig = &config.Config{
TemplatesDirectory: filepath.Join(home, "nuclei-templates"),
}
if r.options.TemplatesDirectory != "" && r.options.TemplatesDirectory != filepath.Join(home, "nuclei-templates") {
r.templatesConfig.TemplatesDirectory, _ = filepath.Abs(r.options.TemplatesDirectory)
2020-08-29 15:26:11 +02:00
}
r.fetchLatestVersionsFromGithub(configDir) // also fetch the latest versions
2020-08-29 15:26:11 +02:00
2021-09-13 15:45:24 +05:30
version, err := semver.Parse(r.templatesConfig.NucleiTemplatesLatestVersion)
if err != nil {
return err
}
2020-08-29 15:26:11 +02:00
2021-09-01 17:36:07 +03:00
// Download the repository and write the revision to a HEAD file.
2021-09-13 15:45:24 +05:30
asset, getErr := r.getLatestReleaseFromGithub(r.templatesConfig.NucleiTemplatesLatestVersion)
2020-08-29 15:26:11 +02:00
if getErr != nil {
return getErr
}
gologger.Verbose().Msgf("Downloading nuclei-templates (v%s) to %s\n", version.String(), r.templatesConfig.TemplatesDirectory)
2020-08-29 15:26:11 +02:00
if _, err := r.downloadReleaseAndUnzip(ctx, version.String(), asset.GetZipballURL()); err != nil {
2020-08-29 15:26:11 +02:00
return err
}
r.templatesConfig.TemplateVersion = version.String()
2020-08-29 15:26:11 +02:00
if err := config.WriteConfiguration(r.templatesConfig); err != nil {
2020-08-29 15:26:11 +02:00
return err
}
2021-07-08 00:37:58 +05:30
gologger.Info().Msgf("Successfully downloaded nuclei-templates (v%s). GoodLuck!\n", version.String())
2020-08-29 15:26:11 +02:00
return nil
}
// Get the configuration currently on disk.
verText := r.templatesConfig.TemplateVersion
2020-08-29 15:26:11 +02:00
indices := reVersion.FindStringIndex(verText)
if indices == nil {
return fmt.Errorf("invalid release found with tag %s", err)
}
if indices[0] > 0 {
verText = verText[indices[0]:]
}
oldVersion, err := semver.Make(verText)
if err != nil {
return err
}
2021-09-13 15:45:24 +05:30
version, err := semver.Parse(r.templatesConfig.NucleiTemplatesLatestVersion)
2020-08-29 15:26:11 +02:00
if err != nil {
return err
}
if version.EQ(oldVersion) {
2021-09-13 15:47:29 +05:30
if r.options.UpdateTemplates {
gologger.Info().Msgf("No new updates found for nuclei templates")
}
return config.WriteConfiguration(r.templatesConfig)
2020-08-29 15:26:11 +02:00
}
if version.GT(oldVersion) {
2021-07-03 16:13:32 +05:30
gologger.Info().Msgf("Your current nuclei-templates v%s are outdated. Latest is v%s\n", oldVersion, version.String())
gologger.Info().Msgf("Downloading latest release...")
2020-08-29 15:26:11 +02:00
if r.options.TemplatesDirectory != "" {
r.templatesConfig.TemplatesDirectory = r.options.TemplatesDirectory
2020-08-29 15:26:11 +02:00
}
r.templatesConfig.TemplateVersion = version.String()
2020-08-29 15:26:11 +02:00
gologger.Verbose().Msgf("Downloading nuclei-templates (v%s) to %s\n", version.String(), r.templatesConfig.TemplatesDirectory)
2021-09-13 15:45:24 +05:30
asset, err := r.getLatestReleaseFromGithub(r.templatesConfig.NucleiTemplatesLatestVersion)
if err != nil {
return err
}
if _, err := r.downloadReleaseAndUnzip(ctx, version.String(), asset.GetZipballURL()); err != nil {
2020-08-29 15:26:11 +02:00
return err
}
if err := config.WriteConfiguration(r.templatesConfig); err != nil {
2020-08-29 15:26:11 +02:00
return err
}
2021-07-08 00:37:58 +05:30
gologger.Info().Msgf("Successfully updated nuclei-templates (v%s). GoodLuck!\n", version.String())
2020-08-29 15:26:11 +02:00
}
return nil
}
// readInternalConfigurationFile reads the internal configuration file for nuclei
func (r *Runner) readInternalConfigurationFile(home, configDir string) error {
templatesConfigFile := filepath.Join(configDir, nucleiConfigFilename)
if _, statErr := os.Stat(templatesConfigFile); !os.IsNotExist(statErr) {
configuration, readErr := config.ReadConfiguration()
if readErr != nil {
return readErr
}
r.templatesConfig = configuration
if configuration.TemplatesDirectory != "" && configuration.TemplatesDirectory != filepath.Join(home, "nuclei-templates") {
r.options.TemplatesDirectory = configuration.TemplatesDirectory
}
}
return nil
}
// checkNucleiIgnoreFileUpdates checks .nuclei-ignore file for updates from GitHub
func (r *Runner) checkNucleiIgnoreFileUpdates(configDir string) bool {
data, err := client.GetLatestIgnoreFile()
if err != nil {
return false
}
if len(data) > 0 {
_ = ioutil.WriteFile(filepath.Join(configDir, nucleiIgnoreFile), data, 0644)
}
if r.templatesConfig != nil {
if err := config.WriteConfiguration(r.templatesConfig); err != nil {
gologger.Warning().Msgf("Could not get ignore-file from server: %s", err)
}
}
return true
}
// getLatestReleaseFromGithub returns the latest release from GitHub
2021-09-13 15:45:24 +05:30
func (r *Runner) getLatestReleaseFromGithub(latestTag string) (*github.RepositoryRelease, error) {
2020-08-29 15:26:11 +02:00
client := github.NewClient(nil)
release, _, err := client.Repositories.GetReleaseByTag(context.Background(), userName, repoName, "v"+latestTag)
2020-08-29 15:26:11 +02:00
if err != nil {
2021-09-13 15:45:24 +05:30
return nil, err
2020-08-29 15:26:11 +02:00
}
if release == nil {
2021-09-13 15:45:24 +05:30
return nil, errors.New("no version found for the templates")
2020-08-29 15:26:11 +02:00
}
2021-09-13 15:45:24 +05:30
return release, nil
2020-08-29 15:26:11 +02:00
}
// downloadReleaseAndUnzip downloads and unzips the release in a directory
func (r *Runner) downloadReleaseAndUnzip(ctx context.Context, version, downloadURL string) (*templateUpdateResults, error) {
2020-08-29 15:26:11 +02:00
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request to %s: %s", downloadURL, err)
2020-08-29 15:26:11 +02:00
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to download a release file from %s: %s", downloadURL, err)
2020-08-29 15:26:11 +02:00
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to download a release file from %s: Not successful status %d", downloadURL, res.StatusCode)
2020-08-29 15:26:11 +02:00
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to create buffer for zip file: %s", err)
2020-08-29 15:26:11 +02:00
}
reader := bytes.NewReader(buf)
2021-09-01 17:36:07 +03:00
zipReader, err := zip.NewReader(reader, reader.Size())
2020-08-29 15:26:11 +02:00
if err != nil {
return nil, fmt.Errorf("failed to uncompress zip file: %s", err)
2020-08-29 15:26:11 +02:00
}
// Create the template folder if it doesn't exist
if err := os.MkdirAll(r.templatesConfig.TemplatesDirectory, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to create template base folder: %s", err)
}
2021-09-01 17:36:07 +03:00
results, err := r.compareAndWriteTemplates(zipReader)
if err != nil {
return nil, fmt.Errorf("failed to write templates: %s", err)
}
if r.options.Verbose {
2021-07-03 16:37:21 +05:30
r.printUpdateChangelog(results, version)
}
checksumFile := filepath.Join(r.templatesConfig.TemplatesDirectory, ".checksum")
if err := writeTemplatesChecksum(checksumFile, results.checksums); err != nil {
return nil, errors.Wrap(err, "could not write checksum")
}
// Write the additions to a cached file for new runs.
additionsFile := filepath.Join(r.templatesConfig.TemplatesDirectory, ".new-additions")
buffer := &bytes.Buffer{}
for _, addition := range results.additions {
buffer.WriteString(addition)
buffer.WriteString("\n")
}
if err := ioutil.WriteFile(additionsFile, buffer.Bytes(), os.ModePerm); err != nil {
return nil, errors.Wrap(err, "could not write new additions file")
}
return results, err
}
type templateUpdateResults struct {
additions []string
deletions []string
modifications []string
totalCount int
checksums map[string]string
}
2021-09-01 17:36:07 +03:00
// compareAndWriteTemplates compares and returns the stats of a template update operations.
func (r *Runner) compareAndWriteTemplates(zipReader *zip.Reader) (*templateUpdateResults, error) {
results := &templateUpdateResults{
checksums: make(map[string]string),
2020-08-29 15:26:11 +02:00
}
// We use file-checksums that are md5 hashes to store the list of files->hashes
// that have been downloaded previously.
// If the path isn't found in new update after being read from the previous checksum,
// it is removed. This allows us fine-grained control over the download process
// as well as solves a long problem with nuclei-template updates.
checksumFile := filepath.Join(r.templatesConfig.TemplatesDirectory, ".checksum")
2021-09-01 17:36:07 +03:00
templateChecksumsMap, _ := createTemplateChecksumsMap(checksumFile)
for _, zipTemplateFile := range zipReader.File {
directory, name := filepath.Split(zipTemplateFile.Name)
2020-08-29 15:26:11 +02:00
if name == "" {
continue
}
paths := strings.Split(directory, string(os.PathSeparator))
finalPath := filepath.Join(paths[1:]...)
2020-08-29 15:26:11 +02:00
2021-04-02 18:44:28 +05:30
if strings.HasPrefix(name, ".") || strings.HasPrefix(finalPath, ".") || strings.EqualFold(name, "README.md") {
continue
}
results.totalCount++
templateDirectory := filepath.Join(r.templatesConfig.TemplatesDirectory, finalPath)
if err := os.MkdirAll(templateDirectory, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to create template folder %s : %s", templateDirectory, err)
2020-08-29 15:26:11 +02:00
}
templatePath := filepath.Join(templateDirectory, name)
isAddition := false
2021-03-09 15:00:22 +05:30
if _, statErr := os.Stat(templatePath); os.IsNotExist(statErr) {
isAddition = true
}
2021-09-01 17:36:07 +03:00
templateFile, err := os.OpenFile(templatePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777)
2020-08-29 15:26:11 +02:00
if err != nil {
2021-09-01 17:36:07 +03:00
templateFile.Close()
return nil, fmt.Errorf("could not create uncompressed file: %s", err)
2020-08-29 15:26:11 +02:00
}
2021-09-01 17:36:07 +03:00
zipTemplateFileReader, err := zipTemplateFile.Open()
2020-08-29 15:26:11 +02:00
if err != nil {
2021-09-01 17:36:07 +03:00
templateFile.Close()
return nil, fmt.Errorf("could not open archive to extract file: %s", err)
2020-08-29 15:26:11 +02:00
}
hasher := md5.New()
2020-08-29 15:26:11 +02:00
// Save file and also read into hasher for md5
2021-09-01 17:36:07 +03:00
if _, err := io.Copy(templateFile, io.TeeReader(zipTemplateFileReader, hasher)); err != nil {
templateFile.Close()
return nil, fmt.Errorf("could not write template file: %s", err)
2020-08-29 15:26:11 +02:00
}
2021-09-01 17:36:07 +03:00
templateFile.Close()
2021-01-15 20:35:15 +05:30
2021-09-01 17:36:07 +03:00
oldChecksum, checksumOK := templateChecksumsMap[templatePath]
checksum := hex.EncodeToString(hasher.Sum(nil))
if isAddition {
results.additions = append(results.additions, filepath.Join(finalPath, name))
} else if checksumOK && oldChecksum[0] != checksum {
results.modifications = append(results.modifications, filepath.Join(finalPath, name))
}
results.checksums[templatePath] = checksum
}
2021-09-01 17:36:07 +03:00
// If we don't find the previous file in the newly downloaded list,
// and it hasn't been changed on the disk, delete it.
for templatePath, templateChecksums := range templateChecksumsMap {
_, ok := results.checksums[templatePath]
if !ok && templateChecksums[0] == templateChecksums[1] {
os.Remove(templatePath)
results.deletions = append(results.deletions, strings.TrimPrefix(strings.TrimPrefix(templatePath, r.templatesConfig.TemplatesDirectory), string(os.PathSeparator)))
}
}
return results, nil
}
2021-09-01 17:36:07 +03:00
// createTemplateChecksumsMap reads the previous checksum file from the disk.
// Creates a map of template paths and their previous and currently calculated checksums as values.
func createTemplateChecksumsMap(checksumsFilePath string) (map[string][2]string, error) {
checksumFile, err := os.Open(checksumsFilePath)
if err != nil {
return nil, err
}
2021-09-01 17:36:07 +03:00
defer checksumFile.Close()
scanner := bufio.NewScanner(checksumFile)
2021-09-01 17:36:07 +03:00
templatePathChecksumsMap := make(map[string][2]string)
for scanner.Scan() {
text := scanner.Text()
if text == "" {
continue
}
2021-09-01 17:36:07 +03:00
parts := strings.Split(text, ",")
if len(parts) < 2 {
continue
}
2021-09-01 17:36:07 +03:00
templatePath := parts[0]
expectedTemplateChecksum := parts[1]
2021-09-01 17:36:07 +03:00
templateFile, err := os.Open(templatePath)
if err != nil {
return nil, err
}
hasher := md5.New()
2021-09-01 17:36:07 +03:00
if _, err := io.Copy(hasher, templateFile); err != nil {
return nil, err
}
2021-09-01 17:36:07 +03:00
templateFile.Close()
2021-09-01 17:36:07 +03:00
values := [2]string{expectedTemplateChecksum}
values[1] = hex.EncodeToString(hasher.Sum(nil))
2021-09-01 17:36:07 +03:00
templatePathChecksumsMap[templatePath] = values
}
2021-09-01 17:36:07 +03:00
return templatePathChecksumsMap, nil
}
// writeTemplatesChecksum writes the nuclei-templates checksum data to disk.
func writeTemplatesChecksum(file string, checksum map[string]string) error {
f, err := os.Create(file)
if err != nil {
return err
}
defer f.Close()
builder := &strings.Builder{}
for k, v := range checksum {
builder.WriteString(k)
builder.WriteString(",")
builder.WriteString(v)
builder.WriteString("\n")
if _, checksumErr := f.WriteString(builder.String()); checksumErr != nil {
return err
}
builder.Reset()
2020-08-29 15:26:11 +02:00
}
return nil
}
func (r *Runner) printUpdateChangelog(results *templateUpdateResults, version string) {
2021-07-03 16:13:32 +05:30
if len(results.additions) > 0 && r.options.Verbose {
gologger.Print().Msgf("\nNewly added templates: \n\n")
for _, addition := range results.additions {
gologger.Print().Msgf("%s", addition)
}
}
gologger.Print().Msgf("\nNuclei Templates v%s Changelog\n", version)
data := [][]string{
{strconv.Itoa(results.totalCount), strconv.Itoa(len(results.additions)), strconv.Itoa(len(results.deletions))},
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Total", "Added", "Removed"})
for _, v := range data {
table.Append(v)
}
table.Render()
}
2021-07-01 14:36:40 +05:30
2021-09-01 17:36:07 +03:00
// fetchLatestVersionsFromGithub fetches the latest versions of nuclei repos from GitHub
//
2021-09-16 17:27:06 +05:30
// This fetches latest nuclei/templates/ignore from https://version-check.nuclei.sh/versions
// If you want to disable this automatic update check, use -nut flag.
func (r *Runner) fetchLatestVersionsFromGithub(configDir string) {
versions, err := client.GetLatestNucleiTemplatesVersion()
2021-07-01 14:36:40 +05:30
if err != nil {
gologger.Warning().Msgf("Could not fetch latest releases: %s", err)
2021-09-13 15:03:04 +05:30
return
2021-07-01 14:36:40 +05:30
}
if r.templatesConfig != nil {
r.templatesConfig.NucleiLatestVersion = versions.Nuclei
r.templatesConfig.NucleiTemplatesLatestVersion = versions.Templates
2021-07-01 14:36:40 +05:30
// If the fetch has resulted in new version of ignore file, update.
if r.templatesConfig.NucleiIgnoreHash == "" || r.templatesConfig.NucleiIgnoreHash != versions.IgnoreHash {
r.templatesConfig.NucleiIgnoreHash = versions.IgnoreHash
r.checkNucleiIgnoreFileUpdates(configDir)
}
2021-07-01 14:36:40 +05:30
}
}
2021-09-01 17:36:07 +03:00
// updateNucleiVersionToLatest implements nuclei auto-update using GitHub Releases.
2021-07-25 03:13:46 +05:30
func updateNucleiVersionToLatest(verbose bool) error {
if verbose {
log.SetLevel(log.DebugLevel)
}
var command string
switch runtime.GOOS {
case "windows":
command = "nuclei.exe"
default:
command = "nuclei"
}
m := &update.Manager{
Command: command,
Store: &githubUpdateStore.Store{
Owner: "projectdiscovery",
Repo: "nuclei",
Version: config.Version,
},
}
releases, err := m.LatestReleases()
if err != nil {
return errors.Wrap(err, "could not fetch latest release")
}
if len(releases) == 0 {
2021-07-25 16:06:12 +05:30
gologger.Info().Msgf("No new updates found for nuclei engine!")
2021-07-25 04:25:31 +05:30
return nil
}
latest := releases[0]
var currentOS string
switch runtime.GOOS {
case "darwin":
currentOS = "macOS"
default:
currentOS = runtime.GOOS
}
final := latest.FindZip(currentOS, runtime.GOARCH)
if final == nil {
return fmt.Errorf("no compatible binary found for %s/%s", currentOS, runtime.GOARCH)
}
tarball, err := final.DownloadProxy(progress.Reader)
if err != nil {
return errors.Wrap(err, "could not download latest release")
}
if err := m.Install(tarball); err != nil {
return errors.Wrap(err, "could not install latest release")
}
2021-07-25 04:31:22 +05:30
gologger.Info().Msgf("Successfully updated to Nuclei %s\n", latest.Version)
return nil
}