2014-06-22 02:06:16 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2016-11-19 18:04:10 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2014-06-22 02:06:16 +00:00
|
|
|
"encoding/csv"
|
2016-11-19 18:04:10 +00:00
|
|
|
"encoding/pem"
|
2014-06-22 02:06:16 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-02-16 03:53:30 +00:00
|
|
|
"io/ioutil"
|
2016-11-19 18:04:10 +00:00
|
|
|
"math/big"
|
2014-06-22 02:06:16 +00:00
|
|
|
"net/http"
|
2015-02-16 03:53:30 +00:00
|
|
|
"net/mail"
|
2016-11-19 18:04:10 +00:00
|
|
|
"os"
|
2018-06-09 18:22:11 +00:00
|
|
|
"regexp"
|
2016-11-19 18:04:10 +00:00
|
|
|
"time"
|
2014-06-22 02:06:16 +00:00
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/models"
|
2016-02-10 04:29:13 +00:00
|
|
|
"github.com/jordan-wright/email"
|
2019-05-31 18:58:18 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2014-06-22 02:06:16 +00:00
|
|
|
)
|
|
|
|
|
2018-06-09 18:22:11 +00:00
|
|
|
var (
|
|
|
|
firstNameRegex = regexp.MustCompile(`(?i)first[\s_-]*name`)
|
|
|
|
lastNameRegex = regexp.MustCompile(`(?i)last[\s_-]*name`)
|
|
|
|
emailRegex = regexp.MustCompile(`(?i)email`)
|
|
|
|
positionRegex = regexp.MustCompile(`(?i)position`)
|
|
|
|
)
|
|
|
|
|
2015-02-16 03:53:30 +00:00
|
|
|
// ParseMail takes in an HTTP Request and returns an Email object
|
|
|
|
// TODO: This function will likely be changed to take in a []byte
|
|
|
|
func ParseMail(r *http.Request) (email.Email, error) {
|
|
|
|
e := email.Email{}
|
|
|
|
m, err := mail.ReadMessage(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(m.Body)
|
|
|
|
e.HTML = body
|
|
|
|
return e, err
|
|
|
|
}
|
|
|
|
|
2015-02-21 17:36:08 +00:00
|
|
|
// ParseCSV contains the logic to parse the user provided csv file containing Target entries
|
2014-06-22 02:06:16 +00:00
|
|
|
func ParseCSV(r *http.Request) ([]models.Target, error) {
|
|
|
|
mr, err := r.MultipartReader()
|
|
|
|
ts := []models.Target{}
|
|
|
|
if err != nil {
|
|
|
|
return ts, err
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
part, err := mr.NextPart()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Skip the "submit" part
|
|
|
|
if part.FileName() == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defer part.Close()
|
|
|
|
reader := csv.NewReader(part)
|
|
|
|
reader.TrimLeadingSpace = true
|
|
|
|
record, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
fi := -1
|
|
|
|
li := -1
|
|
|
|
ei := -1
|
2015-02-21 17:36:08 +00:00
|
|
|
pi := -1
|
2014-06-22 02:06:16 +00:00
|
|
|
fn := ""
|
|
|
|
ln := ""
|
|
|
|
ea := ""
|
2015-02-21 17:36:08 +00:00
|
|
|
ps := ""
|
2014-06-22 02:06:16 +00:00
|
|
|
for i, v := range record {
|
|
|
|
switch {
|
2018-06-09 18:22:11 +00:00
|
|
|
case firstNameRegex.MatchString(v):
|
2014-06-22 02:06:16 +00:00
|
|
|
fi = i
|
2018-06-09 18:22:11 +00:00
|
|
|
case lastNameRegex.MatchString(v):
|
2014-06-22 02:06:16 +00:00
|
|
|
li = i
|
2018-06-09 18:22:11 +00:00
|
|
|
case emailRegex.MatchString(v):
|
2014-06-22 02:06:16 +00:00
|
|
|
ei = i
|
2018-06-09 18:22:11 +00:00
|
|
|
case positionRegex.MatchString(v):
|
2015-02-21 17:36:08 +00:00
|
|
|
pi = i
|
2014-06-22 02:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 16:46:45 +00:00
|
|
|
if fi == -1 && li == -1 && ei == -1 && pi == -1 {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-22 02:06:16 +00:00
|
|
|
for {
|
|
|
|
record, err := reader.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2018-06-09 19:20:14 +00:00
|
|
|
if fi != -1 && len(record) > fi {
|
2014-06-22 02:06:16 +00:00
|
|
|
fn = record[fi]
|
|
|
|
}
|
2018-06-09 19:20:14 +00:00
|
|
|
if li != -1 && len(record) > li {
|
2014-06-22 02:06:16 +00:00
|
|
|
ln = record[li]
|
|
|
|
}
|
2018-06-09 19:20:14 +00:00
|
|
|
if ei != -1 && len(record) > ei {
|
2017-09-24 22:50:58 +00:00
|
|
|
csvEmail, err := mail.ParseAddress(record[ei])
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ea = csvEmail.Address
|
2014-06-22 02:06:16 +00:00
|
|
|
}
|
2018-06-09 19:20:14 +00:00
|
|
|
if pi != -1 && len(record) > pi {
|
2015-02-21 17:36:08 +00:00
|
|
|
ps = record[pi]
|
|
|
|
}
|
2014-06-22 02:06:16 +00:00
|
|
|
t := models.Target{
|
2018-06-09 02:20:52 +00:00
|
|
|
BaseRecipient: models.BaseRecipient{
|
|
|
|
FirstName: fn,
|
|
|
|
LastName: ln,
|
|
|
|
Email: ea,
|
|
|
|
Position: ps,
|
|
|
|
},
|
2014-06-22 02:06:16 +00:00
|
|
|
}
|
|
|
|
ts = append(ts, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ts, nil
|
|
|
|
}
|
2016-11-19 18:04:10 +00:00
|
|
|
|
|
|
|
// CheckAndCreateSSL is a helper to setup self-signed certificates for the administrative interface.
|
|
|
|
func CheckAndCreateSSL(cp string, kp string) error {
|
|
|
|
// Check whether there is an existing SSL certificate and/or key, and if so, abort execution of this function
|
|
|
|
if _, err := os.Stat(cp); !os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(kp); !os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Infof("Creating new self-signed certificates for administration interface")
|
2016-11-19 18:04:10 +00:00
|
|
|
|
|
|
|
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
2020-05-26 02:46:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error generating tls private key: %v", err)
|
|
|
|
}
|
2016-11-19 18:04:10 +00:00
|
|
|
|
|
|
|
notBefore := time.Now()
|
|
|
|
// Generate a certificate that lasts for 10 years
|
|
|
|
notAfter := notBefore.Add(10 * 365 * 24 * time.Hour)
|
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
|
|
|
|
if err != nil {
|
2020-05-26 02:46:36 +00:00
|
|
|
return fmt.Errorf("tls certificate generation: failed to generate a random serial number: %s", err)
|
2016-11-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template := x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"Gophish"},
|
|
|
|
},
|
|
|
|
NotBefore: notBefore,
|
|
|
|
NotAfter: notAfter,
|
|
|
|
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv)
|
|
|
|
if err != nil {
|
2020-05-26 02:46:36 +00:00
|
|
|
return fmt.Errorf("tls certificate generation: failed to create certificate: %s", err)
|
2016-11-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certOut, err := os.Create(cp)
|
|
|
|
if err != nil {
|
2020-05-26 02:46:36 +00:00
|
|
|
return fmt.Errorf("tls certificate generation: failed to open %s for writing: %s", cp, err)
|
2016-11-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
|
|
|
|
certOut.Close()
|
|
|
|
|
|
|
|
keyOut, err := os.OpenFile(kp, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
|
|
|
if err != nil {
|
2020-05-26 02:46:36 +00:00
|
|
|
return fmt.Errorf("tls certificate generation: failed to open %s for writing", kp)
|
2016-11-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := x509.MarshalECPrivateKey(priv)
|
|
|
|
if err != nil {
|
2020-05-26 02:46:36 +00:00
|
|
|
return fmt.Errorf("tls certificate generation: unable to marshal ECDSA private key: %v", err)
|
2016-11-19 18:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b})
|
|
|
|
keyOut.Close()
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Info("TLS Certificate Generation complete")
|
2016-11-19 18:04:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-31 18:58:18 +00:00
|
|
|
|
|
|
|
// GenerateSecureKey creates a secure key to use as an API key
|
|
|
|
func GenerateSecureKey() string {
|
|
|
|
// Inspired from gorilla/securecookie
|
|
|
|
k := make([]byte, 32)
|
|
|
|
io.ReadFull(rand.Reader, k)
|
|
|
|
return fmt.Sprintf("%x", k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHash hashes the provided password and returns the bcrypt hash (using the
|
|
|
|
// default 10 rounds) as a string.
|
|
|
|
func NewHash(pass string) (string, error) {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(pass), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(hash), nil
|
|
|
|
}
|