2023-09-16 16:02:17 +05:30
|
|
|
package net
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"encoding/hex"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"time"
|
|
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2023-11-02 13:33:40 +05:30
|
|
|
errorutil "github.com/projectdiscovery/utils/errors"
|
|
|
|
|
"github.com/projectdiscovery/utils/reader"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
defaultTimeout = time.Duration(5) * time.Second
|
2023-09-16 16:02:17 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Open opens a new connection to the address with a timeout.
|
|
|
|
|
// supported protocols: tcp, udp
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func Open(ctx context.Context, protocol, address string) (*NetConn, error) {
|
|
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
dialer := protocolstate.GetDialersWithId(executionId)
|
|
|
|
|
if dialer == nil {
|
|
|
|
|
return nil, fmt.Errorf("dialers not initialized for %s", executionId)
|
|
|
|
|
}
|
|
|
|
|
conn, err := dialer.Fastdialer.Dial(ctx, protocol, address)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
return &NetConn{conn: conn, timeout: defaultTimeout}, nil
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open opens a new connection to the address with a timeout.
|
|
|
|
|
// supported protocols: tcp, udp
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.OpenTLS('tcp', 'acme.com:443');
|
|
|
|
|
// ```
|
2025-07-09 14:47:26 -05:00
|
|
|
func OpenTLS(ctx context.Context, protocol, address string) (*NetConn, error) {
|
2023-11-02 13:33:40 +05:30
|
|
|
config := &tls.Config{InsecureSkipVerify: true, MinVersion: tls.VersionTLS10}
|
2023-09-16 16:02:17 +05:30
|
|
|
host, _, _ := net.SplitHostPort(address)
|
|
|
|
|
if host != "" {
|
|
|
|
|
c := config.Clone()
|
|
|
|
|
c.ServerName = host
|
|
|
|
|
config = c
|
|
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
executionId := ctx.Value("executionId").(string)
|
|
|
|
|
dialer := protocolstate.GetDialersWithId(executionId)
|
|
|
|
|
if dialer == nil {
|
|
|
|
|
return nil, fmt.Errorf("dialers not initialized for %s", executionId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := dialer.Fastdialer.DialTLSWithConfig(ctx, protocol, address, config)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
return &NetConn{conn: conn, timeout: defaultTimeout}, nil
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
2024-02-07 21:45:40 +05:30
|
|
|
type (
|
|
|
|
|
// NetConn is a connection to a remote host.
|
|
|
|
|
// this is returned/create by Open and OpenTLS functions.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// ```
|
|
|
|
|
NetConn struct {
|
|
|
|
|
conn net.Conn
|
|
|
|
|
timeout time.Duration
|
|
|
|
|
}
|
|
|
|
|
)
|
2023-09-16 16:02:17 +05:30
|
|
|
|
|
|
|
|
// Close closes the connection.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// conn.Close();
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) Close() error {
|
|
|
|
|
err := c.conn.Close()
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SetTimeout sets read/write timeout for the connection (in seconds).
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// conn.SetTimeout(10);
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) SetTimeout(value int) {
|
|
|
|
|
c.timeout = time.Duration(value) * time.Second
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setDeadLine sets read/write deadline for the connection (in seconds).
|
|
|
|
|
// this is intended to be called before every read/write operation.
|
|
|
|
|
func (c *NetConn) setDeadLine() {
|
|
|
|
|
if c.timeout == 0 {
|
|
|
|
|
c.timeout = 5 * time.Second
|
|
|
|
|
}
|
|
|
|
|
_ = c.conn.SetDeadline(time.Now().Add(c.timeout))
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 13:33:40 +05:30
|
|
|
// unsetDeadLine unsets read/write deadline for the connection.
|
|
|
|
|
func (c *NetConn) unsetDeadLine() {
|
|
|
|
|
_ = c.conn.SetDeadline(time.Time{})
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-16 16:02:17 +05:30
|
|
|
// SendArray sends array data to connection
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// conn.SendArray(['hello', 'world']);
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) SendArray(data []interface{}) error {
|
|
|
|
|
c.setDeadLine()
|
2023-11-02 13:33:40 +05:30
|
|
|
defer c.unsetDeadLine()
|
2023-09-16 16:02:17 +05:30
|
|
|
input := types.ToByteSlice(data)
|
|
|
|
|
length, err := c.conn.Write(input)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if length < len(input) {
|
|
|
|
|
return fmt.Errorf("failed to write all bytes (%d bytes written, %d bytes expected)", length, len(input))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SendHex sends hex data to connection
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// conn.SendHex('68656c6c6f');
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) SendHex(data string) error {
|
|
|
|
|
c.setDeadLine()
|
2023-11-02 13:33:40 +05:30
|
|
|
defer c.unsetDeadLine()
|
2023-09-16 16:02:17 +05:30
|
|
|
bin, err := hex.DecodeString(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
length, err := c.conn.Write(bin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if length < len(bin) {
|
|
|
|
|
return fmt.Errorf("failed to write all bytes (%d bytes written, %d bytes expected)", length, len(bin))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send sends data to the connection with a timeout.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// conn.Send('hello');
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) Send(data string) error {
|
|
|
|
|
c.setDeadLine()
|
2023-11-02 13:33:40 +05:30
|
|
|
defer c.unsetDeadLine()
|
2023-09-16 16:02:17 +05:30
|
|
|
bin := []byte(data)
|
|
|
|
|
length, err := c.conn.Write(bin)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if length < len(bin) {
|
|
|
|
|
return fmt.Errorf("failed to write all bytes (%d bytes written, %d bytes expected)", length, len(data))
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
// RecvFull receives data from the connection with a timeout.
|
2023-11-02 13:33:40 +05:30
|
|
|
// If N is 0, it will read all data sent by the server with 8MB limit.
|
2024-02-07 21:45:40 +05:30
|
|
|
// it tries to read until N bytes or timeout is reached.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const data = conn.RecvFull(1024);
|
2024-02-07 21:45:40 +05:30
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
func (c *NetConn) RecvFull(N int) ([]byte, error) {
|
2023-09-16 16:02:17 +05:30
|
|
|
c.setDeadLine()
|
2023-11-02 13:33:40 +05:30
|
|
|
defer c.unsetDeadLine()
|
|
|
|
|
if N == 0 {
|
|
|
|
|
// in utils we use -1 to indicate read all rather than 0
|
|
|
|
|
N = -1
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
bin, err := reader.ConnReadNWithTimeout(c.conn, int64(N), c.timeout)
|
2023-09-16 16:02:17 +05:30
|
|
|
if err != nil {
|
2023-11-02 13:33:40 +05:30
|
|
|
return []byte{}, errorutil.NewWithErr(err).Msgf("failed to read %d bytes", N)
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
2023-11-02 13:33:40 +05:30
|
|
|
return bin, nil
|
2023-09-16 16:02:17 +05:30
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
// Recv is similar to RecvFull but does not guarantee full read instead
|
2024-02-07 21:45:40 +05:30
|
|
|
// it creates a buffer of N bytes and returns whatever is returned by the connection
|
2024-03-01 16:38:56 +05:30
|
|
|
// for reading headers or initial bytes from the server this is usually used.
|
|
|
|
|
// for reading a fixed number of already known bytes (ex: body based on content-length) use RecvFull.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const data = conn.Recv(1024);
|
2024-02-07 21:45:40 +05:30
|
|
|
// log(`Received ${data.length} bytes from the server`)
|
|
|
|
|
// ```
|
2024-03-01 16:38:56 +05:30
|
|
|
func (c *NetConn) Recv(N int) ([]byte, error) {
|
2024-02-07 21:45:40 +05:30
|
|
|
c.setDeadLine()
|
|
|
|
|
defer c.unsetDeadLine()
|
|
|
|
|
if N == 0 {
|
|
|
|
|
N = 4096
|
|
|
|
|
}
|
|
|
|
|
b := make([]byte, N)
|
|
|
|
|
n, err := c.conn.Read(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return []byte{}, errorutil.NewWithErr(err).Msgf("failed to read %d bytes", N)
|
|
|
|
|
}
|
|
|
|
|
return b[:n], nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
// RecvFullString receives data from the connection with a timeout
|
2023-09-16 16:02:17 +05:30
|
|
|
// output is returned as a string.
|
2023-11-02 13:33:40 +05:30
|
|
|
// If N is 0, it will read all data sent by the server with 8MB limit.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
2024-03-01 16:38:56 +05:30
|
|
|
// const data = conn.RecvFullString(1024);
|
|
|
|
|
// ```
|
|
|
|
|
func (c *NetConn) RecvFullString(N int) (string, error) {
|
|
|
|
|
bin, err := c.RecvFull(N)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return string(bin), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RecvString is similar to RecvFullString but does not guarantee full read, instead
|
|
|
|
|
// it creates a buffer of N bytes and returns whatever is returned by the connection
|
|
|
|
|
// for reading headers or initial bytes from the server this is usually used.
|
|
|
|
|
// for reading a fixed number of already known bytes (ex: body based on content-length) use RecvFullString.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
2024-02-07 21:45:40 +05:30
|
|
|
// const data = conn.RecvString(1024);
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) RecvString(N int) (string, error) {
|
|
|
|
|
bin, err := c.Recv(N)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return string(bin), nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-01 16:38:56 +05:30
|
|
|
// RecvFullHex receives data from the connection with a timeout
|
2023-09-16 16:02:17 +05:30
|
|
|
// in hex format.
|
2023-11-02 13:33:40 +05:30
|
|
|
// If N is 0,it will read all data sent by the server with 8MB limit.
|
2024-03-01 16:38:56 +05:30
|
|
|
// until N bytes or timeout is reached.
|
|
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// const data = conn.RecvFullHex(1024);
|
|
|
|
|
// ```
|
|
|
|
|
func (c *NetConn) RecvFullHex(N int) (string, error) {
|
|
|
|
|
bin, err := c.RecvFull(N)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return hex.Dump(bin), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RecvHex is similar to RecvFullHex but does not guarantee full read instead
|
|
|
|
|
// it creates a buffer of N bytes and returns whatever is returned by the connection
|
|
|
|
|
// for reading headers or initial bytes from the server this is usually used.
|
|
|
|
|
// for reading a fixed number of already known bytes (ex: body based on content-length) use RecvFull.
|
2024-02-07 21:45:40 +05:30
|
|
|
// @example
|
|
|
|
|
// ```javascript
|
|
|
|
|
// const net = require('nuclei/net');
|
|
|
|
|
// const conn = net.Open('tcp', 'acme.com:80');
|
|
|
|
|
// const data = conn.RecvHex(1024);
|
|
|
|
|
// ```
|
2023-09-16 16:02:17 +05:30
|
|
|
func (c *NetConn) RecvHex(N int) (string, error) {
|
|
|
|
|
bin, err := c.Recv(N)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
return hex.Dump(bin), nil
|
|
|
|
|
}
|