nuclei/v2/pkg/reporting/exporters/es/elasticsearch.go

156 lines
3.8 KiB
Go
Raw Normal View History

package es
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
2021-10-19 17:15:58 +02:00
"strings"
"time"
"encoding/base64"
"encoding/json"
"github.com/pkg/errors"
"github.com/projectdiscovery/nuclei/v2/pkg/output"
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate"
)
// Options contains necessary options required for elasticsearch communicaiton
type Options struct {
2021-10-19 17:15:58 +02:00
// IP for elasticsearch instance
IP string `yaml:"ip"`
2021-10-19 17:15:58 +02:00
// Port is the port of elasticsearch instance
Port int `yaml:"port"`
2021-10-18 20:54:30 +02:00
// SSL (optional) enables ssl for elasticsearch connection
SSL bool `yaml:"ssl"`
2021-10-18 20:54:30 +02:00
// SSLVerification (optional) disables SSL verification for elasticsearch
SSLVerification bool `yaml:"ssl-verification"`
2021-10-19 17:15:58 +02:00
// Username for the elasticsearch instance
Username string `yaml:"username"`
2021-10-19 17:15:58 +02:00
// Password is the password for elasticsearch instance
Password string `yaml:"password"`
2021-10-19 17:15:58 +02:00
// IndexName is the name of the elasticsearch index
IndexName string `yaml:"index-name"`
}
type data struct {
Event *output.ResultEvent `json:"event"`
Timestamp string `json:"@timestamp"`
}
// Exporter type for elasticsearch
type Exporter struct {
url string
authentication string
elasticsearch *http.Client
}
// New creates and returns a new exporter for elasticsearch
func New(option *Options) (*Exporter, error) {
var ei *Exporter
2021-10-19 17:15:58 +02:00
err := validateOptions(option)
2021-10-18 20:45:46 +02:00
if err != nil {
2021-10-19 17:17:45 +02:00
return nil, err
2021-10-18 20:45:46 +02:00
}
client := &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
DialContext: protocolstate.Dialer.Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: option.SSLVerification},
},
}
// preparing url for elasticsearch
scheme := "http://"
if option.SSL {
scheme = "https://"
}
// if authentication is required
var authentication string
if len(option.Username) > 0 && len(option.Password) > 0 {
auth := base64.StdEncoding.EncodeToString([]byte(option.Username + ":" + option.Password))
auth = "Basic " + auth
authentication = auth
}
url := fmt.Sprintf("%s%s:%d/%s/_doc", scheme, option.IP, option.Port, option.IndexName)
ei = &Exporter{
url: url,
authentication: authentication,
elasticsearch: client,
}
return ei, nil
}
2021-10-18 20:45:46 +02:00
func validateOptions(options *Options) error {
2021-10-19 17:15:58 +02:00
errs := []string{}
2021-10-18 20:45:46 +02:00
if options.IP == "" {
2021-10-19 17:15:58 +02:00
errs = append(errs, "IP")
2021-10-18 20:45:46 +02:00
}
if options.Port == 0 {
2021-10-19 17:15:58 +02:00
errs = append(errs, "Port")
2021-10-18 20:45:46 +02:00
}
if options.Username == "" {
2021-10-19 17:15:58 +02:00
errs = append(errs, "Username")
2021-10-18 20:45:46 +02:00
}
if options.Password == "" {
2021-10-19 17:15:58 +02:00
errs = append(errs, "Password")
2021-10-18 20:45:46 +02:00
}
if options.IndexName == "" {
2021-10-19 17:15:58 +02:00
errs = append(errs, "IndexName")
2021-10-18 20:45:46 +02:00
}
2021-10-19 17:15:58 +02:00
if len(errs) > 0 {
return errors.New("Mandatory reporting configuration fields are missing: " + strings.Join(errs, ","))
}
2021-10-18 20:45:46 +02:00
return nil
}
2021-09-19 16:26:47 +05:30
// Export exports a passed result event to elasticsearch
func (i *Exporter) Export(event *output.ResultEvent) error {
// creating a request
req, err := http.NewRequest(http.MethodPost, i.url, nil)
if err != nil {
return errors.Wrap(err, "could not make request")
}
if len(i.authentication) > 0 {
req.Header.Add("Authorization", i.authentication)
}
req.Header.Add("Content-Type", "application/json")
d := data{
Event: event,
2021-08-21 08:33:27 +05:45
Timestamp: time.Now().Format(time.RFC3339),
}
b, err := json.Marshal(&d)
if err != nil {
return err
}
req.Body = ioutil.NopCloser(bytes.NewReader(b))
res, err := i.elasticsearch.Do(req)
2021-09-18 15:59:01 +05:30
if err != nil {
2021-10-18 20:45:46 +02:00
return err
2021-09-18 15:59:01 +05:30
}
2021-10-18 20:45:46 +02:00
2021-09-18 15:59:01 +05:30
b, err = ioutil.ReadAll(res.Body)
if err != nil {
return errors.New(err.Error() + "error thrown by elasticsearch " + string(b))
}
if res.StatusCode >= 300 {
return errors.New("elasticsearch responded with an error: " + string(b))
}
return nil
}
// Close closes the exporter after operation
func (i *Exporter) Close() error {
return nil
}