2022-03-30 01:46:21 +05:30
|
|
|
package variables
|
|
|
|
|
|
|
|
|
|
import (
|
2022-07-11 22:18:13 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2024-03-25 15:52:20 -04:00
|
|
|
"github.com/invopop/jsonschema"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/expressions"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/generators"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/interactsh"
|
|
|
|
|
protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils"
|
2025-02-11 04:31:37 +07:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
|
2023-05-02 23:49:56 +05:30
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
2022-03-30 01:46:21 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Variable is a key-value pair of strings that can be used
|
|
|
|
|
// throughout template.
|
|
|
|
|
type Variable struct {
|
2023-04-12 01:50:58 +05:30
|
|
|
LazyEval bool `yaml:"-" json:"-"` // LazyEval is used to evaluate variables lazily if it using any expression or global variables
|
2022-04-25 14:08:23 +05:30
|
|
|
utils.InsertionOrderedStringMap `yaml:"-" json:"-"`
|
2022-03-30 01:46:21 +05:30
|
|
|
}
|
|
|
|
|
|
2024-04-08 03:29:42 +05:30
|
|
|
func (variables Variable) JSONSchema() *jsonschema.Schema {
|
2024-03-25 15:52:20 -04:00
|
|
|
gotType := &jsonschema.Schema{
|
2023-05-15 19:15:11 +05:30
|
|
|
Type: "object",
|
|
|
|
|
Title: "variables for the request",
|
|
|
|
|
Description: "Additional variables for the request",
|
2024-03-25 15:52:20 -04:00
|
|
|
AdditionalProperties: &jsonschema.Schema{},
|
2022-03-30 01:46:21 +05:30
|
|
|
}
|
|
|
|
|
return gotType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (variables *Variable) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
|
variables.InsertionOrderedStringMap = utils.InsertionOrderedStringMap{}
|
|
|
|
|
if err := unmarshal(&variables.InsertionOrderedStringMap); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2023-04-12 01:50:58 +05:30
|
|
|
|
2023-05-02 23:49:56 +05:30
|
|
|
if variables.LazyEval || variables.checkForLazyEval() {
|
2023-04-12 01:50:58 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-31 20:54:08 +05:30
|
|
|
evaluated := variables.Evaluate(map[string]interface{}{})
|
2022-11-01 20:28:50 +05:30
|
|
|
|
2022-03-31 20:54:08 +05:30
|
|
|
for k, v := range evaluated {
|
|
|
|
|
variables.Set(k, v)
|
|
|
|
|
}
|
2022-03-30 01:46:21 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-22 11:15:55 +08:00
|
|
|
func (variables *Variable) UnmarshalJSON(data []byte) error {
|
|
|
|
|
variables.InsertionOrderedStringMap = utils.InsertionOrderedStringMap{}
|
|
|
|
|
if err := json.Unmarshal(data, &variables.InsertionOrderedStringMap); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
evaluated := variables.Evaluate(map[string]interface{}{})
|
|
|
|
|
|
|
|
|
|
for k, v := range evaluated {
|
|
|
|
|
variables.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 01:46:21 +05:30
|
|
|
// Evaluate returns a finished map of variables based on set values
|
|
|
|
|
func (variables *Variable) Evaluate(values map[string]interface{}) map[string]interface{} {
|
|
|
|
|
result := make(map[string]interface{}, variables.Len())
|
|
|
|
|
variables.ForEach(func(key string, value interface{}) {
|
2023-08-31 18:03:01 +05:30
|
|
|
if sliceValue, ok := value.([]interface{}); ok {
|
|
|
|
|
// slices cannot be evaluated
|
|
|
|
|
result[key] = sliceValue
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-05-02 23:49:56 +05:30
|
|
|
valueString := types.ToString(value)
|
|
|
|
|
combined := generators.MergeMaps(values, result)
|
|
|
|
|
if value, ok := combined[key]; ok {
|
|
|
|
|
valueString = types.ToString(value)
|
|
|
|
|
}
|
|
|
|
|
result[key] = evaluateVariableValue(valueString, combined, result)
|
2022-03-30 01:46:21 +05:30
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-31 18:03:01 +05:30
|
|
|
// GetAll returns all variables as a map
|
|
|
|
|
func (variables *Variable) GetAll() map[string]interface{} {
|
|
|
|
|
result := make(map[string]interface{}, variables.Len())
|
|
|
|
|
variables.ForEach(func(key string, value interface{}) {
|
|
|
|
|
result[key] = value
|
|
|
|
|
})
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 22:18:13 +05:30
|
|
|
// EvaluateWithInteractsh returns evaluation results of variables with interactsh
|
|
|
|
|
func (variables *Variable) EvaluateWithInteractsh(values map[string]interface{}, interact *interactsh.Client) (map[string]interface{}, []string) {
|
|
|
|
|
result := make(map[string]interface{}, variables.Len())
|
|
|
|
|
|
|
|
|
|
var interactURLs []string
|
|
|
|
|
variables.ForEach(func(key string, value interface{}) {
|
2023-08-31 18:03:01 +05:30
|
|
|
if sliceValue, ok := value.([]interface{}); ok {
|
|
|
|
|
// slices cannot be evaluated
|
|
|
|
|
result[key] = sliceValue
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-07-11 22:18:13 +05:30
|
|
|
valueString := types.ToString(value)
|
|
|
|
|
if strings.Contains(valueString, "interactsh-url") {
|
2023-02-07 09:32:10 +01:00
|
|
|
valueString, interactURLs = interact.Replace(valueString, interactURLs)
|
2022-07-11 22:18:13 +05:30
|
|
|
}
|
2023-05-02 23:49:56 +05:30
|
|
|
combined := generators.MergeMaps(values, result)
|
|
|
|
|
if value, ok := combined[key]; ok {
|
|
|
|
|
valueString = types.ToString(value)
|
|
|
|
|
}
|
|
|
|
|
result[key] = evaluateVariableValue(valueString, combined, result)
|
2022-07-11 22:18:13 +05:30
|
|
|
})
|
|
|
|
|
return result, interactURLs
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-30 01:46:21 +05:30
|
|
|
// evaluateVariableValue expression and returns final value
|
|
|
|
|
func evaluateVariableValue(expression string, values, processing map[string]interface{}) string {
|
|
|
|
|
finalMap := generators.MergeMaps(values, processing)
|
2022-04-13 12:20:45 +05:30
|
|
|
result, err := expressions.Evaluate(expression, finalMap)
|
2022-03-30 01:46:21 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return expression
|
|
|
|
|
}
|
2022-11-01 20:28:50 +05:30
|
|
|
|
2022-04-13 12:20:45 +05:30
|
|
|
return result
|
2022-03-30 01:46:21 +05:30
|
|
|
}
|
2023-04-12 01:50:58 +05:30
|
|
|
|
|
|
|
|
// checkForLazyEval checks if the variables have any lazy evaluation i.e any dsl function
|
|
|
|
|
// and sets the flag accordingly.
|
|
|
|
|
func (variables *Variable) checkForLazyEval() bool {
|
|
|
|
|
variables.ForEach(func(key string, value interface{}) {
|
2023-06-22 13:07:31 +02:00
|
|
|
for _, v := range protocolutils.KnownVariables {
|
|
|
|
|
if stringsutil.ContainsAny(types.ToString(value), v) {
|
|
|
|
|
variables.LazyEval = true
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-12 01:50:58 +05:30
|
|
|
}
|
2024-03-29 13:31:30 +05:30
|
|
|
// this is a hotfix and not the best way to do it
|
|
|
|
|
// will be refactored once we move scan state to scanContext (see: https://github.com/projectdiscovery/nuclei/issues/4631)
|
|
|
|
|
if strings.Contains(types.ToString(value), "interactsh-url") {
|
|
|
|
|
variables.LazyEval = true
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-04-12 01:50:58 +05:30
|
|
|
})
|
|
|
|
|
return variables.LazyEval
|
|
|
|
|
}
|