2014-07-02 01:32:34 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2016-01-04 06:04:10 +00:00
|
|
|
"log"
|
2017-01-28 03:16:47 +00:00
|
|
|
"math/big"
|
2016-01-04 06:04:10 +00:00
|
|
|
"net"
|
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"
|
2017-02-14 17:09:26 +00:00
|
|
|
"github.com/gophish/gophish/util"
|
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 {
|
2016-01-04 06:04:10 +00:00
|
|
|
Id int64 `json:"-"`
|
|
|
|
CampaignId int64 `json:"-"`
|
|
|
|
UserId int64 `json:"-"`
|
|
|
|
RId string `json:"id"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
FirstName string `json:"first_name"`
|
|
|
|
LastName string `json:"last_name"`
|
2016-02-18 03:25:06 +00:00
|
|
|
Position string `json:"position"`
|
2016-01-04 06:04:10 +00:00
|
|
|
Status string `json:"status" sql:"not null"`
|
|
|
|
IP string `json:"ip"`
|
|
|
|
Latitude float64 `json:"latitude"`
|
|
|
|
Longitude float64 `json:"longitude"`
|
2014-07-02 01:32:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// UpdateStatus updates the status of the result in the database
|
2014-07-02 01:32:34 +00:00
|
|
|
func (r *Result) UpdateStatus(s string) error {
|
|
|
|
return db.Table("results").Where("id=?", r.Id).Update("status", s).Error
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return db.Table("results").Where("id=?", r.Id).Updates(map[string]interface{}{
|
|
|
|
"ip": addr,
|
|
|
|
"latitude": city.GeoPoint.Latitude,
|
|
|
|
"longitude": city.GeoPoint.Longitude,
|
|
|
|
}).Error
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:47:16 +00:00
|
|
|
// GenerateId generates a unique key to represent the result
|
|
|
|
// in the database
|
2017-01-28 03:16:47 +00:00
|
|
|
func (r *Result) GenerateId() 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)
|
2017-01-28 03:16:47 +00:00
|
|
|
const alphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
k := make([]byte, 7)
|
2014-07-02 01:32:34 +00:00
|
|
|
for {
|
2017-01-28 03:16:47 +00:00
|
|
|
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()]
|
|
|
|
}
|
|
|
|
r.RId = string(k)
|
2016-11-14 01:40:06 +00:00
|
|
|
err := db.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
|
|
|
|
}
|