2020-12-30 09:24:20 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-30 11:17:22 +00:00
|
|
|
"encoding/hex"
|
2020-12-30 09:24:20 +00:00
|
|
|
"fmt"
|
2020-12-30 15:44:04 +00:00
|
|
|
"net"
|
2020-12-30 09:24:20 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"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-01 14:06:21 +00:00
|
|
|
func (r *Request) ExecuteWithResults(input string, metadata 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 {
|
|
|
|
replacer := replacer.New(map[string]interface{}{"Hostname": address})
|
|
|
|
actualAddress := replacer.Replace(kv.key)
|
|
|
|
if kv.value != "" {
|
|
|
|
if strings.Contains(address, ":") {
|
|
|
|
actualAddress, _, _ = net.SplitHostPort(actualAddress)
|
|
|
|
}
|
|
|
|
actualAddress = net.JoinHostPort(actualAddress, kv.value)
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:06:21 +00:00
|
|
|
err = r.executeAddress(actualAddress, address, input, 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-01-01 14:06:21 +00:00
|
|
|
func (r *Request) executeAddress(actualAddress, address, input string, 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
|
|
|
}
|
|
|
|
|
|
|
|
conn, err := r.dialer.Dial(context.Background(), "tcp", actualAddress)
|
|
|
|
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()
|
|
|
|
conn.SetReadDeadline(time.Now().Add(5 * time.Second))
|
|
|
|
|
2020-12-30 11:17:22 +00:00
|
|
|
reqBuilder := &strings.Builder{}
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if r.options.Options.Debug {
|
|
|
|
gologger.Info().Str("address", actualAddress).Msgf("[%s] Dumped Network request for %s", r.options.TemplateID, actualAddress)
|
2020-12-30 15:44:04 +00:00
|
|
|
|
2020-12-30 11:17:22 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", 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
|
|
|
|
}
|
|
|
|
buffer := make([]byte, bufferSize)
|
|
|
|
n, _ := conn.Read(buffer)
|
|
|
|
resp := string(buffer[:n])
|
|
|
|
|
|
|
|
if r.options.Options.Debug {
|
|
|
|
gologger.Debug().Msgf("[%s] Dumped Network response for %s", r.options.TemplateID, actualAddress)
|
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", resp)
|
|
|
|
}
|
2020-12-30 11:17:22 +00:00
|
|
|
ouputEvent := r.responseToDSLMap(reqBuilder.String(), resp, input, actualAddress)
|
2020-12-30 09:24:20 +00:00
|
|
|
|
2021-01-01 14:06:21 +00:00
|
|
|
event := &output.InternalWrappedEvent{InternalEvent: ouputEvent}
|
2020-12-30 09:24:20 +00:00
|
|
|
if r.CompiledOperators != nil {
|
|
|
|
result, ok := r.Operators.Execute(ouputEvent, r.Match, r.Extract)
|
|
|
|
if !ok {
|
2021-01-01 14:06:21 +00:00
|
|
|
return nil
|
2020-12-30 09:24:20 +00:00
|
|
|
}
|
2021-01-01 14:06:21 +00:00
|
|
|
event.OperatorsResult = result
|
2021-01-01 21:09:27 +00:00
|
|
|
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
|
|
|
|
}
|