2020-12-30 14:54:20 +05:30
|
|
|
package network
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-12-30 16:47:22 +05:30
|
|
|
"encoding/hex"
|
2021-02-24 12:07:16 +05:30
|
|
|
"io"
|
2020-12-30 21:14:04 +05:30
|
|
|
"net"
|
2020-12-30 14:54:20 +05:30
|
|
|
"net/url"
|
2021-10-12 23:28:24 +02:00
|
|
|
"os"
|
2020-12-30 14:54:20 +05:30
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-09-29 19:43:46 +03:00
|
|
|
|
2020-12-30 14:54:20 +05:30
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2021-07-06 18:27:30 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
|
2021-09-15 18:02:22 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/generators"
|
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-04-18 16:10:10 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/interactsh"
|
2020-12-30 21:14:04 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/replacer"
|
2020-12-30 14:54:20 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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 {
|
2021-10-17 18:50:07 +05:30
|
|
|
var address string
|
|
|
|
|
var err error
|
|
|
|
|
|
2021-10-19 21:33:17 +05:30
|
|
|
if request.SelfContained {
|
2021-10-17 18:50:07 +05:30
|
|
|
address = ""
|
2021-10-19 21:33:17 +05:30
|
|
|
} else {
|
|
|
|
|
address, err = getAddress(input)
|
2021-10-17 18:50:07 +05:30
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
if err != nil {
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, input, "network", err)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not get address from url")
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
for _, kv := range request.addresses {
|
2021-02-19 00:10:06 +01:00
|
|
|
actualAddress := replacer.Replace(kv.ip, map[string]interface{}{"Hostname": address})
|
|
|
|
|
if kv.port != "" {
|
2020-12-30 21:14:04 +05:30
|
|
|
if strings.Contains(address, ":") {
|
|
|
|
|
actualAddress, _, _ = net.SplitHostPort(actualAddress)
|
|
|
|
|
}
|
2021-02-19 00:10:06 +01:00
|
|
|
actualAddress = net.JoinHostPort(actualAddress, kv.port)
|
2020-12-30 21:14:04 +05:30
|
|
|
}
|
2021-10-18 19:48:47 +05:30
|
|
|
if input != "" {
|
|
|
|
|
input = actualAddress
|
|
|
|
|
}
|
2020-12-30 21:14:04 +05:30
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
if err := request.executeAddress(actualAddress, address, input, kv.tls, previous, callback); err != nil {
|
2021-02-27 13:08:10 +05:30
|
|
|
gologger.Verbose().Label("ERR").Msgf("Could not make network request for %s: %s\n", actualAddress, err)
|
2020-12-30 21:14:04 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-01 19:36:21 +05:30
|
|
|
return nil
|
2020-12-30 21:14:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// executeAddress executes the request for an address
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) executeAddress(actualAddress, address, input string, shouldUseTLS bool, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2020-12-30 21:14:04 +05:30
|
|
|
if !strings.Contains(actualAddress, ":") {
|
|
|
|
|
err := errors.New("no port provided in network protocol request")
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return err
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-17 11:52:48 +02:00
|
|
|
payloads := generators.BuildPayloadFromOptions(request.options.Options)
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.generator != nil {
|
|
|
|
|
iterator := request.generator.NewIterator()
|
2021-07-06 18:27:30 +05:30
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
value, ok := iterator.Value()
|
|
|
|
|
if !ok {
|
|
|
|
|
break
|
|
|
|
|
}
|
2021-10-17 11:52:48 +02:00
|
|
|
value = generators.MergeMaps(value, payloads)
|
2021-10-01 14:30:04 +03:00
|
|
|
if err := request.executeRequestWithPayloads(actualAddress, address, input, shouldUseTLS, value, previous, callback); err != nil {
|
2021-07-06 18:27:30 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-10-17 11:52:48 +02:00
|
|
|
value := generators.MergeMaps(map[string]interface{}{}, payloads)
|
2021-10-01 14:30:04 +03:00
|
|
|
if err := request.executeRequestWithPayloads(actualAddress, address, input, shouldUseTLS, value, previous, callback); err != nil {
|
2021-07-06 18:27:30 +05:30
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
func (request *Request) executeRequestWithPayloads(actualAddress, address, input string, shouldUseTLS bool, payloads map[string]interface{}, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2021-02-18 01:37:48 +01:00
|
|
|
var (
|
|
|
|
|
hostname string
|
|
|
|
|
conn net.Conn
|
|
|
|
|
err error
|
|
|
|
|
)
|
2021-10-17 11:52:48 +02:00
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
request.dynamicValues = generators.MergeMaps(payloads, map[string]interface{}{"Hostname": address})
|
2021-02-19 00:10:06 +01:00
|
|
|
|
2021-02-26 13:13:11 +05:30
|
|
|
if host, _, splitErr := net.SplitHostPort(actualAddress); splitErr == nil {
|
2021-01-16 12:06:27 +05:30
|
|
|
hostname = host
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-19 00:10:06 +01:00
|
|
|
if shouldUseTLS {
|
2021-10-01 14:30:04 +03:00
|
|
|
conn, err = request.dialer.DialTLS(context.Background(), "tcp", actualAddress)
|
2021-02-18 01:37:48 +01:00
|
|
|
} else {
|
2021-10-01 14:30:04 +03:00
|
|
|
conn, err = request.dialer.Dial(context.Background(), "tcp", actualAddress)
|
2021-02-18 01:37:48 +01:00
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
if err != nil {
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not connect to server request")
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
2021-10-01 14:30:04 +03:00
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(time.Duration(request.options.Options.Timeout) * time.Second))
|
2020-12-30 14:54:20 +05:30
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
hasInteractMarkers := interactsh.HasMatchers(request.CompiledOperators)
|
2021-04-18 16:10:10 +05:30
|
|
|
var interactURL string
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.options.Interactsh != nil && hasInteractMarkers {
|
|
|
|
|
interactURL = request.options.Interactsh.URL()
|
2021-04-18 16:10:10 +05:30
|
|
|
}
|
|
|
|
|
|
2021-02-16 15:13:01 +05:30
|
|
|
responseBuilder := &strings.Builder{}
|
2020-12-30 16:47:22 +05:30
|
|
|
reqBuilder := &strings.Builder{}
|
2021-02-16 15:13:01 +05:30
|
|
|
|
2021-02-24 20:11:21 +05:30
|
|
|
inputEvents := make(map[string]interface{})
|
2021-10-01 14:30:04 +03:00
|
|
|
for _, input := range request.Inputs {
|
2020-12-30 16:47:22 +05:30
|
|
|
var data []byte
|
|
|
|
|
|
|
|
|
|
switch input.Type {
|
|
|
|
|
case "hex":
|
|
|
|
|
data, err = hex.DecodeString(input.Data)
|
|
|
|
|
default:
|
2021-04-18 17:53:59 +05:30
|
|
|
if interactURL != "" {
|
2021-10-01 14:30:04 +03:00
|
|
|
input.Data = request.options.Interactsh.ReplaceMarkers(input.Data, interactURL)
|
2021-04-18 16:10:10 +05:30
|
|
|
}
|
2020-12-30 16:47:22 +05:30
|
|
|
data = []byte(input.Data)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not write request to server")
|
2020-12-30 16:47:22 +05:30
|
|
|
}
|
|
|
|
|
reqBuilder.Grow(len(input.Data))
|
|
|
|
|
|
2021-07-06 20:31:32 +05:30
|
|
|
finalData, dataErr := expressions.EvaluateByte(data, payloads)
|
|
|
|
|
if dataErr != nil {
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", dataErr)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-07-06 20:31:32 +05:30
|
|
|
return errors.Wrap(dataErr, "could not evaluate template expressions")
|
2021-07-06 18:27:30 +05:30
|
|
|
}
|
|
|
|
|
reqBuilder.Write(finalData)
|
|
|
|
|
|
2021-10-07 01:40:49 +05:30
|
|
|
if varErr := expressions.ContainsUnresolvedVariables(string(finalData)); varErr != nil {
|
2021-10-11 13:58:20 +03:00
|
|
|
gologger.Warning().Msgf("[%s] Could not make network request for %s: %v\n", request.options.TemplateID, actualAddress, varErr)
|
2021-10-07 01:40:49 +05:30
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-31 12:55:52 +03:00
|
|
|
if _, err := conn.Write(finalData); err != nil {
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
|
|
|
|
request.options.Progress.IncrementFailedRequestsBy(1)
|
2021-01-01 19:36:21 +05:30
|
|
|
return errors.Wrap(err, "could not write request to server")
|
2020-12-30 16:47:22 +05:30
|
|
|
}
|
2021-02-16 14:46:23 +05:30
|
|
|
|
2021-02-16 15:18:57 +05:30
|
|
|
if input.Read > 0 {
|
2021-02-27 02:23:06 +05:30
|
|
|
buffer := make([]byte, input.Read)
|
2021-02-16 15:13:01 +05:30
|
|
|
n, _ := conn.Read(buffer)
|
|
|
|
|
responseBuilder.Write(buffer[:n])
|
2021-07-06 18:34:25 +05:30
|
|
|
|
|
|
|
|
bufferStr := string(buffer[:n])
|
2021-02-24 20:11:21 +05:30
|
|
|
if input.Name != "" {
|
2021-07-06 18:34:25 +05:30
|
|
|
inputEvents[input.Name] = bufferStr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run any internal extractors for the request here and add found values to map.
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.CompiledOperators != nil {
|
|
|
|
|
values := request.CompiledOperators.ExecuteInternalExtractors(map[string]interface{}{input.Name: bufferStr}, request.Extract)
|
2021-07-06 18:34:25 +05:30
|
|
|
for k, v := range values {
|
|
|
|
|
payloads[k] = v
|
|
|
|
|
}
|
2021-02-24 20:11:21 +05:30
|
|
|
}
|
2021-02-16 14:46:23 +05:30
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Progress.IncrementRequests()
|
2020-12-30 14:54:20 +05:30
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugRequests {
|
2021-06-21 08:52:14 +02:00
|
|
|
requestOutput := reqBuilder.String()
|
2021-10-01 14:30:04 +03:00
|
|
|
gologger.Info().Str("address", actualAddress).Msgf("[%s] Dumped Network request for %s", request.options.TemplateID, actualAddress)
|
2021-06-21 08:52:14 +02:00
|
|
|
gologger.Print().Msgf("%s\nHex: %s", requestOutput, hex.EncodeToString([]byte(requestOutput)))
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Output.Request(request.options.TemplateID, actualAddress, "network", err)
|
2021-01-11 19:59:12 +05:30
|
|
|
gologger.Verbose().Msgf("Sent TCP request to %s", actualAddress)
|
2020-12-30 16:47:22 +05:30
|
|
|
|
2020-12-30 14:54:20 +05:30
|
|
|
bufferSize := 1024
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.ReadSize != 0 {
|
|
|
|
|
bufferSize = request.ReadSize
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-10-19 11:31:36 +02:00
|
|
|
|
|
|
|
|
var (
|
2021-10-12 23:28:24 +02:00
|
|
|
final []byte
|
|
|
|
|
n int
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-19 11:31:36 +02:00
|
|
|
if request.ReadAll {
|
2021-10-19 19:39:33 +02:00
|
|
|
readInterval := time.NewTimer(time.Second * 1)
|
|
|
|
|
// stop the timer and drain the channel
|
|
|
|
|
closeTimer := func(t *time.Timer) {
|
|
|
|
|
if !t.Stop() {
|
|
|
|
|
<-t.C
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-12 23:28:24 +02:00
|
|
|
read_socket:
|
|
|
|
|
for {
|
|
|
|
|
select {
|
2021-10-19 19:39:33 +02:00
|
|
|
case <-readInterval.C:
|
|
|
|
|
closeTimer(readInterval)
|
2021-10-12 23:28:24 +02:00
|
|
|
break read_socket
|
|
|
|
|
default:
|
|
|
|
|
buf := make([]byte, bufferSize)
|
|
|
|
|
nBuf, err := conn.Read(buf)
|
|
|
|
|
if err != nil && !os.IsTimeout(err) {
|
2021-10-19 11:31:36 +02:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
2021-10-19 19:39:33 +02:00
|
|
|
closeTimer(readInterval)
|
2021-10-12 23:28:24 +02:00
|
|
|
return errors.Wrap(err, "could not read from server")
|
|
|
|
|
}
|
|
|
|
|
responseBuilder.Write(buf[:nBuf])
|
|
|
|
|
final = append(final, buf...)
|
|
|
|
|
n += nBuf
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
final = make([]byte, bufferSize)
|
|
|
|
|
n, err = conn.Read(final)
|
|
|
|
|
if err != nil && err != io.EOF {
|
2021-10-19 11:28:33 +02:00
|
|
|
request.options.Output.Request(request.options.TemplateID, address, "network", err)
|
2021-10-12 23:28:24 +02:00
|
|
|
return errors.Wrap(err, "could not read from server")
|
|
|
|
|
}
|
|
|
|
|
responseBuilder.Write(final[:n])
|
2021-02-24 12:07:16 +05:30
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
|
2021-10-01 14:24:45 +03:00
|
|
|
response := responseBuilder.String()
|
2021-10-01 14:30:04 +03:00
|
|
|
outputEvent := request.responseToDSLMap(reqBuilder.String(), string(final[:n]), response, input, actualAddress)
|
|
|
|
|
outputEvent["ip"] = request.dialer.GetDialedIP(hostname)
|
2021-01-16 14:10:24 +05:30
|
|
|
for k, v := range previous {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2021-07-06 20:30:15 +05:30
|
|
|
for k, v := range payloads {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2021-02-24 20:11:21 +05:30
|
|
|
for k, v := range inputEvents {
|
|
|
|
|
outputEvent[k] = v
|
|
|
|
|
}
|
2020-12-30 14:54:20 +05:30
|
|
|
|
2021-10-01 16:52:38 +03:00
|
|
|
var event *output.InternalWrappedEvent
|
2021-07-06 18:27:30 +05:30
|
|
|
if interactURL == "" {
|
2021-10-12 20:06:55 +03:00
|
|
|
event = eventcreator.CreateEventWithAdditionalOptions(request, outputEvent, request.options.Options.Debug || request.options.Options.DebugResponse, func(wrappedEvent *output.InternalWrappedEvent) {
|
2021-10-01 16:52:38 +03:00
|
|
|
wrappedEvent.OperatorsResult.PayloadValues = payloads
|
|
|
|
|
})
|
2021-05-09 01:37:22 +05:30
|
|
|
callback(event)
|
2021-10-01 14:30:04 +03:00
|
|
|
} else if request.options.Interactsh != nil {
|
2021-10-01 16:52:38 +03:00
|
|
|
event = &output.InternalWrappedEvent{InternalEvent: outputEvent}
|
2021-10-01 14:30:04 +03:00
|
|
|
request.options.Interactsh.RequestEvent(interactURL, &interactsh.RequestData{
|
|
|
|
|
MakeResultFunc: request.MakeResultEvent,
|
2021-05-09 02:19:23 +05:30
|
|
|
Event: event,
|
2021-10-01 14:30:04 +03:00
|
|
|
Operators: request.CompiledOperators,
|
|
|
|
|
MatchFunc: request.Match,
|
|
|
|
|
ExtractFunc: request.Extract,
|
2021-05-09 02:19:23 +05:30
|
|
|
})
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
2021-10-01 14:24:45 +03:00
|
|
|
|
2021-10-01 14:30:04 +03:00
|
|
|
if request.options.Options.Debug || request.options.Options.DebugResponse {
|
|
|
|
|
gologger.Debug().Msgf("[%s] Dumped Network response for %s", request.options.TemplateID, actualAddress)
|
|
|
|
|
gologger.Print().Msgf("%s\nHex: %s", response, responsehighlighter.Highlight(event.OperatorsResult, hex.EncodeToString([]byte(response)), request.options.Options.NoColor))
|
2021-10-01 14:24:45 +03:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 19:36:21 +05:30
|
|
|
return nil
|
2020-12-30 14:54:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// getAddress returns the address of the host to make request to
|
|
|
|
|
func getAddress(toTest string) (string, error) {
|
|
|
|
|
if strings.Contains(toTest, "://") {
|
|
|
|
|
parsed, err := url.Parse(toTest)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
toTest = parsed.Host
|
|
|
|
|
}
|
|
|
|
|
return toTest, nil
|
|
|
|
|
}
|