181 lines
6.2 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"
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"
"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:"-"`
// cache any variables that may be needed for operation.
dialer *fastdialer.Dialer
options *protocols.ExecuterOptions
}
// Compile compiles the request generators preparing any requests possible.
func (r *Request) Compile(options *protocols.ExecuterOptions) error {
r.options = options
client, err := networkclientpool.Get(options.Options, &networkclientpool.Configuration{})
if err != nil {
return errors.Wrap(err, "could not get network client")
}
r.dialer = client
if len(r.Matchers) > 0 || len(r.Extractors) > 0 {
compiled := &r.Operators
if err := compiled.Compile(); err != nil {
return errors.Wrap(err, "could not compile operators")
}
r.CompiledOperators = compiled
}
return nil
}
// Requests returns the total number of requests the rule will perform
func (r *Request) Requests() int {
return 1
}
// GetID returns the ID for the request if any.
func (r *Request) GetID() string {
return ""
}
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
func (r *Request) ExecuteWithResults(input string, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
address, err := getAddress(input)
if err != nil {
return nil
}
hostname, _, _ := net.SplitHostPort(address)
2021-09-22 22:41:07 +05:30
config := &tls.Config{InsecureSkipVerify: true, ServerName: hostname}
2021-11-01 18:02:45 +05:30
2021-09-22 22:41:07 +05:30
conn, err := r.dialer.DialTLSWithConfig(context.Background(), "tcp", address, config)
if err != nil {
r.options.Output.Request(r.options.TemplateID, input, "ssl", err)
r.options.Progress.IncrementFailedRequestsBy(1)
return errors.Wrap(err, "could not connect to server")
}
defer conn.Close()
_ = conn.SetReadDeadline(time.Now().Add(time.Duration(r.options.Options.Timeout) * time.Second))
connTLS, ok := conn.(*tls.Conn)
if !ok {
return nil
}
r.options.Output.Request(r.options.TemplateID, address, "ssl", err)
gologger.Verbose().Msgf("Sent SSL request to %s", address)
2021-11-01 18:02:45 +05:30
if r.options.Options.Debug || r.options.Options.DebugRequests {
gologger.Info().Str("address", input).Msgf("[%s] Dumped SSL request for %s", r.options.TemplateID, input)
}
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["response"] = jsonDataString
2021-09-22 22:41:07 +05:30
data["host"] = input
data["not_after"] = float64(cert.NotAfter.Unix())
2021-09-22 22:41:07 +05:30
data["ip"] = r.dialer.GetDialedIP(hostname)
event := eventcreator.CreateEvent(r, data, r.options.Options.Debug || r.options.Options.DebugResponse)
2021-11-01 18:02:45 +05:30
if r.options.Options.Debug || r.options.Options.DebugResponse {
responseOutput := jsonDataString
gologger.Debug().Msgf("[%s] Dumped SSL response for %s", r.options.TemplateID, input)
gologger.Print().Msgf("%s", responsehighlighter.Highlight(event.OperatorsResult, responseOutput, r.options.Options.NoColor))
}
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)
if parsed.Scheme == "https" && port == "" {
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
func (r *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
return protocols.MakeDefaultMatchFunc(data, matcher)
}
// Extract performs extracting operation for an extractor on model and returns true or false.
func (r *Request) Extract(data map[string]interface{}, matcher *extractors.Extractor) map[string]struct{} {
return protocols.MakeDefaultExtractFunc(data, matcher)
}
// MakeResultEvent creates a result event from internal wrapped event
func (r *Request) MakeResultEvent(wrapped *output.InternalWrappedEvent) []*output.ResultEvent {
return protocols.MakeDefaultResultEvent(r, wrapped)
}
// GetCompiledOperators returns a list of the compiled operators
func (r *Request) GetCompiledOperators() []*operators.Operators {
return []*operators.Operators{r.CompiledOperators}
}
func (r *Request) MakeResultEventItem(wrapped *output.InternalWrappedEvent) *output.ResultEvent {
2021-09-22 22:41:07 +05:30
data := &output.ResultEvent{
TemplateID: types.ToString(r.options.TemplateID),
TemplatePath: types.ToString(r.options.TemplatePath),
Info: r.options.TemplateInfo,
Type: "ssl",
Host: types.ToString(wrapped.InternalEvent["host"]),
Matched: types.ToString(wrapped.InternalEvent["host"]),
Metadata: wrapped.OperatorsResult.PayloadValues,
ExtractedResults: wrapped.OperatorsResult.OutputExtracts,
Timestamp: time.Now(),
IP: types.ToString(wrapped.InternalEvent["ip"]),
}
return data
}