2024-03-14 03:08:53 +05:30
|
|
|
package yaml
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/input/formats"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
|
|
|
|
|
YamlUtil "gopkg.in/yaml.v3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// YamlMultiDocFormat is a Yaml format parser for nuclei
|
|
|
|
|
// input HTTP requests with multiple documents separated by ---
|
|
|
|
|
type YamlMultiDocFormat struct {
|
|
|
|
|
opts formats.InputFormatOptions
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates a new JSON format parser
|
|
|
|
|
func New() *YamlMultiDocFormat {
|
|
|
|
|
return &YamlMultiDocFormat{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ formats.Format = &YamlMultiDocFormat{}
|
|
|
|
|
|
|
|
|
|
// proxifyRequest is a request for proxify
|
|
|
|
|
type proxifyRequest struct {
|
|
|
|
|
URL string `json:"url"`
|
|
|
|
|
Request struct {
|
|
|
|
|
Header map[string]string `json:"header"`
|
|
|
|
|
Body string `json:"body"`
|
|
|
|
|
Raw string `json:"raw"`
|
|
|
|
|
} `json:"request"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name returns the name of the format
|
|
|
|
|
func (j *YamlMultiDocFormat) Name() string {
|
|
|
|
|
return "yaml"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (j *YamlMultiDocFormat) SetOptions(options formats.InputFormatOptions) {
|
|
|
|
|
j.opts = options
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse parses the input and calls the provided callback
|
|
|
|
|
// function for each RawRequest it discovers.
|
2025-02-13 18:46:28 +05:30
|
|
|
func (j *YamlMultiDocFormat) Parse(input io.Reader, resultsCb formats.ParseReqRespCallback, filePath string) error {
|
2025-06-12 15:03:33 +02:00
|
|
|
finalInput := input
|
|
|
|
|
|
|
|
|
|
// Apply text templating if enabled
|
|
|
|
|
if j.opts.VarsTextTemplating {
|
|
|
|
|
data, err := io.ReadAll(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not read input")
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2025-06-12 15:03:33 +02:00
|
|
|
tpl := []string{string(data)}
|
|
|
|
|
dvs := mapToKeyValueSlice(j.opts.Variables)
|
2025-06-24 18:32:45 +02:00
|
|
|
finalInput, err = ytt(tpl, dvs, j.opts.VarsFilePaths)
|
2024-03-14 03:08:53 +05:30
|
|
|
if err != nil {
|
2025-06-12 15:03:33 +02:00
|
|
|
return errors.Wrap(err, "could not apply ytt templating")
|
2024-03-14 03:08:53 +05:30
|
|
|
}
|
2025-06-12 15:03:33 +02:00
|
|
|
finalData, err := io.ReadAll(finalInput)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not read templated input")
|
|
|
|
|
}
|
|
|
|
|
finalInput = strings.NewReader(string(finalData))
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
decoder := YamlUtil.NewDecoder(finalInput)
|
|
|
|
|
for {
|
|
|
|
|
var request proxifyRequest
|
|
|
|
|
if err := decoder.Decode(&request); err != nil {
|
|
|
|
|
if err == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
return errors.Wrap(err, "could not decode yaml file")
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 16:50:21 +02:00
|
|
|
raw := request.Request.Raw
|
2025-06-12 15:03:33 +02:00
|
|
|
if raw == "" {
|
2024-03-14 03:08:53 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-12 15:03:33 +02:00
|
|
|
rawRequest, err := types.ParseRawRequestWithURL(raw, request.URL)
|
2024-03-14 03:08:53 +05:30
|
|
|
if err != nil {
|
2025-06-12 15:03:33 +02:00
|
|
|
gologger.Warning().Msgf("multidoc-yaml: Could not parse raw request %s: %s", request.URL, err)
|
2024-03-14 03:08:53 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
resultsCb(rawRequest)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|