2020-12-29 12:08:46 +05:30
|
|
|
package generators
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2021-12-06 11:38:22 +01:00
|
|
|
"path/filepath"
|
2020-12-29 12:08:46 +05:30
|
|
|
"strings"
|
|
|
|
|
|
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:
|
|
|
|
|
// check if it's a multiline string list
|
2021-11-25 18:54:16 +02:00
|
|
|
if len(strings.Split(payloadType, "\n")) != 1 {
|
2020-12-29 12:08:46 +05:30
|
|
|
return errors.New("invalid number of lines in payload")
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|