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"
|
2014-03-28 04:31:51 +00:00
|
|
|
"log"
|
2015-10-23 00:54:01 +00:00
|
|
|
"net/mail"
|
2014-06-05 04:54:46 +00:00
|
|
|
"net/smtp"
|
2014-03-28 04:31:51 +00:00
|
|
|
"os"
|
2014-06-05 04:54:46 +00:00
|
|
|
"strings"
|
2014-06-27 00:55:56 +00:00
|
|
|
"text/template"
|
2014-03-28 04:31:51 +00:00
|
|
|
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2016-01-15 04:50:09 +00:00
|
|
|
"github.com/jordan-wright/email"
|
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.
|
2014-03-28 04:31:51 +00:00
|
|
|
type Worker struct {
|
|
|
|
Queue chan *models.Campaign
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return &Worker{
|
|
|
|
Queue: make(chan *models.Campaign),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches the worker to monitor the database for any jobs.
|
|
|
|
// If a job is found, it launches the job
|
|
|
|
func (w *Worker) Start() {
|
|
|
|
Logger.Println("Background Worker Started Successfully - Waiting for Campaigns")
|
|
|
|
for {
|
|
|
|
processCampaign(<-w.Queue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2014-06-05 04:54:46 +00:00
|
|
|
e := email.Email{
|
|
|
|
Subject: c.Template.Subject,
|
|
|
|
From: c.SMTP.FromAddress,
|
|
|
|
}
|
|
|
|
var auth smtp.Auth
|
|
|
|
if c.SMTP.Username != "" && c.SMTP.Password != "" {
|
|
|
|
auth = smtp.PlainAuth("", c.SMTP.Username, c.SMTP.Password, strings.Split(c.SMTP.Host, ":")[0])
|
|
|
|
}
|
2016-02-13 03:24:05 +00:00
|
|
|
tc := &tls.Config{
|
|
|
|
ServerName: c.SMTP.Host,
|
|
|
|
InsecureSkipVerify: c.SMTP.IgnoreCertErrors,
|
|
|
|
}
|
2015-10-23 00:54:01 +00:00
|
|
|
f, err := mail.ParseAddress(c.SMTP.FromAddress)
|
2015-06-13 04:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-10-23 00:54:01 +00:00
|
|
|
ft := f.Name
|
|
|
|
if ft == "" {
|
|
|
|
ft = f.Address
|
2015-06-13 04:12:43 +00:00
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
for _, t := range c.Results {
|
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-04-06 05:32:20 +00:00
|
|
|
"<img style='display: none' src='" + c.URL + "/track?rid=" + t.RId + "'/>",
|
2015-10-23 00:54:01 +00:00
|
|
|
ft,
|
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
|
|
|
|
var htmlBuff bytes.Buffer
|
|
|
|
var textBuff bytes.Buffer
|
2014-06-27 00:55:56 +00:00
|
|
|
tmpl, err := template.New("html_template").Parse(c.Template.HTML)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
err = tmpl.Execute(&htmlBuff, td)
|
2014-06-27 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
e.HTML = htmlBuff.Bytes()
|
2014-06-27 00:55:56 +00:00
|
|
|
tmpl, err = template.New("text_template").Parse(c.Template.Text)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
err = tmpl.Execute(&textBuff, td)
|
2014-06-27 00:55:56 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
e.Text = textBuff.Bytes()
|
2015-02-14 00:32:28 +00:00
|
|
|
tmpl, err = template.New("text_template").Parse(c.Template.Subject)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
err = tmpl.Execute(&subjBuff, td)
|
2015-02-14 00:32:28 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
e.Subject = string(subjBuff.Bytes())
|
2014-03-28 04:31:51 +00:00
|
|
|
Logger.Println("Creating email using template")
|
2014-06-05 04:54:46 +00:00
|
|
|
e.To = []string{t.Email}
|
2016-03-02 14:44:37 +00:00
|
|
|
e.Attachments = []*email.Attachment{}
|
2016-02-13 04:19:59 +00:00
|
|
|
// Attach the files
|
|
|
|
for _, a := range c.Template.Attachments {
|
|
|
|
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content))
|
|
|
|
_, err = e.Attach(decoder, a.Name, a.Type)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2015-06-13 04:12:43 +00:00
|
|
|
Logger.Printf("Sending Email to %s\n", t.Email)
|
2016-02-13 03:24:05 +00:00
|
|
|
err = e.SendWithTLS(c.SMTP.Host, auth, tc)
|
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
|
|
|
}
|
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 {
|
|
|
|
e := email.Email{
|
|
|
|
Subject: s.Template.Subject,
|
|
|
|
From: s.SMTP.FromAddress,
|
|
|
|
}
|
2016-02-13 03:24:05 +00:00
|
|
|
var auth smtp.Auth
|
|
|
|
if s.SMTP.Username != "" && s.SMTP.Password != "" {
|
|
|
|
auth = smtp.PlainAuth("", s.SMTP.Username, s.SMTP.Password, strings.Split(s.SMTP.Host, ":")[0])
|
|
|
|
}
|
|
|
|
t := &tls.Config{
|
|
|
|
ServerName: s.SMTP.Host,
|
|
|
|
InsecureSkipVerify: s.SMTP.IgnoreCertErrors,
|
|
|
|
}
|
2016-01-25 02:03:53 +00:00
|
|
|
f, err := mail.ParseAddress(s.SMTP.FromAddress)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2016-02-18 03:09:19 +00:00
|
|
|
s.From = f.Name
|
|
|
|
if s.From == "" {
|
|
|
|
s.From = f.Address
|
2016-01-25 02:03:53 +00:00
|
|
|
}
|
|
|
|
Logger.Println("Creating email using template")
|
|
|
|
// Parse the templates
|
|
|
|
var subjBuff bytes.Buffer
|
|
|
|
var htmlBuff bytes.Buffer
|
|
|
|
var textBuff bytes.Buffer
|
|
|
|
tmpl, err := template.New("html_template").Parse(s.Template.HTML)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&htmlBuff, s)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.HTML = htmlBuff.Bytes()
|
|
|
|
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.Text = textBuff.Bytes()
|
|
|
|
tmpl, err = template.New("text_template").Parse(s.Template.Subject)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
err = tmpl.Execute(&subjBuff, s)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
e.Subject = string(subjBuff.Bytes())
|
|
|
|
e.To = []string{s.Email}
|
2016-02-13 04:19:59 +00:00
|
|
|
// Attach the files
|
|
|
|
for _, a := range s.Template.Attachments {
|
|
|
|
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(a.Content))
|
|
|
|
_, err = e.Attach(decoder, a.Name, a.Type)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2016-01-25 02:03:53 +00:00
|
|
|
Logger.Printf("Sending Email to %s\n", s.Email)
|
2016-02-13 03:24:05 +00:00
|
|
|
err = e.SendWithTLS(s.SMTP.Host, auth, t)
|
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
|
|
|
|
}
|