2022-03-14 12:32:05 +05:30
package templates
import (
"fmt"
"strings"
"github.com/logrusorgru/aurora"
2023-05-04 01:43:41 +05:30
"github.com/projectdiscovery/gologger"
2023-10-17 17:44:13 +05:30
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
"github.com/projectdiscovery/nuclei/v3/pkg/model/types/severity"
2023-06-09 02:28:40 +08:00
mapsutil "github.com/projectdiscovery/utils/maps"
2022-03-14 12:32:05 +05:30
)
var (
2023-05-04 01:43:41 +05:30
Colorizer aurora . Aurora
SeverityColorizer func ( severity . Severity ) string
2023-06-09 02:28:40 +08:00
deprecatedProtocolNameTemplates = mapsutil . SyncLockMap [ string , bool ] { Map : mapsutil . Map [ string , bool ] { } } //templates that still use deprecated protocol names
2022-03-14 12:32:05 +05:30
)
// TemplateLogMessage returns a beautified log string for a template
func TemplateLogMessage ( id , name string , authors [ ] string , templateSeverity severity . Severity ) string {
if Colorizer == nil || SeverityColorizer == nil {
return ""
}
// Display the message for the template
return fmt . Sprintf ( "[%s] %s (%s) [%s]" ,
Colorizer . BrightBlue ( id ) . String ( ) ,
Colorizer . Bold ( name ) . String ( ) ,
Colorizer . BrightYellow ( appendAtSignToAuthors ( authors ) ) . String ( ) ,
SeverityColorizer ( templateSeverity ) )
}
// appendAtSignToAuthors appends @ before each author and returns the final string
func appendAtSignToAuthors ( authors [ ] string ) string {
if len ( authors ) == 0 {
return "@none"
}
values := make ( [ ] string , 0 , len ( authors ) )
for _ , k := range authors {
if ! strings . HasPrefix ( k , "@" ) {
values = append ( values , fmt . Sprintf ( "@%s" , k ) )
} else {
values = append ( values , k )
}
}
return strings . Join ( values , "," )
}
2023-05-04 01:43:41 +05:30
// PrintDeprecatedProtocolNameMsgIfApplicable prints a message if deprecated protocol names are used
// Unless mode is silent we print a message for deprecated protocol name
func PrintDeprecatedProtocolNameMsgIfApplicable ( isSilent bool , verbose bool ) {
2023-06-09 02:28:40 +08:00
count := 0
_ = deprecatedProtocolNameTemplates . Iterate ( func ( k string , v bool ) error {
count ++
return nil
} )
if count > 0 && ! isSilent {
gologger . Print ( ) . Msgf ( "[%v] Found %v templates loaded with deprecated protocol syntax, update before v3 for continued support.\n" , aurora . Yellow ( "WRN" ) . String ( ) , count )
2023-05-04 01:43:41 +05:30
}
2023-10-16 14:34:52 +05:30
if config . DefaultConfig . LogAllEvents {
2023-06-09 02:28:40 +08:00
_ = deprecatedProtocolNameTemplates . Iterate ( func ( k string , v bool ) error {
gologger . Print ( ) . Msgf ( " - %s\n" , k )
return nil
} )
2023-05-04 01:43:41 +05:30
}
2023-06-09 02:28:40 +08:00
deprecatedProtocolNameTemplates . Lock ( )
deprecatedProtocolNameTemplates . Map = make ( map [ string ] bool )
deprecatedProtocolNameTemplates . Unlock ( )
2023-05-04 01:43:41 +05:30
}