forked from projectdiscovery/nuclei
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
340 lines (317 loc) · 11.8 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package file
import (
"bufio"
"encoding/hex"
"io"
"os"
"path/filepath"
"strings"
"github.com/docker/go-units"
"github.com/mholt/archiver"
"github.com/pkg/errors"
"github.com/remeh/sizedwaitgroup"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
sliceutil "github.com/projectdiscovery/utils/slice"
)
var _ protocols.Request = &Request{}
// Type returns the type of the protocol request
func (request *Request) Type() templateTypes.ProtocolType {
return templateTypes.FileProtocol
}
type FileMatch struct {
Data string
Line int
ByteIndex int
Match bool
Extract bool
Expr string
Raw string
}
var emptyResultErr = errors.New("Empty result")
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
wg := sizedwaitgroup.New(request.options.Options.BulkSize)
err := request.getInputPaths(input.MetaInput.Input, func(filePath string) {
wg.Add()
func(filePath string) {
defer wg.Done()
archiveReader, _ := archiver.ByExtension(filePath)
switch {
case archiveReader != nil:
switch archiveInstance := archiveReader.(type) {
case archiver.Walker:
err := archiveInstance.Walk(filePath, func(file archiver.File) error {
if !request.validatePath("/", file.Name(), true) {
return nil
}
// every new file in the compressed multi-file archive counts 1
request.options.Progress.AddToTotal(1)
archiveFileName := filepath.Join(filePath, file.Name())
event, fileMatches, err := request.processReader(file.ReadCloser, archiveFileName, input.MetaInput.Input, file.Size(), previous)
if err != nil {
if errors.Is(err, emptyResultErr) {
// no matches but one file elaborated
request.options.Progress.IncrementRequests()
return nil
}
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return err
}
defer file.Close()
dumpResponse(event, request.options, fileMatches, filePath)
callback(event)
// file elaborated and matched
request.options.Progress.IncrementRequests()
return nil
})
if err != nil {
gologger.Error().Msgf("%s\n", err)
return
}
case archiver.Decompressor:
// compressed archive - contains only one file => increments the counter by 1
request.options.Progress.AddToTotal(1)
file, err := os.Open(filePath)
if err != nil {
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return
}
defer file.Close()
fileStat, _ := file.Stat()
tmpFileOut, err := os.CreateTemp("", "")
if err != nil {
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return
}
defer tmpFileOut.Close()
defer os.RemoveAll(tmpFileOut.Name())
if err := archiveInstance.Decompress(file, tmpFileOut); err != nil {
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return
}
_ = tmpFileOut.Sync()
// rewind the file
_, _ = tmpFileOut.Seek(0, 0)
event, fileMatches, err := request.processReader(tmpFileOut, filePath, input.MetaInput.Input, fileStat.Size(), previous)
if err != nil {
if errors.Is(err, emptyResultErr) {
// no matches but one file elaborated
request.options.Progress.IncrementRequests()
return
}
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return
}
dumpResponse(event, request.options, fileMatches, filePath)
callback(event)
// file elaborated and matched
request.options.Progress.IncrementRequests()
}
default:
// normal file - increments the counter by 1
request.options.Progress.AddToTotal(1)
event, fileMatches, err := request.processFile(filePath, input.MetaInput.Input, previous)
if err != nil {
if errors.Is(err, emptyResultErr) {
// no matches but one file elaborated
request.options.Progress.IncrementRequests()
return
}
gologger.Error().Msgf("%s\n", err)
// error while elaborating the file
request.options.Progress.IncrementFailedRequestsBy(1)
return
}
dumpResponse(event, request.options, fileMatches, filePath)
callback(event)
// file elaborated and matched
request.options.Progress.IncrementRequests()
}
}(filePath)
})
wg.Wait()
if err != nil {
request.options.Output.Request(request.options.TemplatePath, input.MetaInput.Input, request.Type().String(), err)
request.options.Progress.IncrementFailedRequestsBy(1)
return errors.Wrap(err, "could not send file request")
}
return nil
}
func (request *Request) processFile(filePath, input string, previousInternalEvent output.InternalEvent) (*output.InternalWrappedEvent, []FileMatch, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, nil, errors.Errorf("Could not open file path %s: %s\n", filePath, err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return nil, nil, errors.Errorf("Could not stat file path %s: %s\n", filePath, err)
}
if stat.Size() >= request.maxSize {
maxSizeString := units.HumanSize(float64(request.maxSize))
gologger.Verbose().Msgf("Limiting %s processed data to %s bytes: exceeded max size\n", filePath, maxSizeString)
}
return request.processReader(file, filePath, input, stat.Size(), previousInternalEvent)
}
func (request *Request) processReader(reader io.Reader, filePath, input string, totalBytes int64, previousInternalEvent output.InternalEvent) (*output.InternalWrappedEvent, []FileMatch, error) {
fileReader := io.LimitReader(reader, request.maxSize)
fileMatches, opResult := request.findMatchesWithReader(fileReader, input, filePath, totalBytes, previousInternalEvent)
if opResult == nil && len(fileMatches) == 0 {
return nil, nil, emptyResultErr
}
// build event structure to interface with internal logic
return request.buildEvent(input, filePath, fileMatches, opResult, previousInternalEvent), fileMatches, nil
}
func (request *Request) findMatchesWithReader(reader io.Reader, input, filePath string, totalBytes int64, previous output.InternalEvent) ([]FileMatch, *operators.Result) {
var bytesCount, linesCount, wordsCount int
isResponseDebug := request.options.Options.Debug || request.options.Options.DebugResponse
totalBytesString := units.BytesSize(float64(totalBytes))
scanner := bufio.NewScanner(reader)
buffer := []byte{}
if request.CompiledOperators.GetMatchersCondition() == matchers.ANDCondition {
scanner.Buffer(buffer, int(defaultMaxReadSize))
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
defaultMaxReadSizeInt := int(defaultMaxReadSize)
if len(data) > defaultMaxReadSizeInt {
return defaultMaxReadSizeInt, data[0:defaultMaxReadSizeInt], nil
}
if !atEOF {
return 0, nil, nil
}
return len(data), data, bufio.ErrFinalToken
})
} else {
scanner.Buffer(buffer, int(chunkSize))
}
var fileMatches []FileMatch
var opResult *operators.Result
for scanner.Scan() {
lineContent := scanner.Text()
n := len(lineContent)
// update counters
currentBytes := bytesCount + n
processedBytes := units.BytesSize(float64(currentBytes))
gologger.Verbose().Msgf("[%s] Processing file %s chunk %s/%s", request.options.TemplateID, filePath, processedBytes, totalBytesString)
dslMap := request.responseToDSLMap(lineContent, input, filePath)
for k, v := range previous {
dslMap[k] = v
}
discardEvent := eventcreator.CreateEvent(request, dslMap, isResponseDebug)
newOpResult := discardEvent.OperatorsResult
if newOpResult != nil {
if opResult == nil {
opResult = newOpResult
} else {
opResult.Merge(newOpResult)
}
if newOpResult.Matched || newOpResult.Extracted {
if newOpResult.Extracts != nil {
for expr, extracts := range newOpResult.Extracts {
for _, extract := range extracts {
fileMatches = append(fileMatches, FileMatch{
Data: extract,
Extract: true,
Line: linesCount + 1,
ByteIndex: bytesCount,
Expr: expr,
Raw: lineContent,
})
}
}
}
if newOpResult.Matches != nil {
for expr, matches := range newOpResult.Matches {
for _, match := range matches {
fileMatches = append(fileMatches, FileMatch{
Data: match,
Match: true,
Line: linesCount + 1,
ByteIndex: bytesCount,
Expr: expr,
Raw: lineContent,
})
}
}
}
for _, outputExtract := range newOpResult.OutputExtracts {
fileMatches = append(fileMatches, FileMatch{
Data: outputExtract,
Match: true,
Line: linesCount + 1,
ByteIndex: bytesCount,
Expr: outputExtract,
Raw: lineContent,
})
}
}
}
currentLinesCount := 1 + strings.Count(lineContent, "\n")
linesCount += currentLinesCount
wordsCount += strings.Count(lineContent, " ")
bytesCount = currentBytes
}
return fileMatches, opResult
}
func (request *Request) buildEvent(input, filePath string, fileMatches []FileMatch, operatorResult *operators.Result, previous output.InternalEvent) *output.InternalWrappedEvent {
exprLines := make(map[string][]int)
exprBytes := make(map[string][]int)
internalEvent := request.responseToDSLMap("", input, filePath)
for k, v := range previous {
internalEvent[k] = v
}
for _, fileMatch := range fileMatches {
exprLines[fileMatch.Expr] = append(exprLines[fileMatch.Expr], fileMatch.Line)
exprBytes[fileMatch.Expr] = append(exprBytes[fileMatch.Expr], fileMatch.ByteIndex)
}
event := eventcreator.CreateEventWithOperatorResults(request, internalEvent, operatorResult)
// Annotate with line numbers if asked by the user
if request.options.Options.ShowMatchLine {
for _, result := range event.Results {
switch {
case result.MatcherName != "":
result.Lines = exprLines[result.MatcherName]
case result.ExtractorName != "":
result.Lines = exprLines[result.ExtractorName]
default:
for _, extractedResult := range result.ExtractedResults {
result.Lines = append(result.Lines, exprLines[extractedResult]...)
}
}
result.Lines = sliceutil.Dedupe(result.Lines)
}
}
return event
}
func dumpResponse(event *output.InternalWrappedEvent, requestOptions *protocols.ExecuterOptions, filematches []FileMatch, filePath string) {
cliOptions := requestOptions.Options
if cliOptions.Debug || cliOptions.DebugResponse {
for _, fileMatch := range filematches {
lineContent := fileMatch.Raw
hexDump := false
if responsehighlighter.HasBinaryContent(lineContent) {
hexDump = true
lineContent = hex.Dump([]byte(lineContent))
}
highlightedResponse := responsehighlighter.Highlight(event.OperatorsResult, lineContent, cliOptions.NoColor, hexDump)
gologger.Debug().Msgf("[%s] Dumped match/extract file snippet for %s at line %d\n\n%s", requestOptions.TemplateID, filePath, fileMatch.Line, highlightedResponse)
}
}
}