From 759f86447dc0348174e012f3b932a2e18fa1861c Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 15 Feb 2015 21:53:30 -0600 Subject: [PATCH] Working on importing emails from source (still doesn't work yet!) --- controllers/api.go | 16 ++++++++++- controllers/route.go | 1 + static/js/app/controllers.js | 28 +++++++++++++++++-- .../app/partials/modals/importEmailModal.html | 19 +++++++++++++ .../js/app/partials/modals/templateModal.html | 8 ++---- util/util.go | 16 +++++++++++ 6 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 static/js/app/partials/modals/importEmailModal.html diff --git a/controllers/api.go b/controllers/api.go index 1a51bbb0..a2cf0970 100644 --- a/controllers/api.go +++ b/controllers/api.go @@ -3,6 +3,7 @@ package controllers import ( "encoding/json" "fmt" + "io/ioutil" "net/http" "strconv" "text/template" @@ -335,8 +336,8 @@ func API_Pages_Id(w http.ResponseWriter, r *http.Request) { } } +// API_Import_Group imports a CSV of group members func API_Import_Group(w http.ResponseWriter, r *http.Request) { - Logger.Println("Parsing CSV....") ts, err := util.ParseCSV(r) if checkError(err, w, "Error deleting template", http.StatusInternalServerError) { return @@ -344,6 +345,19 @@ func API_Import_Group(w http.ResponseWriter, r *http.Request) { JSONResponse(w, ts, http.StatusOK) } +// API_Import_Email allows for the importing of email. +// Returns a Message object +func API_Import_Email(w http.ResponseWriter, r *http.Request) { + if r.Method == "POST" { + body, err := ioutil.ReadAll(r.Body) + if err != nil { + Logger.Println(err) + } + w.Header().Set("Content-Type", "text/plain") + fmt.Fprintf(w, "%s", body) + } +} + // JSONResponse attempts to set the status code, c, and marshal the given interface, d, into a response that // is written to the given ResponseWriter. func JSONResponse(w http.ResponseWriter, d interface{}, c int) { diff --git a/controllers/route.go b/controllers/route.go index c8138cd0..074989db 100644 --- a/controllers/route.go +++ b/controllers/route.go @@ -47,6 +47,7 @@ func CreateAdminRouter() http.Handler { api.HandleFunc("/pages/", Use(API_Pages, mid.RequireAPIKey)) api.HandleFunc("/pages/{id:[0-9]+}", Use(API_Pages_Id, mid.RequireAPIKey)) api.HandleFunc("/import/group", API_Import_Group) + api.HandleFunc("/import/email", API_Import_Email) // Setup static file serving router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) diff --git a/static/js/app/controllers.js b/static/js/app/controllers.js index d49e35ae..6e312527 100644 --- a/static/js/app/controllers.js +++ b/static/js/app/controllers.js @@ -653,13 +653,12 @@ app.controller('TemplateCtrl', function($scope, $modal, TemplateService, ngTable } }) -var TemplateModalCtrl = function($scope, $upload, $modalInstance) { +var TemplateModalCtrl = function($scope, $upload, $modalInstance, $modal) { $scope.editorOptions = { fullPage: true, allowedContent: true } $scope.onFileSelect = function($files) { - console.log($files) angular.forEach($files, function(file, key) { var reader = new FileReader(); reader.onload = function(e) { @@ -686,6 +685,31 @@ var TemplateModalCtrl = function($scope, $upload, $modalInstance) { $scope.removeFile = function(file) { $scope.template.attachments.splice($scope.template.attachments.indexOf(file), 1); } + + $scope.importEmail = function() { + var emailInstance = $modal.open({ + templateUrl: '/js/app/partials/modals/importEmailModal.html', + controller: ImportEmailCtrl, + scope: $scope + }); + + emailInstance.result.then(function(raw) { + $scope.template.subject = raw; + }, function() {}); + }; +}; + +var ImportEmailCtrl = function($scope, $http, $modalInstance) { + $scope.email = {} + $scope.ok = function() { + // Simple POST request example (passing data) : + $http.post('/api/import/email', $scope.email.raw, + { headers : {"Content-Type" : "text/plain"}} + ).success(function(data) {console.log("Success: " + data)}) + .error(function(data) {console.log("Error: " + data)}); + $modalInstance.close($scope.email.raw) + }; + $scope.cancel = function() {$modalInstance.dismiss()} }; app.controller('LandingPageCtrl', function($scope, $modal, LandingPageService, ngTableParams) { diff --git a/static/js/app/partials/modals/importEmailModal.html b/static/js/app/partials/modals/importEmailModal.html new file mode 100644 index 00000000..bcbc3188 --- /dev/null +++ b/static/js/app/partials/modals/importEmailModal.html @@ -0,0 +1,19 @@ + + + + diff --git a/static/js/app/partials/modals/templateModal.html b/static/js/app/partials/modals/templateModal.html index c709bb06..9e25207a 100644 --- a/static/js/app/partials/modals/templateModal.html +++ b/static/js/app/partials/modals/templateModal.html @@ -9,11 +9,9 @@
-
-
- -
-
+
+ +
diff --git a/util/util.go b/util/util.go index 727a9213..a54efde1 100644 --- a/util/util.go +++ b/util/util.go @@ -4,11 +4,27 @@ import ( "encoding/csv" "fmt" "io" + "io/ioutil" "net/http" + "net/mail" + "github.com/jordan-wright/email" "github.com/jordan-wright/gophish/models" ) +// 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 +} + func ParseCSV(r *http.Request) ([]models.Target, error) { mr, err := r.MultipartReader() ts := []models.Target{}