210 lines
7.6 KiB
Go
Raw Normal View History

2021-09-22 22:41:07 +05:30
package ssl
import (
"context"
"crypto/tls"
"net"
"net/url"
"strings"
"time"
2021-11-01 18:02:45 +05:30
jsoniter "github.com/json-iterator/go"
2021-09-22 22:41:07 +05:30
"github.com/pkg/errors"
2021-11-01 18:02:45 +05:30
"github.com/projectdiscovery/cryptoutil"
2021-09-22 22:41:07 +05:30
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/gologger"
2021-09-22 22:41:07 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/operators"
2021-10-29 18:26:06 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors"
"github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers"
2021-09-22 22:41:07 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/expressions"
2021-10-29 18:26:06 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/eventcreator"
2021-11-01 18:02:45 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/helpers/responsehighlighter"
2021-09-22 22:41:07 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/network/networkclientpool"
templateTypes "github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
2021-09-22 22:41:07 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/types"
)
// Request is a request for the SSL protocol
type Request struct {
// Operators for the current request go here.
operators.Operators `yaml:",inline,omitempty"`
CompiledOperators *operators.Operators `yaml:"-"`
// description: |
// Address contains address for the request
Address string `yaml:"address,omitempty" jsonschema:"title=address for the ssl request,description=Address contains address for the request"`
2021-09-22 22:41:07 +05:30
// cache any variables that may be needed for operation.
dialer *fastdialer.Dialer
options *protocols.ExecuterOptions
}
// Compile compiles the request generators preparing any requests possible.
2021-11-03 02:34:48 +05:30
func (request *Request) Compile(options *protocols.ExecuterOptions) error {
request.options = options
2021-09-22 22:41:07 +05:30
client, err := networkclientpool.Get(options.Options, &networkclientpool.Configuration{})
if err != nil {
return errors.Wrap(err, "could not get network client")
}
2021-11-03 02:34:48 +05:30
request.dialer = client
2021-09-22 22:41:07 +05:30
2021-11-03 02:34:48 +05:30
if len(request.Matchers) > 0 || len(request.Extractors) > 0 {
compiled := &request.Operators
2021-09-22 22:41:07 +05:30
if err := compiled.Compile(); err != nil {
return errors.Wrap(err, "could not compile operators")
}
2021-11-03 02:34:48 +05:30
request.CompiledOperators = compiled
2021-09-22 22:41:07 +05:30
}
return nil
}
// Requests returns the total number of requests the rule will perform
2021-11-03 02:34:48 +05:30
func (request *Request) Requests() int {
2021-09-22 22:41:07 +05:30
return 1
}
// GetID returns the ID for the request if any.
2021-11-03 02:34:48 +05:30
func (request *Request) GetID() string {
2021-09-22 22:41:07 +05:30
return ""
}
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
2021-11-03 02:34:48 +05:30
func (request *Request) ExecuteWithResults(input string, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
2021-09-22 22:41:07 +05:30
address, err := getAddress(input)
if err != nil {
return nil
}
hostname, port, _ := net.SplitHostPort(address)
2021-09-22 22:41:07 +05:30
requestOptions := request.options
payloadValues := make(map[string]interface{})
for k, v := range dynamicValues {
payloadValues[k] = v
}
payloadValues["Hostname"] = address
payloadValues["Host"] = hostname
payloadValues["Port"] = port
finalAddress, dataErr := expressions.EvaluateByte([]byte(request.Address), payloadValues)
if dataErr != nil {
requestOptions.Output.Request(requestOptions.TemplateID, input, request.Type().String(), dataErr)
requestOptions.Progress.IncrementFailedRequestsBy(1)
return errors.Wrap(dataErr, "could not evaluate template expressions")
}
addressToDial := string(finalAddress)
2021-09-22 22:41:07 +05:30
config := &tls.Config{InsecureSkipVerify: true, ServerName: hostname}
2021-11-01 18:02:45 +05:30
conn, err := request.dialer.DialTLSWithConfig(context.Background(), "tcp", addressToDial, config)
2021-09-22 22:41:07 +05:30
if err != nil {
requestOptions.Output.Request(requestOptions.TemplateID, input, request.Type().String(), err)
requestOptions.Progress.IncrementFailedRequestsBy(1)
2021-09-22 22:41:07 +05:30
return errors.Wrap(err, "could not connect to server")
}
defer conn.Close()
_ = conn.SetReadDeadline(time.Now().Add(time.Duration(requestOptions.Options.Timeout) * time.Second))
2021-09-22 22:41:07 +05:30
connTLS, ok := conn.(*tls.Conn)
if !ok {
return nil
}
requestOptions.Output.Request(requestOptions.TemplateID, address, request.Type().String(), err)
gologger.Verbose().Msgf("Sent SSL request to %s", address)
if requestOptions.Options.Debug || requestOptions.Options.DebugRequests {
gologger.Debug().Str("address", input).Msgf("[%s] Dumped SSL request for %s", requestOptions.TemplateID, input)
2021-11-01 18:02:45 +05:30
}
state := connTLS.ConnectionState()
if len(state.PeerCertificates) == 0 {
2021-09-22 22:41:07 +05:30
return nil
}
2021-11-01 18:02:45 +05:30
tlsData := cryptoutil.TLSGrab(&state)
jsonData, _ := jsoniter.Marshal(tlsData)
jsonDataString := string(jsonData)
2021-09-22 22:41:07 +05:30
data := make(map[string]interface{})
cert := connTLS.ConnectionState().PeerCertificates[0]
2021-11-01 18:02:45 +05:30
data["type"] = request.Type().String()
2021-11-01 18:02:45 +05:30
data["response"] = jsonDataString
2021-09-22 22:41:07 +05:30
data["host"] = input
data["matched"] = addressToDial
data["not_after"] = float64(cert.NotAfter.Unix())
2021-11-03 02:34:48 +05:30
data["ip"] = request.dialer.GetDialedIP(hostname)
2021-09-22 22:41:07 +05:30
event := eventcreator.CreateEvent(request, data, requestOptions.Options.Debug || requestOptions.Options.DebugResponse)
if requestOptions.Options.Debug || requestOptions.Options.DebugResponse {
gologger.Debug().Msgf("[%s] Dumped SSL response for %s", requestOptions.TemplateID, input)
gologger.Print().Msgf("%s", responsehighlighter.Highlight(event.OperatorsResult, jsonDataString, requestOptions.Options.NoColor, false))
2021-11-01 18:02:45 +05:30
}
2021-10-29 18:26:06 +05:30
callback(event)
2021-09-22 22:41:07 +05:30
return nil
}
// 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
}
_, port, _ := net.SplitHostPort(parsed.Host)
2021-11-03 18:58:00 +05:30
if strings.ToLower(parsed.Scheme) == "https" && port == "" {
2021-09-22 22:41:07 +05:30
toTest = net.JoinHostPort(parsed.Host, "443")
} else {
toTest = parsed.Host
}
return toTest, nil
2021-09-22 22:41:07 +05:30
}
return toTest, nil
}
2021-10-29 18:26:06 +05:30
// Match performs matching operation for a matcher on model and returns:
// true and a list of matched snippets if the matcher type is supports it
// otherwise false and an empty string slice
2021-11-03 02:34:48 +05:30
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
2021-10-29 18:26:06 +05:30
return protocols.MakeDefaultMatchFunc(data, matcher)
}
// Extract performs extracting operation for an extractor on model and returns true or false.
2021-11-03 02:34:48 +05:30
func (request *Request) Extract(data map[string]interface{}, matcher *extractors.Extractor) map[string]struct{} {
2021-10-29 18:26:06 +05:30
return protocols.MakeDefaultExtractFunc(data, matcher)
}
// MakeResultEvent creates a result event from internal wrapped event
2021-11-03 02:34:48 +05:30
func (request *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
return protocols.MakeDefaultResultEvent(request, wrapped)
2021-10-29 18:26:06 +05:30
}
// GetCompiledOperators returns a list of the compiled operators
2021-11-03 02:34:48 +05:30
func (request *Request) GetCompiledOperators() []*operators.Operators {
return []*operators.Operators{request.CompiledOperators}
2021-10-29 18:26:06 +05:30
}
// Type returns the type of the protocol request
func (request *Request) Type() templateTypes.ProtocolType {
return templateTypes.SSLProtocol
}
2021-11-03 02:34:48 +05:30
func (request *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
2021-09-22 22:41:07 +05:30
data := &output.ResultEvent{
2021-11-03 02:34:48 +05:30
TemplateID: types.ToString(request.options.TemplateID),
TemplatePath: types.ToString(request.options.TemplatePath),
Info: request.options.TemplateInfo,
Type: types.ToString(wrapped.InternalEvent["type"]),
2021-09-22 22:41:07 +05:30
Host: types.ToString(wrapped.InternalEvent["host"]),
Matched: types.ToString(wrapped.InternalEvent["host"]),
Metadata: wrapped.OperatorsResult.PayloadValues,
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
Timestamp: time.Now(),
MatcherStatus: true,
2021-09-22 22:41:07 +05:30
IP: types.ToString(wrapped.InternalEvent["ip"]),
}
return data
}