package global
import (
"bytes"
"context"
"embed"
"math/rand"
"net"
"reflect"
"time"
"github.com/Mzack9999/goja"
"github.com/logrusorgru/aurora"
"github.com/projectdiscovery/gologger"
"github.com/projectdiscovery/nuclei/v3/pkg/js/gojs"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/utils/vardump"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
"github.com/projectdiscovery/utils/errkit"
stringsutil "github.com/projectdiscovery/utils/strings"
)
var (
embedFS embed.FS
exports string
knowPorts = []string{"80", "443", "8080", "8081", "8443", "53"}
)
var (
defaultImports = `
var structs = require("nuclei/structs");
var bytes = require("nuclei/bytes");
`
)
func initBuiltInFunc(runtime *goja.Runtime) {
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "Rand",
Signatures: []string{"Rand(n int) []byte"},
Description: "Rand returns a random byte slice of length n",
FuncDecl: func(n int) []byte {
b := make([]byte, n)
for i := range b {
b[i] = byte(rand.Intn(255))
}
return b
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "RandInt",
Signatures: []string{"RandInt() int"},
Description: "RandInt returns a random int",
FuncDecl: func() int64 {
return rand.Int63()
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "log",
Signatures: []string{
"log(msg string)",
"log(msg map[string]interface{})",
},
Description: "log prints given input to stdout with [JS] prefix for debugging purposes ",
FuncDecl: func(call goja.FunctionCall) goja.Value {
arg := call.Argument(0).Export()
switch value := arg.(type) {
case string:
gologger.DefaultLogger.Print().Msgf("[%v] %v", aurora.BrightCyan("JS"), value)
case map[string]interface{}:
gologger.DefaultLogger.Print().Msgf("[%v] %v", aurora.BrightCyan("JS"), vardump.DumpVariables(value))
default:
gologger.DefaultLogger.Print().Msgf("[%v] %v", aurora.BrightCyan("JS"), value)
}
return call.Argument(0)
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "getNetworkPort",
Signatures: []string{
"getNetworkPort(port string, defaultPort string) string",
},
Description: "getNetworkPort registers defaultPort and returns defaultPort if it is a colliding port with other protocols",
FuncDecl: func(call goja.FunctionCall) goja.Value {
inputPort := call.Argument(0).String()
if inputPort == "" || stringsutil.EqualFoldAny(inputPort, knowPorts...) {
return call.Argument(1)
}
return call.Argument(0)
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "isPortOpen",
Signatures: []string{
"isPortOpen(host string, port string, [timeout int]) bool",
},
Description: "isPortOpen checks if given TCP port is open on host. timeout is optional and defaults to 5 seconds",
FuncDecl: func(ctx context.Context, host string, port string, timeout ...int) (bool, error) {
if len(timeout) > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(timeout[0])*time.Second)
defer cancel()
}
if host == "" || port == "" {
return false, errkit.New("isPortOpen: host or port is empty")
}
executionId := ctx.Value("executionId").(string)
dialer := protocolstate.GetDialersWithId(executionId)
if dialer == nil {
panic("dialers with executionId " + executionId + " not found")
}
conn, err := dialer.Fastdialer.Dial(ctx, "tcp", net.JoinHostPort(host, port))
if err != nil {
return false, err
}
_ = conn.Close()
return true, nil
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "isUDPPortOpen",
Signatures: []string{
"isUDPPortOpen(host string, port string, [timeout int]) bool",
},
Description: "isUDPPortOpen checks if the given UDP port is open on the host. Timeout is optional and defaults to 5 seconds.",
FuncDecl: func(ctx context.Context, host string, port string, timeout ...int) (bool, error) {
if len(timeout) > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(timeout[0])*time.Second)
defer cancel()
}
if host == "" || port == "" {
return false, errkit.New("isPortOpen: host or port is empty")
}
executionId := ctx.Value("executionId").(string)
dialer := protocolstate.GetDialersWithId(executionId)
if dialer == nil {
panic("dialers with executionId " + executionId + " not found")
}
conn, err := dialer.Fastdialer.Dial(ctx, "udp", net.JoinHostPort(host, port))
if err != nil {
return false, err
}
_ = conn.Close()
return true, nil
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "ToBytes",
Signatures: []string{
"ToBytes(...interface{}) []byte",
},
Description: "ToBytes converts given input to byte slice",
FuncDecl: func(call goja.FunctionCall) goja.Value {
var buff bytes.Buffer
allVars := []any{}
for _, v := range call.Arguments {
if v.Export() == nil {
continue
}
if v.ExportType().Kind() == reflect.Slice {
rfValue := reflect.ValueOf(v.Export())
for i := 0; i < rfValue.Len(); i++ {
allVars = append(allVars, rfValue.Index(i).Interface())
}
} else {
allVars = append(allVars, v.Export())
}
}
for _, v := range allVars {
buff.WriteString(types.ToString(v))
}
return runtime.ToValue(buff.Bytes())
},
})
_ = gojs.RegisterFuncWithSignature(runtime, gojs.FuncOpts{
Name: "ToString",
Signatures: []string{
"ToString(...interface{}) string",
},
Description: "ToString converts given input to string",
FuncDecl: func(call goja.FunctionCall) goja.Value {
var buff bytes.Buffer
for _, v := range call.Arguments {
exported := v.Export()
if exported != nil {
buff.WriteString(types.ToString(exported))
}
}
return runtime.ToValue(buff.String())
},
})
registerAdditionalHelpers(runtime)
}
func RegisterNativeScripts(runtime *goja.Runtime) error {
initBuiltInFunc(runtime)
dirs, err := embedFS.ReadDir("js")
if err != nil {
return err
}
for _, dir := range dirs {
if dir.IsDir() {
continue
}
contents, err := embedFS.ReadFile("js" + "/" + dir.Name())
if err != nil {
return err
}
_, err = runtime.RunString(string(contents))
if err != nil {
return err
}
}
_, err = runtime.RunString(exports)
if err != nil {
return err
}
_, err = runtime.RunString(defaultImports)
if err != nil {
return errkit.Wrapf(err, "could not import default modules %v", defaultImports)
}
return nil
}