2017-06-09 04:41:38 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gophish/gophish/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *ControllersSuite) getFirstCampaign() models.Campaign {
|
|
|
|
campaigns, err := models.GetCampaigns(1)
|
|
|
|
s.Nil(err)
|
|
|
|
return campaigns[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) openEmail(rid string) {
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/track?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
s.Nil(err)
|
|
|
|
expected, err := ioutil.ReadFile("static/images/pixel.png")
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(bytes.Compare(body, expected), 0)
|
|
|
|
}
|
|
|
|
|
2018-03-19 03:03:00 +00:00
|
|
|
func (s *ControllersSuite) reportedEmail(rid string) {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/report?%s=%s", ps.URL, models.RecipientParameter, rid))
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2017-06-09 04:41:38 +00:00
|
|
|
func (s *ControllersSuite) openEmail404(rid string) {
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/track?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) clickLink(rid string, campaign models.Campaign) {
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(bytes.Compare(body, []byte(campaign.Page.HTML)), 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) clickLink404(rid string) {
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestOpenedPhishingEmail() {
|
|
|
|
campaign := s.getFirstCampaign()
|
|
|
|
result := campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.STATUS_SENDING)
|
|
|
|
|
|
|
|
s.openEmail(result.RId)
|
|
|
|
|
|
|
|
campaign = s.getFirstCampaign()
|
|
|
|
result = campaign.Results[0]
|
2018-05-27 02:26:34 +00:00
|
|
|
lastEvent := campaign.Events[len(campaign.Events)-1]
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Equal(result.Status, models.EVENT_OPENED)
|
2018-05-27 02:26:34 +00:00
|
|
|
s.Equal(lastEvent.Message, models.EVENT_OPENED)
|
|
|
|
s.Equal(result.ModifiedDate, lastEvent.Time)
|
2017-06-09 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 03:03:00 +00:00
|
|
|
func (s *ControllersSuite) TestReportedPhishingEmail() {
|
|
|
|
campaign := s.getFirstCampaign()
|
|
|
|
result := campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.STATUS_SENDING)
|
|
|
|
|
|
|
|
s.reportedEmail(result.RId)
|
|
|
|
|
|
|
|
campaign = s.getFirstCampaign()
|
|
|
|
result = campaign.Results[0]
|
2018-05-27 02:26:34 +00:00
|
|
|
lastEvent := campaign.Events[len(campaign.Events)-1]
|
2018-03-19 03:03:00 +00:00
|
|
|
s.Equal(result.Reported, true)
|
2018-05-27 02:26:34 +00:00
|
|
|
s.Equal(lastEvent.Message, models.EVENT_REPORTED)
|
|
|
|
s.Equal(result.ModifiedDate, lastEvent.Time)
|
2018-03-19 03:03:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 04:41:38 +00:00
|
|
|
func (s *ControllersSuite) TestClickedPhishingLinkAfterOpen() {
|
|
|
|
campaign := s.getFirstCampaign()
|
|
|
|
result := campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.STATUS_SENDING)
|
|
|
|
|
|
|
|
s.openEmail(result.RId)
|
|
|
|
s.clickLink(result.RId, campaign)
|
|
|
|
|
|
|
|
campaign = s.getFirstCampaign()
|
|
|
|
result = campaign.Results[0]
|
2018-05-27 02:26:34 +00:00
|
|
|
lastEvent := campaign.Events[len(campaign.Events)-1]
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Equal(result.Status, models.EVENT_CLICKED)
|
2018-05-27 02:26:34 +00:00
|
|
|
s.Equal(lastEvent.Message, models.EVENT_CLICKED)
|
|
|
|
s.Equal(result.ModifiedDate, lastEvent.Time)
|
2017-06-09 04:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestNoRecipientID() {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/track", ps.URL))
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
|
|
|
|
resp, err = http.Get(ps.URL)
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestInvalidRecipientID() {
|
|
|
|
rid := "XXXXXXXXXX"
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/track?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
|
2018-02-23 05:02:27 +00:00
|
|
|
resp, err = http.Get(fmt.Sprintf("%s/?%s=%s", ps.URL, models.RecipientParameter, rid))
|
2017-06-09 04:41:38 +00:00
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestCompletedCampaignClick() {
|
|
|
|
campaign := s.getFirstCampaign()
|
|
|
|
result := campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.STATUS_SENDING)
|
|
|
|
s.openEmail(result.RId)
|
|
|
|
|
|
|
|
campaign = s.getFirstCampaign()
|
|
|
|
result = campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.EVENT_OPENED)
|
|
|
|
|
|
|
|
models.CompleteCampaign(campaign.Id, 1)
|
|
|
|
s.openEmail404(result.RId)
|
|
|
|
s.clickLink404(result.RId)
|
|
|
|
|
|
|
|
campaign = s.getFirstCampaign()
|
|
|
|
result = campaign.Results[0]
|
|
|
|
s.Equal(result.Status, models.EVENT_OPENED)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ControllersSuite) TestRobotsHandler() {
|
|
|
|
expected := []byte("User-agent: *\nDisallow: /\n")
|
|
|
|
resp, err := http.Get(fmt.Sprintf("%s/robots.txt", ps.URL))
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(resp.StatusCode, http.StatusOK)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
s.Nil(err)
|
|
|
|
s.Equal(bytes.Compare(body, expected), 0)
|
|
|
|
}
|