2020-04-04 00:17:57 +05:30
|
|
|
package requests
|
|
|
|
|
|
|
|
|
|
import (
|
2020-04-04 02:50:32 +05:30
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
2020-04-04 03:26:11 +05:30
|
|
|
"net/url"
|
2020-04-04 02:50:32 +05:30
|
|
|
"strings"
|
|
|
|
|
|
2020-04-04 00:17:57 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/pkg/matchers"
|
2020-04-04 03:26:11 +05:30
|
|
|
"github.com/valyala/fasttemplate"
|
2020-04-04 00:17:57 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Request contains a request to be made from a template
|
|
|
|
|
type Request struct {
|
|
|
|
|
// Method is the request method, whether GET, POST, PUT, etc
|
|
|
|
|
Method string `yaml:"method"`
|
|
|
|
|
// Path contains the path/s for the request
|
|
|
|
|
Path []string `yaml:"path"`
|
|
|
|
|
// Headers contains headers to send with the request
|
|
|
|
|
Headers map[string]string `yaml:"headers,omitempty"`
|
|
|
|
|
// Body is an optional parameter which contains the request body for POST methods, etc
|
|
|
|
|
Body string `yaml:"body,omitempty"`
|
|
|
|
|
// Matchers contains the detection mechanism for the request to identify
|
|
|
|
|
// whether the request was successful
|
|
|
|
|
Matchers []*matchers.Matcher `yaml:"matchers"`
|
|
|
|
|
}
|
2020-04-04 02:50:32 +05:30
|
|
|
|
|
|
|
|
// MakeRequest creates a *http.Request from a request template
|
|
|
|
|
func (r *Request) MakeRequest(baseURL string) ([]*http.Request, error) {
|
2020-04-04 03:26:11 +05:30
|
|
|
parsed, err := url.Parse(baseURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
hostname := parsed.Hostname()
|
2020-04-04 02:50:32 +05:30
|
|
|
|
2020-04-04 03:26:11 +05:30
|
|
|
requests := make([]*http.Request, 0, len(r.Path))
|
2020-04-04 02:50:32 +05:30
|
|
|
for _, path := range r.Path {
|
2020-04-04 03:26:11 +05:30
|
|
|
// Replace the dynamic variables in the URL if any
|
|
|
|
|
t := fasttemplate.New(path, "{{", "}}")
|
|
|
|
|
url := t.ExecuteString(map[string]interface{}{
|
|
|
|
|
"BaseURL": baseURL,
|
|
|
|
|
"Hostname": hostname,
|
|
|
|
|
})
|
2020-04-04 02:50:32 +05:30
|
|
|
|
2020-04-04 03:26:11 +05:30
|
|
|
// Build a request on the specified URL
|
2020-04-04 02:50:32 +05:30
|
|
|
req, err := http.NewRequest(r.Method, url, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the user requested a request body
|
|
|
|
|
if r.Body != "" {
|
|
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(r.Body))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the header values requested
|
|
|
|
|
for header, value := range r.Headers {
|
|
|
|
|
req.Header.Set(header, value)
|
|
|
|
|
}
|
|
|
|
|
requests = append(requests, req)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return requests, nil
|
|
|
|
|
}
|