2014-01-09 06:42:05 +00:00
|
|
|
package models
|
2013-12-12 06:27:43 +00:00
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
import (
|
2016-01-13 02:46:17 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2014-03-25 03:31:33 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2016-01-19 03:13:32 +00:00
|
|
|
"bitbucket.org/liamstask/goose/lib/goose"
|
|
|
|
|
2016-11-19 16:37:22 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
2016-01-13 02:46:17 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2015-02-07 02:24:10 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2016-03-09 04:37:55 +00:00
|
|
|
var db *gorm.DB
|
2014-03-25 03:31:33 +00:00
|
|
|
var err error
|
2015-02-07 02:24:10 +00:00
|
|
|
|
|
|
|
// Logger is a global logger used to show informational, warning, and error messages
|
2014-03-26 04:53:51 +00:00
|
|
|
var Logger = log.New(os.Stdout, " ", log.Ldate|log.Ltime|log.Lshortfile)
|
2013-12-12 07:00:22 +00:00
|
|
|
|
2014-03-28 04:31:51 +00:00
|
|
|
const (
|
2014-07-02 01:32:34 +00:00
|
|
|
CAMPAIGN_IN_PROGRESS string = "In progress"
|
|
|
|
CAMPAIGN_QUEUED string = "Queued"
|
2016-11-19 04:01:38 +00:00
|
|
|
CAMPAIGN_CREATED string = "Created"
|
2015-09-28 03:25:38 +00:00
|
|
|
CAMPAIGN_EMAILS_SENT string = "Emails Sent"
|
2014-07-02 01:32:34 +00:00
|
|
|
CAMPAIGN_COMPLETE string = "Completed"
|
2014-07-07 02:34:02 +00:00
|
|
|
EVENT_SENT string = "Email Sent"
|
2016-01-29 18:34:29 +00:00
|
|
|
EVENT_SENDING_ERROR string = "Error Sending Email"
|
2014-07-07 02:34:02 +00:00
|
|
|
EVENT_OPENED string = "Email Opened"
|
|
|
|
EVENT_CLICKED string = "Clicked Link"
|
2016-02-01 01:50:41 +00:00
|
|
|
EVENT_DATA_SUBMIT string = "Submitted Data"
|
2018-03-19 03:03:00 +00:00
|
|
|
EVENT_REPORTED string = "Email Reported"
|
2017-12-09 21:42:07 +00:00
|
|
|
EVENT_PROXY_REQUEST string = "Proxied request"
|
2014-07-07 02:34:02 +00:00
|
|
|
STATUS_SUCCESS string = "Success"
|
2017-12-09 21:42:07 +00:00
|
|
|
STATUS_QUEUED string = "Queued"
|
2016-02-02 02:42:46 +00:00
|
|
|
STATUS_SENDING string = "Sending"
|
2014-07-07 02:34:02 +00:00
|
|
|
STATUS_UNKNOWN string = "Unknown"
|
2017-12-09 21:42:07 +00:00
|
|
|
STATUS_SCHEDULED string = "Scheduled"
|
|
|
|
STATUS_RETRY string = "Retrying"
|
2014-07-02 01:32:34 +00:00
|
|
|
ERROR string = "Error"
|
2014-03-28 04:31:51 +00:00
|
|
|
)
|
|
|
|
|
2014-03-26 19:50:16 +00:00
|
|
|
// Flash is used to hold flash information for use in templates.
|
|
|
|
type Flash struct {
|
|
|
|
Type string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:24:10 +00:00
|
|
|
// Response contains the attributes found in an API response
|
2014-06-02 03:30:23 +00:00
|
|
|
type Response struct {
|
2014-06-02 04:38:21 +00:00
|
|
|
Message string `json:"message"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Data interface{} `json:"data"`
|
2014-06-02 03:30:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 02:46:17 +00:00
|
|
|
// Copy of auth.GenerateSecureKey to prevent cyclic import with auth library
|
|
|
|
func generateSecureKey() string {
|
|
|
|
k := make([]byte, 32)
|
|
|
|
io.ReadFull(rand.Reader, k)
|
|
|
|
return fmt.Sprintf("%x", k)
|
|
|
|
}
|
|
|
|
|
2016-11-19 16:37:22 +00:00
|
|
|
func chooseDBDriver(name, openStr string) goose.DBDriver {
|
|
|
|
d := goose.DBDriver{Name: name, OpenStr: openStr}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "mysql":
|
|
|
|
d.Import = "github.com/go-sql-driver/mysql"
|
|
|
|
d.Dialect = &goose.MySqlDialect{}
|
|
|
|
|
|
|
|
// Default database is sqlite3
|
|
|
|
default:
|
|
|
|
d.Import = "github.com/mattn/go-sqlite3"
|
|
|
|
d.Dialect = &goose.Sqlite3Dialect{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// Setup initializes the Conn object
|
|
|
|
// It also populates the Gophish Config object
|
2014-03-25 03:38:59 +00:00
|
|
|
func Setup() error {
|
2016-01-19 03:13:32 +00:00
|
|
|
// Setup the goose configuration
|
|
|
|
migrateConf := &goose.DBConf{
|
|
|
|
MigrationsDir: config.Conf.MigrationsPath,
|
|
|
|
Env: "production",
|
2016-11-19 16:37:22 +00:00
|
|
|
Driver: chooseDBDriver(config.Conf.DBName, config.Conf.DBPath),
|
2016-01-19 03:13:32 +00:00
|
|
|
}
|
|
|
|
// Get the latest possible migration
|
|
|
|
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Open our database connection
|
2016-11-19 16:37:22 +00:00
|
|
|
db, err = gorm.Open(config.Conf.DBName, config.Conf.DBPath)
|
2014-03-28 04:31:51 +00:00
|
|
|
db.LogMode(false)
|
2014-03-26 19:50:16 +00:00
|
|
|
db.SetLogger(Logger)
|
2016-08-03 04:28:22 +00:00
|
|
|
db.DB().SetMaxOpenConns(1)
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2014-03-26 19:50:16 +00:00
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2016-01-19 03:13:32 +00:00
|
|
|
// Migrate up to the latest version
|
|
|
|
err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB())
|
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
|
|
|
return err
|
|
|
|
}
|
2018-02-09 01:04:59 +00:00
|
|
|
// Create the admin user if it doesn't exist
|
|
|
|
var userCount int64
|
|
|
|
db.Model(&User{}).Count(&userCount)
|
|
|
|
if userCount == 0 {
|
2015-02-07 02:24:10 +00:00
|
|
|
initUser := User{
|
2014-03-25 03:31:33 +00:00
|
|
|
Username: "admin",
|
|
|
|
Hash: "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS", //gophish
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2016-01-13 02:46:17 +00:00
|
|
|
initUser.ApiKey = generateSecureKey()
|
2015-02-07 02:24:10 +00:00
|
|
|
err = db.Save(&initUser).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
|
|
|
Logger.Println(err)
|
2016-01-19 03:13:32 +00:00
|
|
|
return err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2014-03-25 03:38:59 +00:00
|
|
|
return nil
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|