mirror of
https://github.com/projectdiscovery/nuclei.git
synced 2025-12-17 21:35:26 +00:00
* introducing execution id * wip * . * adding separate execution context id * lint * vet * fixing pg dialers * test ignore * fixing loader FD limit * test * fd fix * wip: remove CloseProcesses() from dev merge * wip: fix merge issue * protocolstate: stop memguarding on last dialer delete * avoid data race in dialers.RawHTTPClient * use shared logger and avoid race conditions * use shared logger and avoid race conditions * go mod * patch executionId into compiled template cache * clean up comment in Parse * go mod update * bump echarts * address merge issues * fix use of gologger * switch cmd/nuclei to options.Logger * address merge issues with go.mod * go vet: address copy of lock with new Copy function * fixing tests * disable speed control * fix nil ExecuterOptions * removing deprecated code * fixing result print * default logger * cli default logger * filter warning from results * fix performance test * hardcoding path * disable upload * refactor(runner): uses `Warning` instead of `Print` for `pdcpUploadErrMsg` Signed-off-by: Dwi Siswanto <git@dw1.io> * Revert "disable upload" This reverts commit 114fbe6663361bf41cf8b2645fd2d57083d53682. * Revert "hardcoding path" This reverts commit cf12ca800e0a0e974bd9fd4826a24e51547f7c00. --------- Signed-off-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Mzack9999 <mzack9999@protonmail.com> Co-authored-by: Dwi Siswanto <git@dw1.io> Co-authored-by: Dwi Siswanto <25837540+dwisiswant0@users.noreply.github.com>
143 lines
4.3 KiB
Go
143 lines
4.3 KiB
Go
package loader
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/alecthomas/chroma/quick"
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
|
pdcpauth "github.com/projectdiscovery/utils/auth/pdcp"
|
|
errorutil "github.com/projectdiscovery/utils/errors"
|
|
)
|
|
|
|
const (
|
|
aiTemplateGeneratorAPIEndpoint = "https://api.projectdiscovery.io/v1/template/ai"
|
|
)
|
|
|
|
type AITemplateResponse struct {
|
|
CanRun bool `json:"canRun"`
|
|
Comment string `json:"comment"`
|
|
Completion string `json:"completion"`
|
|
Message string `json:"message"`
|
|
Name string `json:"name"`
|
|
TemplateID string `json:"template_id"`
|
|
}
|
|
|
|
func getAIGeneratedTemplates(prompt string, options *types.Options) ([]string, error) {
|
|
prompt = strings.TrimSpace(prompt)
|
|
if len(prompt) < 5 {
|
|
return nil, errorutil.New("Prompt is too short. Please provide a more descriptive prompt")
|
|
}
|
|
|
|
if len(prompt) > 3000 {
|
|
return nil, errorutil.New("Prompt is too long. Please limit to 3000 characters")
|
|
}
|
|
|
|
template, templateID, err := generateAITemplate(prompt)
|
|
if err != nil {
|
|
return nil, errorutil.New("Failed to generate template: %v", err)
|
|
}
|
|
|
|
pdcpTemplateDir := filepath.Join(config.DefaultConfig.GetTemplateDir(), "pdcp")
|
|
if err := os.MkdirAll(pdcpTemplateDir, 0755); err != nil {
|
|
return nil, errorutil.New("Failed to create pdcp template directory: %v", err)
|
|
}
|
|
|
|
templateFile := filepath.Join(pdcpTemplateDir, templateID+".yaml")
|
|
err = os.WriteFile(templateFile, []byte(template), 0644)
|
|
if err != nil {
|
|
return nil, errorutil.New("Failed to generate template: %v", err)
|
|
}
|
|
|
|
options.Logger.Info().Msgf("Generated template available at: https://cloud.projectdiscovery.io/templates/%s", templateID)
|
|
options.Logger.Info().Msgf("Generated template path: %s", templateFile)
|
|
|
|
// Check if we should display the template
|
|
// This happens when:
|
|
// 1. No targets are provided (-target/-list)
|
|
// 2. No stdin input is being used
|
|
hasNoTargets := len(options.Targets) == 0 && options.TargetsFilePath == ""
|
|
hasNoStdin := !options.Stdin
|
|
|
|
if hasNoTargets && hasNoStdin {
|
|
// Display the template content with syntax highlighting
|
|
if !options.NoColor {
|
|
var buf bytes.Buffer
|
|
err = quick.Highlight(&buf, template, "yaml", "terminal16m", "monokai")
|
|
if err == nil {
|
|
template = buf.String()
|
|
}
|
|
}
|
|
options.Logger.Debug().Msgf("\n%s", template)
|
|
// FIXME:
|
|
// we should not be exiting the program here
|
|
// but we need to find a better way to handle this
|
|
os.Exit(0)
|
|
}
|
|
|
|
return []string{templateFile}, nil
|
|
}
|
|
|
|
func generateAITemplate(prompt string) (string, string, error) {
|
|
reqBody := map[string]string{
|
|
"prompt": prompt,
|
|
}
|
|
jsonBody, err := json.Marshal(reqBody)
|
|
if err != nil {
|
|
return "", "", errorutil.New("Failed to marshal request body: %v", err)
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, aiTemplateGeneratorAPIEndpoint, bytes.NewBuffer(jsonBody))
|
|
if err != nil {
|
|
return "", "", errorutil.New("Failed to create HTTP request: %v", err)
|
|
}
|
|
|
|
ph := pdcpauth.PDCPCredHandler{}
|
|
creds, err := ph.GetCreds()
|
|
if err != nil {
|
|
return "", "", errorutil.New("Failed to get PDCP credentials: %v", err)
|
|
}
|
|
|
|
if creds == nil {
|
|
return "", "", errorutil.New("PDCP API Key not configured, Create one for free at https://cloud.projectdiscovery.io/")
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set(pdcpauth.ApiKeyHeaderName, creds.APIKey)
|
|
|
|
resp, err := retryablehttp.DefaultClient().Do(req)
|
|
if err != nil {
|
|
return "", "", errorutil.New("Failed to send HTTP request: %v", err)
|
|
}
|
|
defer func() {
|
|
_ = resp.Body.Close()
|
|
}()
|
|
|
|
if resp.StatusCode == http.StatusUnauthorized {
|
|
return "", "", errorutil.New("Invalid API Key or API Key not configured, Create one for free at https://cloud.projectdiscovery.io/")
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
body, _ := io.ReadAll(resp.Body)
|
|
return "", "", errorutil.New("API returned status code %d: %s", resp.StatusCode, string(body))
|
|
}
|
|
|
|
var result AITemplateResponse
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return "", "", errorutil.New("Failed to decode API response: %v", err)
|
|
}
|
|
|
|
if result.TemplateID == "" || result.Completion == "" {
|
|
return "", "", errorutil.New("Failed to generate template")
|
|
}
|
|
|
|
return result.Completion, result.TemplateID, nil
|
|
}
|