2021-11-01 15:47:20 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/gobwas/ws/wsutil"
|
2021-11-25 17:09:20 +02:00
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/testutils"
|
2021-11-01 15:47:20 +05:30
|
|
|
)
|
|
|
|
|
|
2023-07-28 18:50:57 +03:00
|
|
|
var websocketTestCases = []TestCaseInfo{
|
2023-08-04 20:21:22 +05:30
|
|
|
{Path: "protocols/websocket/basic.yaml", TestCase: &websocketBasic{}},
|
|
|
|
|
{Path: "protocols/websocket/cswsh.yaml", TestCase: &websocketCswsh{}},
|
|
|
|
|
{Path: "protocols/websocket/no-cswsh.yaml", TestCase: &websocketNoCswsh{}},
|
|
|
|
|
{Path: "protocols/websocket/path.yaml", TestCase: &websocketWithPath{}},
|
2021-11-01 15:47:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type websocketBasic struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *websocketBasic) Execute(filePath string) error {
|
|
|
|
|
connHandler := func(conn net.Conn) {
|
|
|
|
|
for {
|
|
|
|
|
msg, op, _ := wsutil.ReadClientData(conn)
|
2021-11-25 17:18:54 +02:00
|
|
|
if string(msg) != "hello" {
|
2021-11-01 15:47:20 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
_ = wsutil.WriteServerMessage(conn, op, []byte("world"))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
originValidate := func(origin string) bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2021-11-01 15:51:56 +05:30
|
|
|
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
|
2021-11-01 15:47:20 +05:30
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
2021-11-01 15:47:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type websocketCswsh struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *websocketCswsh) Execute(filePath string) error {
|
|
|
|
|
connHandler := func(conn net.Conn) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
originValidate := func(origin string) bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2021-11-01 15:51:56 +05:30
|
|
|
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
|
2021-11-01 15:47:20 +05:30
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 1)
|
2021-11-01 15:47:20 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type websocketNoCswsh struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *websocketNoCswsh) Execute(filePath string) error {
|
|
|
|
|
connHandler := func(conn net.Conn) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
originValidate := func(origin string) bool {
|
|
|
|
|
return origin == "https://google.com"
|
|
|
|
|
}
|
2021-11-01 15:51:56 +05:30
|
|
|
ts := testutils.NewWebsocketServer("", connHandler, originValidate)
|
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 0)
|
2021-11-01 15:51:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type websocketWithPath struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *websocketWithPath) Execute(filePath string) error {
|
|
|
|
|
connHandler := func(conn net.Conn) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
originValidate := func(origin string) bool {
|
|
|
|
|
return origin == "https://google.com"
|
|
|
|
|
}
|
|
|
|
|
ts := testutils.NewWebsocketServer("/test", connHandler, originValidate)
|
2021-11-01 15:47:20 +05:30
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, strings.ReplaceAll(ts.URL, "http", "ws"), debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2021-12-15 16:03:57 +02:00
|
|
|
|
|
|
|
|
return expectResultsCount(results, 0)
|
2021-11-01 15:47:20 +05:30
|
|
|
}
|