141 lines
3.5 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/v3/pkg/protocols/dns/dnsclientpool"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
sliceutil "github.com/projectdiscovery/utils/slice"
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
2023-09-16 16:02:17 +05:30
stringsutil "github.com/projectdiscovery/utils/strings"
)
var (
HelperFunctions map[string]govaluate.ExpressionFunction
FunctionNames []string
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
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"}
)
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{
"(host string) string",
"(format string) string",
2023-08-07 23:50:16 +05:30
}, false, 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.TypeCAA: rawResp.CAA,
dns.TypeSOA: rawResp.GetSOARecords(),
}
if values, ok := dnsValues[dnsType]; ok {
firstFound, found := sliceutil.FirstNonZero(values)
if found {
return firstFound, nil
}
}
return "", fmt.Errorf("no records found")
2023-08-07 23:50:16 +05:30
}))
javascript protocol for scripting (includes 15+ proto libs) (#4109) * rebase js-layer PR from @ice3man543 * package restructuring * working * fix duplicated event & matcher status * fix lint error * fix response field * add new functions * multiple minor improvements * fix incorrect stats in js protocol * sort output metadata in cli * remove temp files * remove dead code * add unit and integration test * fix lint error * add jsdoclint using llm * fix error in test * add js lint using llm * generate docs of libs * llm lint * remove duplicated docs * update generated docs * update prompt in doclint * update docs * temp disable version check test * fix unit test and add retry * fix panic in it * update and move jsdocs * updated jsdocs * update docs * update container platform in test * dir restructure and adding docs * add api_reference and remove markdown docs * fix imports * add javascript design and contribution docs * add js protocol documentation * update integration test and docs * update doc ext mdx->md * minor update to docs * new integration test and more * move go libs and add docs * gen new net docs and more * final docs update * add new devtool * use fastdialer * fix build fail * use fastdialer + network sandbox support * add reserved keyword 'Port' * update Port to new syntax * misc update * always enable templatectx in js protocol * move docs to 'js-proto-docs' repo * remove scrapefuncs binary --------- Co-authored-by: sandeep <8293321+ehsandeep@users.noreply.github.com>
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
}))
dsl.PrintDebugCallback = func(args ...interface{}) error {
2025-06-17 17:02:57 +05:30
gologger.Debug().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)
}