Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/operators/common/dsl/dsl.go
2070 views
1
package dsl
2
3
import (
4
"fmt"
5
"strings"
6
7
"github.com/Knetic/govaluate"
8
"github.com/miekg/dns"
9
"github.com/projectdiscovery/dsl"
10
"github.com/projectdiscovery/gologger"
11
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/dns/dnsclientpool"
12
"github.com/projectdiscovery/nuclei/v3/pkg/types"
13
sliceutil "github.com/projectdiscovery/utils/slice"
14
stringsutil "github.com/projectdiscovery/utils/strings"
15
)
16
17
var (
18
HelperFunctions map[string]govaluate.ExpressionFunction
19
FunctionNames []string
20
// knownPorts is a list of known ports for protocols implemented in nuclei
21
knowPorts = []string{"80", "443", "8080", "8081", "8443", "53"}
22
)
23
24
func init() {
25
_ = dsl.AddFunction(dsl.NewWithMultipleSignatures("resolve", []string{
26
"(host string) string",
27
"(format string) string",
28
}, false, func(args ...interface{}) (interface{}, error) {
29
argCount := len(args)
30
if argCount == 0 || argCount > 2 {
31
return nil, dsl.ErrInvalidDslFunction
32
}
33
format := "4"
34
var dnsType uint16
35
if len(args) > 1 {
36
format = strings.ToLower(types.ToString(args[1]))
37
}
38
39
switch format {
40
case "4", "a":
41
dnsType = dns.TypeA
42
case "6", "aaaa":
43
dnsType = dns.TypeAAAA
44
case "cname":
45
dnsType = dns.TypeCNAME
46
case "ns":
47
dnsType = dns.TypeNS
48
case "txt":
49
dnsType = dns.TypeTXT
50
case "srv":
51
dnsType = dns.TypeSRV
52
case "ptr":
53
dnsType = dns.TypePTR
54
case "mx":
55
dnsType = dns.TypeMX
56
case "soa":
57
dnsType = dns.TypeSOA
58
case "caa":
59
dnsType = dns.TypeCAA
60
default:
61
return nil, fmt.Errorf("invalid dns type")
62
}
63
64
options := &types.Options{}
65
err := dnsclientpool.Init(options)
66
if err != nil {
67
return nil, err
68
}
69
dnsClient, err := dnsclientpool.Get(options, &dnsclientpool.Configuration{})
70
if err != nil {
71
return nil, err
72
}
73
74
// query
75
rawResp, err := dnsClient.Query(types.ToString(args[0]), dnsType)
76
if err != nil {
77
return nil, err
78
}
79
80
dnsValues := map[uint16][]string{
81
dns.TypeA: rawResp.A,
82
dns.TypeAAAA: rawResp.AAAA,
83
dns.TypeCNAME: rawResp.CNAME,
84
dns.TypeNS: rawResp.NS,
85
dns.TypeTXT: rawResp.TXT,
86
dns.TypeSRV: rawResp.SRV,
87
dns.TypePTR: rawResp.PTR,
88
dns.TypeMX: rawResp.MX,
89
dns.TypeCAA: rawResp.CAA,
90
dns.TypeSOA: rawResp.GetSOARecords(),
91
}
92
93
if values, ok := dnsValues[dnsType]; ok {
94
firstFound, found := sliceutil.FirstNonZero(values)
95
if found {
96
return firstFound, nil
97
}
98
}
99
100
return "", fmt.Errorf("no records found")
101
}))
102
_ = dsl.AddFunction(dsl.NewWithMultipleSignatures("getNetworkPort", []string{
103
"(Port string,defaultPort string) string)",
104
"(Port int,defaultPort int) int",
105
}, false, func(args ...interface{}) (interface{}, error) {
106
if len(args) != 2 {
107
return nil, dsl.ErrInvalidDslFunction
108
}
109
port := types.ToString(args[0])
110
defaultPort := types.ToString(args[1])
111
if port == "" || stringsutil.EqualFoldAny(port, knowPorts...) {
112
return defaultPort, nil
113
}
114
return port, nil
115
}))
116
117
dsl.PrintDebugCallback = func(args ...interface{}) error {
118
gologger.Debug().Msgf("print_debug value: %s", fmt.Sprint(args))
119
return nil
120
}
121
122
HelperFunctions = dsl.HelperFunctions()
123
FunctionNames = dsl.GetFunctionNames(HelperFunctions)
124
}
125
126
type CompilationError struct {
127
DslSignature string
128
WrappedError error
129
}
130
131
func (e *CompilationError) Error() string {
132
return fmt.Sprintf("could not compile DSL expression %q: %v", e.DslSignature, e.WrappedError)
133
}
134
135
func (e *CompilationError) Unwrap() error {
136
return e.WrappedError
137
}
138
139
func GetPrintableDslFunctionSignatures(noColor bool) string {
140
return dsl.GetPrintableDslFunctionSignatures(noColor)
141
}
142
143