2020-12-30 09:24:20 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-30 11:17:22 +00:00
|
|
|
"encoding/hex"
|
2021-02-24 06:37:16 +00:00
|
|
|
"io"
|
2020-12-30 15:44:04 +00:00
|
|
|
"net"
|
2020-12-30 09:24:20 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/output"
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
|
2020-12-30 15:44:04 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/replacer"
|
2020-12-30 09:24:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ protocols.Request = &Request{}
|
|
|
|
|
|
|
|
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
2021-01-16 08:40:24 +00:00
|
|
|
func (r *Request) ExecuteWithResults(input string, metadata, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2020-12-30 09:24:20 +00:00
|
|
|
address, err := getAddress(input)
|
|
|
|
if err != nil {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, input, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not get address from url")
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 15:44:04 +00:00
|
|
|
for _, kv := range r.addresses {
|
2021-02-18 23:10:06 +00:00
|
|
|
actualAddress := replacer.Replace(kv.ip, map[string]interface{}{"Hostname": address})
|
|
|
|
if kv.port != "" {
|
2020-12-30 15:44:04 +00:00
|
|
|
if strings.Contains(address, ":") {
|
|
|
|
actualAddress, _, _ = net.SplitHostPort(actualAddress)
|
|
|
|
}
|
2021-02-18 23:10:06 +00:00
|
|
|
actualAddress = net.JoinHostPort(actualAddress, kv.port)
|
2020-12-30 15:44:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 23:10:06 +00:00
|
|
|
err = r.executeAddress(actualAddress, address, input, kv.tls, previous, callback)
|
2020-12-30 15:44:04 +00:00
|
|
|
if err != nil {
|
2020-12-30 17:50:31 +00:00
|
|
|
gologger.Verbose().Lable("ERR").Msgf("Could not make network request for %s: %s\n", actualAddress, err)
|
2020-12-30 15:44:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
return nil
|
2020-12-30 15:44:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// executeAddress executes the request for an address
|
2021-02-18 23:10:06 +00:00
|
|
|
func (r *Request) executeAddress(actualAddress, address, input string, shouldUseTLS bool, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
2020-12-30 15:44:04 +00:00
|
|
|
if !strings.Contains(actualAddress, ":") {
|
|
|
|
err := errors.New("no port provided in network protocol request")
|
2020-12-30 09:24:20 +00:00
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return err
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 00:37:48 +00:00
|
|
|
var (
|
|
|
|
hostname string
|
|
|
|
conn net.Conn
|
|
|
|
err error
|
|
|
|
)
|
2021-02-18 23:10:06 +00:00
|
|
|
|
2021-02-26 07:43:11 +00:00
|
|
|
if host, _, splitErr := net.SplitHostPort(actualAddress); splitErr == nil {
|
2021-01-16 06:36:27 +00:00
|
|
|
hostname = host
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:10:06 +00:00
|
|
|
if shouldUseTLS {
|
2021-02-18 00:37:48 +00:00
|
|
|
conn, err = r.dialer.DialTLS(context.Background(), "tcp", actualAddress)
|
|
|
|
} else {
|
|
|
|
conn, err = r.dialer.Dial(context.Background(), "tcp", actualAddress)
|
|
|
|
}
|
2020-12-30 09:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not connect to server request")
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2021-02-26 07:43:11 +00:00
|
|
|
_ = conn.SetReadDeadline(time.Now().Add(time.Duration(r.options.Options.Timeout) * time.Second))
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-02-16 09:43:01 +00:00
|
|
|
responseBuilder := &strings.Builder{}
|
2020-12-30 11:17:22 +00:00
|
|
|
reqBuilder := &strings.Builder{}
|
2021-02-16 09:43:01 +00:00
|
|
|
|
2021-02-24 14:41:21 +00:00
|
|
|
inputEvents := make(map[string]interface{})
|
2020-12-30 11:17:22 +00:00
|
|
|
for _, input := range r.Inputs {
|
|
|
|
var data []byte
|
|
|
|
|
|
|
|
switch input.Type {
|
|
|
|
case "hex":
|
|
|
|
data, err = hex.DecodeString(input.Data)
|
|
|
|
default:
|
|
|
|
data = []byte(input.Data)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not write request to server")
|
2020-12-30 11:17:22 +00:00
|
|
|
}
|
|
|
|
reqBuilder.Grow(len(input.Data))
|
|
|
|
reqBuilder.WriteString(input.Data)
|
|
|
|
|
|
|
|
_, err = conn.Write(data)
|
|
|
|
if err != nil {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not write request to server")
|
2020-12-30 11:17:22 +00:00
|
|
|
}
|
2021-02-16 09:16:23 +00:00
|
|
|
|
2021-02-16 09:48:57 +00:00
|
|
|
if input.Read > 0 {
|
2021-02-26 20:53:06 +00:00
|
|
|
buffer := make([]byte, input.Read)
|
2021-02-16 09:43:01 +00:00
|
|
|
n, _ := conn.Read(buffer)
|
|
|
|
responseBuilder.Write(buffer[:n])
|
2021-02-24 14:41:21 +00:00
|
|
|
if input.Name != "" {
|
|
|
|
inputEvents[input.Name] = string(buffer[:n])
|
|
|
|
}
|
2021-02-16 09:16:23 +00:00
|
|
|
}
|
2020-12-30 11:17:22 +00:00
|
|
|
r.options.Progress.IncrementRequests()
|
|
|
|
}
|
2020-12-30 09:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
2021-01-01 14:06:21 +00:00
|
|
|
return errors.Wrap(err, "could not write request to server")
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 11:48:08 +00:00
|
|
|
if r.options.Options.Debug || r.options.Options.DebugRequests {
|
2020-12-30 09:24:20 +00:00
|
|
|
gologger.Info().Str("address", actualAddress).Msgf("[%s] Dumped Network request for %s", r.options.TemplateID, actualAddress)
|
2021-01-16 19:21:43 +00:00
|
|
|
gologger.Print().Msgf("%s", reqBuilder.String())
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 11:17:22 +00:00
|
|
|
r.options.Output.Request(r.options.TemplateID, actualAddress, "network", err)
|
2021-01-11 14:29:12 +00:00
|
|
|
gologger.Verbose().Msgf("Sent TCP request to %s", actualAddress)
|
2020-12-30 11:17:22 +00:00
|
|
|
|
2020-12-30 09:24:20 +00:00
|
|
|
bufferSize := 1024
|
|
|
|
if r.ReadSize != 0 {
|
|
|
|
bufferSize = r.ReadSize
|
|
|
|
}
|
2021-02-16 09:48:57 +00:00
|
|
|
final := make([]byte, bufferSize)
|
2021-02-24 06:37:16 +00:00
|
|
|
n, err := conn.Read(final)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
r.options.Output.Request(r.options.TemplateID, address, "network", err)
|
|
|
|
r.options.Progress.DecrementRequests(1)
|
|
|
|
return errors.Wrap(err, "could not read from server")
|
|
|
|
}
|
2021-02-16 09:48:57 +00:00
|
|
|
responseBuilder.Write(final[:n])
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-01-12 11:48:08 +00:00
|
|
|
if r.options.Options.Debug || r.options.Options.DebugResponse {
|
2020-12-30 09:24:20 +00:00
|
|
|
gologger.Debug().Msgf("[%s] Dumped Network response for %s", r.options.TemplateID, actualAddress)
|
2021-02-16 09:43:01 +00:00
|
|
|
gologger.Print().Msgf("%s", responseBuilder.String())
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
2021-02-16 09:48:57 +00:00
|
|
|
outputEvent := r.responseToDSLMap(reqBuilder.String(), string(final[:n]), responseBuilder.String(), input, actualAddress)
|
2021-01-16 06:36:27 +00:00
|
|
|
outputEvent["ip"] = r.dialer.GetDialedIP(hostname)
|
2021-01-16 08:40:24 +00:00
|
|
|
for k, v := range previous {
|
|
|
|
outputEvent[k] = v
|
|
|
|
}
|
2021-02-24 14:41:21 +00:00
|
|
|
for k, v := range inputEvents {
|
|
|
|
outputEvent[k] = v
|
|
|
|
}
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-01-16 06:36:27 +00:00
|
|
|
event := &output.InternalWrappedEvent{InternalEvent: outputEvent}
|
2020-12-30 09:24:20 +00:00
|
|
|
if r.CompiledOperators != nil {
|
2021-01-16 06:36:27 +00:00
|
|
|
result, ok := r.CompiledOperators.Execute(outputEvent, r.Match, r.Extract)
|
2021-01-13 06:48:56 +00:00
|
|
|
if ok && result != nil {
|
|
|
|
event.OperatorsResult = result
|
|
|
|
event.Results = r.MakeResultEvent(event)
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
callback(event)
|
|
|
|
return nil
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|