-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Javascript Multi-Port Support #6501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
6a6de38
7e04181
ae61257
0759026
8c56052
9b37f1b
c746a8f
232de93
761c7c0
4e8843a
3ea8dc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ var jsTestcases = []TestCaseInfo{ | |
| {Path: "protocols/javascript/net-https.yaml", TestCase: &javascriptNetHttps{}}, | ||
| {Path: "protocols/javascript/oracle-auth-test.yaml", TestCase: &javascriptOracleAuthTest{}, DisableOn: func() bool { return osutils.IsWindows() || osutils.IsOSX() }}, | ||
| {Path: "protocols/javascript/vnc-pass-brute.yaml", TestCase: &javascriptVncPassBrute{}}, | ||
| {Path: "protocols/javascript/multi-ports.yaml", TestCase: &javascriptMultiPortsSSH{}}, | ||
| } | ||
|
|
||
| var ( | ||
|
|
@@ -167,6 +168,32 @@ func (j *javascriptVncPassBrute) Execute(filePath string) error { | |
| return multierr.Combine(errs...) | ||
| } | ||
|
|
||
| type javascriptMultiPortsSSH struct{} | ||
|
|
||
| func (j *javascriptMultiPortsSSH) Execute(filePath string) error { | ||
| finalURL := "scanme.sh" | ||
| errs := []error{} | ||
| for i := 0; i < defaultRetry; i++ { | ||
| results := []string{} | ||
| var err error | ||
| _ = pool.Retry(func() error { | ||
| //let ssh server start | ||
| time.Sleep(3 * time.Second) | ||
| results, err = testutils.RunNucleiTemplateAndGetResults(filePath, finalURL, debug) | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if err := expectResultsCount(results, 1); err == nil { | ||
| return nil | ||
| } else { | ||
| errs = append(errs, err) | ||
| } | ||
| } | ||
| return multierr.Combine(errs...) | ||
| } | ||
|
Comment on lines
+171
to
+180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using
To make this robust and simpler, you can drop the func (j *javascriptMultiPortsSSH) Execute(filePath string) error {
- finalURL := "scanme.sh"
- errs := []error{}
- for i := 0; i < defaultRetry; i++ {
- results := []string{}
- var err error
- _ = pool.Retry(func() error {
- //let ssh server start
- time.Sleep(3 * time.Second)
- results, err = testutils.RunNucleiTemplateAndGetResults(filePath, finalURL, debug)
- return nil
- })
- if err != nil {
- return err
- }
- if err := expectResultsCount(results, 1); err == nil {
- return nil
- } else {
- errs = append(errs, err)
- }
- }
- return multierr.Combine(errs...)
+ finalURL := "scanme.sh"
+ errs := []error{}
+ for i := 0; i < defaultRetry; i++ {
+ // give remote endpoint a brief pause between attempts
+ time.Sleep(3 * time.Second)
+
+ results, err := testutils.RunNucleiTemplateAndGetResults(filePath, finalURL, debug)
+ if err != nil {
+ return err
+ }
+ if err := expectResultsCount(results, 1); err == nil {
+ return nil
+ }
+ errs = append(errs, err)
+ }
+ return multierr.Combine(errs...)
}This removes the potential nil-pointer on 🤖 Prompt for AI Agents |
||
|
|
||
| // purge any given resource if it is not nil | ||
| func purge(resource *dockertest.Resource) { | ||
| if resource != nil && pool != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //go:build !race | ||
| // +build !race | ||
|
|
||
| package nuclei_test | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -36,6 +36,7 @@ import ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/projectdiscovery/utils/errkit" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| iputil "github.com/projectdiscovery/utils/ip" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mapsutil "github.com/projectdiscovery/utils/maps" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sliceutil "github.com/projectdiscovery/utils/slice" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| syncutil "github.com/projectdiscovery/utils/sync" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| urlutil "github.com/projectdiscovery/utils/url" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -133,8 +134,11 @@ func (request *Request) Compile(options *protocols.ExecutorOptions) error { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // "Port" is a special variable and it should not contains any dsl expressions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(request.getPort(), "{{") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errkit.New("'Port' variable cannot contain any dsl expressions") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ports := request.getPorts() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, port := range ports { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.Contains(port, "{{") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errkit.New("'Port' variable cannot contain any dsl expressions") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if request.Init != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -281,12 +285,28 @@ func (request *Request) GetID() string { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ExecuteWithResults executes the protocol requests and returns results instead of writing them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (request *Request) ExecuteWithResults(target *contextargs.Context, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Get default port(s) if specified in template | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ports := request.getPorts() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var errs []error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, port := range ports { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| err := request.executeWithResults(port, target, dynamicValues, previous, callback) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| errs = append(errs, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return errkit.Join(errs...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+288
to
+301
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Templates without Port won't execute. When Apply this diff to execute once with an empty port when no ports are specified: func (request *Request) ExecuteWithResults(target *contextargs.Context, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error {
// Get default port(s) if specified in template
ports := request.getPorts()
+ if len(ports) == 0 {
+ ports = []string{""}
+ }
for _, port := range ports {
err := request.executeWithResults(port, target, dynamicValues, previous, callback)
if err != nil {
return err
}
}
return nil
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // executeWithResults executes the request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (request *Request) executeWithResults(port string, target *contextargs.Context, dynamicValues, previous output.InternalEvent, callback protocols.OutputEventCallback) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| input := target.Clone() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // use network port updates input with new port requested in template file | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // and it is ignored if input port is not standard http(s) ports like 80,8080,8081 etc | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // idea is to reduce redundant dials to http ports | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := input.UseNetworkPort(request.getPort(), request.getExcludePorts()); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := input.UseNetworkPort(port, request.getExcludePorts()); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gologger.Debug().Msgf("Could not network port from constants: %s\n", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -755,13 +775,21 @@ func (request *Request) Type() templateTypes.ProtocolType { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return templateTypes.JavascriptProtocol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (request *Request) getPort() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (request *Request) getPorts() []string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for k, v := range request.Args { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if strings.EqualFold(k, "Port") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return types.ToString(v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| portStr := types.ToString(v) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ports := []string{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, p := range strings.Split(portStr, ",") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| trimmed := strings.TrimSpace(p) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if trimmed != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ports = append(ports, trimmed) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return sliceutil.Dedupe(ports) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return []string{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (request *Request) getExcludePorts() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //go:build !stats | ||
| // +build !stats | ||
|
|
||
| package events | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| //go:build stats | ||
| // +build stats | ||
|
|
||
| package events | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Multiple issues with executor implementation.
The
javascriptMultiPortsSSHexecutor has several problems that deviate from the established pattern:Line 175: Checks
sshResourceinstead ofmultiPortsSShResource, which creates inconsistency with the declared variable at line 28.Line 176: Copy-paste error in comment—says "redis" but should say "SSH" or "multi-port SSH".
Line 179: Uses hardcoded
"scanme.sh"instead of extracting a port from a Docker resource like all other executors (see lines 50-51, 82-83, 114-115, 147-148). This breaks the established pattern of testing against local Docker containers.Missing
defer purge(): Unlike all other executors (lines 52, 84, 116, 149), this executor doesn't clean up the Docker resource, potentially causing resource leaks.Apply this diff to align with the established pattern (assuming you want to use the existing
sshResource):func (j *javascriptMultiPortsSSH) Execute(filePath string) error { if sshResource == nil || pool == nil { - // skip test as redis is not running + // skip test as ssh is not running return nil } - finalURL := "scanme.sh" + tempPort := sshResource.GetPort("2222/tcp") + finalURL := "localhost:" + tempPort + defer purge(sshResource) errs := []error{}Alternatively, if the test genuinely needs to target the external
scanme.shservice (which seems inconsistent with an integration test suite using Docker), please clarify the intent and consider whether this should be a separate test type.