2020-08-17 18:50:18 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-12-07 21:16:31 +00:00
|
|
|
"strings"
|
2020-08-17 18:50:18 +00:00
|
|
|
|
|
|
|
"gopkg.in/check.v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *ModelsSuite) TestAttachment(c *check.C) {
|
|
|
|
ptx := PhishingTemplateContext{
|
|
|
|
BaseRecipient: BaseRecipient{
|
|
|
|
FirstName: "Foo",
|
|
|
|
LastName: "Bar",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
Position: "Space Janitor",
|
|
|
|
},
|
|
|
|
BaseURL: "http://testurl.com",
|
|
|
|
URL: "http://testurl.com/?rid=1234567",
|
|
|
|
TrackingURL: "http://testurl.local/track?rid=1234567",
|
|
|
|
Tracker: "<img alt='' style='display: none' src='http://testurl.local/track?rid=1234567'/>",
|
|
|
|
From: "From Address",
|
|
|
|
RId: "1234567",
|
|
|
|
}
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir("testdata")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open attachment folder 'testdata': %v\n", err)
|
|
|
|
}
|
|
|
|
for _, ff := range files {
|
2020-12-10 17:17:01 +00:00
|
|
|
if !ff.IsDir() && !strings.Contains(ff.Name(), "templated") {
|
2020-08-17 18:50:18 +00:00
|
|
|
fname := ff.Name()
|
|
|
|
fmt.Printf("Checking attachment file -> %s\n", fname)
|
2020-12-10 17:17:01 +00:00
|
|
|
data := readFile("testdata/" + fname)
|
2020-08-17 18:50:18 +00:00
|
|
|
if filepath.Ext(fname) == ".b64" {
|
|
|
|
fname = fname[:len(fname)-4]
|
|
|
|
}
|
|
|
|
a := Attachment{
|
|
|
|
Content: data,
|
|
|
|
Name: fname,
|
|
|
|
}
|
2020-12-10 17:17:01 +00:00
|
|
|
t, err := a.ApplyTemplate(ptx)
|
2020-08-17 18:50:18 +00:00
|
|
|
c.Assert(err, check.Equals, nil)
|
2020-12-07 21:16:31 +00:00
|
|
|
c.Assert(a.vanillaFile, check.Equals, strings.Contains(fname, "without-vars"))
|
|
|
|
c.Assert(a.vanillaFile, check.Not(check.Equals), strings.Contains(fname, "with-vars"))
|
2020-08-17 18:50:18 +00:00
|
|
|
|
2020-12-10 17:17:01 +00:00
|
|
|
// Verfify template was applied as expected
|
|
|
|
tt, err := ioutil.ReadAll(t)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to parse templated file '%s': %v\n", fname, err)
|
|
|
|
}
|
|
|
|
templatedFile := base64.StdEncoding.EncodeToString(tt)
|
|
|
|
expectedOutput := readFile("testdata/" + strings.TrimSuffix(ff.Name(), filepath.Ext(ff.Name())) + ".templated" + filepath.Ext(ff.Name())) // e.g text-file-with-vars.templated.txt
|
|
|
|
c.Assert(templatedFile, check.Equals, expectedOutput)
|
2020-08-17 18:50:18 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-10 17:17:01 +00:00
|
|
|
}
|
2020-08-17 18:50:18 +00:00
|
|
|
|
2020-12-10 17:17:01 +00:00
|
|
|
func readFile(fname string) string {
|
|
|
|
f, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open file '%s': %v\n", fname, err)
|
|
|
|
}
|
|
|
|
reader := bufio.NewReader(f)
|
|
|
|
content, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to read file '%s': %v\n", fname, err)
|
|
|
|
}
|
|
|
|
data := ""
|
|
|
|
if filepath.Ext(fname) == ".b64" {
|
|
|
|
data = string(content)
|
|
|
|
} else {
|
|
|
|
data = base64.StdEncoding.EncodeToString(content)
|
|
|
|
}
|
|
|
|
return data
|
2020-08-17 18:50:18 +00:00
|
|
|
}
|