2014-03-28 04:31:51 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
2014-06-27 00:55:56 +00:00
|
|
|
"bytes"
|
2016-02-10 04:08:41 +00:00
|
|
|
"crypto/tls"
|
2016-02-13 04:19:59 +00:00
|
|
|
"encoding/base64"
|
2016-02-13 03:24:05 +00:00
|
|
|
"encoding/json"
|
2016-01-25 02:03:53 +00:00
|
|
|
"errors"
|
2016-05-30 19:55:45 +00:00
|
|
|
"io"
|
2014-03-28 04:31:51 +00:00
|
|
|
"log"
|
2015-10-23 00:54:01 +00:00
|
|
|
"net/mail"
|
2014-03-28 04:31:51 +00:00
|
|
|
"os"
|
2016-05-30 19:55:45 +00:00
|
|
|
"strconv"
|
2014-06-05 04:54:46 +00:00
|
|
|
"strings"
|
2014-06-27 00:55:56 +00:00
|
|
|
"text/template"
|
2016-06-08 02:42:09 +00:00
|
|
|
"time"
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2016-05-30 19:55:45 +00:00
|
|
|
"gopkg.in/gomail.v2"
|
2014-03-28 04:31:51 +00:00
|
|
|
)
|
|
|
|
|
2015-06-13 04:12:43 +00:00
|
|
|
// Logger is the logger for the worker
|
2014-03-28 04:31:51 +00:00
|
|
|
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
|
|
|
|
2015-02-21 06:11:22 +00:00
|
|
|
// Worker is the background worker that handles watching for new campaigns and sending emails appropriately.
|
2016-06-08 02:42:09 +00:00
|
|
|
type Worker struct{}
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2015-06-13 04:12:43 +00:00
|
|
|
// New creates a new worker object to handle the creation of campaigns
|
2014-03-28 04:31:51 +00:00
|
|
|
func New() *Worker {
|
2016-06-08 02:42:09 +00:00
|
|
|
return &Worker{}
|
2014-03-28 04:31:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 02:42:09 +00:00
|
|
|
// Start launches the worker to poll the database every minute for any jobs.
|
2014-03-28 04:31:51 +00:00
|
|
|
// If a job is found, it launches the job
|
|
|
|
func (w *Worker) Start() {
|
|
|
|
Logger.Println("Background Worker Started Successfully - Waiting for Campaigns")
|
2016-06-08 02:42:09 +00:00
|
|
|
for t := range time.Tick(1 * time.Minute) {
|
|
|
|
cs, err := models.GetQueuedCampaigns(t)
|
|
|
|
// Not really sure of a clean way to catch errors per campaign...
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, c := range cs {
|
|
|
|
go func(c models.Campaign) {
|
|
|
|
processCampaign(&c)
|
|
|
|
}(c)
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func processCampaign(c *models.Campaign) {
|
|
|
|
Logger.Printf("Worker received: %s", c.Name)
|
2014-07-02 01:32:34 +00:00
|
|
|
err := c.UpdateStatus(models.CAMPAIGN_IN_PROGRESS)
|
2014-03-28 04:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
f, err := mail.ParseAddress(c.SMTP.FromAddress)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2014-06-05 04:54:46 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
fn := f.Name
|
|
|
|
if fn == "" {
|
|
|
|
fn = f.Address
|
2014-06-05 04:54:46 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
// Setup the message and dial
|
|
|
|
hp := strings.Split(c.SMTP.Host, ":")
|
|
|
|
if len(hp) < 2 {
|
|
|
|
hp = append(hp, "25")
|
|
|
|
}
|
|
|
|
// Any issues should have been caught in validation, so we just log
|
|
|
|
port, err := strconv.Atoi(hp[1])
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
d := gomail.NewDialer(hp[0], port, c.SMTP.Username, c.SMTP.Password)
|
|
|
|
d.TLSConfig = &tls.Config{
|
2016-02-13 03:24:05 +00:00
|
|
|
ServerName: c.SMTP.Host,
|
|
|
|
InsecureSkipVerify: c.SMTP.IgnoreCertErrors,
|
|
|
|
}
|
2016-07-07 04:25:28 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
hostname = "localhost"
|
|
|
|
}
|
|
|
|
d.LocalName = hostname
|
2016-05-30 19:55:45 +00:00
|
|
|
s, err := d.Dial()
|
|
|
|
// Short circuit if we have an err
|
|
|
|
// However, we still need to update each target
|
2015-06-13 04:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2016-05-30 19:55:45 +00:00
|
|
|
for _, t := range c.Results {
|
|
|
|
es := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
ej, err := json.Marshal(es)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = t.UpdateStatus(models.ERROR)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = c.AddEvent(models.Event{Email: t.Email, Message: models.EVENT_SENDING_ERROR, Details: string(ej)})
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2015-06-13 04:12:43 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
// Send each email
|
|
|
|
e := gomail.NewMessage()
|
2014-03-28 04:31:51 +00:00
|
|
|
for _, t := range c.Results {
|
2016-05-30 19:55:45 +00:00
|
|
|
e.SetHeader("From", c.SMTP.FromAddress)
|
2015-06-13 04:12:43 +00:00
|
|
|
td := struct {
|
|
|
|
models.Result
|
2015-08-26 23:17:03 +00:00
|
|
|
URL string
|
|
|
|
TrackingURL string
|
2015-10-23 00:54:01 +00:00
|
|
|
Tracker string
|
|
|
|
From string
|
2015-06-13 04:12:43 +00:00
|
|
|
}{
|
|
|
|
t,
|
2015-10-23 00:54:01 +00:00
|
|
|
c.URL + "?rid=" + t.RId,
|
|
|
|
c.URL + "/track?rid=" + t.RId,
|
2016-11-26 23:29:50 +00:00
|
|
|
"<img alt='' style='display: none' src='" + c.URL + "/track?rid=" + t.RId + "'/>",
|
2016-05-30 19:55:45 +00:00
|
|
|
fn,
|
2015-06-13 04:12:43 +00:00
|
|
|
}
|
2014-06-27 00:55:56 +00:00
|
|
|
// Parse the templates
|
2015-06-13 04:12:43 +00:00
|
|
|
var subjBuff bytes.Buffer
|
2016-05-30 19:55:45 +00:00
|
|
|
tmpl, err := template.New("text_template").Parse(c.Template.Subject)
|
2014-06-27 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
err = tmpl.Execute(&subjBuff, td)
|
2014-06-27 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
e.SetHeader("Subject", subjBuff.String())
|
|
|
|
Logger.Println("Creating email using template")
|
|
|
|
e.SetHeader("To", t.Email)
|
2016-08-03 04:18:53 +00:00
|
|
|
if c.Template.Text != "" {
|
|
|
|
var textBuff bytes.Buffer
|
|
|
|
tmpl, err = template.New("text_template").Parse(c.Template.Text)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&textBuff, td)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.SetBody("text/plain", textBuff.String())
|
2015-02-14 00:32:28 +00:00
|
|
|
}
|
2016-08-03 04:18:53 +00:00
|
|
|
if c.Template.HTML != "" {
|
|
|
|
var htmlBuff bytes.Buffer
|
|
|
|
tmpl, err = template.New("html_template").Parse(c.Template.HTML)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&htmlBuff, td)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
if c.Template.Text == "" {
|
|
|
|
e.SetBody("text/html", htmlBuff.String())
|
|
|
|
} else {
|
|
|
|
e.AddAlternative("text/html", htmlBuff.String())
|
|
|
|
}
|
2015-02-14 00:32:28 +00:00
|
|
|
}
|
2016-02-13 04:19:59 +00:00
|
|
|
// Attach the files
|
|
|
|
for _, a := range c.Template.Attachments {
|
2016-05-30 19:55:45 +00:00
|
|
|
e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {
|
2017-02-10 16:00:34 +00:00
|
|
|
Attach, _ := base64.StdEncoding.DecodeString(a.Content)
|
|
|
|
Attach2 := bytes.Replace(Attach, []byte("{{.RId}}"), []byte(t.RId), -1)
|
|
|
|
a.Content = base64.StdEncoding.EncodeToString([]byte(Attach2))
|
|
|
|
// Name2 := strings.Replace(a.Name, ".", "_" + t.RId + ".", -1)
|
2016-05-30 19:55:45 +00:00
|
|
|
return a.Name, gomail.SetCopyFunc(func(w io.Writer) error {
|
|
|
|
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content))
|
|
|
|
_, err = io.Copy(w, decoder)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}(a))
|
2016-02-13 04:19:59 +00:00
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
Logger.Printf("Sending Email to %s\n", t.Email)
|
2016-05-30 19:55:45 +00:00
|
|
|
err = gomail.Send(s, e)
|
2014-06-05 04:54:46 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2016-02-13 03:24:05 +00:00
|
|
|
es := struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
ej, err := json.Marshal(es)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-10-23 03:29:10 +00:00
|
|
|
err = t.UpdateStatus(models.ERROR)
|
2014-06-26 02:01:01 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-02-13 03:24:05 +00:00
|
|
|
err = c.AddEvent(models.Event{Email: t.Email, Message: models.EVENT_SENDING_ERROR, Details: string(ej)})
|
2016-01-29 18:34:29 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-06-26 02:01:01 +00:00
|
|
|
} else {
|
2014-07-07 02:34:02 +00:00
|
|
|
err = t.UpdateStatus(models.EVENT_SENT)
|
2014-06-26 02:01:01 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-01-15 04:50:09 +00:00
|
|
|
err = c.AddEvent(models.Event{Email: t.Email, Message: models.EVENT_SENT})
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
e.Reset()
|
2015-10-23 00:54:01 +00:00
|
|
|
}
|
|
|
|
err = c.UpdateStatus(models.CAMPAIGN_EMAILS_SENT)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2014-03-28 04:31:51 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-25 02:03:53 +00:00
|
|
|
|
|
|
|
func SendTestEmail(s *models.SendTestEmailRequest) error {
|
2016-05-30 19:55:45 +00:00
|
|
|
hp := strings.Split(s.SMTP.Host, ":")
|
|
|
|
if len(hp) < 2 {
|
|
|
|
hp = append(hp, "25")
|
2016-01-25 02:03:53 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
port, err := strconv.Atoi(hp[1])
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
2016-02-13 03:24:05 +00:00
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
d := gomail.NewDialer(hp[0], port, s.SMTP.Username, s.SMTP.Password)
|
|
|
|
d.TLSConfig = &tls.Config{
|
2016-02-13 03:24:05 +00:00
|
|
|
ServerName: s.SMTP.Host,
|
|
|
|
InsecureSkipVerify: s.SMTP.IgnoreCertErrors,
|
|
|
|
}
|
2016-07-07 04:25:28 +00:00
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
hostname = "localhost"
|
|
|
|
}
|
|
|
|
d.LocalName = hostname
|
2016-05-30 19:55:45 +00:00
|
|
|
dc, err := d.Dial()
|
2016-01-25 02:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
e := gomail.NewMessage()
|
|
|
|
e.SetHeader("From", s.SMTP.FromAddress)
|
|
|
|
e.SetHeader("To", s.Email)
|
2016-01-25 02:03:53 +00:00
|
|
|
Logger.Println("Creating email using template")
|
|
|
|
// Parse the templates
|
|
|
|
var subjBuff bytes.Buffer
|
2016-05-30 19:55:45 +00:00
|
|
|
tmpl, err := template.New("text_template").Parse(s.Template.Subject)
|
2016-01-25 02:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&subjBuff, s)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
e.SetHeader("Subject", subjBuff.String())
|
|
|
|
if s.Template.Text != "" {
|
|
|
|
var textBuff bytes.Buffer
|
|
|
|
tmpl, err = template.New("text_template").Parse(s.Template.Text)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&textBuff, s)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.SetBody("text/plain", textBuff.String())
|
|
|
|
}
|
|
|
|
if s.Template.HTML != "" {
|
|
|
|
var htmlBuff bytes.Buffer
|
|
|
|
tmpl, err = template.New("html_template").Parse(s.Template.HTML)
|
2016-02-13 04:19:59 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2016-05-30 19:55:45 +00:00
|
|
|
err = tmpl.Execute(&htmlBuff, s)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
// If we don't have a text part, make the html the root part
|
|
|
|
if s.Template.Text == "" {
|
|
|
|
e.SetBody("text/html", htmlBuff.String())
|
|
|
|
} else {
|
|
|
|
e.AddAlternative("text/html", htmlBuff.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Attach the files
|
|
|
|
for _, a := range s.Template.Attachments {
|
|
|
|
e.Attach(func(a models.Attachment) (string, gomail.FileSetting) {
|
|
|
|
return a.Name, gomail.SetCopyFunc(func(w io.Writer) error {
|
|
|
|
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content))
|
|
|
|
_, err = io.Copy(w, decoder)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}(a))
|
2016-02-13 04:19:59 +00:00
|
|
|
}
|
2016-01-25 02:03:53 +00:00
|
|
|
Logger.Printf("Sending Email to %s\n", s.Email)
|
2016-05-30 19:55:45 +00:00
|
|
|
err = gomail.Send(dc, e)
|
2016-01-25 02:03:53 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
// For now, let's split the error and return
|
|
|
|
// the last element (the most descriptive error message)
|
|
|
|
serr := strings.Split(err.Error(), ":")
|
|
|
|
return errors.New(serr[len(serr)-1])
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|