2021-01-01 15:28:28 +05:30
|
|
|
package file
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-30 13:17:47 +03:00
|
|
|
"encoding/hex"
|
|
|
|
|
"fmt"
|
2021-01-01 15:28:28 +05:30
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-29 19:43:46 +03:00
|
|
|
"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"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
|
2021-10-01 14:24:45 +03:00
|
|
|
"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.
|
2021-10-01 14:30:04 +03:00
|
|
|
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
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
err := request.getInputPaths(input, func(data string) {
|
2021-01-14 22:43:08 +05:30
|
|
|
wg.Add()
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
go func(filePath string) {
|
2021-01-14 22:43:08 +05:30
|
|
|
defer wg.Done()
|
|
|
|
|
|
2021-09-29 19:43:46 +03:00
|
|
|
file, err := os.Open(filePath)
|
2021-01-14 22:43:08 +05:30
|
|
|
if err != nil {
|
2021-09-29 19:43:46 +03:00
|
|
|
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 {
|
2021-09-29 19:43:46 +03:00
|
|
|
gologger.Error().Msgf("Could not stat file path %s: %s\n", filePath, err)
|
2021-01-14 22:43:08 +05:30
|
|
|
return
|
|
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
if stat.Size() >= int64(request.MaxSize) {
|
2021-09-29 19:43:46 +03:00
|
|
|
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 {
|
2021-09-29 19:43:46 +03:00
|
|
|
gologger.Error().Msgf("Could not read file path %s: %s\n", filePath, err)
|
2021-01-14 22:43:08 +05:30
|
|
|
return
|
|
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
fileContent := tostring.UnsafeToString(buffer)
|
2021-09-29 19:43:46 +03:00
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
gologger.Verbose().Msgf("[%s] Sent FILE request to %s", request.options.TemplateID, filePath)
|
2021-10-30 13:17:47 +03:00
|
|
|
outputEvent := request.responseToDSLMap(fileContent, 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
|
|
|
|
2021-10-12 20:06:55 +03:00
|
|
|
event := eventcreator.CreateEvent(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse)
|
2021-10-01 14:24:45 +03:00
|
|
|
|
2021-10-30 13:17:47 +03:00
|
|
|
debug(event, request, filePath, fileContent)
|
2021-10-01 14:24:45 +03:00
|
|
|
|
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 {
|
2021-10-30 12:55:02 +03:00
|
|
|
request.options.Output.Request(request.options.TemplatePath, input, "file", err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not send file request")
|
2021-01-01 15:28:28 +05:30
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementRequests()
|
2021-01-01 19:36:21 +05:30
|
|
|
return nil
|
2021-01-01 15:28:28 +05:30
|
|
|
}
|
2021-10-30 13:17:47 +03:00
|
|
|
|
|
|
|
|
func debug(event *output.InternalWrappedEvent, request *Request, filePath string, fileContent string) {
|
|
|
|
|
if request.options.Options.Debug || request.options.Options.DebugResponse {
|
|
|
|
|
hexDump := false
|
|
|
|
|
if !responsehighlighter.IsASCII(fileContent) {
|
|
|
|
|
hexDump = true
|
|
|
|
|
fileContent = hex.Dump([]byte(fileContent))
|
|
|
|
|
}
|
|
|
|
|
logHeader := fmt.Sprintf("[%s] Dumped file request for %s\n", request.options.TemplateID, filePath)
|
|
|
|
|
gologger.Debug().Msgf("%s\n%s", logHeader, responsehighlighter.Highlight(event.OperatorsResult, fileContent, request.options.Options.NoColor, hexDump))
|
|
|
|
|
}
|
|
|
|
|
}
|