nuclei/v2/pkg/protocols/file/request.go

114 lines
3.3 KiB
Go
Raw Normal View History

2021-01-01 15:28:28 +05:30
package file
import (
"io/ioutil"
"os"
"strings"
2021-01-01 15:28:28 +05:30
"github.com/logrusorgru/aurora"
2021-01-01 15:28:28 +05:30
"github.com/pkg/errors"
"github.com/remeh/sizedwaitgroup"
2021-01-01 15:28:28 +05:30
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
2021-01-01 15:28:28 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/tostring"
)
var _ protocols.Request = &Request{}
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
2021-08-23 16:21:18 +03:00
func (r *Request) ExecuteWithResults(input string, metadata /*TODO review unused parameter*/, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
2021-02-09 18:15:07 +05:30
wg := sizedwaitgroup.New(r.options.Options.BulkSize)
2021-01-14 22:43:08 +05:30
2021-01-01 15:28:28 +05:30
err := r.getInputPaths(input, func(data string) {
2021-01-14 22:43:08 +05:30
wg.Add()
go func(filePath string) {
2021-01-14 22:43:08 +05:30
defer wg.Done()
file, err := os.Open(filePath)
2021-01-14 22:43:08 +05:30
if err != nil {
gologger.Error().Msgf("Could not open file path %s: %s\n", filePath, err)
2021-01-14 22:43:08 +05:30
return
}
defer file.Close()
2021-01-01 15:28:28 +05:30
2021-01-14 22:43:08 +05:30
stat, err := file.Stat()
if err != nil {
gologger.Error().Msgf("Could not stat file path %s: %s\n", filePath, err)
2021-01-14 22:43:08 +05:30
return
}
if stat.Size() >= int64(r.MaxSize) {
gologger.Verbose().Msgf("Could not process path %s: exceeded max size\n", filePath)
2021-01-14 22:43:08 +05:30
return
}
2021-01-01 15:28:28 +05:30
2021-01-14 22:43:08 +05:30
buffer, err := ioutil.ReadAll(file)
if err != nil {
gologger.Error().Msgf("Could not read file path %s: %s\n", filePath, err)
2021-01-14 22:43:08 +05:30
return
}
dataStr := tostring.UnsafeToString(buffer)
gologger.Verbose().Msgf("[%s] Sent FILE request to %s", r.options.TemplateID, filePath)
outputEvent := r.responseToDSLMap(dataStr, input, filePath)
2021-01-16 14:10:24 +05:30
for k, v := range previous {
outputEvent[k] = v
}
2021-01-01 15:28:28 +05:30
event := createEvent(r, filePath, dataStr, outputEvent)
2021-01-14 22:43:08 +05:30
callback(event)
}(data)
2021-01-01 15:28:28 +05:30
})
2021-01-14 22:43:08 +05:30
wg.Wait()
2021-01-01 15:28:28 +05:30
if err != nil {
r.options.Output.Request(r.options.TemplateID, input, "file", err)
r.options.Progress.IncrementFailedRequestsBy(1)
return errors.Wrap(err, "could not send file request")
2021-01-01 15:28:28 +05:30
}
r.options.Progress.IncrementRequests()
return nil
2021-01-01 15:28:28 +05:30
}
// TODO extract duplicated code
func createEvent(request *Request, filePath string, response string, outputEvent output.InternalEvent) *output.InternalWrappedEvent {
debugResponse := func(data string) {
if request.options.Options.Debug || request.options.Options.DebugResponse {
gologger.Info().Msgf("[%s] Dumped file request for %s", request.options.TemplateID, filePath)
gologger.Print().Msgf("%s", data)
}
}
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
if request.CompiledOperators != nil {
matcher := func(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
isMatch, matched := request.Match(data, matcher)
var result = response
if len(matched) != 0 {
if !request.options.Options.NoColor {
colorizer := aurora.NewAurora(true)
for _, currentMatch := range matched {
result = strings.ReplaceAll(result, currentMatch, colorizer.Green(currentMatch).String())
}
}
debugResponse(result)
}
return isMatch, matched
}
result, ok := request.CompiledOperators.Execute(outputEvent, matcher, request.Extract)
if ok && result != nil {
event.OperatorsResult = result
event.Results = request.MakeResultEvent(event)
}
} else {
debugResponse(response)
}
return event
}