mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2026-02-07 19:13:11 +08:00
misc improvements in js protocol execution (#4643)
* js protocol timeout using -timeout flag * fix zgrab smb hang * fix lint error * custom timeout field in js protocol * minor update: bound checking * add 6 * -timeout in code protocol by default
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package code
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
@@ -26,11 +27,13 @@ import (
|
||||
protocolutils "github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
|
||||
templateTypes "github.com/projectdiscovery/nuclei/v3/pkg/templates/types"
|
||||
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
||||
contextutil "github.com/projectdiscovery/utils/context"
|
||||
errorutil "github.com/projectdiscovery/utils/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
pythonEnvRegex = `os\.getenv\(['"]([^'"]+)['"]\)`
|
||||
pythonEnvRegex = `os\.getenv\(['"]([^'"]+)['"]\)`
|
||||
TimeoutMultiplier = 6 // timeout multiplier for code protocol
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -121,12 +124,17 @@ func (request *Request) GetID() string {
|
||||
}
|
||||
|
||||
// ExecuteWithResults executes the protocol requests and returns results instead of writing them.
|
||||
func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
|
||||
func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) (err error) {
|
||||
metaSrc, err := gozero.NewSourceWithString(input.MetaInput.Input, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
// catch any panics just in case
|
||||
if r := recover(); r != nil {
|
||||
gologger.Error().Msgf("[%s] Panic occurred in code protocol: %s\n", request.options.TemplateID, r)
|
||||
err = fmt.Errorf("panic occurred: %s", r)
|
||||
}
|
||||
if err := metaSrc.Cleanup(); err != nil {
|
||||
gologger.Warning().Msgf("%s\n", err)
|
||||
}
|
||||
@@ -150,9 +158,24 @@ func (request *Request) ExecuteWithResults(input *contextargs.Context, dynamicVa
|
||||
allvars[name] = v
|
||||
metaSrc.AddVariable(gozerotypes.Variable{Name: name, Value: v})
|
||||
}
|
||||
gOutput, err := request.gozero.Eval(context.Background(), request.src, metaSrc)
|
||||
if err != nil && gOutput == nil {
|
||||
return errorutil.NewWithErr(err).Msgf("[%s] Could not execute code on local machine %v", request.options.TemplateID, input.MetaInput.Input)
|
||||
timeout := TimeoutMultiplier * request.options.Options.Timeout
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second)
|
||||
defer cancel()
|
||||
// Note: we use contextutil despite the fact that gozero accepts context as argument
|
||||
gOutput, err := contextutil.ExecFuncWithTwoReturns(ctx, func() (*gozerotypes.Result, error) {
|
||||
return request.gozero.Eval(ctx, request.src, metaSrc)
|
||||
})
|
||||
if gOutput == nil {
|
||||
// write error to stderr buff
|
||||
var buff bytes.Buffer
|
||||
if err != nil {
|
||||
buff.WriteString(err.Error())
|
||||
} else {
|
||||
buff.WriteString("no output something went wrong")
|
||||
}
|
||||
gOutput = &gozerotypes.Result{
|
||||
Stderr: buff,
|
||||
}
|
||||
}
|
||||
gologger.Verbose().Msgf("[%s] Executed code on local machine %v", request.options.TemplateID, input.MetaInput.Input)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user