github.com/projectdiscovery/nuclei/v2@v2.9.15/pkg/templates/log.go (about)

     1  package templates
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/logrusorgru/aurora"
     8  	"github.com/projectdiscovery/gologger"
     9  	"github.com/projectdiscovery/nuclei/v2/pkg/model/types/severity"
    10  	mapsutil "github.com/projectdiscovery/utils/maps"
    11  )
    12  
    13  var (
    14  	Colorizer                       aurora.Aurora
    15  	SeverityColorizer               func(severity.Severity) string
    16  	deprecatedProtocolNameTemplates = mapsutil.SyncLockMap[string, bool]{Map: mapsutil.Map[string, bool]{}} //templates that still use deprecated protocol names
    17  )
    18  
    19  // TemplateLogMessage returns a beautified log string for a template
    20  func TemplateLogMessage(id, name string, authors []string, templateSeverity severity.Severity) string {
    21  	if Colorizer == nil || SeverityColorizer == nil {
    22  		return ""
    23  	}
    24  	// Display the message for the template
    25  	return fmt.Sprintf("[%s] %s (%s) [%s]",
    26  		Colorizer.BrightBlue(id).String(),
    27  		Colorizer.Bold(name).String(),
    28  		Colorizer.BrightYellow(appendAtSignToAuthors(authors)).String(),
    29  		SeverityColorizer(templateSeverity))
    30  }
    31  
    32  // appendAtSignToAuthors appends @ before each author and returns the final string
    33  func appendAtSignToAuthors(authors []string) string {
    34  	if len(authors) == 0 {
    35  		return "@none"
    36  	}
    37  
    38  	values := make([]string, 0, len(authors))
    39  	for _, k := range authors {
    40  		if !strings.HasPrefix(k, "@") {
    41  			values = append(values, fmt.Sprintf("@%s", k))
    42  		} else {
    43  			values = append(values, k)
    44  		}
    45  	}
    46  	return strings.Join(values, ",")
    47  }
    48  
    49  // PrintDeprecatedProtocolNameMsgIfApplicable prints a message if deprecated protocol names are used
    50  // Unless mode is silent we print a message for deprecated protocol name
    51  func PrintDeprecatedProtocolNameMsgIfApplicable(isSilent bool, verbose bool) {
    52  	count := 0
    53  	_ = deprecatedProtocolNameTemplates.Iterate(func(k string, v bool) error {
    54  		count++
    55  		return nil
    56  	})
    57  	if count > 0 && !isSilent {
    58  		gologger.Print().Msgf("[%v] Found %v templates loaded with deprecated protocol syntax, update before v3 for continued support.\n", aurora.Yellow("WRN").String(), count)
    59  	}
    60  	if verbose {
    61  		_ = deprecatedProtocolNameTemplates.Iterate(func(k string, v bool) error {
    62  			gologger.Print().Msgf("  - %s\n", k)
    63  			return nil
    64  		})
    65  	}
    66  	deprecatedProtocolNameTemplates.Lock()
    67  	deprecatedProtocolNameTemplates.Map = make(map[string]bool)
    68  	deprecatedProtocolNameTemplates.Unlock()
    69  }