2021-08-20 06:58:58 -05:00
|
|
|
package es
|
|
|
|
|
|
|
|
|
|
import (
|
2021-08-25 13:53:44 +05:30
|
|
|
"bytes"
|
2021-08-20 06:58:58 -05:00
|
|
|
"crypto/tls"
|
2021-12-07 18:01:34 +02:00
|
|
|
"encoding/base64"
|
2021-08-20 06:58:58 -05:00
|
|
|
"fmt"
|
2022-02-23 13:54:46 +01:00
|
|
|
"io"
|
2021-08-20 06:58:58 -05:00
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-08-25 13:53:44 +05:30
|
|
|
"github.com/pkg/errors"
|
2021-12-07 18:01:34 +02:00
|
|
|
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/output"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
2025-02-11 04:31:37 +07:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/utils/json"
|
2022-03-09 12:31:12 +01:00
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2024-02-01 00:42:38 +03:00
|
|
|
"github.com/projectdiscovery/useragent"
|
2021-08-20 06:58:58 -05:00
|
|
|
)
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
// Options contains necessary options required for elasticsearch communication
|
2021-08-20 06:58:58 -05:00
|
|
|
type Options struct {
|
2022-09-28 01:25:51 +05:30
|
|
|
// Host is the hostname of the elasticsearch instance
|
|
|
|
|
Host string `yaml:"host" validate:"required_without=IP"`
|
2021-10-19 17:15:58 +02:00
|
|
|
// IP for elasticsearch instance
|
2022-09-28 01:25:51 +05:30
|
|
|
IP string `yaml:"ip" validate:"required,ip"`
|
2021-10-19 17:15:58 +02:00
|
|
|
// Port is the port of elasticsearch instance
|
2022-09-28 01:25:51 +05:30
|
|
|
Port int `yaml:"port" validate:"gte=0,lte=65535"`
|
2021-10-18 20:54:30 +02:00
|
|
|
// SSL (optional) enables ssl for elasticsearch connection
|
2021-08-25 13:53:44 +05:30
|
|
|
SSL bool `yaml:"ssl"`
|
2021-10-18 20:54:30 +02:00
|
|
|
// SSLVerification (optional) disables SSL verification for elasticsearch
|
2021-08-25 13:53:44 +05:30
|
|
|
SSLVerification bool `yaml:"ssl-verification"`
|
2021-10-19 17:15:58 +02:00
|
|
|
// Username for the elasticsearch instance
|
2021-11-20 13:25:27 +05:30
|
|
|
Username string `yaml:"username" validate:"required"`
|
2021-10-19 17:15:58 +02:00
|
|
|
// Password is the password for elasticsearch instance
|
2021-11-20 13:25:27 +05:30
|
|
|
Password string `yaml:"password" validate:"required"`
|
2021-10-19 17:15:58 +02:00
|
|
|
// IndexName is the name of the elasticsearch index
|
2022-09-27 02:40:34 +05:30
|
|
|
IndexName string `yaml:"index-name" validate:"required"`
|
|
|
|
|
|
|
|
|
|
HttpClient *retryablehttp.Client `yaml:"-"`
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type data struct {
|
|
|
|
|
Event *output.ResultEvent `json:"event"`
|
|
|
|
|
Timestamp string `json:"@timestamp"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Exporter type for elasticsearch
|
|
|
|
|
type Exporter struct {
|
2021-08-25 13:53:44 +05:30
|
|
|
url string
|
|
|
|
|
authentication string
|
|
|
|
|
elasticsearch *http.Client
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New creates and returns a new exporter for elasticsearch
|
|
|
|
|
func New(option *Options) (*Exporter, error) {
|
|
|
|
|
var ei *Exporter
|
|
|
|
|
|
2022-03-09 12:31:12 +01:00
|
|
|
var client *http.Client
|
|
|
|
|
if option.HttpClient != nil {
|
|
|
|
|
client = option.HttpClient.HTTPClient
|
|
|
|
|
} else {
|
|
|
|
|
client = &http.Client{
|
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
|
Transport: &http.Transport{
|
|
|
|
|
MaxIdleConns: 10,
|
|
|
|
|
MaxIdleConnsPerHost: 10,
|
|
|
|
|
DialContext: protocolstate.Dialer.Dial,
|
2022-05-12 13:13:56 +02:00
|
|
|
DialTLSContext: protocolstate.Dialer.DialTLS,
|
2022-03-09 12:31:12 +01:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: option.SSLVerification},
|
|
|
|
|
},
|
|
|
|
|
}
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
2022-03-09 12:31:12 +01:00
|
|
|
|
2021-08-20 06:58:58 -05:00
|
|
|
// preparing url for elasticsearch
|
2021-08-25 13:53:44 +05:30
|
|
|
scheme := "http://"
|
|
|
|
|
if option.SSL {
|
|
|
|
|
scheme = "https://"
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
|
|
|
|
// if authentication is required
|
2021-08-25 13:53:44 +05:30
|
|
|
var authentication string
|
|
|
|
|
if len(option.Username) > 0 && len(option.Password) > 0 {
|
|
|
|
|
auth := base64.StdEncoding.EncodeToString([]byte(option.Username + ":" + option.Password))
|
2021-08-20 06:58:58 -05:00
|
|
|
auth = "Basic " + auth
|
2021-08-25 13:53:44 +05:30
|
|
|
authentication = auth
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
2022-09-28 01:25:51 +05:30
|
|
|
var addr string
|
|
|
|
|
if option.Host != "" {
|
|
|
|
|
addr = option.Host
|
|
|
|
|
} else {
|
|
|
|
|
addr = option.IP
|
|
|
|
|
}
|
|
|
|
|
if option.Port != 0 {
|
|
|
|
|
addr += fmt.Sprintf(":%d", option.Port)
|
|
|
|
|
}
|
|
|
|
|
url := fmt.Sprintf("%s%s/%s/_doc", scheme, addr, option.IndexName)
|
2021-08-20 06:58:58 -05:00
|
|
|
|
|
|
|
|
ei = &Exporter{
|
2021-08-25 13:53:44 +05:30
|
|
|
url: url,
|
|
|
|
|
authentication: authentication,
|
|
|
|
|
elasticsearch: client,
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
|
|
|
|
return ei, nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 16:26:47 +05:30
|
|
|
// Export exports a passed result event to elasticsearch
|
2021-12-07 18:01:34 +02:00
|
|
|
func (exporter *Exporter) Export(event *output.ResultEvent) error {
|
2021-08-25 13:53:44 +05:30
|
|
|
// creating a request
|
2021-12-07 18:01:34 +02:00
|
|
|
req, err := http.NewRequest(http.MethodPost, exporter.url, nil)
|
2021-08-25 13:53:44 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return errors.Wrap(err, "could not make request")
|
|
|
|
|
}
|
2021-12-07 18:01:34 +02:00
|
|
|
if len(exporter.authentication) > 0 {
|
|
|
|
|
req.Header.Add("Authorization", exporter.authentication)
|
2021-08-25 13:53:44 +05:30
|
|
|
}
|
2024-02-01 00:42:38 +03:00
|
|
|
userAgent := useragent.PickRandom()
|
|
|
|
|
req.Header.Set("User-Agent", userAgent.Raw)
|
2021-08-25 13:53:44 +05:30
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2021-08-20 06:58:58 -05:00
|
|
|
|
|
|
|
|
d := data{
|
|
|
|
|
Event: event,
|
2021-08-21 08:33:27 +05:45
|
|
|
Timestamp: time.Now().Format(time.RFC3339),
|
2021-08-20 06:58:58 -05:00
|
|
|
}
|
|
|
|
|
b, err := json.Marshal(&d)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-08-25 13:22:08 +02:00
|
|
|
req.Body = io.NopCloser(bytes.NewReader(b))
|
2021-08-20 06:58:58 -05:00
|
|
|
|
2021-12-07 18:01:34 +02:00
|
|
|
res, err := exporter.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
|
|
|
}
|
2025-02-11 04:31:37 +07:00
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
2022-02-23 13:54:46 +01:00
|
|
|
b, err = io.ReadAll(res.Body)
|
2021-08-20 06:58:58 -05:00
|
|
|
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
|
2021-12-07 18:01:34 +02:00
|
|
|
func (exporter *Exporter) Close() error {
|
2021-08-20 06:58:58 -05:00
|
|
|
return nil
|
|
|
|
|
}
|