From d631074e35d6cbf978669d8b6824d029812b8455 Mon Sep 17 00:00:00 2001 From: Ice3man543 Date: Mon, 21 Dec 2020 14:31:32 +0530 Subject: [PATCH] Separating matchers, extractors and requests as protocols and operators --- v2/pkg/{ => operators}/extractors/compile.go | 0 v2/pkg/{ => operators}/extractors/doc.go | 0 v2/pkg/{ => operators}/extractors/extract.go | 0 .../{ => operators}/extractors/extractors.go | 0 v2/pkg/{ => operators}/matchers/compile.go | 0 v2/pkg/{ => operators}/matchers/doc.go | 0 v2/pkg/{ => operators}/matchers/match.go | 5 - v2/pkg/{ => operators}/matchers/match_test.go | 0 v2/pkg/{ => operators}/matchers/matchers.go | 0 v2/pkg/{ => operators}/matchers/util.go | 0 v2/pkg/operators/operators.go | 27 +++++ v2/pkg/protocols/dns/dns.go | 107 ++++++++++++++++++ v2/pkg/protocols/http/http.go | 1 + v2/pkg/protocols/protocols.go | 5 + v2/pkg/types/types.go | 1 + v2/pkg/workflows/var.go | 1 - 16 files changed, 141 insertions(+), 6 deletions(-) rename v2/pkg/{ => operators}/extractors/compile.go (100%) rename v2/pkg/{ => operators}/extractors/doc.go (100%) rename v2/pkg/{ => operators}/extractors/extract.go (100%) rename v2/pkg/{ => operators}/extractors/extractors.go (100%) rename v2/pkg/{ => operators}/matchers/compile.go (100%) rename v2/pkg/{ => operators}/matchers/doc.go (100%) rename v2/pkg/{ => operators}/matchers/match.go (99%) rename v2/pkg/{ => operators}/matchers/match_test.go (100%) rename v2/pkg/{ => operators}/matchers/matchers.go (100%) rename v2/pkg/{ => operators}/matchers/util.go (100%) create mode 100644 v2/pkg/operators/operators.go create mode 100644 v2/pkg/protocols/dns/dns.go create mode 100644 v2/pkg/protocols/http/http.go create mode 100644 v2/pkg/protocols/protocols.go create mode 100644 v2/pkg/types/types.go diff --git a/v2/pkg/extractors/compile.go b/v2/pkg/operators/extractors/compile.go similarity index 100% rename from v2/pkg/extractors/compile.go rename to v2/pkg/operators/extractors/compile.go diff --git a/v2/pkg/extractors/doc.go b/v2/pkg/operators/extractors/doc.go similarity index 100% rename from v2/pkg/extractors/doc.go rename to v2/pkg/operators/extractors/doc.go diff --git a/v2/pkg/extractors/extract.go b/v2/pkg/operators/extractors/extract.go similarity index 100% rename from v2/pkg/extractors/extract.go rename to v2/pkg/operators/extractors/extract.go diff --git a/v2/pkg/extractors/extractors.go b/v2/pkg/operators/extractors/extractors.go similarity index 100% rename from v2/pkg/extractors/extractors.go rename to v2/pkg/operators/extractors/extractors.go diff --git a/v2/pkg/matchers/compile.go b/v2/pkg/operators/matchers/compile.go similarity index 100% rename from v2/pkg/matchers/compile.go rename to v2/pkg/operators/matchers/compile.go diff --git a/v2/pkg/matchers/doc.go b/v2/pkg/operators/matchers/doc.go similarity index 100% rename from v2/pkg/matchers/doc.go rename to v2/pkg/operators/matchers/doc.go diff --git a/v2/pkg/matchers/match.go b/v2/pkg/operators/matchers/match.go similarity index 99% rename from v2/pkg/matchers/match.go rename to v2/pkg/operators/matchers/match.go index a2a25840..3584d8d9 100644 --- a/v2/pkg/matchers/match.go +++ b/v2/pkg/operators/matchers/match.go @@ -105,7 +105,6 @@ func (m *Matcher) matchSizeCode(length int) bool { // Return on the first match. return true } - return false } @@ -134,7 +133,6 @@ func (m *Matcher) matchWords(corpus string) bool { return true } } - return false } @@ -163,7 +161,6 @@ func (m *Matcher) matchRegex(corpus string) bool { return true } } - return false } @@ -193,7 +190,6 @@ func (m *Matcher) matchBinary(corpus string) bool { return true } } - return false } @@ -230,6 +226,5 @@ func (m *Matcher) matchDSL(mp map[string]interface{}) bool { return true } } - return false } diff --git a/v2/pkg/matchers/match_test.go b/v2/pkg/operators/matchers/match_test.go similarity index 100% rename from v2/pkg/matchers/match_test.go rename to v2/pkg/operators/matchers/match_test.go diff --git a/v2/pkg/matchers/matchers.go b/v2/pkg/operators/matchers/matchers.go similarity index 100% rename from v2/pkg/matchers/matchers.go rename to v2/pkg/operators/matchers/matchers.go diff --git a/v2/pkg/matchers/util.go b/v2/pkg/operators/matchers/util.go similarity index 100% rename from v2/pkg/matchers/util.go rename to v2/pkg/operators/matchers/util.go diff --git a/v2/pkg/operators/operators.go b/v2/pkg/operators/operators.go new file mode 100644 index 00000000..830830e9 --- /dev/null +++ b/v2/pkg/operators/operators.go @@ -0,0 +1,27 @@ +package operators + +import ( + "github.com/projectdiscovery/nuclei/v2/pkg/operators/extractors" + "github.com/projectdiscovery/nuclei/v2/pkg/operators/matchers" +) + +// Operators contains the operators that can be applied on protocols +type Operators struct { + // Matchers contains the detection mechanism for the request to identify + // whether the request was successful + Matchers []*matchers.Matcher `yaml:"matchers"` + // Extractors contains the extraction mechanism for the request to identify + // and extract parts of the response. + Extractors []*extractors.Extractor `yaml:"extractors"` + // MatchersCondition is the condition of the matchers + // whether to use AND or OR. Default is OR. + MatchersCondition string `yaml:"matchers-condition"` + + // cached variables that may be used along with request. + matchersCondition matchers.ConditionType +} + +// GetMatchersCondition returns the condition for the matchers +func (r *Operators) GetMatchersCondition() matchers.ConditionType { + return r.matchersCondition +} diff --git a/v2/pkg/protocols/dns/dns.go b/v2/pkg/protocols/dns/dns.go new file mode 100644 index 00000000..91cda772 --- /dev/null +++ b/v2/pkg/protocols/dns/dns.go @@ -0,0 +1,107 @@ +package dns + +import ( + "strings" + + "github.com/miekg/dns" +) + +// Request contains a DNS protocol request to be made from a template +type Request struct { + // Recursion specifies whether to recurse all the answers. + Recursion bool `yaml:"recursion"` + // Path contains the path/s for the request + Name string `yaml:"name"` + // Type is the type of DNS request to make + Type string `yaml:"type"` + // Class is the class of the DNS request + Class string `yaml:"class"` + // Retries is the number of retries for the DNS request + Retries int `yaml:"retries"` + // Raw contains a raw request + Raw string `yaml:"raw,omitempty"` + + // cache any variables that may be needed for operation. + class uint16 + questionType uint16 +} + +// Compile compiles the protocol request for further execution. +func (r *Request) Compile() error { + r.class = classToInt(r.Class) + r.questionType = questionTypeToInt(r.Type) + return nil +} + +// Requests returns the total number of requests the YAML rule will perform +func (r *Request) Requests() int64 { + return 1 +} + +// Make returns the request to be sent for the protocol +func (r *Request) Make(domain string) (*dns.Msg, error) { + domain = dns.Fqdn(domain) + + // Build a request on the specified URL + req := new(dns.Msg) + req.Id = dns.Id() + req.RecursionDesired = r.Recursion + + var q dns.Question + + replacer := newReplacer(map[string]interface{}{"FQDN": domain}) + + q.Name = dns.Fqdn(replacer.Replace(r.Name)) + q.Qclass = classToInt(r.Class) + q.Qtype = questionTypeToInt(r.Type) + req.Question = append(req.Question, q) + return req, nil +} + +// questionTypeToInt converts DNS question type to internal representation +func questionTypeToInt(Type string) uint16 { + Type = strings.TrimSpace(strings.ToUpper(Type)) + question := dns.TypeA + + switch Type { + case "A": + question = dns.TypeA + case "NS": + question = dns.TypeNS + case "CNAME": + question = dns.TypeCNAME + case "SOA": + question = dns.TypeSOA + case "PTR": + question = dns.TypePTR + case "MX": + question = dns.TypeMX + case "TXT": + question = dns.TypeTXT + case "AAAA": + question = dns.TypeAAAA + } + return uint16(question) +} + +// classToInt converts a dns class name to it's internal representation +func classToInt(class string) uint16 { + class = strings.TrimSpace(strings.ToUpper(class)) + result := dns.ClassINET + + switch class { + case "INET": + result = dns.ClassINET + case "CSNET": + result = dns.ClassCSNET + case "CHAOS": + result = dns.ClassCHAOS + case "HESIOD": + result = dns.ClassHESIOD + case "NONE": + result = dns.ClassNONE + case "ANY": + result = dns.ClassANY + } + return uint16(result) +} diff --git a/v2/pkg/protocols/http/http.go b/v2/pkg/protocols/http/http.go new file mode 100644 index 00000000..d02cfda6 --- /dev/null +++ b/v2/pkg/protocols/http/http.go @@ -0,0 +1 @@ +package http diff --git a/v2/pkg/protocols/protocols.go b/v2/pkg/protocols/protocols.go new file mode 100644 index 00000000..c3a0d532 --- /dev/null +++ b/v2/pkg/protocols/protocols.go @@ -0,0 +1,5 @@ +package protocols + +// Protocol is an interface implemented by a protocol to be templated. +type Protocol interface { +} diff --git a/v2/pkg/types/types.go b/v2/pkg/types/types.go new file mode 100644 index 00000000..ab1254f4 --- /dev/null +++ b/v2/pkg/types/types.go @@ -0,0 +1 @@ +package types diff --git a/v2/pkg/workflows/var.go b/v2/pkg/workflows/var.go index 02765564..5d304721 100644 --- a/v2/pkg/workflows/var.go +++ b/v2/pkg/workflows/var.go @@ -229,6 +229,5 @@ func iterableToMapString(t tengo.Object) map[string]string { } } } - return m }