Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/input/formats/json/json.go
2070 views
1
package json
2
3
import (
4
"io"
5
6
"github.com/pkg/errors"
7
"github.com/projectdiscovery/gologger"
8
"github.com/projectdiscovery/nuclei/v3/pkg/input/formats"
9
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
10
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
11
)
12
13
// JSONFormat is a JSON format parser for nuclei
14
// input HTTP requests
15
type JSONFormat struct {
16
opts formats.InputFormatOptions
17
}
18
19
// New creates a new JSON format parser
20
func New() *JSONFormat {
21
return &JSONFormat{}
22
}
23
24
var _ formats.Format = &JSONFormat{}
25
26
// proxifyRequest is a request for proxify
27
type proxifyRequest struct {
28
URL string `json:"url"`
29
Request struct {
30
Header map[string]string `json:"header"`
31
Body string `json:"body"`
32
Raw string `json:"raw"`
33
Endpoint string `json:"endpoint"`
34
} `json:"request"`
35
}
36
37
// Name returns the name of the format
38
func (j *JSONFormat) Name() string {
39
return "jsonl"
40
}
41
42
func (j *JSONFormat) SetOptions(options formats.InputFormatOptions) {
43
j.opts = options
44
}
45
46
// Parse parses the input and calls the provided callback
47
// function for each RawRequest it discovers.
48
func (j *JSONFormat) Parse(input io.Reader, resultsCb formats.ParseReqRespCallback, filePath string) error {
49
decoder := json.NewDecoder(input)
50
for {
51
var request proxifyRequest
52
err := decoder.Decode(&request)
53
if err == io.EOF {
54
break
55
}
56
if err != nil {
57
return errors.Wrap(err, "could not decode json file")
58
}
59
60
if request.URL == "" && request.Request.Endpoint != "" {
61
request.URL = request.Request.Endpoint
62
}
63
rawRequest, err := types.ParseRawRequestWithURL(request.Request.Raw, request.URL)
64
if err != nil {
65
gologger.Warning().Msgf("jsonl: Could not parse raw request %s: %s\n", request.URL, err)
66
continue
67
}
68
resultsCb(rawRequest)
69
}
70
return nil
71
}
72
73