2021-02-06 00:36:43 +05:30
|
|
|
package offlinehttp
|
|
|
|
|
|
|
|
|
|
import (
|
2022-02-23 13:54:46 +01:00
|
|
|
"io"
|
2021-02-06 00:36:43 +05:30
|
|
|
"net/http/httputil"
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-29 19:43:46 +03:00
|
|
|
"github.com/remeh/sizedwaitgroup"
|
|
|
|
|
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2022-10-03 12:12:20 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/contextargs"
|
2021-10-01 16:52:38 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
|
2021-02-06 00:36:43 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/tostring"
|
2023-06-26 19:25:51 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/utils"
|
2021-11-03 19:53:45 +05:30
|
|
|
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
|
2021-02-06 00:36:43 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
|
|
|
|
const maxSize = 5 * 1024 * 1024
|
|
|
|
|
|
2021-11-03 19:53:45 +05:30
|
|
|
// Type returns the type of the protocol request
|
|
|
|
|
func (request *Request) Type() templateTypes.ProtocolType {
|
2022-10-20 17:23:00 +05:30
|
|
|
return templateTypes.OfflineHTTPProtocol
|
2021-11-03 19:53:45 +05:30
|
|
|
}
|
|
|
|
|
|
2021-02-06 00:36:43 +05:30
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2022-10-03 12:12:20 +02:00
|
|
|
func (request *Request) ExecuteWithResults(input *contextargs.Context, metadata /*TODO review unused parameter*/, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2021-10-01 14:30:04 +03:00
|
|
|
wg := sizedwaitgroup.New(request.options.Options.BulkSize)
|
2021-02-06 00:36:43 +05:30
|
|
|
|
2022-11-09 14:18:56 +01:00
|
|
|
err := request.getInputPaths(input.MetaInput.Input, func(data string) {
|
2021-02-06 00:36:43 +05:30
|
|
|
wg.Add()
|
|
|
|
|
|
|
|
|
|
go func(data string) {
|
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
|
|
file, err := os.Open(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not open file path %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
|
|
stat, err := file.Stat()
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not stat file path %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if stat.Size() >= int64(maxSize) {
|
|
|
|
|
gologger.Verbose().Msgf("Could not process path %s: exceeded max size\n", data)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 13:54:46 +01:00
|
|
|
buffer, err := io.ReadAll(file)
|
2021-02-06 00:36:43 +05:30
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not read file path %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
dataStr := tostring.UnsafeToString(buffer)
|
|
|
|
|
|
|
|
|
|
resp, err := readResponseFromString(dataStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not read raw response %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests {
|
|
|
|
|
gologger.Info().Msgf("[%s] Dumped offline-http request for %s", request.options.TemplateID, data)
|
2021-02-06 00:36:43 +05:30
|
|
|
gologger.Print().Msgf("%s", dataStr)
|
|
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
gologger.Verbose().Msgf("[%s] Sent OFFLINE-HTTP request to %s", request.options.TemplateID, data)
|
2021-02-06 00:36:43 +05:30
|
|
|
|
|
|
|
|
dumpedResponse, err := httputil.DumpResponse(resp, true)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not dump raw http response %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-23 13:54:46 +01:00
|
|
|
body, err := io.ReadAll(resp.Body)
|
2021-02-06 00:36:43 +05:30
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("Could not read raw http response body %s: %s\n", data, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 19:25:51 +02:00
|
|
|
outputEvent := request.responseToDSLMap(resp, data, data, data, tostring.UnsafeToString(dumpedResponse), tostring.UnsafeToString(body), utils.HeadersToString(resp.Header), 0, nil)
|
2021-02-06 00:36:43 +05:30
|
|
|
outputEvent["ip"] = ""
|
|
|
|
|
for k, v := range previous {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 20:06:55 +03:00
|
|
|
event := eventcreator.CreateEvent(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse)
|
2021-10-01 16:52:38 +03:00
|
|
|
callback(event)
|
2021-02-06 00:36:43 +05:30
|
|
|
}(data)
|
|
|
|
|
})
|
|
|
|
|
wg.Wait()
|
|
|
|
|
if err != nil {
|
2022-11-09 14:18:56 +01:00
|
|
|
request.options.Output.Request(request.options.TemplatePath, input.MetaInput.Input, "file", err)
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-02-06 00:36:43 +05:30
|
|
|
return errors.Wrap(err, "could not send file request")
|
|
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementRequests()
|
2021-02-06 00:36:43 +05:30
|
|
|
return nil
|
|
|
|
|
}
|