2020-12-29 12:08:46 +05:30
|
|
|
package generators
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2021-12-06 11:38:22 +01:00
|
|
|
"path/filepath"
|
2020-12-29 12:08:46 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2023-10-26 19:07:04 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2023-03-24 00:44:32 +05:30
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
2022-11-06 21:24:23 +01:00
|
|
|
folderutil "github.com/projectdiscovery/utils/folder"
|
2020-12-29 12:08:46 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// validate validates the payloads if any.
|
2021-11-05 03:01:41 +05:30
|
|
|
func (g *PayloadGenerator) validate(payloads map[string]interface{}, templatePath string) error {
|
2020-12-29 12:08:46 +05:30
|
|
|
for name, payload := range payloads {
|
2021-11-25 18:54:16 +02:00
|
|
|
switch payloadType := payload.(type) {
|
2020-12-29 12:08:46 +05:30
|
|
|
case string:
|
2025-09-15 23:48:02 +05:30
|
|
|
if strings.ContainsRune(payloadType, '\n') {
|
|
|
|
|
continue
|
2020-12-29 12:08:46 +05:30
|
|
|
}
|
|
|
|
|
|
2024-09-20 17:11:22 -04:00
|
|
|
// For historical reasons, "validate" checks to see if the payload file exist.
|
|
|
|
|
// If we're using a custom helper function, then we need to skip any validation beyond just checking the string syntax.
|
|
|
|
|
// Actually attempting to load the file will determine whether or not it exists.
|
|
|
|
|
if g.options.LoadHelperFileFunction != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-06 16:04:39 +01:00
|
|
|
// check if it's a file and try to load it
|
2022-12-20 21:59:28 +01:00
|
|
|
if fileutil.FileExists(payloadType) {
|
2020-12-29 12:08:46 +05:30
|
|
|
continue
|
|
|
|
|
}
|
2023-10-26 19:07:04 +05:30
|
|
|
// if file already exists in nuclei-templates directory, skip any further checks
|
|
|
|
|
if fileutil.FileExists(filepath.Join(config.DefaultConfig.GetTemplateDir(), payloadType)) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2020-12-29 12:08:46 +05:30
|
|
|
|
2023-10-26 19:07:04 +05:30
|
|
|
// in below code, we calculate all possible paths from root and try to resolve the payload
|
|
|
|
|
// at each level of the path. if the payload is found, we break the loop and continue
|
|
|
|
|
// ex: template-path: /home/user/nuclei-templates/cves/2020/CVE-2020-1234.yaml
|
|
|
|
|
// then we check if helper file "my-payload.txt" exists at below paths:
|
|
|
|
|
// 1. /home/user/nuclei-templates/cves/2020/my-payload.txt
|
|
|
|
|
// 2. /home/user/nuclei-templates/cves/my-payload.txt
|
|
|
|
|
// 3. /home/user/nuclei-templates/my-payload.txt
|
|
|
|
|
// 4. /home/user/my-payload.txt
|
|
|
|
|
// 5. /home/my-payload.txt
|
2020-12-29 12:08:46 +05:30
|
|
|
changed := false
|
|
|
|
|
|
2021-12-06 21:05:45 +01:00
|
|
|
dir, _ := filepath.Split(templatePath)
|
|
|
|
|
templatePathInfo, _ := folderutil.NewPathInfo(dir)
|
|
|
|
|
payloadPathsToProbe, _ := templatePathInfo.MeshWith(payloadType)
|
2021-12-06 11:38:22 +01:00
|
|
|
|
2021-12-05 15:11:14 +01:00
|
|
|
for _, payloadPath := range payloadPathsToProbe {
|
2022-12-20 21:59:28 +01:00
|
|
|
if fileutil.FileExists(payloadPath) {
|
2021-11-25 18:54:16 +02:00
|
|
|
payloads[name] = payloadPath
|
2020-12-29 12:08:46 +05:30
|
|
|
changed = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !changed {
|
2021-11-25 18:54:16 +02:00
|
|
|
return fmt.Errorf("the %s file for payload %s does not exist or does not contain enough elements", payloadType, name)
|
2020-12-29 12:08:46 +05:30
|
|
|
}
|
|
|
|
|
case interface{}:
|
2021-11-25 18:54:16 +02:00
|
|
|
loadedPayloads := types.ToStringSlice(payloadType)
|
2020-12-29 12:08:46 +05:30
|
|
|
if len(loadedPayloads) == 0 {
|
|
|
|
|
return fmt.Errorf("the payload %s does not contain enough elements", name)
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return fmt.Errorf("the payload %s has invalid type", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|