2021-08-03 17:03:50 +00:00
//go:generate dstdocgen -path "" -structure Template -output templates_doc.go -package templates
2020-04-03 18:47:57 +00:00
package templates
import (
2021-12-16 10:09:38 +00:00
"encoding/json"
validate "github.com/go-playground/validator/v10"
2021-07-12 14:20:01 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/model"
2020-12-29 11:03:25 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols"
2020-12-29 10:08:14 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/dns"
2021-01-01 09:58:28 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/file"
2021-02-21 11:01:34 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/headless"
2020-12-29 10:08:14 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/http"
2020-12-30 09:24:20 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/network"
2021-11-02 21:04:48 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/ssl"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/websocket"
2021-11-03 11:48:35 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/templates/types"
2020-12-29 10:08:14 +00:00
"github.com/projectdiscovery/nuclei/v2/pkg/workflows"
2021-12-16 10:09:38 +00:00
"go.uber.org/multierr"
"gopkg.in/yaml.v2"
2020-04-03 18:47:57 +00:00
)
2021-08-04 08:50:48 +00:00
// Template is a YAML input file which defines all the requests and
// other metadata for a template.
2020-04-03 18:47:57 +00:00
type Template struct {
2021-07-27 10:33:56 +00:00
// description: |
2021-09-01 10:18:01 +00:00
// ID is the unique id for the template.
2021-07-27 10:33:56 +00:00
//
// #### Good IDs
//
2021-08-03 15:06:26 +00:00
// A good ID uniquely identifies what the requests in the template
2021-07-27 10:33:56 +00:00
// are doing. Let's say you have a template that identifies a git-config
// file on the webservers, a good name would be `git-config-exposure`. Another
// example name is `azure-apps-nxdomain-takeover`.
// examples:
// - name: ID Example
2021-09-01 10:23:30 +00:00
// value: "\"CVE-2021-19520\""
2021-10-20 19:58:36 +00:00
ID string ` yaml:"id" jsonschema:"title=id of the template,description=The Unique ID for the template,example=cve-2021-19520,pattern=^([a-zA-Z0-9]+[-_])*[a-zA-Z0-9]+$" `
2021-07-27 10:33:56 +00:00
// description: |
2021-08-19 11:21:02 +00:00
// Info contains metadata information about the template.
2021-08-04 08:50:48 +00:00
// examples:
// - value: exampleInfoStructure
2021-08-24 14:32:55 +00:00
Info model . Info ` yaml:"info" jsonschema:"title=info for the template,description=Info contains metadata for the template" `
2021-07-27 10:33:56 +00:00
// description: |
2021-08-04 08:50:48 +00:00
// Requests contains the http request to make in the template.
// examples:
// - value: exampleNormalHTTPRequest
2021-08-23 18:20:45 +00:00
RequestsHTTP [ ] * http . Request ` yaml:"requests,omitempty" json:"requests,omitempty" jsonschema:"title=http requests to make,description=HTTP requests to make for the template" `
2021-07-27 10:33:56 +00:00
// description: |
// DNS contains the dns request to make in the template
2021-08-04 08:50:48 +00:00
// examples:
// - value: exampleNormalDNSRequest
2021-08-23 18:20:45 +00:00
RequestsDNS [ ] * dns . Request ` yaml:"dns,omitempty" json:"dns,omitempty" jsonschema:"title=dns requests to make,description=DNS requests to make for the template" `
2021-07-27 10:33:56 +00:00
// description: |
// File contains the file request to make in the template
2021-08-04 08:50:48 +00:00
// examples:
// - value: exampleNormalFileRequest
2021-08-23 18:20:45 +00:00
RequestsFile [ ] * file . Request ` yaml:"file,omitempty" json:"file,omitempty" jsonschema:"title=file requests to make,description=File requests to make for the template" `
2021-07-27 10:33:56 +00:00
// description: |
// Network contains the network request to make in the template
2021-08-04 08:50:48 +00:00
// examples:
// - value: exampleNormalNetworkRequest
2021-08-23 18:20:45 +00:00
RequestsNetwork [ ] * network . Request ` yaml:"network,omitempty" json:"network,omitempty" jsonschema:"title=network requests to make,description=Network requests to make for the template" `
2021-07-27 10:33:56 +00:00
// description: |
// Headless contains the headless request to make in the template.
2021-08-23 18:20:45 +00:00
RequestsHeadless [ ] * headless . Request ` yaml:"headless,omitempty" json:"headless,omitempty" jsonschema:"title=headless requests to make,description=Headless requests to make for the template" `
2021-09-22 17:11:07 +00:00
// description: |
// SSL contains the SSL request to make in the template.
RequestsSSL [ ] * ssl . Request ` yaml:"ssl,omitempty" json:"ssl,omitempty" jsonschema:"title=ssl requests to make,description=SSL requests to make for the template" `
2021-09-27 12:32:49 +00:00
// description: |
// Websocket contains the Websocket request to make in the template.
RequestsWebsocket [ ] * websocket . Request ` yaml:"websocket,omitempty" json:"websocket,omitempty" jsonschema:"title=websocket requests to make,description=Websocket requests to make for the template" `
2021-02-03 19:39:29 +00:00
2021-07-27 10:33:56 +00:00
// description: |
// Workflows is a yaml based workflow declaration code.
2021-08-23 18:20:45 +00:00
workflows . Workflow ` yaml:",inline,omitempty" jsonschema:"title=workflows to run,description=Workflows to run for the template" `
2021-05-01 12:58:24 +00:00
CompiledWorkflow * workflows . Workflow ` yaml:"-" json:"-" jsonschema:"-" `
2020-12-30 07:56:55 +00:00
2021-10-19 15:59:18 +00:00
// description: |
2021-10-19 16:58:48 +00:00
// Self Contained marks Requests for the template as self-contained
2021-10-19 15:59:18 +00:00
SelfContained bool ` yaml:"self-contained,omitempty" jsonschema:"title=mark requests as self-contained,description=Mark Requests for the template as self-contained" `
2021-11-29 10:31:06 +00:00
// description: |
// Stop execution once first match is found
StopAtFirstMatch bool ` yaml:"stop-at-first-match,omitempty" jsonschema:"title=stop at first match,description=Stop at first match for the template" `
2021-10-19 15:59:18 +00:00
2020-12-29 12:32:45 +00:00
// TotalRequests is the total number of requests for the template.
2021-05-01 12:58:24 +00:00
TotalRequests int ` yaml:"-" json:"-" `
2020-12-29 12:32:45 +00:00
// Executer is the actual template executor for running template requests
2021-05-01 12:58:24 +00:00
Executer protocols . Executer ` yaml:"-" json:"-" `
2021-06-05 17:30:59 +00:00
Path string ` yaml:"-" json:"-" `
2020-07-31 15:13:51 +00:00
}
2021-10-27 10:23:04 +00:00
2021-11-04 21:31:41 +00:00
// TemplateProtocols is a list of accepted template protocols
var TemplateProtocols = [ ] string {
2021-10-27 10:23:04 +00:00
"dns" ,
"file" ,
"http" ,
"headless" ,
"network" ,
"workflow" ,
2021-11-01 10:17:20 +00:00
"ssl" ,
"websocket" ,
2021-10-27 10:23:04 +00:00
}
// Type returns the type of the template
2021-11-25 14:57:43 +00:00
func ( template * Template ) Type ( ) types . ProtocolType {
2021-10-27 10:23:04 +00:00
switch {
2021-11-25 14:57:43 +00:00
case len ( template . RequestsDNS ) > 0 :
2021-11-03 11:48:35 +00:00
return types . DNSProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsFile ) > 0 :
2021-11-03 11:48:35 +00:00
return types . FileProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsHTTP ) > 0 :
2021-11-03 11:48:35 +00:00
return types . HTTPProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsHeadless ) > 0 :
2021-11-03 11:48:35 +00:00
return types . HeadlessProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsNetwork ) > 0 :
2021-11-03 11:48:35 +00:00
return types . NetworkProtocol
2021-11-25 14:57:43 +00:00
case len ( template . Workflow . Workflows ) > 0 :
2021-11-03 11:48:35 +00:00
return types . WorkflowProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsSSL ) > 0 :
2021-11-03 13:28:00 +00:00
return types . SSLProtocol
2021-11-25 14:57:43 +00:00
case len ( template . RequestsWebsocket ) > 0 :
2021-11-03 13:28:00 +00:00
return types . WebsocketProtocol
2021-10-27 10:23:04 +00:00
default :
2021-11-03 11:48:35 +00:00
return types . InvalidProtocol
2021-10-27 10:23:04 +00:00
}
}
2021-12-16 10:09:38 +00:00
// MarshalYAML forces recursive struct validation during marshal operation
func ( template * Template ) MarshalYAML ( ) ( [ ] byte , error ) {
out , marshalErr := yaml . Marshal ( template )
errValidate := validate . New ( ) . Struct ( template )
return out , multierr . Append ( marshalErr , errValidate )
}
// MarshalYAML forces recursive struct validation after unmarshal operation
func ( template * Template ) UnmarshalYAML ( unmarshal func ( interface { } ) error ) error {
type Alias Template
alias := & Alias { }
err := unmarshal ( alias )
if err != nil {
return err
}
* template = Template ( * alias )
return validate . New ( ) . Struct ( template )
}
// MarshalJSON forces recursive struct validation during marshal operation
func ( template * Template ) MarshalJSON ( ) ( [ ] byte , error ) {
out , marshalErr := json . Marshal ( template )
errValidate := validate . New ( ) . Struct ( template )
return out , multierr . Append ( marshalErr , errValidate )
}
// UnmarshalJSON forces recursive struct validation after unmarshal operation
func ( template * Template ) UnmarshalJSON ( data [ ] byte ) error {
type Alias Template
alias := & Alias { }
err := json . Unmarshal ( data , alias )
if err != nil {
return err
}
* template = Template ( * alias )
return validate . New ( ) . Struct ( template )
}