2014-01-09 06:42:05 +00:00
|
|
|
package controllers
|
2013-12-03 04:56:55 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-15 21:42:32 +00:00
|
|
|
"compress/gzip"
|
|
|
|
"context"
|
2014-01-06 06:09:41 +00:00
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2017-12-11 03:40:46 +00:00
|
|
|
"net/url"
|
2018-12-15 21:42:32 +00:00
|
|
|
"time"
|
2014-01-06 06:09:41 +00:00
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
"github.com/NYTimes/gziphandler"
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/auth"
|
2016-08-06 23:58:34 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
2016-09-15 03:24:51 +00:00
|
|
|
ctx "github.com/gophish/gophish/context"
|
2019-03-27 03:17:20 +00:00
|
|
|
"github.com/gophish/gophish/controllers/api"
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2016-01-10 17:03:17 +00:00
|
|
|
mid "github.com/gophish/gophish/middleware"
|
|
|
|
"github.com/gophish/gophish/models"
|
2018-12-15 21:42:32 +00:00
|
|
|
"github.com/gophish/gophish/util"
|
|
|
|
"github.com/gophish/gophish/worker"
|
2016-09-15 03:24:51 +00:00
|
|
|
"github.com/gorilla/csrf"
|
2018-12-15 21:42:32 +00:00
|
|
|
"github.com/gorilla/handlers"
|
2016-01-25 02:03:53 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/sessions"
|
2018-12-15 21:42:32 +00:00
|
|
|
"github.com/jordan-wright/unindexed"
|
2013-12-03 04:56:55 +00:00
|
|
|
)
|
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
// AdminServerOption is a functional option that is used to configure the
|
|
|
|
// admin server
|
|
|
|
type AdminServerOption func(*AdminServer)
|
|
|
|
|
|
|
|
// AdminServer is an HTTP server that implements the administrative Gophish
|
|
|
|
// handlers, including the dashboard and REST API.
|
|
|
|
type AdminServer struct {
|
|
|
|
server *http.Server
|
|
|
|
worker worker.Worker
|
|
|
|
config config.AdminServer
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithWorker is an option that sets the background worker.
|
|
|
|
func WithWorker(w worker.Worker) AdminServerOption {
|
|
|
|
return func(as *AdminServer) {
|
|
|
|
as.worker = w
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdminServer returns a new instance of the AdminServer with the
|
|
|
|
// provided config and options applied.
|
|
|
|
func NewAdminServer(config config.AdminServer, options ...AdminServerOption) *AdminServer {
|
|
|
|
defaultWorker, _ := worker.New()
|
|
|
|
defaultServer := &http.Server{
|
|
|
|
ReadTimeout: 10 * time.Second,
|
|
|
|
Addr: config.ListenURL,
|
|
|
|
}
|
|
|
|
as := &AdminServer{
|
|
|
|
worker: defaultWorker,
|
|
|
|
server: defaultServer,
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
for _, opt := range options {
|
|
|
|
opt(as)
|
|
|
|
}
|
|
|
|
as.registerRoutes()
|
|
|
|
return as
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start launches the admin server, listening on the configured address.
|
|
|
|
func (as *AdminServer) Start() error {
|
|
|
|
if as.worker != nil {
|
|
|
|
go as.worker.Start()
|
|
|
|
}
|
|
|
|
if as.config.UseTLS {
|
|
|
|
err := util.CheckAndCreateSSL(as.config.CertPath, as.config.KeyPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("Starting admin server at https://%s", as.config.ListenURL)
|
|
|
|
return as.server.ListenAndServeTLS(as.config.CertPath, as.config.KeyPath)
|
|
|
|
}
|
|
|
|
// If TLS isn't configured, just listen on HTTP
|
|
|
|
log.Infof("Starting admin server at http://%s", as.config.ListenURL)
|
|
|
|
return as.server.ListenAndServe()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown attempts to gracefully shutdown the server.
|
|
|
|
func (as *AdminServer) Shutdown() error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
|
|
|
|
defer cancel()
|
|
|
|
return as.server.Shutdown(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupAdminRoutes creates the routes for handling requests to the web interface.
|
2014-07-02 01:32:34 +00:00
|
|
|
// This function returns an http.Handler to be used in http.ListenAndServe().
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) registerRoutes() {
|
2013-12-03 04:56:55 +00:00
|
|
|
router := mux.NewRouter()
|
|
|
|
// Base Front-end routes
|
2018-12-15 21:42:32 +00:00
|
|
|
router.HandleFunc("/", Use(as.Base, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/login", as.Login)
|
|
|
|
router.HandleFunc("/logout", Use(as.Logout, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/campaigns", Use(as.Campaigns, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/campaigns/{id:[0-9]+}", Use(as.CampaignID, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/templates", Use(as.Templates, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/users", Use(as.Users, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/landing_pages", Use(as.LandingPages, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/sending_profiles", Use(as.SendingProfiles, mid.RequireLogin))
|
|
|
|
router.HandleFunc("/settings", Use(as.Settings, mid.RequireLogin))
|
2019-02-20 02:33:50 +00:00
|
|
|
router.HandleFunc("/register", Use(as.Register, mid.RequireLogin, mid.RequirePermission(models.PermissionModifySystem)))
|
2013-12-03 04:56:55 +00:00
|
|
|
// Create the API routes
|
2019-03-27 03:17:20 +00:00
|
|
|
api := api.NewServer(api.WithWorker(as.worker))
|
|
|
|
router.PathPrefix("/api/").Handler(api)
|
2013-12-03 04:56:55 +00:00
|
|
|
|
2014-02-04 21:23:09 +00:00
|
|
|
// Setup static file serving
|
2018-12-15 21:42:32 +00:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(unindexed.Dir("./static/")))
|
2014-02-03 23:21:56 +00:00
|
|
|
|
2014-02-04 21:23:09 +00:00
|
|
|
// Setup CSRF Protection
|
2016-09-15 03:24:51 +00:00
|
|
|
csrfHandler := csrf.Protect([]byte(auth.GenerateSecureKey()),
|
|
|
|
csrf.FieldName("csrf_token"),
|
2018-12-15 21:42:32 +00:00
|
|
|
csrf.Secure(as.config.UseTLS))
|
|
|
|
adminHandler := csrfHandler(router)
|
|
|
|
adminHandler = Use(adminHandler.ServeHTTP, mid.CSRFExceptions, mid.GetContext)
|
|
|
|
|
|
|
|
// Setup GZIP compression
|
|
|
|
gzipWrapper, _ := gziphandler.NewGzipLevelHandler(gzip.BestCompression)
|
|
|
|
adminHandler = gzipWrapper(adminHandler)
|
|
|
|
|
|
|
|
// Setup logging
|
|
|
|
adminHandler = handlers.CombinedLoggingHandler(log.Writer(), adminHandler)
|
|
|
|
as.server.Handler = adminHandler
|
2014-06-29 21:44:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 04:11:44 +00:00
|
|
|
// Use allows us to stack middleware to process the request
|
|
|
|
// Example taken from https://github.com/gorilla/mux/pull/36#issuecomment-25849172
|
2014-01-11 04:37:42 +00:00
|
|
|
func Use(handler http.HandlerFunc, mid ...func(http.Handler) http.HandlerFunc) http.HandlerFunc {
|
2014-01-11 04:11:44 +00:00
|
|
|
for _, m := range mid {
|
|
|
|
handler = m(handler)
|
|
|
|
}
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
2018-12-15 21:42:32 +00:00
|
|
|
type templateParams struct {
|
2019-02-20 02:33:50 +00:00
|
|
|
Title string
|
|
|
|
Flashes []interface{}
|
|
|
|
User models.User
|
|
|
|
Token string
|
|
|
|
Version string
|
|
|
|
ModifySystem bool
|
2018-12-15 21:42:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTemplateParams returns the default template parameters for a user and
|
|
|
|
// the CSRF token.
|
|
|
|
func newTemplateParams(r *http.Request) templateParams {
|
2019-02-20 02:33:50 +00:00
|
|
|
user := ctx.Get(r, "user").(models.User)
|
|
|
|
modifySystem, _ := user.HasPermission(models.PermissionModifySystem)
|
2018-12-15 21:42:32 +00:00
|
|
|
return templateParams{
|
2019-02-20 02:33:50 +00:00
|
|
|
Token: csrf.Token(r),
|
|
|
|
User: user,
|
|
|
|
ModifySystem: modifySystem,
|
|
|
|
Version: config.Version,
|
2018-12-15 21:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 23:56:56 +00:00
|
|
|
// Register creates a new user
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Register(w http.ResponseWriter, r *http.Request) {
|
2013-12-03 04:56:55 +00:00
|
|
|
// If it is a post request, attempt to register the account
|
|
|
|
// Now that we are all registered, we can log the user in
|
2018-12-15 21:42:32 +00:00
|
|
|
params := templateParams{Title: "Register", Token: csrf.Token(r)}
|
2014-02-05 00:39:01 +00:00
|
|
|
session := ctx.Get(r, "session").(*sessions.Session)
|
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
|
|
|
params.Flashes = session.Flashes()
|
|
|
|
session.Save(r, w)
|
2015-02-07 23:30:22 +00:00
|
|
|
templates := template.New("template")
|
|
|
|
_, err := templates.ParseFiles("templates/register.html", "templates/flashes.html")
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2015-02-07 23:30:22 +00:00
|
|
|
}
|
|
|
|
template.Must(templates, err).ExecuteTemplate(w, "base", params)
|
2014-02-05 00:39:01 +00:00
|
|
|
case r.Method == "POST":
|
|
|
|
//Attempt to register
|
|
|
|
succ, err := auth.Register(r)
|
|
|
|
//If we've registered, redirect to the login page
|
|
|
|
if succ {
|
2017-12-11 00:11:32 +00:00
|
|
|
Flash(w, r, "success", "Registration successful!")
|
2014-02-05 00:39:01 +00:00
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/login", 302)
|
2016-02-13 22:37:12 +00:00
|
|
|
return
|
2014-02-05 00:39:01 +00:00
|
|
|
}
|
2016-02-13 22:37:12 +00:00
|
|
|
// Check the error
|
|
|
|
m := err.Error()
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-12-11 00:11:32 +00:00
|
|
|
Flash(w, r, "danger", m)
|
2016-02-13 22:37:12 +00:00
|
|
|
session.Save(r, w)
|
|
|
|
http.Redirect(w, r, "/register", 302)
|
|
|
|
return
|
2014-02-05 00:39:01 +00:00
|
|
|
}
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:56:56 +00:00
|
|
|
// Base handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Base(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Dashboard"
|
2014-01-30 21:08:14 +00:00
|
|
|
getTemplate(w, "dashboard").ExecuteTemplate(w, "base", params)
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:49:16 +00:00
|
|
|
// Campaigns handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Campaigns(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Campaigns"
|
2015-06-15 21:49:16 +00:00
|
|
|
getTemplate(w, "campaigns").ExecuteTemplate(w, "base", params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CampaignID handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) CampaignID(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Campaign Results"
|
2015-06-15 21:49:16 +00:00
|
|
|
getTemplate(w, "campaign_results").ExecuteTemplate(w, "base", params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Templates handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Templates(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Email Templates"
|
2015-06-15 21:49:16 +00:00
|
|
|
getTemplate(w, "templates").ExecuteTemplate(w, "base", params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Users(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Users & Groups"
|
2015-06-15 21:49:16 +00:00
|
|
|
getTemplate(w, "users").ExecuteTemplate(w, "base", params)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LandingPages handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) LandingPages(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Landing Pages"
|
2015-06-15 21:49:16 +00:00
|
|
|
getTemplate(w, "landing_pages").ExecuteTemplate(w, "base", params)
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:15:34 +00:00
|
|
|
// SendingProfiles handles the default path and template execution
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) SendingProfiles(w http.ResponseWriter, r *http.Request) {
|
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Sending Profiles"
|
2016-02-22 03:09:14 +00:00
|
|
|
getTemplate(w, "sending_profiles").ExecuteTemplate(w, "base", params)
|
2016-02-20 23:15:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:56:56 +00:00
|
|
|
// Settings handles the changing of settings
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Settings(w http.ResponseWriter, r *http.Request) {
|
2014-02-10 19:02:44 +00:00
|
|
|
switch {
|
2015-06-21 21:10:47 +00:00
|
|
|
case r.Method == "GET":
|
2018-12-15 21:42:32 +00:00
|
|
|
params := newTemplateParams(r)
|
|
|
|
params.Title = "Settings"
|
2015-06-21 21:10:47 +00:00
|
|
|
getTemplate(w, "settings").ExecuteTemplate(w, "base", params)
|
2014-02-10 19:02:44 +00:00
|
|
|
case r.Method == "POST":
|
|
|
|
err := auth.ChangePassword(r)
|
2014-06-02 03:30:23 +00:00
|
|
|
msg := models.Response{Success: true, Message: "Settings Updated Successfully"}
|
2014-02-10 19:02:44 +00:00
|
|
|
if err == auth.ErrInvalidPassword {
|
2014-05-29 04:29:41 +00:00
|
|
|
msg.Message = "Invalid Password"
|
|
|
|
msg.Success = false
|
2019-03-27 03:17:20 +00:00
|
|
|
api.JSONResponse(w, msg, http.StatusBadRequest)
|
2015-08-15 21:03:39 +00:00
|
|
|
return
|
2016-02-13 22:37:12 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
msg.Message = err.Error()
|
2014-05-29 04:29:41 +00:00
|
|
|
msg.Success = false
|
2019-03-27 03:17:20 +00:00
|
|
|
api.JSONResponse(w, msg, http.StatusBadRequest)
|
2015-08-15 21:03:39 +00:00
|
|
|
return
|
2014-05-29 04:29:41 +00:00
|
|
|
}
|
2019-03-27 03:17:20 +00:00
|
|
|
api.JSONResponse(w, msg, http.StatusOK)
|
2014-02-10 19:02:44 +00:00
|
|
|
}
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 23:56:56 +00:00
|
|
|
// Login handles the authentication flow for a user. If credentials are valid,
|
|
|
|
// a session is created
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Login(w http.ResponseWriter, r *http.Request) {
|
2014-01-10 03:21:54 +00:00
|
|
|
params := struct {
|
|
|
|
User models.User
|
|
|
|
Title string
|
|
|
|
Flashes []interface{}
|
2014-02-03 23:21:56 +00:00
|
|
|
Token string
|
2016-09-15 03:24:51 +00:00
|
|
|
}{Title: "Login", Token: csrf.Token(r)}
|
2014-01-10 03:21:54 +00:00
|
|
|
session := ctx.Get(r, "session").(*sessions.Session)
|
2014-01-06 06:09:41 +00:00
|
|
|
switch {
|
|
|
|
case r.Method == "GET":
|
2014-02-02 20:47:06 +00:00
|
|
|
params.Flashes = session.Flashes()
|
|
|
|
session.Save(r, w)
|
2014-05-27 01:29:12 +00:00
|
|
|
templates := template.New("template")
|
|
|
|
_, err := templates.ParseFiles("templates/login.html", "templates/flashes.html")
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-05-27 01:29:12 +00:00
|
|
|
}
|
|
|
|
template.Must(templates, err).ExecuteTemplate(w, "base", params)
|
2014-01-06 06:09:41 +00:00
|
|
|
case r.Method == "POST":
|
|
|
|
//Attempt to login
|
2016-09-15 03:24:51 +00:00
|
|
|
succ, u, err := auth.Login(r)
|
2014-02-06 16:49:53 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-01-31 22:25:02 +00:00
|
|
|
}
|
2014-01-07 06:58:48 +00:00
|
|
|
//If we've logged in, save the session and redirect to the dashboard
|
|
|
|
if succ {
|
2016-09-15 03:24:51 +00:00
|
|
|
session.Values["id"] = u.Id
|
2014-01-06 06:09:41 +00:00
|
|
|
session.Save(r, w)
|
2017-12-11 03:40:46 +00:00
|
|
|
next := "/"
|
|
|
|
url, err := url.Parse(r.FormValue("next"))
|
|
|
|
if err == nil {
|
|
|
|
path := url.Path
|
|
|
|
if path != "" {
|
|
|
|
next = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
http.Redirect(w, r, next, 302)
|
2014-01-07 06:58:48 +00:00
|
|
|
} else {
|
2014-02-05 16:57:53 +00:00
|
|
|
Flash(w, r, "danger", "Invalid Username/Password")
|
2017-12-11 00:11:32 +00:00
|
|
|
params.Flashes = session.Flashes()
|
|
|
|
session.Save(r, w)
|
|
|
|
templates := template.New("template")
|
|
|
|
_, err := templates.ParseFiles("templates/login.html", "templates/flashes.html")
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2017-12-11 00:11:32 +00:00
|
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
template.Must(templates, err).ExecuteTemplate(w, "base", params)
|
2014-01-06 06:09:41 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 16:41:53 +00:00
|
|
|
// Logout destroys the current user session
|
2018-12-15 21:42:32 +00:00
|
|
|
func (as *AdminServer) Logout(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 16:41:53 +00:00
|
|
|
session := ctx.Get(r, "session").(*sessions.Session)
|
|
|
|
delete(session.Values, "id")
|
|
|
|
Flash(w, r, "success", "You have successfully logged out")
|
2017-12-11 00:11:32 +00:00
|
|
|
session.Save(r, w)
|
2015-02-07 16:41:53 +00:00
|
|
|
http.Redirect(w, r, "/login", 302)
|
|
|
|
}
|
|
|
|
|
2014-01-10 03:21:54 +00:00
|
|
|
func getTemplate(w http.ResponseWriter, tmpl string) *template.Template {
|
2014-02-01 02:49:22 +00:00
|
|
|
templates := template.New("template")
|
2019-02-20 02:33:50 +00:00
|
|
|
_, err := templates.ParseFiles("templates/base.html", "templates/nav.html", "templates/"+tmpl+".html", "templates/flashes.html")
|
2014-02-01 02:49:22 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-02-01 02:49:22 +00:00
|
|
|
}
|
|
|
|
return template.Must(templates, err)
|
2013-12-03 04:56:55 +00:00
|
|
|
}
|
2014-01-31 04:46:25 +00:00
|
|
|
|
2015-02-07 16:41:53 +00:00
|
|
|
// Flash handles the rendering flash messages
|
2014-02-05 16:57:53 +00:00
|
|
|
func Flash(w http.ResponseWriter, r *http.Request, t string, m string) {
|
|
|
|
session := ctx.Get(r, "session").(*sessions.Session)
|
|
|
|
session.AddFlash(models.Flash{
|
|
|
|
Type: t,
|
|
|
|
Message: m,
|
|
|
|
})
|
|
|
|
}
|