2022-04-28 01:03:01 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-09 17:24:24 +02:00
|
|
|
"errors"
|
2022-04-28 01:03:01 +05:30
|
|
|
"log"
|
|
|
|
|
"os"
|
2023-06-09 17:24:24 +02:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
osutils "github.com/projectdiscovery/utils/os"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/templates/signer"
|
2022-04-28 01:03:01 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/testutils"
|
2023-06-09 17:24:24 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils"
|
2022-04-28 01:03:01 +05:30
|
|
|
)
|
|
|
|
|
|
2023-08-04 20:21:22 +05:30
|
|
|
var codeTestCases = []TestCaseInfo{
|
|
|
|
|
{Path: "protocols/code/py-snippet.yaml", TestCase: &codeSnippet{}},
|
|
|
|
|
{Path: "protocols/code/py-file.yaml", TestCase: &codeFile{}},
|
|
|
|
|
{Path: "protocols/code/py-env-var.yaml", TestCase: &codeEnvVar{}},
|
|
|
|
|
{Path: "protocols/code/unsigned.yaml", TestCase: &unsignedCode{}},
|
|
|
|
|
{Path: "protocols/code/rsa-signed.yaml", TestCase: &rsaSignedCode{}},
|
|
|
|
|
{Path: "protocols/code/py-interactsh.yaml", TestCase: &codeSnippet{}},
|
|
|
|
|
{Path: "protocols/code/ps1-snippet.yaml", TestCase: &codeSnippet{}, DisableOn: func() bool { return !osutils.IsWindows() }},
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
|
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
var (
|
|
|
|
|
ecdsaPrivateKeyAbsPath string
|
|
|
|
|
ecdsaPublicKeyAbsPath string
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
// rsaPrivateKeyAbsPath string
|
|
|
|
|
rsaPublicKeyAbsPath string
|
|
|
|
|
)
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
func init() {
|
|
|
|
|
var err error
|
|
|
|
|
ecdsaPrivateKeyAbsPath, err = filepath.Abs("protocols/code/ecdsa-priv-key.pem")
|
2022-04-28 01:03:01 +05:30
|
|
|
if err != nil {
|
2023-06-09 17:24:24 +02:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
ecdsaPublicKeyAbsPath, err = filepath.Abs("protocols/code/ecdsa-pub-key.pem")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
|
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
// rsaPrivateKeyAbsPath, err = filepath.Abs("protocols/code/rsa-priv-key.pem")
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// panic(err)
|
|
|
|
|
// }
|
|
|
|
|
rsaPublicKeyAbsPath, err = filepath.Abs("protocols/code/rsa-pub-key.pem")
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
signTemplates()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// signTemplates tests the signing procedure on various platforms
|
|
|
|
|
func signTemplates() {
|
|
|
|
|
signerOptions := &signer.Options{
|
|
|
|
|
PrivateKeyName: ecdsaPrivateKeyAbsPath,
|
|
|
|
|
PublicKeyName: ecdsaPublicKeyAbsPath,
|
|
|
|
|
Algorithm: signer.ECDSA,
|
|
|
|
|
}
|
|
|
|
|
sign, err := signer.New(signerOptions)
|
2023-03-14 16:57:48 +01:00
|
|
|
if err != nil {
|
2023-06-09 17:24:24 +02:00
|
|
|
log.Fatalf("couldn't create crypto engine: %s\n", err)
|
2023-03-14 16:57:48 +01:00
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-08-04 20:21:22 +05:30
|
|
|
for _, v := range codeTestCases {
|
|
|
|
|
templatePath := v.Path
|
|
|
|
|
testCase := v.TestCase
|
|
|
|
|
|
|
|
|
|
if v.DisableOn != nil && v.DisableOn() {
|
|
|
|
|
// skip ps1 test case on non-windows platforms
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
templatePath, err := filepath.Abs(templatePath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// skip
|
|
|
|
|
// - unsigned test case
|
|
|
|
|
if _, ok := testCase.(*unsignedCode); ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// - already rsa signed
|
|
|
|
|
if _, ok := testCase.(*rsaSignedCode); ok {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := utils.ProcessFile(sign, templatePath); err != nil {
|
|
|
|
|
log.Fatalf("Could not walk directory: %s\n", err)
|
|
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
2023-06-09 17:24:24 +02:00
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
func prepareEnv(keypath string) {
|
|
|
|
|
os.Setenv("NUCLEI_SIGNATURE_PUBLIC_KEY", keypath)
|
|
|
|
|
os.Setenv("NUCLEI_SIGNATURE_ALGORITHM", "ecdsa")
|
|
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
func tearDownEnv() {
|
|
|
|
|
os.Unsetenv("NUCLEI_SIGNATURE_PUBLIC_KEY")
|
|
|
|
|
os.Unsetenv("NUCLEI_SIGNATURE_ALGORITHM")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type codeSnippet struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *codeSnippet) Execute(filePath string) error {
|
|
|
|
|
prepareEnv(ecdsaPublicKeyAbsPath)
|
|
|
|
|
defer tearDownEnv()
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "input", debug)
|
2022-04-28 01:03:01 +05:30
|
|
|
if err != nil {
|
2023-06-09 17:24:24 +02:00
|
|
|
return err
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
2023-06-09 17:24:24 +02:00
|
|
|
return expectResultsCount(results, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type codeFile struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *codeFile) Execute(filePath string) error {
|
|
|
|
|
prepareEnv(ecdsaPublicKeyAbsPath)
|
|
|
|
|
defer tearDownEnv()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "input", debug)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
2023-06-09 17:24:24 +02:00
|
|
|
return expectResultsCount(results, 1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type codeEnvVar struct{}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *codeEnvVar) Execute(filePath string) error {
|
|
|
|
|
prepareEnv(ecdsaPublicKeyAbsPath)
|
|
|
|
|
defer tearDownEnv()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "input", debug, "-V", "baz=baz")
|
2022-04-28 01:03:01 +05:30
|
|
|
if err != nil {
|
2023-06-09 17:24:24 +02:00
|
|
|
return err
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
2023-06-09 17:24:24 +02:00
|
|
|
return expectResultsCount(results, 1)
|
|
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
type unsignedCode struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *unsignedCode) Execute(filePath string) error {
|
|
|
|
|
prepareEnv(ecdsaPublicKeyAbsPath)
|
|
|
|
|
defer tearDownEnv()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "input", debug)
|
|
|
|
|
|
|
|
|
|
// should error out
|
2022-04-28 01:03:01 +05:30
|
|
|
if err != nil {
|
2023-06-09 17:24:24 +02:00
|
|
|
return nil
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|
|
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
// this point should never be reached
|
|
|
|
|
return errors.Join(expectResultsCount(results, 1), errors.New("unsigned template was executed"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type rsaSignedCode struct{}
|
|
|
|
|
|
|
|
|
|
// Execute executes a test case and returns an error if occurred
|
|
|
|
|
func (h *rsaSignedCode) Execute(filePath string) error {
|
|
|
|
|
prepareEnv(rsaPublicKeyAbsPath)
|
|
|
|
|
defer tearDownEnv()
|
|
|
|
|
|
|
|
|
|
results, err := testutils.RunNucleiTemplateAndGetResults(filePath, "input", debug)
|
|
|
|
|
|
|
|
|
|
// should error out
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-04-28 01:03:01 +05:30
|
|
|
|
2023-06-09 17:24:24 +02:00
|
|
|
// this point should never be reached
|
|
|
|
|
return errors.Join(expectResultsCount(results, 1), errors.New("unsigned template was executed"))
|
2022-04-28 01:03:01 +05:30
|
|
|
}
|