/
smtp.go
430 lines (375 loc) · 10.3 KB
/
smtp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
Based largely on Go-Guerrilla SMTPd, whose license and copyright follows:
Copyright (c) 2012 Flashmob, GuerrillaMail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Portions of file encoding scheme from https://gist.github.com/rmulley/6603544
**/
package main
import (
"bufio"
"bytes"
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/rand"
"net"
"net/smtp"
"os"
"path/filepath"
"strconv"
"strings"
"time"
log "github.com/sandia-minimega/minimega/v2/pkg/minilog"
)
type SMTPClientSession struct {
conn net.Conn
bufin *bufio.Reader
bufout *bufio.Writer
state int
response string
mail_from string
rcpt_to []string
data string
tls_on bool
}
const (
INIT = iota
COMMANDS
STARTTLS
DATA
QUIT
)
const (
alphanum = "0123456789abcdefghijklmnopqrstuvwxyz"
)
type mail struct {
To string
From string
Subject string
Msg string
File string
}
var (
timeout = time.Duration(100)
max_size = 131072
myFQDN = "protonuke.local"
TLSconfig *tls.Config
smtpPort = ":25"
email []mail
)
func smtpClient(protocol string) {
log.Debugln("smtpClient")
// replace the email corpus if specified
if *f_smtpmail != "" {
f, err := os.Open(*f_smtpmail)
if err != nil {
log.Fatalln(err)
}
dec := json.NewDecoder(f)
err = dec.Decode(&email)
if err != nil {
log.Fatalln(err)
}
} else {
// populate the email list with the builtin list
for _, m := range builtinmail {
email = append(email, mail{
Msg: m,
})
}
}
t := NewEventTicker(*f_mean, *f_stddev, *f_min, *f_max)
for {
t.Tick()
h, o := randomHost()
log.Debug("smtp host %v from %v", h, o)
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
m := email[r.Intn(len(email))]
if m.To == "" {
toLen := r.Intn(10) + 1
for i := 0; i < toLen; i++ {
m.To += string(alphanum[r.Intn(len(alphanum))])
}
m.To += "@" + h
}
if m.From == "" {
fromLen := r.Intn(10) + 1
for i := 0; i < fromLen; i++ {
m.From += string(alphanum[r.Intn(len(alphanum))])
}
m.From += "@protonuke.org"
}
start := time.Now().UnixNano()
err := smtpSendMail(h, m, protocol)
stop := time.Now().UnixNano()
elapsed := uint64(stop - start)
if err != nil {
log.Errorln(err)
} else {
log.Info("smtp %v %vns", h, elapsed)
smtpReportChan <- 1
}
}
}
func smtpGetFile(m mail) ([]byte, string, error) {
var filename string
// open and read the file if there is one and if it's a directory pick
// a random file
if m.File != "" {
fi, err := os.Stat(m.File)
if err != nil {
return nil, "", err
}
if fi.IsDir() {
// pick a random file from the directory
var files []string
err := filepath.Walk(m.File, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if err != nil {
return err
}
files = append(files, path)
return nil
})
if err != nil {
return nil, "", err
}
if len(files) == 0 {
return nil, "", fmt.Errorf("no files in directory %v found", m.File)
}
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s)
filename = files[r.Intn(len(files))]
} else {
filename = m.File
}
}
if filename == "" {
return nil, "", nil
}
log.Debug("got filename: %v", filename)
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, "", err
}
encoded := base64.StdEncoding.EncodeToString(buf)
return []byte(encoded), filepath.Base(filename), nil
}
func smtpSendMail(server string, m mail, protocol string) error {
// url notation requires leading and trailing [] on ipv6 addresses
if isIPv6(server) {
server = "[" + server + "]"
}
filedata, filename, err := smtpGetFile(m)
if err != nil {
return err
}
conn, err := net.Dial(protocol, server+smtpPort)
if err != nil {
return err
}
defer conn.Close()
c, err := smtp.NewClient(conn, server)
if err != nil {
return err
}
defer c.Close()
if *f_smtpTls {
err = c.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
log.Warnln("could not start tls")
}
}
c.Mail(m.From)
c.Rcpt(m.To)
wc, err := c.Data()
if err != nil {
return err
}
boundary := "PROTONUKE-MIME-BOUNDARY"
// header
header := fmt.Sprintf("Subject: %s\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=%s\r\n--%s", m.Subject, boundary, boundary)
// body
body := fmt.Sprintf("\r\nContent-Type: text/html\r\nContent-Transfer-Encoding:8bit\r\n\r\n%s\r\n--%s", m.Msg, boundary)
wc.Write([]byte(header + body))
if len(filedata) != 0 {
var buf bytes.Buffer
maxLineLen := 500
nbrLines := len(filedata) / maxLineLen
//append lines to buffer
for i := 0; i < nbrLines; i++ {
buf.Write(filedata[i*maxLineLen : (i+1)*maxLineLen])
buf.WriteString("\n")
}
buf.Write(filedata[nbrLines*maxLineLen:])
file := fmt.Sprintf("\r\nContent-Type: application/csv; name=\"file.csv\"\r\nContent-Transfer-Encoding:base64\r\nContent-Disposition: attachment; filename=\"%s\"\r\n\r\n%s\r\n--%s--", filename, buf.String(), boundary)
wc.Write([]byte(file))
}
wc.Close()
return nil
}
func NewSMTPClientSession(c net.Conn) *SMTPClientSession {
ret := &SMTPClientSession{conn: c, state: INIT}
ret.bufin = bufio.NewReader(c)
ret.bufout = bufio.NewWriter(c)
ret.rcpt_to = []string{}
return ret
}
func smtpServer(p string) {
log.Debugln("smtpServer")
certfile, keyfile := generateCerts()
cert, err := tls.LoadX509KeyPair(certfile, keyfile)
if err != nil {
log.Fatalln("couldn't get cert: ", err)
}
TLSconfig = &tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.VerifyClientCertIfGiven, ServerName: myFQDN}
listener, err := net.Listen(p, "0.0.0.0"+smtpPort)
if err != nil {
log.Fatalln(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Debugln(err)
continue
}
client := NewSMTPClientSession(conn)
go client.Handler()
smtpReportChan <- 1
}
}
func (s *SMTPClientSession) Close() {
s.conn.Close()
}
func (s *SMTPClientSession) Handler() {
defer s.Close()
start := time.Now().UnixNano()
for {
switch s.state {
case INIT:
s.addResponse("220 protonuke: Less for outcasts, more for weirdos.")
s.state = COMMANDS
case COMMANDS:
input, err := s.readSmtp()
input = strings.Trim(input, "\r\n")
cmd := strings.ToUpper(input)
if err != nil {
log.Debugln(err)
return
}
switch {
case strings.HasPrefix(cmd, "HELO"):
s.addResponse("250 You're all so great and we're gonna keep you listening all day")
case strings.HasPrefix(cmd, "EHLO"):
r := "250-" + myFQDN + " Serving mail almost makes you wish for a nuclear winter\r\n"
r += "250-8BITMIME\r\n250-STARTTLS\r\n250 HELP"
s.addResponse(r)
case strings.HasPrefix(cmd, "MAIL FROM:"):
if len(input) > 10 {
s.mail_from = input[10:]
}
s.addResponse("250 Ok")
case strings.HasPrefix(cmd, "RCPT TO:"):
if len(input) > 8 {
s.rcpt_to = append(s.rcpt_to, input[8:])
}
s.addResponse("250 Ok")
case strings.HasPrefix(cmd, "DATA"):
s.addResponse("354 End data with <CR><LF>.<CR><LF>")
s.state = DATA
case strings.HasPrefix(cmd, "STARTTLS") && !s.tls_on:
s.addResponse("220 They asked what do you know about theoretical physics? I said I had a theoretical degree in physics.")
s.state = STARTTLS
case strings.HasPrefix(cmd, "QUIT"):
s.addResponse("221 Beware the Battle Cattle!")
s.state = QUIT
case strings.HasPrefix(cmd, "NOOP"):
s.addResponse("250 Is it time?")
case strings.HasPrefix(cmd, "RSET"):
s.mail_from = ""
s.rcpt_to = []string{}
s.addResponse("250 I forgot to remember to forget")
default:
s.addResponse("500 unrecognized command")
}
case DATA:
input, err := s.readSmtp()
if err != nil {
log.Debugln(err)
}
s.data = input
log.Debugln("Got email message:")
log.Debugln(s)
s.addResponse("250 Ok: Now that is a delivery service you can count on")
s.state = COMMANDS
case STARTTLS:
// I'm just going to pull this from GoGuerrilla, thanks guys
var tlsConn *tls.Conn
tlsConn = tls.Server(s.conn, TLSconfig)
err := tlsConn.Handshake() // not necessary to call here, but might as well
if err == nil {
s.conn = net.Conn(tlsConn)
s.bufin = bufio.NewReader(s.conn)
s.bufout = bufio.NewWriter(s.conn)
s.tls_on = true
} else {
log.Debugln("Could not TLS handshake:", err)
}
s.state = COMMANDS
case QUIT:
stop := time.Now().UnixNano()
log.Info("smtp %v %vns", s.conn.RemoteAddr(), uint64(stop-start))
return
}
size, _ := s.bufout.WriteString(s.response)
s.bufout.Flush()
s.response = s.response[size:]
}
}
func (s *SMTPClientSession) addResponse(r string) {
s.response += r + "\r\n"
}
// Read a line from the client.
// Take directly from Flashmob's GoGuerrilla (MIT license, github.com/flashmob/go-guerrilla)
// Slight modification to be a method on SMTPClientSession
func (s *SMTPClientSession) readSmtp() (input string, err error) {
var reply string
// Command state terminator by default
suffix := "\r\n"
if s.state == DATA {
suffix = "\r\n.\r\n"
}
for err == nil {
s.conn.SetDeadline(time.Now().Add(timeout * time.Second))
reply, err = s.bufin.ReadString('\n')
if reply != "" {
input = input + reply
if len(input) > max_size {
err = errors.New("Maximum DATA size exceeded (" + strconv.Itoa(max_size) + ")")
return input, err
}
}
if err != nil {
break
}
if strings.HasSuffix(input, suffix) {
break
}
}
return input, err
}