2014-07-02 01:32:34 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2018-05-27 02:26:34 +00:00
|
|
|
"encoding/json"
|
2017-01-28 03:16:47 +00:00
|
|
|
"math/big"
|
2016-01-04 06:04:10 +00:00
|
|
|
"net"
|
2017-12-09 21:42:07 +00:00
|
|
|
"time"
|
2014-07-02 01:32:34 +00:00
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2014-07-02 01:32:34 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2016-01-04 06:04:10 +00:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
2014-07-02 01:32:34 +00:00
|
|
|
)
|
|
|
|
|
2016-01-04 06:04:10 +00:00
|
|
|
type mmCity struct {
|
|
|
|
GeoPoint mmGeoPoint `maxminddb:"location"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type mmGeoPoint struct {
|
|
|
|
Latitude float64 `maxminddb:"latitude"`
|
|
|
|
Longitude float64 `maxminddb:"longitude"`
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// Result contains the fields for a result object,
|
|
|
|
// which is a representation of a target in a campaign.
|
2014-07-02 01:32:34 +00:00
|
|
|
type Result struct {
|
2018-05-27 02:26:34 +00:00
|
|
|
Id int64 `json:"-"`
|
|
|
|
CampaignId int64 `json:"-"`
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
RId string `json:"id"`
|
|
|
|
Status string `json:"status" sql:"not null"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
|
|
|
SendDate time.Time `json:"send_date"`
|
|
|
|
Reported bool `json:"reported" sql:"not null"`
|
|
|
|
ModifiedDate time.Time `json:"modified_date"`
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient
|
2014-07-02 01:32:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 02:26:34 +00:00
|
|
|
func (r *Result) createEvent(status string, details interface{}) (*Event, error) {
|
|
|
|
e := &Event{Email: r.Email, Message: status}
|
|
|
|
if details != nil {
|
|
|
|
dj, err := json.Marshal(details)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
e.Details = string(dj)
|
|
|
|
}
|
2020-03-01 02:19:54 +00:00
|
|
|
AddEvent(e, r.CampaignId)
|
2018-05-27 02:26:34 +00:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEmailSent updates a Result to indicate that the email has been
|
|
|
|
// successfully sent to the remote SMTP server
|
|
|
|
func (r *Result) HandleEmailSent() error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventSent, nil)
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-10 01:58:05 +00:00
|
|
|
r.SendDate = event.Time
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = EventSent
|
2018-05-27 02:26:34 +00:00
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEmailError updates a Result to indicate that there was an error when
|
|
|
|
// attempting to send the email to the remote SMTP server.
|
|
|
|
func (r *Result) HandleEmailError(err error) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventSendingError, EventError{Error: err.Error()})
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = Error
|
2018-05-27 02:26:34 +00:00
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEmailBackoff updates a Result to indicate that the email received a
|
|
|
|
// temporary error and needs to be retried
|
|
|
|
func (r *Result) HandleEmailBackoff(err error, sendDate time.Time) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventSendingError, EventError{Error: err.Error()})
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = StatusRetry
|
2018-05-27 02:26:34 +00:00
|
|
|
r.SendDate = sendDate
|
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEmailOpened updates a Result in the case where the recipient opened the
|
|
|
|
// email.
|
|
|
|
func (r *Result) HandleEmailOpened(details EventDetails) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventOpened, details)
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Don't update the status if the user already clicked the link
|
|
|
|
// or submitted data to the campaign
|
2018-12-16 03:38:51 +00:00
|
|
|
if r.Status == EventClicked || r.Status == EventDataSubmit {
|
2018-05-27 02:26:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = EventOpened
|
2018-05-27 02:26:34 +00:00
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
2014-07-02 01:32:34 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 02:26:34 +00:00
|
|
|
// HandleClickedLink updates a Result in the case where the recipient clicked
|
|
|
|
// the link in an email.
|
|
|
|
func (r *Result) HandleClickedLink(details EventDetails) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventClicked, details)
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Don't update the status if the user has already submitted data via the
|
|
|
|
// landing page form.
|
2018-12-16 03:38:51 +00:00
|
|
|
if r.Status == EventDataSubmit {
|
2018-05-27 02:26:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = EventClicked
|
2018-05-27 02:26:34 +00:00
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleFormSubmit updates a Result in the case where the recipient submitted
|
|
|
|
// credentials to the form on a Landing Page.
|
|
|
|
func (r *Result) HandleFormSubmit(details EventDetails) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventDataSubmit, details)
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-16 03:38:51 +00:00
|
|
|
r.Status = EventDataSubmit
|
2018-05-27 02:26:34 +00:00
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// HandleEmailReport updates a Result in the case where they report a simulated
|
|
|
|
// phishing email using the HTTP handler.
|
|
|
|
func (r *Result) HandleEmailReport(details EventDetails) error {
|
2018-12-16 03:38:51 +00:00
|
|
|
event, err := r.createEvent(EventReported, details)
|
2018-05-27 02:26:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.Reported = true
|
|
|
|
r.ModifiedDate = event.Time
|
|
|
|
return db.Save(r).Error
|
2018-03-19 03:03:00 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// UpdateGeo updates the latitude and longitude of the result in
|
|
|
|
// the database given an IP address
|
2016-01-04 06:04:10 +00:00
|
|
|
func (r *Result) UpdateGeo(addr string) error {
|
|
|
|
// Open a connection to the maxmind db
|
|
|
|
mmdb, err := maxminddb.Open("static/db/geolite2-city.mmdb")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer mmdb.Close()
|
|
|
|
ip := net.ParseIP(addr)
|
|
|
|
var city mmCity
|
|
|
|
// Get the record
|
|
|
|
err = mmdb.Lookup(ip, &city)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Update the database with the record information
|
2018-05-27 02:26:34 +00:00
|
|
|
r.IP = addr
|
|
|
|
r.Latitude = city.GeoPoint.Latitude
|
|
|
|
r.Longitude = city.GeoPoint.Longitude
|
|
|
|
return db.Save(r).Error
|
2016-01-04 06:04:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-09 02:20:52 +00:00
|
|
|
func generateResultId() (string, error) {
|
|
|
|
const alphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
k := make([]byte, 7)
|
|
|
|
for i := range k {
|
|
|
|
idx, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphaNum))))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
k[i] = alphaNum[idx.Int64()]
|
|
|
|
}
|
|
|
|
return string(k), nil
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// GenerateId generates a unique key to represent the result
|
|
|
|
// in the database
|
2019-12-03 05:00:11 +00:00
|
|
|
func (r *Result) GenerateId(tx *gorm.DB) error {
|
2014-07-02 01:32:34 +00:00
|
|
|
// Keep trying until we generate a unique key (shouldn't take more than one or two iterations)
|
|
|
|
for {
|
2018-06-09 02:20:52 +00:00
|
|
|
rid, err := generateResultId()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-01-28 03:16:47 +00:00
|
|
|
}
|
2018-06-09 02:20:52 +00:00
|
|
|
r.RId = rid
|
2019-12-03 05:00:11 +00:00
|
|
|
err = tx.Table("results").Where("r_id=?", r.RId).First(&Result{}).Error
|
2016-03-09 04:37:55 +00:00
|
|
|
if err == gorm.ErrRecordNotFound {
|
2014-07-02 01:32:34 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-01-28 03:16:47 +00:00
|
|
|
return nil
|
2014-07-02 01:32:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// GetResult returns the Result object from the database
|
|
|
|
// given the ResultId
|
2014-07-02 01:32:34 +00:00
|
|
|
func GetResult(rid string) (Result, error) {
|
|
|
|
r := Result{}
|
|
|
|
err := db.Where("r_id=?", rid).First(&r).Error
|
|
|
|
return r, err
|
|
|
|
}
|