From b42a0279aa097f9bb748410333a8916882ca937b Mon Sep 17 00:00:00 2001 From: Glenn Wilkinson Date: Thu, 26 Mar 2020 17:45:55 +0000 Subject: [PATCH] Added support for additional charsets --- imap/imap.go | 3 +++ imap/monitor.go | 12 +++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/imap/imap.go b/imap/imap.go index 431a55d6..d4d57e57 100644 --- a/imap/imap.go +++ b/imap/imap.go @@ -13,6 +13,7 @@ import ( "github.com/emersion/go-imap" "github.com/emersion/go-imap/client" + "github.com/emersion/go-message/charset" "github.com/emersion/go-message/mail" log "github.com/gophish/gophish/logger" "github.com/gophish/gophish/models" @@ -120,6 +121,8 @@ func (mbox *Mailbox) DeleteEmails(seqs []uint32) error { // GetUnread will find all unread emails in the folder and return them as a list. func (mbox *Mailbox) GetUnread(markAsRead, delete bool) ([]Email, error) { + + imap.CharsetReader = charset.Reader var emails []Email imapclient, err := mbox.newClient() diff --git a/imap/monitor.go b/imap/monitor.go index 0c0f3346..19c96f24 100644 --- a/imap/monitor.go +++ b/imap/monitor.go @@ -82,7 +82,7 @@ func monitor(uid int64, ctx context.Context) { im := imapSettings[0] // 3. Check if IMAP is enabled if im.Enabled { - log.Debug("Checking IMAP for user ", uid, ": ", im.Username, "@", im.Host) + log.Debug("Checking IMAP for user ", uid, ": ", im.Username, " -> ", im.Host) checkForNewEmails(im) time.Sleep((time.Duration(im.IMAPFreq) - 10) * time.Second) // Subtract 10 to compensate for the default sleep of 10 at the bottom } @@ -136,6 +136,7 @@ func checkForNewEmails(im models.IMAP) { err = models.SuccessfulLogin(&im) if len(msgs) > 0 { + log.Debugf("%d new emails for %s", len(msgs), im.Username) var reportingFailed []uint32 // SeqNums of emails that were unable to be reported to phishing server, mark as unread var campaignEmails []uint32 // SeqNums of campaign emails. If DeleteReportedCampaignEmail is true, we will delete these for _, m := range msgs { @@ -150,12 +151,13 @@ func checkForNewEmails(im models.IMAP) { } rids, err := checkRIDs(m.Email) // Search email Text, HTML, and each attachment for rid parameters + if err != nil { log.Errorf("Error searching email for rids from user '%s': %s", m.Email.From, err.Error()) } else { if len(rids) < 1 { // In the future this should be an alert in Gophish - log.Infof("User '%s' reported email with subject '%s'. This is not a GoPhish campaign; you should investigate it.\n", m.Email.From, m.Email.Subject) + log.Infof("User '%s' reported email with subject '%s'. This is not a GoPhish campaign; you should investigate it.", m.Email.From, m.Email.Subject) } for rid := range rids { log.Infof("User '%s' reported email with rid %s", m.Email.From, rid) @@ -179,7 +181,7 @@ func checkForNewEmails(im models.IMAP) { // Check if any emails were unable to be reported, so we can mark them as unread if len(reportingFailed) > 0 { - log.Debugf("Marking %d emails as unread as failed to report\n", len(reportingFailed)) + log.Debugf("Marking %d emails as unread as failed to report", len(reportingFailed)) err := mailServer.MarkAsUnread(reportingFailed) // Set emails as unread that we failed to report to GoPhish if err != nil { log.Error("Unable to mark emails as unread: ", err.Error()) @@ -187,7 +189,7 @@ func checkForNewEmails(im models.IMAP) { } // If the DeleteReportedCampaignEmail flag is set, delete reported Gophish campaign emails if im.DeleteReportedCampaignEmail == true && len(campaignEmails) > 0 { - log.Debugf("Deleting %d campaign emails\n", len(campaignEmails)) + log.Debugf("Deleting %d campaign emails", len(campaignEmails)) err := mailServer.DeleteEmails(campaignEmails) // Delete GoPhish campaign emails. if err != nil { log.Error("Failed to delete emails: ", err.Error()) @@ -216,7 +218,7 @@ func checkRIDs(em *email.Email) (map[string]int, error) { //Next check each attachment for _, a := range em.Attachments { - if a.Header.Get("Content-Type") == "message/rfc822" { + if a.Header.Get("Content-Type") == "message/rfc822" || (len(a.Filename) > 3 && a.Filename[len(a.Filename)-4:] == ".eml") { //Let's decode the email rawBodyStream := bytes.NewReader(a.Content)