Separating matchers, extractors and requests as protocols and operators

dev
Ice3man543 2020-12-21 14:31:32 +05:30
parent e6958d7aea
commit d631074e35
16 changed files with 141 additions and 6 deletions

View File

@ -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
}

View File

@ -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
}

107
v2/pkg/protocols/dns/dns.go Normal file
View File

@ -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)
}

View File

@ -0,0 +1 @@
package http

View File

@ -0,0 +1,5 @@
package protocols
// Protocol is an interface implemented by a protocol to be templated.
type Protocol interface {
}

1
v2/pkg/types/types.go Normal file
View File

@ -0,0 +1 @@
package types

View File

@ -229,6 +229,5 @@ func iterableToMapString(t tengo.Object) map[string]string {
}
}
}
return m
}