124 lines
2.8 KiB
Go
Raw Normal View History

2020-12-22 03:54:55 +05:30
package dsl
import (
2020-10-16 14:18:50 +02:00
"fmt"
"strings"
"github.com/Knetic/govaluate"
"github.com/miekg/dns"
"github.com/projectdiscovery/dsl"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/dns/dnsclientpool"
2020-12-24 12:13:18 +05:30
"github.com/projectdiscovery/nuclei/v2/pkg/types"
sliceutil "github.com/projectdiscovery/utils/slice"
)
var (
HelperFunctions map[string]govaluate.ExpressionFunction
FunctionNames []string
)
2022-03-16 14:12:26 +05:30
2021-11-26 17:14:25 +02:00
func init() {
_ = dsl.AddMultiSignatureHelperFunction("resolve", []string{
"(host string) string",
"(format string) string",
}, func(args ...interface{}) (interface{}, error) {
argCount := len(args)
if argCount == 0 || argCount > 2 {
return nil, dsl.ErrinvalidDslFunction
2021-09-22 22:41:07 +05:30
}
format := "4"
var dnsType uint16
if len(args) > 1 {
format = strings.ToLower(types.ToString(args[1]))
}
switch format {
case "4", "a":
dnsType = dns.TypeA
case "6", "aaaa":
dnsType = dns.TypeAAAA
case "cname":
dnsType = dns.TypeCNAME
case "ns":
dnsType = dns.TypeNS
case "txt":
dnsType = dns.TypeTXT
case "srv":
dnsType = dns.TypeSRV
case "ptr":
dnsType = dns.TypePTR
case "mx":
dnsType = dns.TypeMX
case "soa":
dnsType = dns.TypeSOA
case "caa":
dnsType = dns.TypeCAA
default:
return nil, fmt.Errorf("invalid dns type")
}
2020-12-22 03:54:55 +05:30
err := dnsclientpool.Init(&types.Options{})
if err != nil {
return nil, err
}
dnsClient, err := dnsclientpool.Get(nil, &dnsclientpool.Configuration{})
if err != nil {
return nil, err
}
// query
rawResp, err := dnsClient.Query(types.ToString(args[0]), dnsType)
if err != nil {
return nil, err
}
dnsValues := map[uint16][]string{
dns.TypeA: rawResp.A,
dns.TypeAAAA: rawResp.AAAA,
dns.TypeCNAME: rawResp.CNAME,
dns.TypeNS: rawResp.NS,
dns.TypeTXT: rawResp.TXT,
dns.TypeSRV: rawResp.SRV,
dns.TypePTR: rawResp.PTR,
dns.TypeMX: rawResp.MX,
dns.TypeSOA: rawResp.SOA,
dns.TypeCAA: rawResp.CAA,
}
if values, ok := dnsValues[dnsType]; ok {
firstFound, found := sliceutil.FirstNonZero(values)
if found {
return firstFound, nil
}
}
return "", fmt.Errorf("no records found")
})
dsl.PrintDebugCallback = func(args ...interface{}) error {
gologger.Info().Msgf("print_debug value: %s", fmt.Sprint(args))
return nil
}
HelperFunctions = dsl.HelperFunctions()
FunctionNames = dsl.GetFunctionNames(HelperFunctions)
}
type CompilationError struct {
DslSignature string
WrappedError error
}
func (e *CompilationError) Error() string {
return fmt.Sprintf("could not compile DSL expression %q: %v", e.DslSignature, e.WrappedError)
}
func (e *CompilationError) Unwrap() error {
return e.WrappedError
}
func GetPrintableDslFunctionSignatures(noColor bool) string {
return dsl.GetPrintableDslFunctionSignatures(noColor)
}