2021-02-27 07:03:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2021-11-04 21:31:41 +00:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
|
2021-02-27 07:03:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var networkTestcases = map[string]testutils.TestCase{
|
2021-10-19 16:47:44 +00:00
|
|
|
"network/basic.yaml": &networkBasic{},
|
|
|
|
"network/hex.yaml": &networkBasic{},
|
|
|
|
"network/multi-step.yaml": &networkMultiStep{},
|
|
|
|
"network/self-contained.yaml": &networkRequestSelContained{},
|
2022-04-01 20:44:00 +00:00
|
|
|
"network/variables.yaml": &networkVariables{},
|
2021-02-27 07:03:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 14:43:40 +00:00
|
|
|
const defaultStaticPort = 5431
|
|
|
|
|
2021-02-27 07:03:27 +00:00
|
|
|
type networkBasic struct{}
|
|
|
|
|
2021-09-03 14:25:50 +00:00
|
|
|
// Execute executes a test case and returns an error if occurred
|
2021-02-27 07:03:27 +00:00
|
|
|
func (h *networkBasic) Execute(filePath string) error {
|
|
|
|
var routerErr error
|
|
|
|
|
2022-03-07 08:37:30 +00:00
|
|
|
ts := testutils.NewTCPServer(nil, defaultStaticPort, func(conn net.Conn) {
|
2021-02-27 07:03:27 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
data := make([]byte, 4)
|
|
|
|
if _, err := conn.Read(data); err != nil {
|
|
|
|
routerErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if string(data) == "PING" {
|
|
|
|
_, _ = conn.Write([]byte("PONG"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
defer ts.Close()
|
|
|
|
|
2021-10-01 15:23:06 +00:00
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
|
2021-02-27 07:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if routerErr != nil {
|
|
|
|
return routerErr
|
|
|
|
}
|
2021-12-15 14:03:57 +00:00
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
2021-02-27 07:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type networkMultiStep struct{}
|
|
|
|
|
2021-09-03 14:25:50 +00:00
|
|
|
// Execute executes a test case and returns an error if occurred
|
2021-02-27 07:03:27 +00:00
|
|
|
func (h *networkMultiStep) Execute(filePath string) error {
|
|
|
|
var routerErr error
|
|
|
|
|
2022-03-07 08:37:30 +00:00
|
|
|
ts := testutils.NewTCPServer(nil, defaultStaticPort, func(conn net.Conn) {
|
2021-02-27 07:03:27 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
data := make([]byte, 5)
|
|
|
|
if _, err := conn.Read(data); err != nil {
|
|
|
|
routerErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if string(data) == "FIRST" {
|
|
|
|
_, _ = conn.Write([]byte("PING"))
|
|
|
|
}
|
|
|
|
|
|
|
|
data = make([]byte, 6)
|
|
|
|
if _, err := conn.Read(data); err != nil {
|
|
|
|
routerErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if string(data) == "SECOND" {
|
|
|
|
_, _ = conn.Write([]byte("PONG"))
|
|
|
|
}
|
|
|
|
_, _ = conn.Write([]byte("NUCLEI"))
|
|
|
|
})
|
|
|
|
defer ts.Close()
|
|
|
|
|
2021-10-01 15:23:06 +00:00
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
|
2021-02-27 07:03:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if routerErr != nil {
|
|
|
|
return routerErr
|
|
|
|
}
|
2021-10-12 17:18:53 +00:00
|
|
|
|
|
|
|
var expectedResultsSize int
|
|
|
|
if debug {
|
|
|
|
expectedResultsSize = 3
|
|
|
|
} else {
|
|
|
|
expectedResultsSize = 1
|
|
|
|
}
|
2021-12-15 14:03:57 +00:00
|
|
|
|
|
|
|
return expectResultsCount(results, expectedResultsSize)
|
2021-02-27 07:03:27 +00:00
|
|
|
}
|
2021-10-19 16:47:44 +00:00
|
|
|
|
|
|
|
type networkRequestSelContained struct{}
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
func (h *networkRequestSelContained) Execute(filePath string) error {
|
2022-03-07 08:37:30 +00:00
|
|
|
ts := testutils.NewTCPServer(nil, defaultStaticPort, func(conn net.Conn) {
|
2021-10-19 16:47:44 +00:00
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, _ = conn.Write([]byte("Authentication successful"))
|
2022-01-27 20:29:28 +00:00
|
|
|
})
|
2021-10-19 16:47:44 +00:00
|
|
|
defer ts.Close()
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "", debug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-15 14:03:57 +00:00
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
2021-10-19 16:47:44 +00:00
|
|
|
}
|
2022-04-01 20:44:00 +00:00
|
|
|
|
|
|
|
type networkVariables struct{}
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
func (h *networkVariables) Execute(filePath string) error {
|
|
|
|
var routerErr error
|
|
|
|
|
|
|
|
ts := testutils.NewTCPServer(nil, defaultStaticPort, func(conn net.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
data := make([]byte, 4)
|
|
|
|
if _, err := conn.Read(data); err != nil {
|
|
|
|
routerErr = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if string(data) == "PING" {
|
2022-04-20 10:06:02 +00:00
|
|
|
_, _ = conn.Write([]byte("aGVsbG8="))
|
2022-04-01 20:44:00 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, ts.URL, debug)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if routerErr != nil {
|
|
|
|
return routerErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
|
|
|
}
|