2021-11-17 01:28:35 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2023-02-07 16:10:40 +08:00
|
|
|
"strings"
|
2021-11-17 01:28:35 +01:00
|
|
|
|
|
|
|
|
"github.com/alecthomas/jsonschema"
|
|
|
|
|
"github.com/pkg/errors"
|
2022-02-07 16:41:55 +02:00
|
|
|
|
2021-12-18 20:06:51 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http/signer"
|
2021-11-17 01:28:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SignatureType is the type of signature
|
|
|
|
|
type SignatureType int
|
|
|
|
|
|
|
|
|
|
// Supported values for the SignatureType
|
|
|
|
|
const (
|
2021-11-18 22:28:10 +01:00
|
|
|
AWSSignature SignatureType = iota + 1
|
|
|
|
|
signatureLimit
|
2021-11-17 01:28:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// signatureTypeMappings is a table for conversion of signature type from string.
|
|
|
|
|
var signatureTypeMappings = map[SignatureType]string{
|
2021-11-20 02:26:16 +01:00
|
|
|
AWSSignature: "AWS",
|
2021-11-17 01:28:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetSupportedSignaturesTypes() []SignatureType {
|
|
|
|
|
var result []SignatureType
|
2021-11-18 22:28:10 +01:00
|
|
|
for index := SignatureType(1); index < signatureLimit; index++ {
|
|
|
|
|
result = append(result, index)
|
2021-11-17 01:28:35 +01:00
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func toSignatureType(valueToMap string) (SignatureType, error) {
|
|
|
|
|
normalizedValue := normalizeValue(valueToMap)
|
|
|
|
|
for key, currentValue := range signatureTypeMappings {
|
|
|
|
|
if normalizedValue == currentValue {
|
|
|
|
|
return key, nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1, errors.New("invalid signature type: " + valueToMap)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (t SignatureType) String() string {
|
|
|
|
|
return signatureTypeMappings[t]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SignatureTypeHolder is used to hold internal type of the signature
|
|
|
|
|
type SignatureTypeHolder struct {
|
|
|
|
|
Value SignatureType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder SignatureTypeHolder) JSONSchemaType() *jsonschema.Type {
|
|
|
|
|
gotType := &jsonschema.Type{
|
|
|
|
|
Type: "string",
|
|
|
|
|
Title: "type of the signature",
|
|
|
|
|
Description: "Type of the signature",
|
|
|
|
|
}
|
|
|
|
|
for _, types := range GetSupportedSignaturesTypes() {
|
|
|
|
|
gotType.Enum = append(gotType.Enum, types.String())
|
|
|
|
|
}
|
|
|
|
|
return gotType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder *SignatureTypeHolder) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|
|
|
|
var marshalledTypes string
|
|
|
|
|
if err := unmarshal(&marshalledTypes); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
computedType, err := toSignatureType(marshalledTypes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
holder.Value = computedType
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-07 16:10:40 +08:00
|
|
|
func (holder *SignatureTypeHolder) UnmarshalJSON(data []byte) error {
|
2023-02-27 14:15:51 +08:00
|
|
|
s := strings.Trim(string(data), `"`)
|
|
|
|
|
if s == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
computedType, err := toSignatureType(s)
|
2023-02-07 16:10:40 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
holder.Value = computedType
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-27 14:15:51 +08:00
|
|
|
func (holder SignatureTypeHolder) MarshalJSON() ([]byte, error) {
|
2021-11-17 01:28:35 +01:00
|
|
|
return json.Marshal(holder.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (holder SignatureTypeHolder) MarshalYAML() (interface{}, error) {
|
|
|
|
|
return holder.Value.String(), nil
|
|
|
|
|
}
|
2021-12-18 20:06:51 +01:00
|
|
|
|
2022-02-07 16:41:55 +02:00
|
|
|
var ErrNoIgnoreList = errors.New("unknown signature types")
|
2021-12-18 20:06:51 +01:00
|
|
|
|
|
|
|
|
// GetVariablesNamesSkipList depending on the signature type
|
|
|
|
|
func GetVariablesNamesSkipList(signature SignatureType) map[string]interface{} {
|
|
|
|
|
switch signature {
|
|
|
|
|
case AWSSignature:
|
|
|
|
|
return signer.AwsSkipList
|
|
|
|
|
default:
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|