2020-12-22 03:54:55 +05:30
|
|
|
package dsl
|
2020-05-04 23:24:59 +02:00
|
|
|
|
|
|
|
|
import (
|
2020-10-16 14:18:50 +02:00
|
|
|
"fmt"
|
2020-05-04 23:24:59 +02:00
|
|
|
"strings"
|
2022-03-10 10:57:59 +01:00
|
|
|
|
2020-05-04 23:24:59 +02:00
|
|
|
"github.com/Knetic/govaluate"
|
2023-03-03 23:51:24 +01:00
|
|
|
"github.com/miekg/dns"
|
2023-03-27 18:48:50 +05:30
|
|
|
"github.com/projectdiscovery/dsl"
|
2023-03-28 23:12:08 +02:00
|
|
|
"github.com/projectdiscovery/gologger"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/dns/dnsclientpool"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
2023-03-03 23:51:24 +01:00
|
|
|
sliceutil "github.com/projectdiscovery/utils/slice"
|
2023-09-16 16:02:17 +05:30
|
|
|
stringsutil "github.com/projectdiscovery/utils/strings"
|
2020-05-04 23:24:59 +02:00
|
|
|
)
|
|
|
|
|
|
2022-08-23 13:16:41 +05:30
|
|
|
var (
|
|
|
|
|
HelperFunctions map[string]govaluate.ExpressionFunction
|
2023-03-27 18:48:50 +05:30
|
|
|
FunctionNames []string
|
2023-09-16 16:02:17 +05:30
|
|
|
// knownPorts is a list of known ports for protocols implemented in nuclei
|
|
|
|
|
knowPorts = []string{"80", "443", "8080", "8081", "8443", "53"}
|
2023-02-25 23:24:46 +01:00
|
|
|
)
|
2022-03-16 14:12:26 +05:30
|
|
|
|
2021-11-26 17:14:25 +02:00
|
|
|
func init() {
|
2023-08-07 23:50:16 +05:30
|
|
|
_ = dsl.AddFunction(dsl.NewWithMultipleSignatures("resolve", []string{
|
2023-03-27 18:48:50 +05:30
|
|
|
"(host string) string",
|
|
|
|
|
"(format string) string",
|
2023-08-07 23:50:16 +05:30
|
|
|
}, false, func(args ...interface{}) (interface{}, error) {
|
2023-03-27 18:48:50 +05:30
|
|
|
argCount := len(args)
|
|
|
|
|
if argCount == 0 || argCount > 2 {
|
2023-05-02 16:22:41 +05:30
|
|
|
return nil, dsl.ErrInvalidDslFunction
|
2021-09-22 22:41:07 +05:30
|
|
|
}
|
2023-03-27 18:48:50 +05:30
|
|
|
format := "4"
|
|
|
|
|
var dnsType uint16
|
|
|
|
|
if len(args) > 1 {
|
|
|
|
|
format = strings.ToLower(types.ToString(args[1]))
|
2021-07-18 05:35:06 +05:30
|
|
|
}
|
2021-12-09 10:32:01 +02:00
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
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")
|
2021-12-07 17:34:36 +02:00
|
|
|
}
|
2020-12-22 03:54:55 +05:30
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
err := dnsclientpool.Init(&types.Options{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-12-07 17:34:36 +02:00
|
|
|
}
|
2023-03-27 18:48:50 +05:30
|
|
|
dnsClient, err := dnsclientpool.Get(nil, &dnsclientpool.Configuration{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-12-07 17:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
// query
|
|
|
|
|
rawResp, err := dnsClient.Query(types.ToString(args[0]), dnsType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-12-07 17:34:36 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
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.TypeCAA: rawResp.CAA,
|
2023-06-08 11:35:53 +05:30
|
|
|
dns.TypeSOA: rawResp.GetSOARecords(),
|
2022-06-08 17:43:52 +03:00
|
|
|
}
|
|
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
if values, ok := dnsValues[dnsType]; ok {
|
|
|
|
|
firstFound, found := sliceutil.FirstNonZero(values)
|
|
|
|
|
if found {
|
|
|
|
|
return firstFound, nil
|
2022-06-08 17:43:52 +03:00
|
|
|
}
|
|
|
|
|
}
|
2022-09-06 22:14:29 +03:00
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
return "", fmt.Errorf("no records found")
|
2023-08-07 23:50:16 +05:30
|
|
|
}))
|
2023-09-16 16:02:17 +05:30
|
|
|
_ = dsl.AddFunction(dsl.NewWithMultipleSignatures("getNetworkPort", []string{
|
|
|
|
|
"(Port string,defaultPort string) string)",
|
|
|
|
|
"(Port int,defaultPort int) int",
|
|
|
|
|
}, false, func(args ...interface{}) (interface{}, error) {
|
|
|
|
|
if len(args) != 2 {
|
|
|
|
|
return nil, dsl.ErrInvalidDslFunction
|
|
|
|
|
}
|
|
|
|
|
port := types.ToString(args[0])
|
|
|
|
|
defaultPort := types.ToString(args[1])
|
|
|
|
|
if port == "" || stringsutil.EqualFoldAny(port, knowPorts...) {
|
|
|
|
|
return defaultPort, nil
|
|
|
|
|
}
|
|
|
|
|
return port, nil
|
|
|
|
|
}))
|
2022-11-14 02:38:12 +02:00
|
|
|
|
2023-03-28 23:12:08 +02:00
|
|
|
dsl.PrintDebugCallback = func(args ...interface{}) error {
|
|
|
|
|
gologger.Info().Msgf("print_debug value: %s", fmt.Sprint(args))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
HelperFunctions = dsl.HelperFunctions()
|
|
|
|
|
FunctionNames = dsl.GetFunctionNames(HelperFunctions)
|
2023-02-09 20:22:42 +05:30
|
|
|
}
|
|
|
|
|
|
2022-08-29 10:11:32 +02:00
|
|
|
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
|
|
|
|
|
}
|
2023-01-15 23:28:51 +07:00
|
|
|
|
2023-03-27 18:48:50 +05:30
|
|
|
func GetPrintableDslFunctionSignatures(noColor bool) string {
|
|
|
|
|
return dsl.GetPrintableDslFunctionSignatures(noColor)
|
2023-01-15 23:28:51 +07:00
|
|
|
}
|