2022-12-02 03:57:00 +05:30
|
|
|
package customtemplates
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/gologger"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CustomGithubTemplateDirectory = "github"
|
|
|
|
|
CustomS3TemplateDirectory = "s3"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Provider interface {
|
|
|
|
|
Download(location string, ctx context.Context)
|
|
|
|
|
Update(location string, ctx context.Context)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseCustomTemplates function reads the options.GithubTemplateRepo list,
|
|
|
|
|
// Checks the given repos are valid or not and stores them into runner.CustomTemplates
|
|
|
|
|
func ParseCustomTemplates(options *types.Options) []Provider {
|
2022-12-21 22:48:43 +05:30
|
|
|
if options.Cloud {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-12-02 03:57:00 +05:30
|
|
|
var customTemplates []Provider
|
|
|
|
|
gitHubClient := getGHClientIncognito()
|
|
|
|
|
|
|
|
|
|
for _, repoName := range options.GithubTemplateRepo {
|
|
|
|
|
owner, repo, err := getOwnerAndRepo(repoName)
|
|
|
|
|
if err != nil {
|
2022-12-21 22:48:43 +05:30
|
|
|
gologger.Error().Msgf("%s", err)
|
2022-12-02 03:57:00 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
githubRepo, err := getGithubRepo(gitHubClient, owner, repo, options.GithubToken)
|
|
|
|
|
if err != nil {
|
2022-12-21 22:48:43 +05:30
|
|
|
gologger.Error().Msgf("%s", err)
|
2022-12-02 03:57:00 +05:30
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
customTemplateRepo := &customTemplateGithubRepo{
|
|
|
|
|
owner: owner,
|
|
|
|
|
reponame: repo,
|
|
|
|
|
gitCloneURL: githubRepo.GetCloneURL(),
|
|
|
|
|
githubToken: options.GithubToken,
|
|
|
|
|
}
|
|
|
|
|
customTemplates = append(customTemplates, customTemplateRepo)
|
|
|
|
|
}
|
|
|
|
|
if options.AwsBucketName != "" {
|
|
|
|
|
s3c, err := getS3Client(context.TODO(), options.AwsAccessKey, options.AwsSecretKey, options.AwsRegion)
|
|
|
|
|
if err != nil {
|
|
|
|
|
gologger.Error().Msgf("error downloading s3 bucket %s %s", options.AwsBucketName, err)
|
|
|
|
|
return customTemplates
|
|
|
|
|
}
|
|
|
|
|
ctBucket := &customTemplateS3Bucket{
|
|
|
|
|
bucketName: options.AwsBucketName,
|
|
|
|
|
s3Client: s3c,
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(options.AwsBucketName, "/") {
|
|
|
|
|
bPath := strings.SplitN(options.AwsBucketName, "/", 2)
|
|
|
|
|
ctBucket.bucketName = bPath[0]
|
|
|
|
|
ctBucket.prefix = bPath[1]
|
|
|
|
|
}
|
|
|
|
|
customTemplates = append(customTemplates, ctBucket)
|
|
|
|
|
}
|
|
|
|
|
return customTemplates
|
|
|
|
|
}
|