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

92 lines
2.8 KiB
Go
Raw Normal View History

2021-01-01 15:28:28 +05:30
package file
import (
"io/ioutil"
"os"
"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/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
2021-01-01 15:28:28 +05:30
"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.
func (request *Request) ExecuteWithResults(input string, metadata /*TODO review unused parameter*/, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
wg := sizedwaitgroup.New(request.options.Options.BulkSize)
2021-01-14 22:43:08 +05:30
err := request.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(request.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", request.options.TemplateID, filePath)
outputEvent := request.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(request, outputEvent)
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", responsehighlighter.Highlight(event.OperatorsResult, dataStr, request.options.Options.NoColor))
}
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 {
request.options.Output.Request(request.options.TemplateID, input, "file", err)
request.options.Progress.IncrementFailedRequestsBy(1)
return errors.Wrap(err, "could not send file request")
2021-01-01 15:28:28 +05:30
}
request.options.Progress.IncrementRequests()
return nil
2021-01-01 15:28:28 +05:30
}
func createEvent(request *Request, outputEvent output.InternalEvent) *output.InternalWrappedEvent {
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
if request.CompiledOperators != nil {
result, ok := request.CompiledOperators.Execute(outputEvent, request.Match, request.Extract)
if ok && result != nil {
event.OperatorsResult = result
event.Results = request.MakeResultEvent(event)
}
}
return event
}